Announcement

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

  • Margins after stcox

    Hello
    I have fit a Cox model using gender as a categorical variable and bmi as a continuous,with interaction.
    stcox c.bmi##i.gender,nohr This is Hosmer,Lemeshow and May Ch.4 exercises Question 1.(g) p.130.

    Now I want the hazard ratio for gender at bmi =15,20,25,30,35.
    This is simple to do with lincom:
    lincom 1.gender+1.gender#bmi*15,hr and so on for each value of bmi.
    This is the answer for bmi=15
    ------------------------------------------------------------------------------
    _t | Haz. Ratio Std. Err. z P>|z| [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    (1) | .8284485 .2734861 -0.57 0.569 .4337787 1.582205
    ------------------------------------------------------------------------------


    But I wanted a quick way with margins.If I use
    margins gender,at(bmi=(15(5)35)),I get
    ------------------------------------------------------------------------------
    | Delta-method
    | Margin Std. Err. z P>|z| [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    _at#gender |
    1 0 | .1728764 .0608328 2.84 0.004 .0536464 .2921064
    1 1 | .1432192 .0911335 1.57 0.116 -.0353991 .3218375
    2 0 | .0963047 .0451844 2.13 0.033 .007745 .1848644
    2 1 | .0979411 .0604691 1.62 0.105 -.0205762 .2164584
    3 0 | .0536487 .0314637 1.71 0.088 -.008019 .1153165
    3 1 | .0669775 .041052 1.63 0.103 -.0134829 .1474379
    4 0 | .0298862 .0210331 1.42 0.155 -.0113379 .0711104
    4 1 | .0458029 .0285439 1.60 0.109 -.0101422 .101748
    5 0 | .0166488 .0136698 1.22 0.223 -.0101435 .0434411
    5 1 | .0313225 .0202816 1.54 0.122 -.0084287 .0710737
    ------------------------------------------------------------------------------
    and then if you divide each gender hazard ratio for each value of bmi you get the same as the lincom value(for bmi=1(15),.1432192/.1728764=.
    8284485,same as the lincom value for bmi=15.)
    How do I get margins to give me the lincom output all at once,so I don't have to write 5 lincom commands?
    Regards
    Chris

  • #2
    This is the closest I can get to what you want

    Code:
    webuse cancer, clear
    
    stcox c.age##i.drug, nohr
    lincom 2.drug + 2.drug#c.age*50
    lincom 3.drug + 3.drug#c.age*50
    
    margins i.drug  , at(age = 50) predict(xb) contrast(effects)
    At this point, you need to exponentiate the point estimates and the limits of the CIs (-margins- does not have an -eform- option)

    Comment


    • #3
      Thanks very much for this Andrea.
      This works although as we have to exponentiate the 5 contrasts,I'm not sure this is much less work than writing 5 lincom commands.
      On the other hand,it does obviate making mistakes with the lincom commands.
      In any case,that's a valuable little piece of code.
      Regards
      Chris

      Comment


      • #4
        I forgot about margins' -post- option...

        Code:
        webuse cancer, clear
        
        stcox c.age##i.drug, nohr
        lincom 2.drug + 2.drug#c.age*50, eform
        lincom 3.drug + 3.drug#c.age*50, eform
        
        qui margins i.drug  , at(age = 50) predict(xb) contrast(effects) post
        
        mata 
        b = st_matrix("e(b)")
        V = st_matrix("e(V)")
        exp(b)
        exp(b:-1.96*sqrt(diagonal(V))')
        exp(b:+1.96*sqrt(diagonal(V))')
        end

        Comment

        Working...
        X