I am using stcurve
in Stata to plot survival probability. I need to plot the graph for all data and then for specific variables. I can generate the graphs in two different charts, but I need to have all three lines together in one chart.
I have tried the addplot()
option but I get the error that stcurve
is not a twoway
graph. Do you have any idea how to do this?
This is the code that I have used which generates the graphs in two different charts separately:
stcurve, survival graphregion(lcolor(white) ilcolor(white) ifcolor(white) ) plotregion( lcolor(black)) title("Survival Function", size(vlarge)) ytitle("Survival probabilities", size(large)) xtitle("Time", size(large)) xlabel(,labsize(medium)) ylabel(,labsize(medium))
stcurve, survival at1( def=0) at2( def=1) graphregion(lcolor(white) ilcolor(white) ifcolor(white) ) plotregion( lcolor(black)) legend(label(1 "X Firms") label(2 "Y Firms")) legend(size(large)) lwidth(thin thick) title("Survival Function", size(vlarge)) ytitle("Survival probabilities", size(large)) xtitle("Time", size(large)) xlabel(,labsize(medium)) ylabel(,labsize(medium))
I am not sure if I understood correctly what you want. It would have been useful if you had added the stset
and stcox
code necessary before running stcurve
.
If the Kaplan-Meier hazard graph is identical to your first stcurve, survival
you can try a dirty fix by generating a variable e.g.
sts gen s2=s
after running stset
then plotting it as a line against your time variable. i.e. adding this to the end of the second graph:
addplot(line s2 your_timevar, sort c(J) title("Survival probabilities"))
The equality of KM hazard and Cox hazard only holds if the first graph does not have any more predictors than failvar
in the stset
. So if you ran stcox, estimate
after stset timevar, failure(failvar) id(idvar)
it works, but if you have more variables in the stcox
call this will not give you the correct plot.
edit:
As the above quick solution does not work, there is another dirty workaround: save the results from stcurve
in a file (option outfile
), then plot the "new" data as twoway
graphs. Something like this:
stcurve, survival name("surv1") outfile(stcurve1.dta, replace)
stcurve, survival name("surv2") at1( def=0) at2( def=1) outfile(stcure2.dta, replace)
use stcurve1.dta, clear
rename surv1 surv1_A
rename _t _tA
append using stcurve2.dta
twoway line surv1 _t, sort || line surv1_A _tA, sort
I do not know if this will work with your data: it may be that you need to manipulate the new variables in the outfiles in some way to get the desired results, and you need to add the options you want to the twoway
graphs. There surely are many better and easier ways of plotting this when you have the data for the graphs in separate datafiles, but this is the first solution that sprang to mind.