Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: predict and resid in a nested loop


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: predict and resid in a nested loop
Date   Wed, 31 Jul 2013 18:08:10 +0100

The bad news is that there are bugs everywhere. The good news is that
they seem easy to spot. The important news is that to learn how to
debug yourself you must start using

set trace on
set traced 1

and look at the output line by line. Then you can see what Stata sees and why.

Assume that when you start your code no variable called -f- exists.

Then the first time you try

predict f

that should work. But the second time you try it, it won't work,
because -f- already exists and -predict- requires a new variable name.

You would get the same problem with -predict r-, except that Stata
bailed out first.

The solution is to

    drop f r

once you have used them.

I guess varlist not allowed r(101); arises because you have an
unnecessary space in the -predict- command giving the impression that
you are trying to specify two new variable names. (Similar errors can
be spotted in several places).

You have a line

  predict f  if `i' `y'

which first time it is met becomes

 predict f if 1 1989

Stata is happy with

if 1

which doesn't bite, but "1989" by itself makes no sense. Basically,
your line is incomplete. You needed something like

predict f if industry == `i' & year == `y'

Nick
[email protected]


On 31 July 2013 17:43, Nahla Betelmal <[email protected]> wrote:
> Dear Statalist,
>
> I am trying to get the fitted values and residuals from
> cross-sectional regression in a nested loop ( regression per industry
> and year). I tried four ways according to my search in previous
> threads, but obviously I am missing something!
>
> In the first two loops I got the error message : f already defined  r(110);
>
> In the third one I got the error message: varlist not allowed r(101);
>  (I also put the & sign between i and y, but same error)
>
> In the last one I got the error message : invalid '1989' r(198);
>
>
>
>
>
> gen fitted=.
> gen resi =.
> forval y=1989/2012 {
>       forval  i= 1/55 {
>          reg DV IV_1 IV_2 IV_3 if  industry== `i' & year==`y'
>                     predict f
>                      predict r, resid
>                           replace fitted= f if industry== `i' & year==`y'
>                           replace resi = r  if industry== `i' & year==`y'
>          }
> }
>
> f already defined  r(110);
>
>
> forval y=1989/2012 {
>       forval  i= 1/55 {
>          reg DV IV_1 IV_2 IV_3 if  industry== `i' & year==`y'
>                     predict f if industry== `i' & year==`y'
>                      predict r if industry== `i' & year==`y', resid
>          }
> }
>
> f already defined  r(110);
>
>
>
> gen fitted=.
> gen resi =.
> forval y=1989/2012 {
>       forval  i= 1/55 {
>          reg DV IV_1 IV_2 IV_3 if  industry== `i' & year==`y'
>                     predict f `i' `y'
>                      predict r `i' `y', resid
>                           replace fitted= f if industry== `i' & year==`y'
>                           replace resi = r  if industry== `i' & year==`y'
>          }
> }
> varlist not allowed r(101);
>
>
>
> gen fitted=.
> gen resi =.
> forval y=1989/2012 {
>       forval  i= 1/55 {
>          reg DV IV_1 IV_2 IV_3 if  industry== `i' & year==`y'
>                     predict f  if `i' `y'
>                      predict r if `i' `y', resid
>                           replace fitted= f if industry== `i' & year==`y'
>                           replace resi = r  if industry== `i' & year==`y'
>          }
> }
>
> invalid '1989' r(198);
>
>
>
> Thank you, your help is highly appreciated
>
> Nahla Betelmal
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index