Hi,
I am trying to estimate predicted values and residuals from a regression of y on x1 x2 x3 performed by industry (two digit sic) and year.
Also, the condition is that there are at least 10 observation for each industry/year, otherwise no estimation should be performed
My sample has 29000 observations, two-digit sic goes from 10 to 99 and year spans from 1999 until 2014.
I have tried the following code and seems to work. Does it seem right to the experts?
gen y_hat=. // empty variable for predictions
gen y_res=. // empty variable for residuals
tempvar acc_tot_fitted acc_tot_res // temporary variables for each set of predictions
levelsof sic_2_digit, local(levels)
foreach x of local levels {
foreach z of numlist 1999/2014 {
capture reg y x1 x2 x3 if sic_2_digit==`x' & year==`z' & sic_year_numerosity>9
if !_rc {
predict `y_hat' // predictions are now in temporary variable
replace y_hat=`y_hat' if e(sample) // transfer predictions from temp variable
predict `y_res', residuals // residuals are now in temporary variable
replace y_res=`y_res' if e(sample) // transfer residuals from temp variable
drop `y_hat' `acc_tot_res' // drop temporary variables in preparation for next regression
}
}
}
Also, as a side, I would like to pull out the average R-square from the regressions.
Thanks.
Kind regards
Amedeo
I am trying to estimate predicted values and residuals from a regression of y on x1 x2 x3 performed by industry (two digit sic) and year.
Also, the condition is that there are at least 10 observation for each industry/year, otherwise no estimation should be performed
My sample has 29000 observations, two-digit sic goes from 10 to 99 and year spans from 1999 until 2014.
I have tried the following code and seems to work. Does it seem right to the experts?
gen y_hat=. // empty variable for predictions
gen y_res=. // empty variable for residuals
tempvar acc_tot_fitted acc_tot_res // temporary variables for each set of predictions
levelsof sic_2_digit, local(levels)
foreach x of local levels {
foreach z of numlist 1999/2014 {
capture reg y x1 x2 x3 if sic_2_digit==`x' & year==`z' & sic_year_numerosity>9
if !_rc {
predict `y_hat' // predictions are now in temporary variable
replace y_hat=`y_hat' if e(sample) // transfer predictions from temp variable
predict `y_res', residuals // residuals are now in temporary variable
replace y_res=`y_res' if e(sample) // transfer residuals from temp variable
drop `y_hat' `acc_tot_res' // drop temporary variables in preparation for next regression
}
}
}
Also, as a side, I would like to pull out the average R-square from the regressions.
Thanks.
Kind regards
Amedeo

Comment