+ Reply to Thread
Results 1 to 13 of 13

Thread: How to draw cubic splines after Cox regression in Stata 11.1?

  1. #1
    Points: 992, Level: 16
    Level completed: 92%, Points required for next Level: 8

    Location
    Stockholm, Sweden
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question How to draw cubic splines after Cox regression in Stata 11.1?




    Hi everyone,

    In a cohort study, I would like to draw cubic splines, including HR and 95% CI, after Cox regression adjusted for age and sex. Yet, I have not found out the solution.

    Suppose that there are variables as follows: observetime, censor, variablex (the independent variable we are interested in, continuous), age, sex. Knots would be set as 5.

    Could you tell me how to perform it in Stata 11.1, please? I appreciate it very much!

    Best regards,

    Xiaoyan

  2. #2
    RoboStataRaptor
    Points: 11,861, Level: 71
    Level completed: 53%, Points required for next Level: 189
    bukharin's Avatar
    Location
    Sydney, Australia
    Posts
    1,312
    Thanks
    12
    Thanked 326 Times in 315 Posts

    Re: How to draw cubic splines after Cox regression in Stata 11.1?

    I'm going to assume that if censor is 1 they were censored, and if it's 0 they died. If it's the other way around you'll need to modify the code. First you need to set up your survival analysis with:
    Code: 
    stset observetime, fail(censor==0)
    Now you need to model the effect of variablex in a flexible way. If I were you I'd use fractional polynomials instead of cubic splines. It's much easier - all you need to do is:
    Code: 
    fracpoly: stcox variablex age sex
    fracplot
    (what you're seeing on the y axis is the log hazard ratio)

    If you really want to use cubic splines, one option would be to use the recently published -xblc- command. To install -xblc- use the following commands:
    Code: 
    net sj 11-3 st0215_1
    net install st0215_1
    Here's an example of -xblc- using the cancer dataset that comes with Stata:
    Code: 
    sysuse cancer, clear
    
    * create spline variables for age
    mkspline spl_age=age, cubic nknots(5)
    
    * model effect of spline variables adjusting for treatment
    stcox spl_age* i.drug
    
    * calculate effect of age; we'll set the reference at 47 (youngest) but could also use 56 (mean)
    quietly levelsof age, local(ages)
    quietly xblc spl_age*, covname(spl_age1) at(`ages') reference(47) eform gen(ptage hr lb ub)
    
    * graph the result
    twoway (line hr ptage, sort) (line lb ub ptage, sort lc(black black) lp(- -)), ///
    	legend(off) yscale(log) ylab(0.25 .5 1 2 4 8 16 32) ///
    	xtitle(Age) ytitle(Hazard ratio) yline(1) ///
    	title(Effect of age) subtitle(Adjusted for treatment)

  3. #3
    Points: 992, Level: 16
    Level completed: 92%, Points required for next Level: 8

    Location
    Stockholm, Sweden
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How to draw cubic splines after Cox regression in Stata 11.1?

    Thank you very much, Bukharin.

    You are right. The fractional polynomial is very easy to draw according to your suggestion. But is it different with a cubic spline?

    Unfortunately, I still do not understand how to draw cubic splines. But thanks all the same for posting the example.

  4. #4
    Points: 2, Level: 1
    Level completed: 3%, Points required for next Level: 48

    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to draw cubic splines after Cox regression in Stata 11.1?

    Hi there,

    I am using the xblc command after logistic (and Cox) regression with cubic splines. The xblc command has been working really well, until I added several variables to my adjusted models. The xblc command will not work with more than 7 variables! It works with any combination of 7, but returns an error message when I add an 8th variable.

    Has anyone else had this problem?
    Does anyone know of a solution?
    Any insight would be much appreciated.

    Thank you!


    Quote Originally Posted by bukharin View Post
    I'm going to assume that if censor is 1 they were censored, and if it's 0 they died. If it's the other way around you'll need to modify the code. First you need to set up your survival analysis with:
    Code: 
    stset observetime, fail(censor==0)
    Now you need to model the effect of variablex in a flexible way. If I were you I'd use fractional polynomials instead of cubic splines. It's much easier - all you need to do is:
    Code: 
    fracpoly: stcox variablex age sex
    fracplot
    (what you're seeing on the y axis is the log hazard ratio)

    If you really want to use cubic splines, one option would be to use the recently published -xblc- command. To install -xblc- use the following commands:
    Code: 
    net sj 11-3 st0215_1
    net install st0215_1
    Here's an example of -xblc- using the cancer dataset that comes with Stata:
    Code: 
    sysuse cancer, clear
    
    * create spline variables for age
    mkspline spl_age=age, cubic nknots(5)
    
    * model effect of spline variables adjusting for treatment
    stcox spl_age* i.drug
    
    * calculate effect of age; we'll set the reference at 47 (youngest) but could also use 56 (mean)
    quietly levelsof age, local(ages)
    quietly xblc spl_age*, covname(spl_age1) at(`ages') reference(47) eform gen(ptage hr lb ub)
    
    * graph the result
    twoway (line hr ptage, sort) (line lb ub ptage, sort lc(black black) lp(- -)), ///
    	legend(off) yscale(log) ylab(0.25 .5 1 2 4 8 16 32) ///
    	xtitle(Age) ytitle(Hazard ratio) yline(1) ///
    	title(Effect of age) subtitle(Adjusted for treatment)

  5. #5
    Points: 2, Level: 1
    Level completed: 3%, Points required for next Level: 48

    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How to draw cubic splines after Cox regression in Stata 11.1?

    Dear

    However, after execute fracplot, I find it is partial predictor+residual of sth. on Y-axis. what does that mean? how can I correlate it with hazard ratio?




    Quote Originally Posted by bukharin View Post
    I'm going to assume that if censor is 1 they were censored, and if it's 0 they died. If it's the other way around you'll need to modify the code. First you need to set up your survival analysis with:
    Code: 
    stset observetime, fail(censor==0)
    Now you need to model the effect of variablex in a flexible way. If I were you I'd use fractional polynomials instead of cubic splines. It's much easier - all you need to do is:
    Code: 
    fracpoly: stcox variablex age sex
    fracplot
    (what you're seeing on the y axis is the log hazard ratio)

    If you really want to use cubic splines, one option would be to use the recently published -xblc- command. To install -xblc- use the following commands:
    Code: 
    net sj 11-3 st0215_1
    net install st0215_1
    Here's an example of -xblc- using the cancer dataset that comes with Stata:
    Code: 
    sysuse cancer, clear
    
    * create spline variables for age
    mkspline spl_age=age, cubic nknots(5)
    
    * model effect of spline variables adjusting for treatment
    stcox spl_age* i.drug
    
    * calculate effect of age; we'll set the reference at 47 (youngest) but could also use 56 (mean)
    quietly levelsof age, local(ages)
    quietly xblc spl_age*, covname(spl_age1) at(`ages') reference(47) eform gen(ptage hr lb ub)
    
    * graph the result
    twoway (line hr ptage, sort) (line lb ub ptage, sort lc(black black) lp(- -)), ///
    	legend(off) yscale(log) ylab(0.25 .5 1 2 4 8 16 32) ///
    	xtitle(Age) ytitle(Hazard ratio) yline(1) ///
    	title(Effect of age) subtitle(Adjusted for treatment)

  6. The Following User Says Thank You to zh_zhang1984 For This Useful Post:

    xiexf (06-25-2014)

  7. #6
    Points: 2, Level: 1
    Level completed: 3%, Points required for next Level: 48

    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to draw cubic splines after Cox regression in Stata 11.1?

    What if your age variable has too many values to use? Are there any other alternative options?

    Thanks!

  8. #7
    TS Contributor
    Points: 5,073, Level: 45
    Level completed: 62%, Points required for next Level: 77
    maartenbuis's Avatar
    Location
    Konstanz
    Posts
    371
    Thanks
    3
    Thanked 146 Times in 123 Posts

    Re: How to draw cubic splines after Cox regression in Stata 11.1?

    You can use postrcspline available from SSC. You will need to specify the link(log) option after stcox in order to get the odds ratios instead of the log(odds ratios).

  9. The Following User Says Thank You to maartenbuis For This Useful Post:

    xiexf (06-24-2014)

  10. #8
    Points: 61, Level: 1
    Level completed: 22%, Points required for next Level: 39

    Location
    Beijing
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: How to draw cubic splines after Cox regression in Stata 11.1?

    Hello
    I analysed some clinical data and I used the command of "fractional polynomial" but I am not very sure about the result of y axis "partial predictor+residual of sth", is it the log HR of mortality? If so, what is the command if I specify the link(log) option after stcox in order to get the odds ratios instead of the log(odds ratios)?
    thanks a lot

  11. #9
    RoboStataRaptor
    Points: 11,861, Level: 71
    Level completed: 53%, Points required for next Level: 189
    bukharin's Avatar
    Location
    Sydney, Australia
    Posts
    1,312
    Thanks
    12
    Thanked 326 Times in 315 Posts

    Re: How to draw cubic splines after Cox regression in Stata 11.1?

    Yes it's log(HR). My usual trick is not to change the y axis to HR, but to relabel it. For example, relabel the log HR of 0 as a HR of 1 (since log(1)=0).
    Code: 
    webuse cancer
    fracpoly: stcox age
    fracplot, ylab(`=log(0.25)' "0.25" `=log(0.5)' "0.5" 0 "1" `=log(2)' "2" `=log(5)' "4") ytitle(Hazard ratio)

  12. The Following 2 Users Say Thank You to bukharin For This Useful Post:

    abcstat (07-06-2017), xiexf (06-25-2014)

  13. #10
    Points: 61, Level: 1
    Level completed: 22%, Points required for next Level: 39

    Location
    Beijing
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: How to draw cubic splines after Cox regression in Stata 11.1?

    Thanks very much for your reply.
    I have some other questions.
    First, as I adjusted several variables in the cox regression, when I used multiply fractional polynomials model to analysis, the shape of the graph changed, almost a straight line of HR appeared. I don’t know how to explain it, there must be some restriction when choose the fractional or multivariable fractional polynomials model. Would you please tell me?
    Second, when I used the command of restrict cubic spline in the post, I got another different graph with very wide CIs range, that’s may because of the trick of yaxis HR instead of logHR. But I read a paper about the two methods “ fractional polynomials VS restric cubic spline” Actually, there is some difference between them, I want to consult how to choose an appropriate and right method in analysis. Or is there any rules in choosing these two methods?

    Thanks a lot again.
    Last edited by xiexf; 06-25-2014 at 12:32 AM.

  14. #11
    RoboStataRaptor
    Points: 11,861, Level: 71
    Level completed: 53%, Points required for next Level: 189
    bukharin's Avatar
    Location
    Sydney, Australia
    Posts
    1,312
    Thanks
    12
    Thanked 326 Times in 315 Posts

    Re: How to draw cubic splines after Cox regression in Stata 11.1?

    It's not too surprising that the shape would change when you add other variables, it will depend on the relationship between your other variables and the one of interest. Yes FP and RC splines are different techniques so will occasionally give you slightly different results. Did you try centering and scaling the variable before the RC spline transformation? Sometimes this makes a difference. Your subject matter expertise should also help to decide best fit (ie what kind of relationship would you expect between your variable and mortality?)

  15. The Following User Says Thank You to bukharin For This Useful Post:

    xiexf (06-26-2014)

  16. #12
    Points: 61, Level: 1
    Level completed: 22%, Points required for next Level: 39

    Location
    Beijing
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Arrow Re: How to draw cubic splines after Cox regression in Stata 11.1?

    Thank you, I think I didn’t stata my problems clearly, the first question I told yesterday is the models about “FP” and “MFP”. Please see the command and plot below, I adjust the same 9 variables in the two models, the graphs are different, so I am not sure which command FP or MFP is right for this case.
    1. FP:
    stset studytime, fail(died==1)
    fracpoly: stcox sbp age var5 var6 var7 var8 var9 var10 var11
    fracplot, addplot (histogram drug, frequency yaxis(2))
    figure 1Click image for larger version

Name:	1.jpg
Views:	482
Size:	12.7 KB
ID:	4594

    2. MFP:
    stset studytime, fail(died==1)
    MFP: stcox sbp age var5 var6 var7 var8 var9 var10 var11
    fracplot, addplot (histogram drug, frequency yaxis(2))
    figure 2Click image for larger version

Name:	2.jpg
Views:	376
Size:	12.1 KB
ID:	4595
    I did not center and scale the variable before the RC spline transformation, I excuted the following command for RC, and the plot changed greatly.

    stset studytime, fail(died==1)
    mkspline spl_drug=drug, cubic nknots(5)
    stcox spl_drug* age var5 var6 var7 var8 var9 var10 var11
    quietly levelsof drug, local(drugs)
    quietly xblc spl_drug*, covname(spl_drug1) at(`drugs') reference(1.922) eform gen(ptdrug hr lb ub)
    twoway (line hr ptdrug, sort) (line lb ub ptdrug, sort lc(black black) lp(- -)), ///
    legend(off) yscale(log) ylab(-0.25 .5 1 2 4 8 16 32) ///
    xtitle(spb) ytitle(HR) yline(1) ///
    figure 3Click image for larger version

Name:	3.jpg
Views:	595
Size:	7.9 KB
ID:	4596
    Last edited by xiexf; 06-26-2014 at 04:17 AM.

  17. #13
    Points: 4, Level: 1
    Level completed: 7%, Points required for next Level: 46

    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: How to draw cubic splines after Cox regression in Stata 11.1?


    Thanks for this discussion. I'm still having trouble understanding the y-axis. What do negative values mean? I see there are some negative values in the plots presented here, and my plot is entirely negative.
    Attached Images

+ Reply to Thread

           




Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats