Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Re: residuals for each year


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: residuals for each year
Date   Sun, 17 Oct 2004 17:11:03 +0100

(Previous posting premature!) 

Something like this would be my preferred approach. 
 
What may not be apparent is that each call to 
 
tempvar temp 
 
invokes a new temporary variable, which you can 
then refer to it as (clearly) `temp'; any and all 
temporary variables previously referrable to as 
`temp' are now inaccessible. You would have to copy 
their names before you assign your tempname 
to something else to maintain access to them. 

That is, first time around the loop, Stata's 
response to 

tempvar temp 

is to assign a new name (which might be something
like __000007, say) which you and Stata are 
agreeing to refer to as `temp', and this name 
is then used to create a new variable. Second time 
around the loop, the response to the same command 
is to assign yet another a new name, say __0000008, 
and so forth. __000007 is there but you would 
not know from the code how to look at it any more. 

In Kit's case the only consequence will be a bundle of 
41 temporary variables all in memory. Kit may have 
a few Gb to spare, and be unworried by the amount
of memory used. However another way to do it 
is 

gen eps = .
forvalues y = 1960/2000 {
	qui regress y x1 x2 x3 if year==`y'
	tempvar temp
	qui predict double `temp' if e(sample), resid
	qui replace eps = `temp' if e(sample)
	drop `temp' 
}

which cuts down on the memory impact. Kit's doing 
it this way because -predict- does not have a 
-replace- option. In other circumstances, one way to be 
parsimonious with memory is to do with some working variable
exactly what Kit is doing here with -eps-: generating
it outside the loop and replacing each time
around the loop. 
 
Nick 
[email protected] 
 
Christopher F Baum

> > As useful as the by: prefix is, this is an example of where 
> a little bit of Stata programming goes a long way.
> > 
> > forvalues y = 1960/2000 {
> >       qui regress y x1 x2 x3 if year==`y'
> >       qui predict double eps`y' if e(sample), resid
> >      }
> > 
> > If you really want them in different variables. A common 
> > variation that 
> > I have used often that stuffs them all into one variable 
> > (saves a whole 
> > lot of space) is
> > 
> > gen eps = .
> > forvalues y = 1960/2000 {
> >       qui regress y x1 x2 x3 if year==`y'
> >       tempvar temp
> >       qui predict double `temp' if e(sample), resid
> >       qui replace eps = `temp' if e(sample)
> >      }
> > 
> > Note that the 'if e(sample)' will generate residuals only for 
> > in-sample 
> > observations; e.g. if there are 1960 obs that do not enter the 
> > regression due to missing values in the reglist, they will not have 
> > residuals.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index