I have using following cox model to investigate the association between two exposures and a time to event outcome (0=no event, 1=event).
Model= coxph(Surv(time, as.numeric(event))~V1+V2, data=data) Where v1=exposure1, v2=exposure2

I would like to predict the difference in risk due to difference in these two exposures. Thus I created a new dataset with the new values of exposures and used that dataset to predict a diference in rsik due to difference in exposures. This new dataset with new values looks like this

newdata= data.frame(V1=c(-2.17, -1.99), V2=c(.44, .43))

For this, I used the following function on this newdata to predict risk associated with new exposure values.

Prediction=predict(Model, newdata=newdata) 

this gives me this output (hazards)

-0.66 , -0.60

Based on this output I can calculate the difference in risk due to difference in exposure like this


But I am struggling to calculate the 95% confidence interval of this difference in risk. Any suggestions on how I can calculate this?

Somebody in this post (Cox regression. Find 95% confidence interval for comparison of two groups) suggested to use ‘contrast’ function from rms package but I am not sure how to use this function since I have never used that package. Any suggestions on how to calculate such 95%CI?

  • $\begingroup$ Just to clarify, 1) are the exposures 1 or 0? + 2) on what scale do you want the answer? Do you want a (log-)hazard ratio (if your exposures are just 0 and 1, that's an easy answer to get)? Do you want a hazard difference? Do you want the difference (or ratio or odds ratio) in the probability of an event by a specific time (e.g. after 1 year)? When you say difference in risk, I would normally interpret that as a difference in such probabilities, but that would require specifying a time-horizon and is not entirely consistent with other things you mentioned. Thus, it would be good to clarify. $\endgroup$
    – Björn
    Commented Jun 17, 2021 at 8:35
  • $\begingroup$ hi Thanks. i would like to get log(hazard ratio). so i indicated the values from 'predict' function and those i got from type='lp'. those are actually log(HR). later i would like calculate the difference of these two log(HR) and find out the 95%CI of this difference. $\endgroup$ Commented Jun 17, 2021 at 12:18

1 Answer 1


The question to which you linked shows how to calculate the CI for any linear combination of predictor values, using the coefficient estimates and the covariance matrix of those estimates. It would be a good idea for you to try to do that, to make sure that you understand the principles--in particular, how the individual coefficient standard errors aren't always enough to do this correctly (although in this particular example there probably won't be much of a difference).

The advantage of working in the linear-predictor scale is that a simple difference between two cases is easily written in terms of a single case. (V1, V2) for your two cases are (-2.17, 0.44) and (-1.99, .43) respectively. Thus, with $\text{lp}_i$ the linear predictor for case i and coefficient estimates $\beta_{V1}$, $\beta_{V2}$:

$$\text{lp}_1 = -2.17 \beta_{V1} + 0.44 \beta_{V2}$$ $$\text{lp}_2 = -1.99 \beta_{V1} + 0.43 \beta_{V2}$$ $$\text{lp}_1 - \text{lp}_2 = (-2.17+1.99) \beta_{V1} + (0.44 - 0.43) \beta_{V2}= -0.18 \beta_{V1} + 0.01 \beta_{V2}$$

So a test of a significant difference between these two cases, or the CI for the difference, resolves to calculations on this difference in linear-predictor values, equivalent to a single case with (V1, V2) of (-0.18, 0.01). Most prediction routines provide the option to calculate confidence intervals in addition to point estimates; for predict.coxph() you have to specify se.fit=TRUE, while that's the default in survfit.coxph().


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.