You could use -foreach- to loop across the variables, generate the residuals
for each variable, and use -foreach- again to loop across the residuals for
the second stage.
It is usually a bad idea to save estimated coefficients and R2 values as
variables.
Using an example Kit Baum gave two weeks ago for similar question
(http://www.stata.com/statalist/archive/2005-07/msg00204.html) you could do
something like :
sysuse auto,clear
mat res = J(2,2,0)
local i = 1
foreach var of varlist mpg price {
qui reg weight `var'
qui predict res_`var' , res
mat res[`i', 1] = _b[`var']
mat res[`i', 2] = e(r2)
local rn "`rn' `var'"
local ++i
}
mat colnames res = beta r2
mat rownames res = `rn'
mat list res
mat res2 = J(2,2,0)
local i = 1
foreach var of varlist mpg price {
qui reg gear res_`var'
mat res2[`i', 1] = _b[res_`var']
mat res2[`i', 2] = e(r2)
local ++i
}
mat colnames res2 = beta_2 r2_2
mat results = res, res2
mat list results
Hope this helps,
Scott
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of kelly johnson
> Sent: Monday, July 25, 2005 10:37 AM
> To: [email protected]
> Subject: st: Multi-stage OLS loop
>
> Hi,
>
> I have a quick question abotu writing a loop for a multi-stage OLS
> regression.
>
> Suppose I have data as follows:
>
> Date Var1 Var2 ... var20 Var21
>
> with about 1500 observations for each.
>
> How can I can i write a quick loop to do the following:
>
> regress var 2-var20 on var1, individually
>
> (ie. reg Var1 Var2
> reg Var1 Var2....
> reg Var1 Var20)
>
> and save the beta, r-square [e(r2)], and residuals.
>
> In the second stage, I want to reress Var21 on the residuals from stage 1,
> saving the beta and r-square.
>
> How can I make the out put be like this:
>
> Var1 beta (from stage 1) r-square (from stage 1) beta (from stage 2)
> r-square (from stage 2)
> ...
> Var20 beta (from stage 1) r-square (from stage 1) beta (from stage 2)
> r-square (from stage 2)
>
> Thanks!
>
> fuaad
>
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today - it's FREE!
> http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>
> *
> * 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/
*
* 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/