Hi,
I've been trying to overlay a couple lines (and their confidence intervals) on top of each other on the same plot using the twoway command. I have three waves of data on dementia probability and respondent death that I've reshaped into a long dataset to run cox models.
The first line is for the linear prediction of hazard ratios from the cox model, while the other line is the non-linear predicted hazard ratios using restricted cubic splines. I would (ideally) also like to label the knots on the graph as well. However, I've found that whenever I try to add a second x-axis on to the top of the graph with the knot values, it seems to shift the lines/confidence intervals/labels ever so slightly. Essentially, the end result is that things are slightly mis-aligned on the graph which looks wonky. Has anyone encountered this before?
I've included my code below (as well as a picture too) though unfortunately the full dataset doesn't fit...
Thanks!
David
I've been trying to overlay a couple lines (and their confidence intervals) on top of each other on the same plot using the twoway command. I have three waves of data on dementia probability and respondent death that I've reshaped into a long dataset to run cox models.
The first line is for the linear prediction of hazard ratios from the cox model, while the other line is the non-linear predicted hazard ratios using restricted cubic splines. I would (ideally) also like to label the knots on the graph as well. However, I've found that whenever I try to add a second x-axis on to the top of the graph with the knot values, it seems to shift the lines/confidence intervals/labels ever so slightly. Essentially, the end result is that things are slightly mis-aligned on the graph which looks wonky. Has anyone encountered this before?
I've included my code below (as well as a picture too) though unfortunately the full dataset doesn't fit...
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str70 prim_key byte Wave float(Death dementia_prob_simple time_since_baseline) "BBBNE" 1 0 .06534394 4.1724844 "BBBNE" 2 0 .1509865 6.948665 "BBBNE" 3 0 .1410623 7.942505 "BBBTW" 1 0 .05781248 3.720739 "BBBTW" 2 0 .065200105 6.537988 "BBBTW" 3 0 .4451951 7.794661 "BBCCY" 1 0 .05144565 3.548255 "BBCCY" 2 0 . 6.401095 "BBCCY" 3 0 .08231283 7.394935 "BBCNO" 1 1 .05144565 .3477071 "BBDXD" 1 1 .1861319 2.902122 "BBEGB" 1 0 .18657447 3.600274 "BBEGB" 2 1 .0765395 4.5338807 "BBEGG" 1 0 .11616056 3.356605 "BBEGG" 2 1 .024751397 4.054757 "BBGGQ" 1 0 .29465842 3.430527 "BBGGQ" 3 0 .0966789 7.526352 "BBGMM" 1 1 .49544325 1.0239562 "BBKZB" 1 0 . 3.488022 "BBKZB" 2 0 .09272194 6.329911 "BBKZB" 3 0 .19332263 7.430527 "BBLHY" 1 0 .26920962 3.819302 "BBLHY" 2 0 .0969391 6.68857 "BBLHY" 3 0 .06505657 7.912389 "BBMHM" 1 0 .08249108 3.6413414 "BBMHM" 2 0 .08249108 6.494182 "BBMHM" 3 0 .0766625 7.430527 "BBNGG" 1 0 .48434275 4.276523 "BBNGG" 2 0 .3607324 6.781656 "BBNGG" 3 0 .04028451 7.509925 "BBOZP" 1 0 .09272194 3.942505 "BBOZP" 2 0 .09714066 6.861054 "BBOZP" 3 0 .2009024 7.989048 "BBPBZ" 1 0 .58996 3.542779 "BBPBZ" 2 0 .2143845 6.584531 "BBPBZ" 3 0 .09252381 7.531827 "BBPGJ" 1 0 .4215171 3.526352 "BBPGJ" 2 0 .035693496 6.433949 "BBPGJ" 3 0 .453618 7.507187 "BBQBT" 1 0 .18111786 3.967146 "BBQBT" 2 1 . 6.184805 "BBQMB" 1 0 .04019347 4.0109515 "BBQMB" 2 0 .19396223 6.913073 "BBQMB" 3 0 .12604809 8 "BBSPW" 1 0 .09252381 3.523614 "BBSPW" 2 0 .05133073 6.286106 "BBSPW" 3 0 .07345817 7.531827 "BBVJD" 1 0 .05121605 3.8439424 "BBVJD" 2 0 .05794102 6.324435 "BBVJD" 3 0 .04554057 7.600274 "BBVOD" 1 0 .26022545 3.359343 "BBVOD" 2 0 .07345817 6.351814 "BBVOD" 3 0 .073618785 7.304586 "BBXZT" 1 0 .2447587 3.980835 "BBXZT" 2 0 .23076133 6.976044 "BBXZT" 3 0 .05806671 7.906913 "BBZFQ" 1 0 .35109 3.726215 "BBZFQ" 2 0 .08605283 6.557153 "BBZFQ" 3 0 . 7.548254 "BCBFX" 1 0 .26886767 3.890486 "BCBFX" 2 0 .4652211 6.704997 "BCBFX" 3 0 . 7.739904 "BCCOV" 1 0 .08266968 3.351129 "BCCOV" 2 0 .04554057 6.652977 "BCCOV" 3 0 . 7.394935 "BCDCG" 1 0 .09252381 3.969884 "BCDCG" 2 0 . 6.880219 "BCDCG" 3 0 . 7.991786 "BCDWD" 1 0 .02174053 3.778234 "BCDWD" 2 0 .04028451 6.63655 "BCDWD" 3 0 . 7.356605 "BCEMZ" 1 0 . 3.756331 "BCEMZ" 2 0 . 6.557153 "BCEMZ" 3 0 .08249108 7.800137 "BCEOD" 1 0 .08249108 3.5345654 "BCEOD" 2 0 .04028451 6.406571 "BCEOD" 3 0 .1512816 7.531827 "BCEQF" 1 1 . 3.471595 "BCGKO" 1 0 . 3.342916 "BCGKO" 3 0 .04037575 7.438741 "BCGPK" 1 0 .2359014 3.9890485 "BCGPK" 2 0 .3322652 6.899384 "BCGPK" 3 0 .1750519 7.972621 "BCHQX" 1 0 . 3.373032 "BCHQX" 2 0 .11324286 6.223135 "BCHQX" 3 0 .05794102 7.214237 "BCKLZ" 1 1 .14457992 2.9075975 "BCKPB" 1 0 .167826 3.7727585 "BCKPB" 2 0 .17446114 6.590007 "BCKPB" 3 0 .2444374 7.816564 "BCLCB" 1 0 .05133073 3.512663 "BCLCB" 2 0 .07345817 6.376454 "BCLCB" 3 0 .07345817 7.45243 "BCLKX" 1 0 .167826 3.8631074 "BCLKX" 2 0 .3322652 6.819986 "BCLKX" 3 0 .05781248 7.947981 "BCLOF" 1 1 .332128 2.800821 "BCLZW" 1 0 .09252381 3.561944 "BCLZW" 2 1 .2223685 5.470226 "BCNOD" 1 0 .07345817 3.663244 end
Code:
stset time_since_baseline, failure(Death == 1) id(prim_key) browse dementia_prob_simple sort dementia_prob_simple capture drop _rs_rcs_1 _rcs_1_* RCS_HR RCS_STD_ERROR RCS_lower RCS_upper // LINEAR HAZARD RATIOS stcox dementia_prob_simple predict HR, hr predict STD_ERROR, stdp generate Lower = HR - (1.96*STD_ERROR) generate Upper = HR + (1.96*STD_ERROR) // NON-LINEAR HAZARD RATIOS makespline rcs dementia_prob_simple, knots(4) matrix list r(knots) matrix Knots = r(knots) stcox _rcs_1_* predict RCS_HR, hr predict RCS_STD_ERROR, stdp generate RCS_Lower = RCS_HR - (1.96*RCS_STD_ERROR) generate RCS_Upper = RCS_HR + (1.96*RCS_STD_ERROR) // ALIGNED PLOT (BUT NO LABELED KNOTS) graph twoway rarea Lower Upper dementia_prob_simple, fcolor(stred%50) lcolor(stred%0) /// || line HR dementia_prob_simple, lcolor(stc2) /// || rarea RCS_Lower RCS_Upper dementia_prob_simple, fcolor(ltblue%80) lcolor(ltblue%0) /// || line RCS_HR dementia_prob_simple, lcolor(stc1) /// || , legend(on position(4) ring(0) row(2) label(1 "95% CI") label(2 "Linear") label(3 "95% CI") label(4 "Restricted Cubic Splines; 4 Knots") order(2 1 4 3) size(small)) /// ytitle("Hazard Ratio") title("Predicted Hazard Ratios") /// ylabel(1(1)8) xlabel(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%") /// xline(`=el(Knots,1,1)' `=el(Knots,1,2)' `=el(Knots,1,3)' `=el(Knots,1,4)') // MIS-ALIGNED PLOT graph twoway rarea Lower Upper dementia_prob_simple, fcolor(stred%50) lcolor(stred%0) /// || line HR dementia_prob_simple, lcolor(stc2) xaxis(1) xlabel(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%", axis(1)) /// || rarea RCS_Lower RCS_Upper dementia_prob_simple, fcolor(ltblue%80) lcolor(ltblue%0) /// || line RCS_HR dementia_prob_simple, lcolor(stc1) xaxis(2) xlabel(`=el(Knots,1,1)' "Knot 1" `=el(Knots,1,2)' "Knot 2" `=el(Knots,1,3)' "Knot 3" `=el(Knots,1,4)' "Knot 4", axis(2) angle(45)) /// || , legend(on position(4) ring(0) row(2) label(1 "95% CI") label(2 "Linear") label(3 "95% CI") label(4 "Restricted Cubic Splines; 4 Knots") order(2 1 4 3) size(small)) /// ytitle("Hazard Ratio") title("Predicted Hazard Ratios") /// ylabel(1(1)8) xlabel(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%") /// xline(`=el(Knots,1,1)' `=el(Knots,1,2)' `=el(Knots,1,3)' `=el(Knots,1,4)')
David
Comment