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: Rolling Regressions


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: Rolling Regressions
Date   Thu, 17 Mar 2011 17:51:22 +0000

You are basically correct about being incorrect. 

Here 

if count_month==60 {

would be interpreted by Stata as 

if count_month[1] == 60 {

which is not what you want. 

But Stata [sic] allows this. It is perfectly legal. It just does not do what you want. 

As a matter of technique, if you just want a single count, then putting the result in a variable, as in

egen count_month=count(retrf) if year<`i'&year>=(`i'-5)&sic2==`j'

is unnecessary, as 

count if retrf < . &  year <`i' &year>=(`i'-5) &sic2==`j'

would produce a result in r(N) that you could use. 

Other postings in this thread have pointed you to better solutions. 

Nick 
[email protected] 

Philipp Krueger

I have monthly data and I am trying to run regressions on rolling 
windows of the past 60 months (years: t-5). The resulting beta 
coefficient should be saved into an existing variable for the months of 
the current year t. I think that I am trying to use the “if command” as 
if it was an “if qualifier”, something which STATA apparently does not 
allow.  My logic is to run the regression whenever 60 months of data are 
available in a period “t” for group “j” and if there is no data 
available for the last 60 months, setting the variable beta to missing. 
Unfortunately, my code does not work:

gen beta=.
forvalues i=1986(1)2000 {
disp "year `i' "
foreach j of numlist 1 2 7{
egen count_month=count(retrf) if year<`i'&year>=(`i'-5)&sic2==`j'
	
if count_month==60 {
	reg retrf mktrf if year<`i' &year>=(`i' -5)&sic2==`j'
	quietly mat b=get(_b)
	quietly replace beta=b[1,1] if year==`i' &sic2==`j'
}
else {
	quietly replace beta=. if year==1986&sic2==1
}
drop count_month
	}
}


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