Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: looping a regression, exporting the graphs


From   Tim Evans <Tim.Evans@wmciu.nhs.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: looping a regression, exporting the graphs
Date   Thu, 20 Oct 2011 15:34:02 +0100

Hi,

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.

Any ideas greatly appreciated.
_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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index