# st: Rolling your own [was: Programming question]

 From "Scott Merryman" To Subject st: Rolling your own [was: Programming question] Date Fri, 4 Mar 2005 19:35:44 -0600

```If you clone -rollreg- and alter line 402 so it reads
local f =`in1' -1

local lower = `lower' -1

you will now get coefficients for observations 1/5.

Below, is another way of running a rolling regression and calculating the
standard deviation of the residuals.

gen b_x1 =.
gen b_x2 = .
gen b_cons =.
gen sd_residual =.
local max = 0
local min = 1
qui  {
count
local total = r(N)
levels id, local(levels)
foreach l of local levels {
sum y if id == `l'
local max = r(N) + `max'
disp `l'
forv i = `min'/`=`max' -4'  {
local j = `i' + 4
if `j' <= `total' {
reg y x1 x2 in `i'/`j'
tempvar  res
predict `res' if e(sample), res
sum `res'
replace sd_residual = r(sd) in `j'
replace b_x1 = _b[x1] in `j'
replace b_x2 = _b[x2] in `j'
replace b_cons = _b[_cons] in `j'
}
}
local min = `min' + `max'
}
}

Hope this helps,
Scott

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-
> statalist@hsphsun2.harvard.edu] On Behalf Of Cameron Hooper
> Sent: Friday, March 04, 2005 3:36 PM
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: Programming question
>
> Hi
>
> I agree that rollreg is probably the best solution. I started with Kit
> Baum's routine and just got to wondering if I could produce a quick and
> dirty solution that would work for my data. Since I knew nothing about
> programming in Stata, the dirty aspect has been realized but not the
> quick! Still, its been a good learning exercise and thanks to the
> replies I've received from people on this list I've learnt a lot in a
> short period of time.
>
> Cheers,
>
> Cameron
>

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