The following command does everything I need it to:
statsby _b _se, saving(File, replace) by(panelid) verbose nodots: regress y x1 x2 x3 x4
Unfortunately, it is pretty slow. I was wondering if there is a way to speed it up with a simple program. I found this program on statalist that computes rolling betas. I think it would be faster (is that right?) Is there a way to alter it to my simple setting, where I am running regressions by a panelid.
The code below seems to work with tsset to do rolling regressions. How is that? Is it the s`w'. command? I have not seen that before.
program rolling_beta
version 11.2
syntax varlist(numeric), window(real)
* get dependent and indpendent vars from varlist
tempvar x y x2 y2 xy xs ys xys x2s y2s covxy varx vary
tokenize "`varlist'"
generate `y' = `1'
generate `x' = `2'
local w = `window'
* generate products
generate `xy' = `x'*`y'
generate `x2' = `x'*`x'
generate `y2' = `y'*`y'
* generate cumulative sums
generate `xs' = sum(`x')
generate `ys' = sum(`y')
generate `xys' = sum(`xy')
generate `x2s' = sum(`x2')
generate `y2s' = sum(`y2')
* generate variances and covariances
generate `covxy' = (s`w'.`xys' - s`w'.`xs'*s`w'.`ys'/`w')/`w'
generate `varx' = (s`w'.`x2s' - s`w'.`xs'*s`w'.`xs'/`w')/`w'
generate `vary' = (s`w'.`y2s' - s`w'.`ys'*s`w'.`ys'/`w')/`w'
* generate alpha, beta, r2, s2
generate beta = `covxy'/`varx'
generate alpha = (s`w'.`ys' - beta*s`w'.`xs')/`w'
generate r2 = `covxy'*`covxy'/`varx'/`vary'
generate s2 = `vary'*`w'*(1 - r2)/(`w' - 2)
end
statsby _b _se, saving(File, replace) by(panelid) verbose nodots: regress y x1 x2 x3 x4
Unfortunately, it is pretty slow. I was wondering if there is a way to speed it up with a simple program. I found this program on statalist that computes rolling betas. I think it would be faster (is that right?) Is there a way to alter it to my simple setting, where I am running regressions by a panelid.
The code below seems to work with tsset to do rolling regressions. How is that? Is it the s`w'. command? I have not seen that before.
program rolling_beta
version 11.2
syntax varlist(numeric), window(real)
* get dependent and indpendent vars from varlist
tempvar x y x2 y2 xy xs ys xys x2s y2s covxy varx vary
tokenize "`varlist'"
generate `y' = `1'
generate `x' = `2'
local w = `window'
* generate products
generate `xy' = `x'*`y'
generate `x2' = `x'*`x'
generate `y2' = `y'*`y'
* generate cumulative sums
generate `xs' = sum(`x')
generate `ys' = sum(`y')
generate `xys' = sum(`xy')
generate `x2s' = sum(`x2')
generate `y2s' = sum(`y2')
* generate variances and covariances
generate `covxy' = (s`w'.`xys' - s`w'.`xs'*s`w'.`ys'/`w')/`w'
generate `varx' = (s`w'.`x2s' - s`w'.`xs'*s`w'.`xs'/`w')/`w'
generate `vary' = (s`w'.`y2s' - s`w'.`ys'*s`w'.`ys'/`w')/`w'
* generate alpha, beta, r2, s2
generate beta = `covxy'/`varx'
generate alpha = (s`w'.`ys' - beta*s`w'.`xs')/`w'
generate r2 = `covxy'*`covxy'/`varx'/`vary'
generate s2 = `vary'*`w'*(1 - r2)/(`w' - 2)
end
Comment