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]

RE: st: using foreach in regression models - combing graphs from the different variables


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: using foreach in regression models - combing graphs from the different variables
Date   Tue, 25 Oct 2011 17:00:43 +0100

-graph combine- is fussy about " ". Once again, please see the help. I put them in the previous code because you had spaces, but I think you need .gph as well the way you did it. 

Nick 
n.j.cox@durham.ac.uk 


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Tim Evans
Sent: 25 October 2011 16:46
To: 'statalist@hsphsun2.harvard.edu'
Subject: RE: st: using foreach in regression models - combing graphs from the different variables

Nick,

Maybe this answers your comment about saving - this is stated in the code below:


    foreach var of varlist agegrp id07 NRCENT site__4  {
	di as text "Does `var' affect being seen first in a specialist centre? (Unadjusted)"
		cap noi logistic firstcentrespec2 i.`var'
		preserve
		cap noi qui do "C:\labelling" /*
		*/	"O/R of surgery (`var')"
		capture noisily qui graph save "C:\Test\Unadjusted_`var'_firstseenspec", replace	
		restore
		di as text "End of Does `var' affect you seeing a specialist (ever)? (Unadjusted)"
		local files `files' `"Unadjusted_`var'_firstseenspec"'  
	}

	graph combine `files'

When I do try this, Stata returns the following error:

Unadjusted_agegrp_firstseenspec is not a memory graph
r(198);

This is the code from the .do file that draws the graphs (and otherwise not shwon) - I currently save the graph when back in the active .do file

twoway bar estimate0 axis, xscale(log) base(1) horizontal barw(0.5) color(gs13) ///
		xline(1) yla(1/`=_N', noticks valuelabel labsize(small) ang(h)) xla(, labsize(small)) ||             ///
		  rcap min95 max95 axis, horizontal               ///
  ||   bar estimate1 axis, xscale(log) base(1) horizontal barw(0.5) color(red) ///
		xline(1) yla(1/`=_N', noticks valuelabel labsize(vsmall) ang(h))  ||             ///
		  rcap min95 max95 axis, horizontal               ///
		legend(order(3 "Significant" 1 "Not significant") size(small)) ytitle("") ///
  xtitle("Odds ratio", size(small)) ytitle("") title(`1',size(small)) 


Best wishes

Tim


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 25 October 2011 16:22
To: 'statalist@hsphsun2.harvard.edu'
Subject: RE: st: using foreach in regression models - combing graphs from the different variables

The help for -graph combine- explains that you can give the details in various forms. You choose. 

Otherwise what you have looks right at first glance -- so long as the do-file, not shown here, does include a -graph save- to the right place. That's a big if: asking for comments on code we cannot see as a whole is clearly asking a lot! 

The only real test is to try it. 

Nick 
n.j.cox@durham.ac.uk 


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Tim Evans
Sent: 25 October 2011 16:08
To: 'statalist@hsphsun2.harvard.edu'
Subject: RE: st: using foreach in regression models - combing graphs from the different variables

Hi Nick,

Thanks for your comments. I'm stuggling with a) the placement of the local files command, and b) `"<details of new file.....>"' - I'm just not getting what I need to place in that part - is this the file directory?

I tried a few things namely this:

loc covariates "i.NRCENT i.site__4 sex i.id07 i.yydx"
	foreach var of varlist agegrp id07 NRCENT site__4  {
	di as text "Does `var' affect you getting surgery (any from HES) (Adjusted)"
	cap noi logistic everrcsurgeryhes2 i.`var' `covariates'
	preserve
		qui do "C:\TEST \labelling" "O/R of surgery (any from HES) (`var')"
		qui graph export "C:\Test\Adjusted_`var'_surgery (any from HES).png", replace	
		restore
		di as text "End of Does `var' affect you getting surgery (any from HES) (Adjusted)"
		local files `files' `"Unadjusted_`var'_firstseenspec"'  
	}

graph combine `files'

I'm probably barking up the wrong tree though. I assumed I would 'collect' all the files at the end of, but still in the loop.

Point taken regarding spaces in file names....


Best wishes

Tim


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 25 October 2011 15:49
To: 'statalist@hsphsun2.harvard.edu'
Subject: RE: st: using foreach in regression models - combing graphs from the different variables

There are various problems here, if I understand you correctly. One is that the -graph combine- statement must be outside the -foreach- loop and not inside it. A second is that you are just asking -graph combine- to work on one graph, so it is not at all surprising that this does not work. A third is that -graph combine- will not work on *.png! 

A way forward is to accumulate filenames within the loop so that you have a statement something like 

local files `files' `"<details of new file each time round the loop>"'  

and then outside the loop you go 

graph combine `files' 

In essence, you have to name the files, or equivalently the graphs, to be -combine-d. This is quite explicit in the help. 

I see here only code that if corrected would produce 4 graphs.  

It's no doubt a generational thing, but I would never put spaces in filenames in Windows, even though it's allowed, as it's more likely to bite me than to benefit me. I see here "C:\TEST \" and "C:\Test\"; I don't know how Windows would react to that. 

.png as final thing to be exported is your concern. 

Nick 
n.j.cox@durham.ac.uk 

Tim Evans

I have modified my regression commands in order that I can loop through the variables and graph them. I'm stuck on how to use graph combine to combine these graphs into one page (5 graphs on each page), following on the from the loop as follows:

BEGIN CODE

loc covariates "i.NRCENT i.site__4 sex i.id07 i.yydx"
	foreach var of varlist agegrp id07 NRCENT site__4  {
	di as text "Does `var' affect you getting surgery (any from HES) (Adjusted)"
	cap noi logistic everrcsurgeryhes2 i.`var' `covariates'
	preserve
		qui do "C:\TEST \labelling" /*
		*/ "O/R of surgery (any from HES) (`var')"
		qui graph export "C:\Test\Adjusted_`var'_surgery (any from HES).png", replace	
		restore
		di as text "End of Does `var' affect you getting surgery (any from HES) (Adjusted)"
		graph combine "C:\Test\Unadjusted_`var'_ any surgery from HES.png" ****THIS DID NOT WORK***
  
	}



I don't know whether I can use regexm to say the filename begins with 'Unadjusted' and ends with 'from HES' and incorporate this into the graph combine, but essentially I don't explicitly name the graphs as they are stored and don't want to name them explicitly (by this I mean type each one out as I may in future add more to my varlist).

I'm assuming I will have to save as .gph before saving the final version as a png if that's my chosen format?

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


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