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]

st: RE: loop using rolling, recursive


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: loop using rolling, recursive
Date   Tue, 14 Dec 2010 17:27:54 +0000

I am surprised at your report that this works at all. 

foreach x x1 x2 x3 x4 { 

won't work because it does not match any allowed syntax for -foreach-. That could be 

foreach x in x1 x2 x3 x4 { 

Perhaps you copied that down incorrectly. 

Also, if -rolling- creates -sd_`x'-, why try to create it using -generate-? That will fail as -sd_`x'- already exists. 

Beyond that, this loop won't work because second time around the request 

gen date = end + 1

will fail as -date- already exists. You should get an error message to that effect. As you do not define the variable -end- in your posting, it is not clear what you want that command to do. 

Beyond that, the intent of your code is still not clear to me. The result r(sd) will be the last standard deviation calculated by -summarize-; putting that constant into a variable is not illegal but not usually helpful. 

You are trying to write something much more complicated than you understand. Try first to write a very simple loop using -rolling-. Then see what else you need. 

Nick 
[email protected] 

[email protected]

I'm trying to make a loop to calculate the standard deviation of a variable using the rolling, recursive command. My database is from 1990Q1-2009Q2. My final goal is to calculate the standard deviation of a set of variables using a recursive scheme (calculate for the second half of my sample using info from the first half while keeping the first observation fixed). So I want to compute the standard deviation for 1999Q4 using info from 1990Q1-1999Q3, then for 2000Q1 using info from 1990Q1-1999Q4 and so on.

The code below is only working for the first explanatory variable. Any ideas of how to create a loop to calculate the standard deviation using a recursive scheme?

foreach x x1 x2 x3 x4 {
rolling sd_`x'=r(sd), recursive window(39) : summarize `x'
gen sd_`x'= r(sd)
gen sd2_`x'=sd_`x'*2
gen date=end+1
format date %tq
}


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