Dear Statalist,

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)
However, I have noticed that the sums of the residuals by farm are far from equal to zero, which has led me to believe that the above code is incorrect: below is an example of this from one farm:

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 |
     +-------------------------------------------------------+
Alternatively, I tried to use code as suggested in previous posts for the same aim (such as: https://www.stata.com/statalist/archive/2008-02/msg00296.html ; https://www.statalist.org/forums/forum/general-stata-discussion/general/491152-predicted-values-and-residuals-with-by ; https://www.stata.com/support/faqs/d...ach/index.html), but I seem to still be having problems. I tried to use the following code but to no avail:

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);
I am not sure what could be the reason behind the error, or if there is a better way to do what I want, but I will appreciate any and all help and advice on the matter.

Many thanks,

Guy Low, MSc