/* Plot observed vs. predicted survival curves by categories of X */ /* 12/20/96 J.M.Garrett STB-35 gr23 */ /* Form: stcoxkm, by(xvar) options */ /* Must have stset data before using this command */ /* Options allowed: noshow, symbol, connect, titles, labels */ program define stcoxkm version 5.0 st_is #delimit ; local options "BY(string) L1title(string) L2title(string) NOSHow YLAbel(string) * "; #delimit cr local if "opt" parse "`*'" preserve local t: char _dta[st_t] local d: char _dta[st_d] capture keep `if' keep `by' `d' `t' quietly drop if `t'==. | `d'==. | `by'==. local xlbl : variable label `by' if "`xlbl'"=="" {local xlbl="`by'"} local timelbl : variable label `t' * Create dummy variables from X and create variable lists quietly tab `by', gen(x) local numcat=_result(2) local i=1 while `i'<=`numcat' { if `i'>1 {local xlist `xlist' x`i'} local varlbl`i' : variable label x`i' local i=`i'+1 } * Create observed survival curves for each category of x local i=1 while `i'<=`numcat' { quietly sts gen obs`i'=s if x`i'==1 format obs`i' %3.2f label var obs`i' "Observed: `varlbl`i''" local i=`i'+1 } * Run Cox model and create survival probabilies if "`noshow'"=="" {st_show} quietly stcox `xlist', bases(bsurv) predict rh local i=1 while `i'<=`numcat' { quietly gen surv`i'=bsurv^rh if x`i'==1 format surv`i' %3.2f label var surv`i' "Predicted: `varlbl`i''" local i=`i'+1 } label var `t' "`timelbl'" * Plot results if "`l1title'"=="" { local l1title "Observed vs. Predicted Survival Probabilities" } if "`l2title'"=="" {local l2title "By Categories of `xlbl'"} if "`ylabel'"=="" {local ylabel "ylabel(0,.25,.50,.75,1)"} else local ylabel "ylabel(`ylabel')" graph obs* surv* `t', sort `ylabel' l1("`l1title'") l2("`l2title'") `options' end