I am running regressions on farm economic data which I have set as panel data - each farm has five years' worth of observations. In this effort, I am trying to determine whether a short-run linear cost function (TC = a + bQ, where TC = total cost, a and b are constant, and Q is the quantity produced) or a short-run quadratic cost function (TC = a + bQ + cQ^2, where idem and c is also a constant) would work best with my data. To do so, I want to compare the sum of squared residuals (SSR) for each farm-level regression. I would like to save the residuals resulting from my regressions as a new variable so that I can then calculate the SSRs and compare the two models. Up to now I have the following code for the quadratic regression, which I will use as an example:
Code:
keep if COUNTRY == "XXX" xtset ID YEAR statsby, by(ID) saving (y.XXX.a.dta): xtreg SE131 c.SE281##c.SE281, fe merge m:1 ID using "M:\[...]\y.XXX.a.dta" drop _merge gen RES1 = . quietly bysort ID: xtreg SE131 c.SE281##c.SE281, fe predict temp, residuals replace RES1 = temp drop temp gen RES1_SQ = RES1^2 bysort ID: egen SSR1 = total (RES1_SQ)
Code:
. list in 1/5 +-------------------------------------------------------+ | _b_SE281 _b_cons RES1 RES1_SQ SSR1 | |-------------------------------------------------------| 1. | .0812744 149415.2 -28628.63 8.20e+08 8.13e+09 | 2. | .0812744 149415.2 3330.112 1.11e+07 8.13e+09 | 3. | .0812744 149415.2 -77954.47 6.08e+09 8.13e+09 | 4. | .0812744 149415.2 -30600 9.36e+08 8.13e+09 | 5. | .0812744 149415.2 -16828.83 2.83e+08 8.13e+09 | +-------------------------------------------------------+
Code:
. keep if COUNTRY == "XXX" . xtset ID YEAR . statsby, by(ID) saving (y.XXX.a.dta): xtreg SE131 c.SE281##c.SE281, fe . merge m:1 ID using "M:\[...]\y.XXX.a.dta" . drop _merge . egen group = group(ID) . gen FIT1 = . . su group, meanonly . forval g = 1\`r(max)' { 2. xtreg SE131 SE281, fe if group == `g' 3. predict temp, residuals 4. replace FIT1 = temp if group == `g' 5. drop temp 6. } invalid syntax r(198);
Many thanks,
Guy Low, MSc
0 Response to Predicting and saving residuals after running regressions on several sample units
Post a Comment