Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: How to normalize variables in stata


From   Xixi Lin <[email protected]>
To   statalist <[email protected]>
Subject   Re: st: How to normalize variables in stata
Date   Thu, 25 Apr 2013 14:18:39 -0400

Thank you so much!

On Thu, Apr 25, 2013 at 2:07 PM, Nick Cox <[email protected]> wrote:
> I'd call this standardisation, not normalisation. Your problem is
> essentially that of Paul Byatta posted yesterday
> http://www.stata.com/statalist/archive/2013-04/msg01088.html
>
> It seems that you have three variables -x1-, -x2-,-x3- and various -Period-s.
>
> forval i = 1/3 {
>        egen mean = mean(x`i'), by(Period)
>        egen sd = sd(x`i'), by(Period)
>        gen N_x`i' = (x`i' - mean) / sd
>        drop mean sd
> }
>
> should help.
>
> The problem with your code is the lines
>
> qui sum `x' if Period==`i', meanonly
> replace sd_`x'=r(sd) if Period==`i'
>
> With the -meanonly- option -summarize- does not calculate the sd.
> When you refer to r(sd) afterwards, Stata just substitutes missing,
> and this is propagated to all your new variables.
>
> But as above, you don't need so much code.
>
> Nick
> [email protected]
>
> On 25 April 2013 18:53, Xixi Lin <[email protected]> wrote:
>
>> I am trying to normalize variables, and the following is my code:
>> foreach x in x1 x2 x3{
>> gen N_`x' = .
>> gen mean_`x'=.
>> gen sd_`x'=.
>> }
>> forvalues i=2/120{
>> foreach x in x1 x2 x3{
>> qui sum `x' if Period==`i', meanonly
>> replace mean_`x'=r(mean) if Period==`i'
>> qui sum `x' if Period==`i', meanonly
>> replace sd_`x'=r(sd) if Period==`i'
>> }
>> }
>>
>>
>> forvalues i=2/120{
>> foreach x in x1 x2 x3{
>> replace N_`x'=(`x'-mean_`x')/sd_`x' if Period==`i'
>> }
>> }
>>
>> however, the above code does not working, because it creates all
>> missing values for standard deviation. Can anyone help me to fix the
>> code?
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index