Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: How to standardize a variable with a define mean


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: How to standardize a variable with a define mean
Date   Wed, 1 Feb 2012 11:23:18 +0000

Maarten is quite correct here in fixing your syntax, but note that -egen- is not needed here given that you have already -summarized- BMI. 

su BMI 
gen stdmi = (BMI - r(mean)) / r(sd) 

That way the whole business of putting value of r(mean) into a scalar, only to have to take it out again, is avoided. 

Even going the -egen- route the -scalar- is not needed and you don't even need to feed r(mean) to -egen- as by default the -std()- function works with the mean and standard deviation of the expression specified, here a variable name. 

. egen stdmpg1 = std(mpg)

. su mpg

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
         mpg |        74     21.2973    5.785503         12         41

. gen stdmpg2 = (mpg - r(mean)) / r(sd)

. su stdmpg*

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
     stdmpg1 |        74   -7.00e-09           1  -1.606999    3.40553
     stdmpg2 |        74   -7.00e-09           1  -1.606999    3.40553

So, the problem needs one line or two, depending on whether you choose convenience or Stata minimalism, but not more. 

Nick 
n.j.cox@durham.ac.uk 

Maarten Buis

On Wed, Feb 1, 2012 at 10:44 AM, Alberto R Osella wrote:
> I want to standardize a variable (BMI) with a given mean value. I've
> obtained that value from a -summarize- command, that is r(mean).
> Now I want to pass the value of r(mean) to the egen command. Here the
> commands and the error returned by Stata.
>
> . sum BMI
> . scalar meanbmi=r(mean)
> . display meanbmi
> 25.8435
> . egen stdbmi=std(BMI), mean(meanbmi) std(1)
> option mean() incorrectly specified
> r(198);

try:

egen stdbmi=std(BMI), mean(`=meanbmi') std(1)

The trick is that meanbmi is the name of a scalar while the -mean()-
option expects a number. By surrounding that scalar name with " `= "
and " ' " you are forcing Stata to first evaluate the scalar name,
resulting in a number, which is exactly what the -mean()- option
wanted.


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index