I am new to R and I just wanted to know if it is possible to test an interaction between a time-varying covariate and a time-invariant covariate in survival analysis?

A related question is, can you plot the time-dependent covariate effect on survival for different groups?


coxph4b <- coxph(Surv(start, stop, frust_event) ~ avoid + newSDQtgr + avoid:newSDQtgr + frailty(id),
                 ties = c ("efron"),
                 data = frustrec2a)
frust.surv <- survfit(Surv(second, frust_event) ~ avoid:newSDQtgr, data = frustrec2)
plot(frust.surv, lty = 2:3)
legend(100, .9, c("avoid", "no avoid"), lty = 2:3)
title("Kaplan-Meier Curves for children's recurring aggression")
lsurv2 <- survfit(Surv(second, frust_event) ~ avoid*newSDQtgr, frustrec2, type='fleming')
plot(lsurv2, lty=2:3, fun="cumhaz",
     xlab="Seconds", ylab="Cumulative Hazard")
  • $\begingroup$ There is a frailty term in the model. What does that represent? Is this a repeated-events model perhaps? The coxme package is now preferred over a coxph frailty term for mixed-effects Cox models, if that's what you really need. $\endgroup$
    – EdM
    Oct 2, 2020 at 21:38
  • $\begingroup$ @EdM Yes, this is a repeated-events model. Does that change the way I use the survfit function? $\endgroup$
    – D.Ade
    Oct 2, 2020 at 21:40
  • $\begingroup$ survfit certainly handles repeated events with cumulative hazards; see Section 2.2 of the main vignette in the survival package. Work on specifying a proper newdata data frame for the conditions you wish to plot, as noted in my comments below my answer. Also, please add to the question information about the predictors; avoid seems to be a 2-level factor, but what type of data is newSDQtgr and which of them might be time-varying? $\endgroup$
    – EdM
    Oct 2, 2020 at 21:58
  • $\begingroup$ @EdM avoid is the time-dependent covariate (0 = occurs, 1 = does not occur) on each second of the task. newSDQtgr is a grouping variable for 0= low problems and 1= high problems $\endgroup$
    – D.Ade
    Oct 2, 2020 at 22:37

1 Answer 1


Survival analysis is performed at event times. What matters at each event time in a Cox proportional hazards regression is the set of current values of covariates for the case that had the event, versus current covariate values for those still at risk who didn't have the event.

A regression coefficient and interaction terms involving a time-varying covariate are thus no different from those for a time-invariant covariate. The regression coefficients and associated hazard ratios are still constant over time. That means that the effect on survival associated with any particular value of the time-varying covariate is constant over time. All that differs is that the values of the time-varying covariates can vary with time.

Once you have a model you certainly can plot predicted survival for members of any particular groups or combinations of covariate values, and you can even allow for time-varying values of covariates in the new data that you provide to the model if you format the data properly. This can be done, for example, with the survfit.coxph() function in the R survival package. The help page includes the following critical warning, however:

... although predictions with a time-dependent covariate path can be useful, it is very easy to create a prediction that is senseless. Users are encouraged to seek out a text that discusses the issue in detail.

Things are more complicated if you are considering time-varying coefficients for covariates. The time-dependent vignette provided with the survival package is an introduction to issues with respect to time-dependent covariates and coefficients.

  • $\begingroup$ Thank you! I really appreciate your help. I seem to run into an issue with the survfit.coxph() function though when I try to make an interaction term with the time-varying covariate and the time-invariant covariate. R produces this error: Error in survfit.formula(Surv(second, frust_event) ~ avoid * newSDQtgr, : Interaction terms are not valid for this function. $\endgroup$
    – D.Ade
    Oct 2, 2020 at 17:40
  • $\begingroup$ @D.Ade just to check: did you first run coxph() to get a coxph.object describing your model, design a newdata data frame (following the highly detailed instructions for time-dependent covariates on the survfit.coxph manual page) to describe the conditions for which you want to generate the plot, and then submit that coxph.object and the newdata to survfit()? $\endgroup$
    – EdM
    Oct 2, 2020 at 17:59
  • $\begingroup$ Yes I feel like I followed the right path to get the coxph.object. I've updated my original question with the code I was following. Have I done something wrong here? $\endgroup$
    – D.Ade
    Oct 2, 2020 at 21:02
  • $\begingroup$ thanks for editing the code, is it now correct? I still get the error in R. $\endgroup$
    – D.Ade
    Oct 2, 2020 at 21:26
  • 1
    $\begingroup$ @D.Ade you need to specify a newdata data frame that contains the sets of covariate values for which you want to generate the plots, e.g. specific values of avoid and newSDQtgr, potentially over time. I don't see that in your code. The manual page for survfit.coxph has very explicit instructions on how to set up that data frame when there are time-dependent covariates. Then you call survfit(coxph4b,newdata) to get a survfit object that you can summarize and plot as desired. $\endgroup$
    – EdM
    Oct 2, 2020 at 21:28

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.