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: looping a regression, exporting the graphs
From 
 
Nick Cox <[email protected]> 
To 
 
"'[email protected]'" <[email protected]> 
Subject 
 
st: RE: looping a regression, exporting the graphs 
Date 
 
Thu, 20 Oct 2011 15:46:15 +0100 
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/