I'm trying to understand how the margins and marginsplot work after stcox command in Stata.

So let's start with a dumb example:

webuse stan3
stcox i.posttran i.surg

That gives me result

          _t | Haz. Ratio   Std. Err.      z    P>|z|     [95% Conf. Interval]
  1.posttran |   1.173034   .3444712     0.54   0.587     .6597023    2.085801
   1.surgery |   .3449043   .1482592    -2.48   0.013     .1485267    .8009264

Now, when I use

margins, over( posttran surgery )

The output is

Predictive margins                                Number of obs   =        172
Model VCE    : OIM

Expression   : Relative hazard, predict()
over         : posttran surgery

                 |            Delta-method
                 |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
posttran#surgery |
            0 0  |          1          .        .       .            .           .
            0 1  |   .3449043   .1482592     2.33   0.020     .0543217    .6354869
            1 0  |   1.173034   .3444712     3.41   0.001     .4978826    1.848185
            1 1  |   .4045843   .2057569     1.97   0.049     .0013083    .8078604

What is puzzling me here is the fact that estimates and their Std. errors are the same as when using stcox. However - the CI, z and p are now different.

What is causing such behaviour?

Update: I also posted the question on Statalist.


2 Answers 2


I have not used -margins- much. I do not know if it is a bug or a design feature, but I have always been wary of using -margins- with ratio outcomes. I think the results are only correct with linear predictors.

For your Cox model, you can use margins on the linear predictor by

. margins , over(posttran surgery) exp(predict(xb))

You can then manually exponentiate the results to get the same results presented by -stcox-

NOTE: The -exp- above is an abbreviation of -expression- (an option to -margins-), not the exponential function. Also, the command

. margins , over(posttran surgery) exp(exp(predict(xb)))

(where the second -exp- is the exponential function) does not produce the correct results. Instead it produces the same output as the default option. In short, you have to use -margins-, then exponentiate. You cannot exponentiate then use -margins-.

  • $\begingroup$ +1 for "I have always been wary of using -margins- with ratio outcomes". I prefer to use the lincom command after a (for example) logistic or stcox model. $\endgroup$
    – boscovich
    Commented Feb 23, 2013 at 8:21
  • $\begingroup$ @timbb Thanks for input. The second option gives the correct HRs, the CIs problem stays however :/ Would it be possible to use expression and/or predict to ge tthe correct CIs as well? $\endgroup$
    – user22
    Commented Feb 25, 2013 at 10:07
  • $\begingroup$ @andrea lincom is a good choice indeed. The reason I wanted to explore margins was to get the access to marginsplot which can be (I think) very useful in exploratory work with lots of data/models.. $\endgroup$
    – user22
    Commented Feb 25, 2013 at 10:09

This looks like an error in Stata to me.

A Cox model obtains coefficient estimates and their standard errors for a linear model in the Log hazard, that is:

$\log{\lambda_t(\mathbf{X}_i)} = \log{\lambda_t(\cdot)} + \boldsymbol{\beta}\mathbf{X}_i$

So CIs are symmetric around the log of the hazard ratios and asymmetric around the hazard ratios. You can verify that by checking the model output.

It appears that margins are some kind of post estimate in your Cox model. It enumerates all possible contrasts in the data for variables in the over statement.

\begin{equation} \widehat{\frac{\lambda_t(\mathbf{X}_i)}{\lambda_t(\mathbf{X}_0)}} = \exp \left( \hat{\boldsymbol{\beta}} \mathbf{X}_i - \hat{\boldsymbol{\beta}} \mathbf{X}_0\right) \end{equation}

With $\mathbf{X}_0$ being the reference group (no posttrans, no surgery). The problem is that STATA treats inference and standard errors as if the model were linear in the hazards and not log hazards, they have null hypothesis

$\mathcal{H}_0 : \frac{\lambda_t(\mathbf{X}_i)}{\lambda_t(\mathbf{X}_0)} = 0$

when it should be

$\mathcal{H}_0 : \frac{\lambda_t(\mathbf{X}_i)}{\lambda_t(\mathbf{X}_0)} = 1$

The 95% confidence intervals are symmetric around the hazard ratios when they should be asymmetric and symmetric around the log hazard ratios. The resulting z-test statistic and p-value represent the probability of obtaining the test statistic you obtained or one more extreme under the assumption that the ratio of hazards comparing the two groups is infinite (not 1), which is nonsense.

It looks like their implementation may be flawed and I would write to them.

  • $\begingroup$ The error would be probably quite surprising here. Stata 12's release announcement mentions that: "... Graphs of margins, marginal effects, contrasts, and pairwise comparisons. Margins and effects can be obtained from linear or nonlinear (for example, probability) responses. New command marginsplot is available after stcox, stcrreg, and streg. ..." So perhaps it is possible, just my solution is clumsy? $\endgroup$
    – user22
    Commented Feb 25, 2013 at 14:19
  • 1
    $\begingroup$ @radek I'm glad I was clear. I don't have an answer to the solution you've provided. I think the results from margins would make sense if linear combinations of parameters were first computed on the log scale then re-exponentiated to report hazard ratios. @andrea's suggestion of using the lincom command instead is something that I would echo. That command has never given me unexpected results. $\endgroup$
    – AdamO
    Commented Feb 25, 2013 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.