Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • overlaying two hazard rate plots

    Dear All, Suppose that I compute the following hazard rates (unfortunately I cannot share the real data, but here is an example using generic data). I am using Stata 16.
    Code:
    Code:
     use https://www.stata-press.com/data/r17/drug2
    
    
    stset, noshow
    
    global in "yourdirectory"
    cd "$in"
       sts graph if drug ==0, hazard    
        qui graph save "plot1.gph",replace   
       
       sts graph if drug ==1 hazard  
        qui graph save "plot2.gph",replace  
        
         graph combine "$in/plot1.gph" "$in/plot2.gph"
    This gives me a combined graph. Suppose that the dataset used to get the plot for drug==0 is different than the one for drug ==1, that the datasets used to generate each of these plots are too big to be opened at the same time, and I want to overlay them in a similar way as in https://www.statalist.org/forums/for...combine-graphs.

    If these were twoway plots, this should be straightforward, but because I use the sts graph command, the usual add plot command does not work. What I want is to overlay two plots that I previously save as.gph (like the graph combine command but overlaying the plots),

    Any input would be appreciated, thanks!

  • #2
    What you ask seems unnecessary, as sts graph has a -by()- option. So you want:

    Code:
    use https://www.stata-press.com/data/r17/drug2
    sts graph, by(drug) hazard
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	20.8 KB
ID:	1644114






    In any case, you can save the outputs from sts graph using the option -outfile()- . Then combine with twoway, but there should be no need for this.

    Code:
    use https://www.stata-press.com/data/r17/drug2
    sts graph if drug, hazard saving(gr1, replace)  
    sts graph if !drug, hazard saving(gr2, replace)  
    gr combine gr1.gph gr2.gph, saving(gr12, replace)
    *COMBINE
    tempfile out out0
    sts graph if !drug, hazard outfile(`out0', replace) nodraw
    sts graph if drug, hazard outfile(`out', replace) nodraw
    use `out', clear
    gen which=1
    append using `out0'
    replace which=0 if missing(which)
    separate hazard, by(which) veryshortlabel
    tw (line hazard? _t, sort connect(J) title("Smoothed hazard estimate") ///
    xtitle("analysis time") leg(order(1 "placebo" 2 "drug")) saving(gr3, replace))
    gr combine gr12.gph gr3.gph, row(1)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	74.1 KB
ID:	1644113

    Last edited by Andrew Musau; 07 Jan 2022, 23:16.

    Comment

    Working...
    X