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   Wed, 1 Dec 2010 20:55:54 +0000

I am pleased that you are making progress and not so pleased to learn that my code is difficult to understand. 

You are probably confusing the roles of -graph save- and -graph export-. 

-graph save- does not have options other than -asis- and -replace-. This is explicit in the help. You can specify file extensions such as .png -- I guess that is what you mean -- but it makes no difference. The file produced is still a Stata graph file and external programs will not be able to understand it as an example of the file format you invoke. This is on all fours with the fact that naming a dog "cat" will not turn it into a cat. Naming a .gph file with another extension is allowed, but serves no useful purpose. 

To see this, open your file in Stata's do file editor. You will see a readable ASCII file with instructions that Stata (and only Stata) can understand. 

It's been standard in Stata for at least 20 years that saving a graph file means "saving it in Stata's own proprietary format". 

-graph export- is, I believe, what you seek. 

Nick 
[email protected] 

Rodrigo Briceño

Nick, after two days of trial and error I finally understood the
solution you proposed me. I will post here my code just in case any
other person find it useful. As usual when making progress some other
problems come around. I have tried the graph save command with the
options: wmf, emf, tiff, png and pdf, but none of them allowed me to
see the graph produced. The loop generated the graphs (individual
files) but when I double click on them or try to insert-picture in
Microsoft Office applications (word-excel) I can't see the graph. Do
I'm missing something on my code (as an example I put the pdf option
here...but I tried all)?

gen resid = .
foreach i of local isin {
quietly regress liq `X' if isin2==`i'
predict ehat`i' if e(sample), res
twoway (tsline ehat`i'), name (gr_`b`i'') nodraw
graph save "D:\data\grafico_isin_`i'.pdf", replace
list ehat`i' if isin2==`i'
}

foreach i of local isin2 {
quietly regress liq `X1' if isin2==`i'
predict ehat`i' if e(sample), res
twoway (tsline ehat`i'), name (gr_`b`i'') nodraw
graph save "D:\data\grafico_isin2_`i'.pdf", replace
list ehat`i' if isin2==`i'
}

Thanks Stata!

2010/11/29 Nick Cox <[email protected]>:
> You seem to be making progress but I am mystified by your puzzlement: "maybe Stata won't allow me to produce this table of residuals automatically".
>
> Not so. You are already using -predict- to save the residuals. As Maarten has already suggested, you can use (e.g.) -list- to list the residuals or -outsheet- to export them to a file. If neither is what you want, you need to explain precisely why.
>
> Nick
> [email protected]
>
> Rodrigo Briceño
>
> Thanks again Nick. I reviewed your example and I have a clearer idea
> now. Regarding your recommendations/comments
>
> 1. Maarten and you are totally right
> 2. Yes, that was not proper. I did this because I have two sets of
> regressions, one set includes one more variable than the other. Since
> I had the wrong idea about how residuals were stored I decided to do
> it separately, but as you mention this is not right. I corrected
> already that.
> 3. I'm talking about MS Excel (sorry again). My need: to copy
> repeteadly a set of 54 residuals derived from the regressions that I
> ran. I think that the problem is that maybe Stata won't allow me to
> produce this table of residuals automatically. What I wanted is an
> automatic procedure that execute a similar process like the one
> produced by estimates store, and estout. You run regressions, collect
> coefficients (estimate store) and produce a table of coefficients
> (estout). My analogy: run regressions, produce residuals, collect
> residuals from each regression and then produce a table of residuals
> for each regression (each regression is a column with 54 rows-
> corresponding to residuals)
>
> I appreciate all your effort trying to understand me and providing me help.
>
>
> 2010/11/26 Nick Cox <[email protected]>:
>> 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