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: saving residuals for many regressions


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: saving residuals for many regressions
Date   Fri, 26 Nov 2010 17:05:11 +0000

Your code and your comments don't seem to match up fully. My tentative conclusion is that you don't fully understand what your code is doing. 

1. If you -generate- a variable -resid- to hold residuals and then replace it group by group with residuals from each regression, then after a loop it will hold all the residuals, generated groupwise. Contrary to your statement, -resid- does not just contain the last set of residuals.  

2. However, if you also -drop resid- afterwards, then clearly it will no longer be available. I must be misunderstanding one of your questions as it seems obvious to me that you shouldn't -drop- it. 

3. There is reference here to "excel", presumably other software called MS Excel, on which I don't offer advice on the list. The key question for anyone prepared to help on that score is at exactly what point(s) you are copying to Excel, which is not made clear in this post. 

Perhaps if you work with simpler examples, you will understand better. 

sysuse auto, clear
gen res = . 
l res 
gen gpm = 1 / mpg
regress gpm weight if foreign
predict ehat1, res 
replace res = ehat1 if e(sample)
l res ehat1 foreign
regress gpm weight if !foreign
predict ehat0, res
replace res = ehat0 if e(sample)
l res ehat1 ehat0 foreign 

Nick 
[email protected] 

Rodrigo Briceño

Nick, thanks for your advice. I actually bought "The workflow of data
analysis using stata" that has very useful information for me.

Please let me explain what I was doing:

1. run a regression, produce the residuals, list them and copy to
excel, produce the graph of residuals against time and copy in excel

2. I wrote a mail to the list and Maarten helped me to produce a loop
for doing this process automatically. The good: the 7 graphs were
produced automatically. The bad: the set of residuals for each
regression is replaced each time that the loop started again: at the
end Iam just able to get the last set of residuals (from my last
regression)

3. What is my last desire: to find a way to run the loop, collect each
set of residuals (don't know how) and produce the 7 graphs. My last
goal is to have the full set of residuals (for the 7 regressions) in
excel, and the 7 graphs.

So far I have:

set more off
loc X = "sap liqmk m2-m12 mes pprom"
loc X1= "sap liqmk m2-m12 mes pprom dv"
local isin "5 103 107"
local isin2 "164 274 282 214"
local b5 "CRALDSFH0070"
local b103 "CRCBANEA0019"
local b107 "CRCIMP0A0053"
local b164 "CRG0000B0210"
local b274 "US105756AR10"
local b282 "US195325BE41"
local b214 "CRGNACIB0043"

gen resid = .
foreach i of local isin {
quietly regress liq `X' if isin2==`i'
predict ehat if e(sample), res
replace resid=ehat if e(sample)
twoway (tsline ehat), name (gr_`b`i'')
list ehat if isin2==`i'
drop ehat
}
drop resid

gen resid = .
foreach i of local isin2 {
quietly regress liq `X1' if isin2==`i'
predict ehat if e(sample), res
replace resid=ehat if e(sample)
twoway (tsline ehat), name (gr_`b`i'')
list ehat if isin2==`i'
drop ehat
}
drop resid

Thanks for your time.

2010/11/26 Nick Cox <[email protected]>:

> Rodrigo might find going through an introductory text on Stata useful. There are several suggestions on the StataCorp website, but my personal favourite has to be Larry Hamilton's text, which got me into Stata in the first place.
>
> However, as Rodrigo is using 9.2, if I recall correctly, slightly out-of-date editions will be best for him.
>
> See http://www.stata.com/bookstore/statabooks.html for the present crop.

Maarten buis

> --- On Thu, 25/11/10, Rodrigo Briceño wrote:
>> I don't know how to save the residuals of each
>> regression. In your example my question is which
>> command to use to save a table with the 74 residuals
>> estimated for each of the 4 regressions.
>
> If you want a display in the form of a table, type
> -list rep78 resid-, if you want to export it, use
> your favourite way of exporting Stata datasets,
> e.g. -outsheet- or -xmlsave-.
>

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