I vote for efficiency whenever possible, but
it is not clear that inefficiency is in
fact a major issue here. Stata's still
going to look at every observation to
decide whether it is true that year == `y'.
I tried the following experiments. You
can try too. Method 1 was actually
_slower_ on my machine, but there's not
much in it. The difference could be an artefact of
something or other, but it doesn't seem
a big deal either way. Of course, a couple
of little experiments are just that.
clear
set obs 10000
gen y = invnorm(uniform())
gen x = invnorm(uniform())
egen group = seq(), to(100)
gen res = .
set rmsg on
* method 1
qui forval i = 1/100 {
reg y x if group == `i'
predict temp if group == `i', res
replace res = temp if group == `i'
drop temp
}
* method 2
qui forval i = 1/100 {
reg y x if group == `i'
predict temp, res
replace res = temp if group == `i'
drop temp
}
Nick
[email protected]
Christopher F Baum
> On Oct 19, 2004, at 2:33, David wrote:
>
> >
> > for each y of local Y {
> > reg y x1 x2 x3 if year==`y'
> > predict temp, residual
> > replace residual=temp if year==`y'
> > drop temp
> > }
> >
>
> Two issues:
>
> (1) it is usually a good idea to 'predict double' when computing
> residuals unless memory is a real issue.
>
> (2) the 'predict' above is overwhelmingly inefficient
> (computationally)
> because it will predict over the whole sample. Use 'predict temp if
> e(sample), residual' to avoid this. The replace if-clause can
> also read
> if e(sample), but that will not matter, since only values computed in
> the predict will be copied.
*
* 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/