Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: moving window regression

From   Christopher F Baum <>
Subject   st: Re: moving window regression
Date   Fri, 4 Jun 2004 11:11:44 +0100

On Jun 3, 2004, at 7:33 AM, Michael wrote:

Subject: st: Re: Beginner needs help trying to run rolling regressions in STATA 8 (in windows)

I wrote an ado back in 1999 (Stata v6) that does rolling regressions and
fills in the predicted value for each point.

program define rollpred
version 6.0
syntax varlist , [begin(int 200) end(int 10)] gen(str)
confirm new var `gen'
gen `gen'=.
tempvar pred
local obs=_N
local i=1
local j=`i'+(`begin'-`end')
local n=`i'+`begin'
quietly {
while `j'<`obs' {
reg `varlist' in `i'/`j'
cap drop `pred'
predict `pred' in `n'
replace `gen'=`pred' in `n'
local i=`i'+1
local j=`j'+1
local n=`n'+1

For each datapoint, it does a regression on the points starting "begin"
points before it and ending "end" points before it. It then uses predict to
get a prediction for the current point. You could use this as a start for
what you want to do.

A code fragment, not a full program, that does something of this sort is available as a class handout

I will have more to say on the subject at this month's SUGUK meeting in London.


* For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index