Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Graphing change in the hazard ratios after stcox tvc() option

    Hi,
    I am using Cox proportional hazar model with time dependent variables, I want to graph how hazard ratio changes over time for each variable. This is the code I use,
    Code:
    xi:stcox i.migstatus i.sex_gen i.cohort i.eductv i.preg i.numkid, tvc(i.migstatus i.sex_gen i.eductv i.preg i.numkid ) texp(_t)
    I know tvc option limits a lot of post estimation, so below gives same result;
    Code:
    stset beg, failure(typeb=2) origin(origin16) id(id)
    stsplit, at(failures)
    xi:stcox i.migstatus i.sex_gen i.cohort i.eductv i.preg i.numkid c._t#i.migstatus c._t#i.sex_gen c._t#i.eductv c._t#i.preg c._t#i.numkid
    I see somewhere they recommend using "coefplot" package (https://stackoverflow.com/questions/...ient-over-time). I first tab _t, and try to get hazard ratios at specific time with "if"option, but it doesn't work at all output doesn't give anything.

    Thus if you have any suggestions to represent a graph of coefficients changing over time ( it doesn't have to be all time points), I appreciate a lot.

  • #2
    To start, unless you're using a quite old version, you won't need the "xi:" prefix.

    That said, maybe the user-written program - stcurve_tvc - can do the trick for you.
    Best regards,

    Marcos

    Comment


    • #3
      Your plots of the time-varying hazard ratios will be straight lines. A simple approach would be to just use
      Code:
      twoway function y=exp([beta1]+x*[beta2])
      where beta1 and beta2 are the estimated regression coefficients.

      You're likely to get more help if you post a fully worked example based on either your data (use -dataex-) or one of the Stata sample data sets (see the FAQ). Here's an example of one way to do what you want using the drugtr data. There will almost certainly be better ways; hopefully this code will encourage others to post better solutions.

      I would suggest using -stpm2- instead of -stcox-. -stpm2- is much more powerful for modelling non-proportional effects and plotting predictions. There's an example in my code. one advantage of -stpm2- is you are not assuming a linear form for the time-varying effects.

      Code:
      clear
      webuse drugtr
      
      // center age at 55 
      replace age=age-55
      
      // data are already stset; fit a Cox model before we change anything
      stcox i.drug age
      
      // need to generate an ID number to stsplit
      generate id=_n
      stset studytime, failure(died) id(id)
      stsplit, at(failures)
      
      // verify we get the same estimates as the original data
      // when fitting the main effcts model
      stcox i.drug age
      
      // now include interactions with time
      stcox i.drug c.age c._t#i.drug c._t#c.age
      
      // predict the hr (exponential of the linear predictor)
      predict hr, hr
      
      // plot the hazard ratio for drug as a function of time
      twoway line hr _t if age==0&drug==1, sort name(stcox, replace) 
      
      //***********************************************************
      // Now use stpm2 instead
      // ssc install stpm2
      //***********************************************************
      
      clear
      webuse drugtr
      
      // center age at 55 
      replace age=age-55
      
      // main effects model
      stpm2 drug age, scale(hazard) df(3) eform
      
      // allow time-varying effects of drug and age
      // note that we did not have to split
      // time varying effcts are modelled as splines (with 2df)
      stpm2 drug age, scale(hazard) df(3) tvc(drug age) dftvc(2) eform
      
      predict hr, hrnumerator(drug 1)
      twoway line hr _t, sort name(stpm2, replace)

      Comment


      • #4
        Sorry for my late reply. I am using Stata 14.1. I am not allowed to share my data, unfortunately. But I will share what I have tried so far. In Paul Dickman's example using stpm2 gives the curve as what I really aim at, so I tried that one.

        Code:
        //first stset data
        stset endcohabm if agefirst>=15, failure(typee==1) origin(origincohab) id(id)
        
        //then I created my time-varying variables by stsplit( eductv preg numkid are time varying variables)
        // fit the main effect model
        stpm2 i.migstatus i.sex_gen b2.agef i.cohort i.eductv i.preg i.numkid, scale(hazard) df(3)
        
        //fit model with tvc()
        // without xi prefix syntax giving error " factor variables not allowed for tvc() option r(198)"
        // all variable in tvc() are dummy variables and are found to be time-dependent when I use stcox and check the ph assumption
        xi:stpm2 i.migstatus i.sex_gen b2.agef i.cohort i.eductv i.preg i.numkid, scale(hazard) df(3)  tvc( i.sex_gen i.preg i.numkid) dftvc(2)  eform
        
        predict hr, hrnumerator(preg 1)
        // gives error " if not found r(111)"
        I don't know if it is related to version, my code, or something else?

        Comment


        • #5
          I suggest creating dummy variables yourself. I use "tab , gen()" to create the dummy variables.

          The problem with your last error is that "preg" is not a variable in your model. When you use xi: stpm2 i.preg, Stata creates dummy variables (with names like _Ipreg_1) that it puts in the model.

          It's possible you could use

          Code:
          predict hr, hrnumerator(_Ipreg_1 1)
          but you would need to have a good understanding of how xi: works and how stpm2 is coded. I would suggest creating dummy variables yourself. If you look at Paul Lambert's tutorials you will see that he does just that (and he has a fairly good understanding of how stpm2 works).

          Comment


          • #6
            it's been a while, but why does Paul Dickman in post3 centre the age at 55?

            Comment

            Working...
            X