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]

st: RE: Cycling Through Regressions and Storing Residuals


From   "FINERTY A.J.J." <[email protected]>
To   <[email protected]>
Subject   st: RE: Cycling Through Regressions and Storing Residuals
Date   Wed, 13 Feb 2013 23:03:27 -0000

Dear Statalisters,

After some experimenting, I solved the problem by using the following
code, adapted from a response given to another member by Dr Nick Cox.

gen oresid = .
quietly forvalues i = 1/21 {
reg output time time2 if id == `i'
predict newresids, residuals 
replace cresid = newresids if id == `i'  
drop newresids
}

gen cresid = .
quietly forvalues i = 1/21 {
reg govtconsumption time time2 if id == `i'
predict newresids, residuals 
replace cresid = newresids if id == `i'  
drop newresids
}

gen iresid = .
quietly forvalues i = 1/21 {
reg govtinvestment time time2 if id == `i'
predict newresids, residuals 
replace cresid = newresids if id == `i'  
drop newresids
}

Hopefully this will prove useful to someone else one day.

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of FINERTY
A.J.J.
Sent: 13 February 2013 21:43
To: [email protected]
Subject: st: Cycling Through Regressions and Storing Residuals

Dear Statalist members,

I am new to Stata (and Statalist), so please accept my apologies if this
question lacks the finesse of one posed by a more seasoned user. 

I have panel (longitudinal) data on three variables (not that it's
relevant, but the variables are output, general government consumption
and general government investment). Each of these variables is observed
over 21 countries and a maximum of 99 time periods. 

For each country, I want to de-trend each variable quadratically, i.e. I
want to find the residuals of the regression for each country (denoted
by the variable id, ranging from 1 to 21) of e.g. output on time and
time^2.

I'm sure that this must be a common sort of thing to want to do and that
there's some way to implement a foreach loop, but I can't work out what
it is.

One potential complication is that the data are unbalanced; output,
consumption and investment data are not available for every country in
every time period.

I would be most grateful if anyone could help.

Best wishes,

Alistair Finerty.

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