On Feb 2, 2008 1:47 PM, William Bishop <[email protected]> wrote:
> I'm trying to do a rolling regression by id that regresses on previous
> dates and then gets the prediction for the current observation.
>
> The code below does the regressions but the predictions are not made
> after each regression but only after the final regression. So what
> winds up happening is that all the predictions are based off the final
> regression.
>
> forval i = 3/`Max' {
> bys id (date) : regress varlist if _n < `i'
>
> predict temp, xb
> bys id (date) : replace p1 = temp if _n == `i'
> drop temp
> }
>
> I'm looking to have the predictions made after each regression. Can
> this be done using bys?
No, but you can loop over the groups. Perhaps, something like this will help:
webuse grunfeld,clear
qui {
gen predict = .
levelsof com, loc(levels)
foreach l of local levels {
forv i = 5/20 {
reg invest kstock if com == `l' & time <=`i'
predict foo if e(sample)
replace predict = foo if com == `l' & time == `i'
drop foo
}
}
}
Scott
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/