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: RE: AW: RE: AW: Regressing and storing residuals in one line.


From   Dani Tilley <[email protected]>
To   [email protected]
Subject   Re: st: RE: AW: RE: AW: Regressing and storing residuals in one line.
Date   Mon, 28 Jun 2010 08:43:36 -0700 (PDT)

If I define a tempvar and drop it at the end of the loop, can I still refer to it elsewhere in the program (i.e. outside the loop)?



----- Original Message ----
From: Nick Cox <[email protected]>
To: [email protected]
Sent: Mon, June 28, 2010 10:32:01 AM
Subject: st: RE: AW: RE: AW: Regressing and storing residuals in one line.

If you are doing this lots of times for real, you could end up with
storage problems with dozens of temporary variables. If that doesn't
bite, then OK. 

Nick 
[email protected] 

Martin Weiss

The 

*************
drop `foo'
*************

line could be safely omitted, btw. Stata just makes up new tempnames,
and
discards them all at the conclusion of the do-file.

Nick Cox

Such residuals have rather poorly defined properties, but let's set that
on one side. 

A single variable can be obtained through a minor variation on Martin's
recipe: 

sysuse auto, clear
qui levelsof rep78
gen residual = . 

foreach lev in `r(levels)'{
    tempvar foo 
    qui regress price weight length if rep78==`lev'
    predict `foo', res
    replace residual = `foo' if rep78 == `lev'
    drop `foo' 
}

Nick 
[email protected] 

Martin Weiss

Just loop through the thing:

*************
sysuse auto, clear
qui levelsof rep78

foreach lev in `r(levels)'{
    qui regress price weight length if rep78==`lev'
    predict res`lev', res
}
*************

Dani Tilley

I'm trying to run several regressions (one for each level of a
categorical
variable) and store the residuals from each regression in a local macro
or
new 
variable I could later manipulate. I figured I could use:

bysort category: regress y x1 x2 

to run the regressions, but I need a second line of code (predict name,
residuals) to get the residuals when bysort allows only one. Is there a
way
around this? 


*
*   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/


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