# Re: st: RE: How to 'predict' residual by region? Seasonal adjustment?

 From "Joao Ricardo F. Lima" To statalist@hsphsun2.harvard.edu Subject Re: st: RE: How to 'predict' residual by region? Seasonal adjustment? Date Fri, 8 Aug 2008 14:56:05 -0300

```Yes Nick, you are right.

Only after her answer I understood.

JR

2008/8/8 Nick Cox <n.j.cox@durham.ac.uk>:
> This is going round in circles!
>
> Kieran showed how to get just one residual variable.
>
> Nick
> n.j.cox@durham.ac.uk
>
>
> Galina An
>
> Thank you very much, Ricardo, it works well!
> Though it generates several residual variables, which I can collapse
> into 1 afterwards.
>
> Joao Ricardo F. Lima wrote:
>
>>
>> ************************begin example**********************
>> sysuse auto, clear
>> recode rep78 1/2= 3
>> tab rep78 , gen(rep)
>>
>> gen constant = .
>> levelsof rep78, local(levels)
>> foreach levl of local levels {
>>        reg mpg rep1 rep2 trunk if rep78 == `levl'
>>        replace constant=_b[_cons] if rep78 == `levl'
>>        predict x`levl', resid
>> }
>> **************end example************************************
>>
>
>> 2008/8/7 Galina An <ang@kenyon.edu>:
>>
>>> Great! Thank you very much, Kieran!
>
>>> Kieran McCaul wrote:
>>>
>>>> You need modify the code that Richard suggested earlier.
>>>>
>>>>
>>>> gen r == .
>>>> levelsof region_code, local(levels)
>>>> foreach levl of local levels {
>>>>        reg migration t4 t2 t3 if region_code == `levl'
>>>>        predict x, resid
>>>>        replace r = x if region_code == `levl'
>>>>        drop x
>>>> }
>>>>
>>>
>>>> -----Original Message-----
>>>> From: owner-statalist@hsphsun2.harvard.edu
>>>> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Galina An
>
>>>> I have another related question. I need to 'predict' residual by
> region.
>>>>
>>>> When I run the following code it only uses the coefficients from the
> last
>>>> regression to predict residuals:
>>>>
>>>> by region: reg  migration t2 t3 t4
>>>> predict  r,  resid
>>>>
>>>> if I try to do the loop, it says that r is already defined:
>>>>
>>>> levelsof region_code, local(levels)
>>>> foreach levl of local levels {
>>>>        reg migration t4 t2 t3 if region_code == `levl'
>>>>       predict r, resid
>>>> }
>>>>
>
> *
> *   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/
>

--
-------------------------------
Joao Ricardo Lima
Professor
UFPB-CCA-DCFS
+553138923914
-------------------------------
*
*   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/
```