Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Predicting residuals after regressing


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Predicting residuals after regressing
Date   Thu, 7 Feb 2008 18:14:20 -0000

Your code isn't quite correct in that -predict- will predict
out-of-sample and so you will be overwriting good residuals with your
-replace- statement. 

gen newvar = . 

qui forvalues i = 1/10000 {
	reg y x if companyid == `i'
	predict temp, residuals 
	replace newvar = temp if companyid == `i' 
	drop temp
}

Yvonne Capstick

I am performing regressions company-by-company and would like to save
the residuals. I know that you typically use

"predict newvar, residuals"

to do this. However, predict only works immediately after you have run
the regression. Since I am regressing company-by-company, rather than on
the whole sample, I have to use:

sort companyid
by companyid: reg y x

but I then can't use "predict newvar, residuals" since "predict" only
works after the last regression.

Is there a simple way for me to combine "predict" with the "by
companyid" form? I know an alternative way to do this would be to use 

gen newvar

forvalues i = 1/10000 {

reg y x if companyid == `i'
predict temp, residuals
replace newvar = temp if temp ~= .
drop temp
}

but I wondered if there is a more elegant way to do this ratehr than
hving to loop through all the firms and create and drop a new "temp"
variable.

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



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