Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculating the Z-score for a variable in a single command line

    Hello Everyone,

    I have a variable in a Likert scale that I want to convert to a z-score. I was wondering if it is possible to calculate it via a single Stata command line. I used the following command below, but I guess I am mistaken somewhere:

    egen average_z= (average_1-mean(average_1))/sd(average_1)

    The snapshot of the variable is posted below; I would really appreciate your advice on this!


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double average_1
    2
    4
    2
    3
    2
    2
    2
    2
    3
    5
    3
    3
    1
    2
    2
    5
    3
    1
    2
    1
    1
    2
    1
    1
    1
    1
    1
    3
    3
    3
    1
    3
    2
    1
    1
    1
    2
    2
    1
    1
    2
    2
    1
    2
    4
    2
    1
    3
    3
    1
    2
    4
    2
    3
    3
    2
    4
    1
    3
    1
    2
    2
    3
    1
    2
    1
    3
    3
    1
    3
    2
    3
    2
    1
    1
    2
    5
    3
    2
    3
    3
    4
    2
    2
    1
    1
    1
    2
    1
    2
    1
    1
    3
    1
    1
    1
    3
    2
    2
    3
    end
    label values average_1 average_1
    label def average_1 1 "1", modify
    label def average_1 2 "2", modify
    label def average_1 3 "3", modify
    label def average_1 4 "4", modify
    label def average_1 5 "5", modify
    Last edited by Nick Baradar; 28 Apr 2022, 05:56.

  • #2
    The three-line command I use looks like this:

    Code:
    egen average_mean=mean(average_1)
    egen average_st=sd(average_1)
    gen average_z= (average_1- average_mean)/average_st
    Last edited by Nick Baradar; 28 Apr 2022, 05:59.

    Comment


    • #3
      Have you tried using the community contributed -zscore- or -zanthro-?

      Comment


      • #4
        Originally posted by Mead Over View Post
        Have you tried using the community contributed -zscore- or -zanthro-?
        Dear Mead, thank you very much for your advice. I have not tried it. Should I install a package for it?

        Normally, I avoid using external packages since several people often work on the same project, and it becomes difficult to keep up with the nuances of those packages and keep everyone informed. That is why I was wondering if it would be possible to calculate it with a single command line.

        Comment


        • #5
          Computationally, at some level, you need at least two cycles: one to obtain the mean and SD, and another to transform the original values. I suggest the following approach that represents the two cycles:

          Code:
          summarize varname
          generate newvar = (varname-r(mean)) / r(sd)
          Obviously, you can pack those lines into one program that you then call in one line. I do not see a huge advantage here.

          Comment


          • #6
            The error in #1 is that you may only use one egen function call at a time. That said, everyone seems to be overlooking


            Code:
            egen z = std(average_1) 
            which qualifies as one line.

            That said working with the mean and SD of a Likert item spreads everyone who knows what it means on a spectrum from "unsound in principle" to "useful in practice".

            Comment


            • #7
              Nick Baradar , I understand your concern that: ...
              Originally posted by Nick Baradar View Post
              Normally, I avoid using external packages since several people often work on the same project, and it becomes difficult to keep up with the nuances of those packages and keep everyone informed.
              Another reason to be careful with user-contributed programs, as discussed in this thread started by diana gold , is the increasingly common requirement that code be published such that results can be replicated by other users years from now.

              However, by eschewing the use of all user-contributed packages, one incurs the sometimes large cost of reinventing the wheel. Also, in cases where one's StataCorp-only implementation requires multiple lines of code, eschewing the use of community-contributed code will probably make one's own code harder for others to read and understand. On a philosophical note, I believe Stata is preferable to other competing programming environments because it provides the optimal mix of StataCorp's own (quality-controlled) code and community-contributed code. Along a continuum from Eviews (entirely proprietary with no scripting language) and R (entirely open source), I believe Stata excellently combines the advantages of both extremes: quality (of their own rigorously maintained code) and flexibility (with the community-debugged community-contributed programs). By avoiding community-contributed commands, users deny themselves the benefits of one of the best reasons to use Stata.

              While these issues don't arise if you only want to standardize a variable, for those wishing to distribute a "frozen" collection of user-contributed Stata programs, I think Diana's updated program, dependencies, which benefits from daniel klein 's suggestions, is really helpful. Her version 0.2 of 16nov2019 is available from SSC by clicking on "install" after typing::

              Code:
              ssc describe dependencies




              Comment

              Working...
              X