************************************************** //load a dataset provided by STATA ************************************************** clear webuse drugtr ************************************************** // ************************************************** stset **************************************************************************************************** //Cox Regress with the restricted cubic spline **************************************************************************************************** ************************************************** //step 1: Restricted cubic spline (you may specify your own knots using "knots(numlist)" instead of "nkonts(5)") ************************************************** mkspline sage=age, cubic nknots(5) display ************************************************** //step 2: run the Cox regression with the newly created variables (sage1 sage2 sage3 sage4) ************************************************** stcox drug sage1 sage2 sage3 sage4 ************************************************** //step 3: download the userwritten command- xblc and predict the HR for every "existing-age" from 47 to 65 in the dataset) ************************************************** levelsof age if inrange(age, 47,65) xblc sage1 sage2 sage3 sage4, covname(age) at(`r(levels)') eform reference(50) generate(agenew hr lb ub) sort drug age ************************************************** //step 4: Graphically display the HR for every age ************************************************** two (sc hr agenew, sort(agenew) connect(L) mc(gs5) lc(gs5)) /// (sc lb agenew, ms(i) sort connect(L) lp(dash) lc(gs5)) /// (sc ub agenew, ms(i) sort connect(L) lp(dash) lc(gs5)) /// , name(f2, replace) /// xtitle(Age) ytitle(Hazard ratio with 95%CI) xlab(45(5)65) /// yline(1, lp(dash) lc(red)) yscale(log) /// title("Version 2: example on Cox regression with restricted cubic spline", size(medium)) /// legend(off) graph export CoxRegCubicv2.png, replace