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: RE: mean and sd of lagged variables


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: RE: mean and sd of lagged variables
Date   Fri, 29 Jul 2011 14:38:03 +0100

On second thoughts, you can perhaps make this a two-step: First 

gen y3 = L3.invest 
gen y2 = L2.invest
gen y1 = L1.invest 

and then use -egen, rowmean()- and -egen, rowsd()- on those three variables. Note that the row SD function uses n - 1 not n in the divisor. 

Nick 
[email protected] 

Nick Cox

I think you're right to be puzzled, as on closer inspection -rolling- doesn't offer precisely what you want, so your manual method seems the way forward. But it has a syntax for collecting saved results, which you used, so I too am puzzled by your last question. 

On a small detail, -sqrt()- is to be preferred very marginally to powering with 0.5. 

Ben Hoen

Well Nick, you have blown right past my level of competency.  I could not
figure out how to execute a rolling command correctly.

I did get a manual mean and sd to work, but now I am curious about how to
use rolling, assuming that if I learn to use it for this purpose it will
have many other uses.

Here is the ado file that is producing what I want, manually

use http://www.stata-press.com/data/imeus/grunfeld,clear
keep if company==1
tsset year
g ml3i=(L3.invest + L2.invest + l1.invest)/3
g sdl3i= (((L3.invest-ml3i)^2 + (L2.invest-ml3i)^2 + ///
(L1.invest-ml3i)^2)/3)^.5
label var ml3i "ma lag3 of invest"
label var sdl3i "sd lag3 of invest"
list year invest ml3i sdl3i, sep(20)

And this was what I was working on with .rolling until I gave up:

rolling ml3ri=r(mean) sdl3ri=r(sd), window(3):summarize invest, detail
list year invest ml3ri sdl3ri, sep(20)

Can you see where my error(s) was(were)? Will the rolling command allow me
to add variables like .generate or .egen (the sample ado seemed to discard
the working data for new data - but again, maybe I was not getting it)?

Nick Cox 

. help rolling 

Ben Hoen

I am trying to create a mean and standard deviation of the previous 3
periods in a time-series dataset and can't seem to get it to work.

Here is sample code: 

use http://www.stata-press.com/data/imeus/grunfeld,clear
keep if company==1
g x=int(invest)
g year=year-1934
keep year x
sort year

*ml3x equals average of previous 3 years of x
*g ml3x=?????

*sdl3x equals standard deviation of previous 3 years of x
*g sdl3x=??????

list, sep(20)

Here is what I want to produce:

year	x	ml3x	sdl3x
1	317		
2	391	317	
3	410	354	52
4	257	373	49
5	330	353	83
6	461	332	77
7	512	349	103
8	448	434	94
9	499	474	34
10	547	486	34
11	561	498	50
12	688	536	33
13	568	599	78
14	529	606	71
15	555	595	83
16	642	551	20
17	755	575	59
18	891	651	100
19	1304	763	125
20	1486	983	286

It is the two g commands (or maybe they are supposed to be egen commands)
that I cannot figure out how to do.


*
*   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/

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index