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 21:25:17 +0100

```I can't see that e(sample) would make any difference here.

You want lots of regressions.

The code could be made faster by using -in- rather than -if-.

As for the rest, it seems that you are ignoring my most recent advice.

Nick

On Wed, May 9, 2012 at 8:09 PM, Rudy Hover <rudy.hover@gmail.com> wrote:
> Thank you Nick. Actually my Stata SE is running your command for about half an hour now and my computer is getting hot, I doubt whether that is a good sign. However I have 400,000 firm/month observations, I guess the number of regressions could be an overload. Could it perhaps be more efficient to use the e(sample) command?
>
> I'm thinking of
>
> 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 if e(sample)==1, residuals
> replace residuals=temp if firmno==`f' & year==`y'
> drop temp
> }
> }
>
> So I use the foreach loop within the forvalues command. The problem is it still ends with the 'no observations' error somewhere.
>
>
>
> On May 9, 2012, at 7:18 PM, Nick Cox wrote:
>
>> 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
>>
>> 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/
```