Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# RE: st: RE: predicting residuals in regression

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject RE: st: RE: predicting residuals in regression Date Wed, 9 May 2012 18:22:00 +0100

```Should be consistent on -residual- or -residuals-.

Nick
n.j.cox@durham.ac.uk

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 09 May 2012 18:19
To: 'statalist@hsphsun2.harvard.edu'
Subject: RE: st: RE: predicting residuals in regression

I wouldn't do it that way. First set aside observations with missing values.

And also singletons.

gen touse = !missing(varA, varB, firmo, year)

bysort touse firmno year : replace touse = 0 if _N == 1

Now

egen group = group(firmno year) if touse

su group
local max = r(max)

gen residual = .

qui forval i = 1/`max' {
reg VarA VarB if group == `i'
predict temp, residuals
replace residuals=temp if group == `i'
drop temp
}

Nick
n.j.cox@durham.ac.uk

Rudy Hover

oh i'm sorry that was a typo, the code is

gen residuals=.
levelsof firmno, local(firmnolist)

forvalues y=1999/2006 {
foreach f of local firmnolist {
reg VarA VarB if firmno==`f' & year==`y'
predict temp, residuals
replace residuals=temp if firmno==`f' & year==`y'
drop temp
}
}

I found out that the regression is running, but only for (some of) the year 1999 the residuals are generated.

However after 26 iterations which look like

405566 missing values generated
12 real changes made

Stata errors with:
no observations
r(2000);

The 12 real changes are correct: that's the 12 months per year for which it creates residuals. How do I find out for what variable it finds no observations?

On May 9, 2012, at 6:59 PM, Nick Cox wrote:

> Note that your inner loop is
>
> foreach f of local firmnolist
>
> whereas you started with
>
> levelsof permno, local(permnolist)
>
> I guess you need
>
> levelsof firmno, local(firmnolist)
>
> before your loops. Otherwise the macro -firmnolist- will be empty and no regressions will be carried out.
>
> Nick
> n.j.cox@durham.ac.uk
>
> Rudy Hover
>
> I have panel on thousands of firms over 6 years. I want to regress using the foreach command:
>
> gen residuals=.
> levelsof permno, local(permnolist)
>
> forvalues y=1999/2006 {
> foreach f of local firmnolist {
> reg VarA VarB if firmno==`f' & year==`y'
> predict temp, residuals
> replace residuals=temp if firmno==`f' & year==`y'
> drop temp
> }
> }
>
> However I'm getting a variable residuals only containing missing values (.)
> What am I doing wrong?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```