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

st: Predicting residuals after regressing

From   Yvonne Capstick <>
To   <>
Subject   st: Predicting residuals after regressing
Date   Thu, 7 Feb 2008 13:00:22 -0500


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.

Many thanks,

Climb to the top of the charts! Play the word scramble challenge with star power.
*   For searches and help try:

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