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: RE: RE: looping a regression, exporting the graphs
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
st: RE: RE: RE: looping a regression, exporting the graphs
Date
Thu, 20 Oct 2011 16:46:54 +0100
Good. A detail that is utterly obvious when pointed out, but sometimes missed, is to make sure each graph has some kind of title explaining what is being plotted.
Nick
[email protected]
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Tim Evans
Sent: 20 October 2011 16:31
To: '[email protected]'
Subject: st: RE: RE: looping a regression, exporting the graphs
Nick that worked a treat, I've now reduced 300 lines of code to a more manageable 16 lines - using:
foreach y of varlist Early1-Late3a {
preserve
capture noisily xi: logit `y' i.eth2 age i.invsurg2 i.region i.dep if dep!=9 & sex==2 & ICD10=="C50", or
capture noisily qui do "C:Test\labelling" "O/R of early detection (`y'_UK)"
capture noisily qui graph export "C:\Test\UK_`y'.png", replace
restore
}
foreach y of varlist Early1-Late3a {
preserve
capture noisily xi: logit `y' i.eth2 age i.invsurg2 i.region i.dep i.screening2 if dep!=9 & sex==2 & ICD10=="C50" /*
*/ & !inlist(region, 2, 7, 11) & NEW==2, or
capture noisily qui do "C:Test\labelling" "O/R of early detection (`y'_England_screening_`y')"
capture noisily qui graph export "C:\Test\England_screening_`y'.png", replace
restore
}
I run it twice due to the addition of extra conditions in the second run, I suppose I could create another variable to go in the varlist sequence, but it's probably not worth it and I can at least see what the difference is here, rather than guessing some time later what I have coded up. I used capture noisily because I was finding my analysis was stopping and not graphing (or at least stopping halfway through the run, but I may need to revisit this to see why - I think due to collinearity in the regression but I'll have to take another look.
Best wishes
Tim
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: 20 October 2011 15:46
To: '[email protected]'
Subject: st: RE: looping a regression, exporting the graphs
This is legal code:
foreach y of varlist Early1-Late3a {
xi: logit `y' i.eth2 age i.invsurg2 i.region i.dep if dep!=9 & sex==2 & ICD10=="C50", or
qui graph export "T:\Test\Early1_UK_`y'.png", replace
}
assuming that the gap generates a graph for that variable. You'd need to pass the text "`y'" to the do-file as a an argument. _The do-file cannot see your local macro; it can only receive it as an argument._
The help and documentation for -foreach- are a little thin unless you know the command already. There are longer-winded tutorials at
SJ-3-2 pr0009 . . . . . . . . . . . . . Speaking Stata: Problems with lists
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
Q2/03 SJ 3(2):185--202 (no commands)
discusses ways of working through lists held in macros
SJ-2-2 pr0005 . . . . . . Speaking Stata: How to face lists with fortitude
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
Q2/02 SJ 2(2):202--222 (no commands)
demonstrates the usefulness of for, foreach, forvalues, and
local macros for interactive (non programming) tasks
-- especially 2(2): 202-222 (2002) (I didn't fix that; it just fell out that way....)
As these articles are >> 3 years old, .pdf copies are freely available and available for free at www.stata-journal.com
. search foreach, sj
will give clickable pointers.
Nick
[email protected]
Tim Evans
I am running a number of logistic regression models (Stata 11.2, Windows XP) and then exporting a graph out of each regression using the following as dependant variables:
Early1
Early2
Early1a
Early2a
Late1
Late2
Late3
Late1a
Late2a
Late3a
Each model is run twice, once for the whole dataset and once for a sub-sample, giving me 20 regression commands
The trouble with this is I have a lot of code and its tricky keeping a check of it when I change something in one model that I want to run in later models. I've already stripped out my graph formatting into a separate do file for this purpose.
What I want to know is, is there a way I can bundle up my dependant variables in a loop or similar and then have one regression command that loops around? Crucially to this, is that I also need the graphs that I am labelling to match up to the regression I have run - so some reference to a macro containing the definition (i.e. the name of the dependant variable, along with whether it is for England or UK.
Would I use something like:?
foreach varname of varlist Early1-Late3a {
xi: logit XXX i.eth2 age i.invsurg2 i.region i.dep if dep!=9 & sex==2 & ICD10=="C50", or
qui do "T:\Test\labelling" "O/R of early detection (Early1_UK)"
qui graph export "T:\Test\Early1_UK.png", replace
}
I'm not sure how I would treat the dependant variables in the loop, or how to get the labelling and export to work though or the exporting graph with a new name for each graph.
My models are thus (I've only pasted 2 of the 20!):
***BEGIN***
***EARLY1 Analysis
preserve
xi: logit Early1 i.eth2 age i.invsurg2 i.region i.dep if dep!=9 & sex==2 & ICD10=="C50", or
qui do "T:\Test\labelling" "O/R of early detection (Early1_UK)"
qui graph export "T:\Test\Early1_UK.png", replace
restore
I then run this again, but I'm only interested in a sub-sample of the above
preserve
xi: logit Early1 i.eth2 age i.invsurg2 i.region i.dep i.screening2 if dep!=9 & sex==2 & ICD10=="C50" /*
*/ & !inlist(region, 2, 7, 11) & NEW==2, or
qui do "T:\Test\labelling" "O/R of early detection (Early1_England)"
qui graph export "T:\Test\Early1_England_screening.png", replace
restore
***EARLY2 Analysis
preserve
xi: logit Early2 i.eth2 age i.invsurg2 i.region i.dep if dep!=9 & sex==2 & ICD10=="C50", or
qui do "T:\Test\labelling" "O/R of early detection (Early2_UK)"
qui graph export "T:\Test\Early2_UK.png", replace
restore
I then run this again, but I'm only interested in a sub-sample of the above
preserve
xi: logit Early2 i.eth2 age i.invsurg2 i.region i.dep i.screening2 if dep!=9 & sex==2 & ICD10=="C50" /*
*/ & !inlist(region, 2, 7, 11) & NEW==2, or
qui do "T:\Test\labelling" "O/R of early detection (Early2_England)"
qui graph export "T:\Test\Early2_England_screening.png", replace
restore
AND SO ON.
*
* 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/
_DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or all of which may be confidential or legally privileged. It is for the exclusive use of the intended recipient(s) only. If an addressing or transmission error has misdirected this e-mail and you are not the intended recipient(s), please notify the author by replying to this e-mail. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this e-mail or any attachments, as this may be unlawful.
*
* 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/
*
* 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/