I am working on a project using Cox models with time varying covariates. My questions are:

  1. What are some good examples of conducting this analysis?
  2. What is the best R package to conduct this analysis?

Any suggestions are appreciated!


2 Answers 2


I'm also working with data that involves including a time varying covariate.

I'll start off this answer by giving an example that I'll use throughout. Say we have a longitudinal study where there are two treatments available, treatment and non-treatment. Participants in the study can freely move between the two. The event of interest will be called event.

First of all your data needs to be in Long format; this simply means each ID or subject has multiple rows of information.

ID    Treatment    Start        Stop        Event
1        0        01/01/2002  01/02/2002      0
1        0        01/02/2002  01/03/2002      0
1        1        01/03/2002  01/04/2002      0  
1        0        01/04/2002  01/05/2002      1
2        0        01/01/2002  01/02/2002      0
2        1        01/02/2002  01/03/2002      1

I'm also working on the assumption that there are start and stop dates recorded for each treatment interval.

R has a command in the package survival that creates proper start and stop times. Surv objects don't take Date classed vectors. The command is called tmerge


This article by Therneau goes through a full example of how to use this command to create a time-varying data frame. This will create numeric start and stop times for the treatment intervals, usually called tstart and tstop.

Once you have your data ready, the package to use is the normal Cox regression: coxph().

Rather than putting one time variable in the Surv part, you put two:

coxph(Surv(tstart, tstop, event) ~ ., data=data))

Where . is all covariates, but they can be entered by selection.

The above is what I have used to analyse data that is time-varying. If anyone would like to expand on it I would be very welcoming since I am not an expert.

  • $\begingroup$ Nice example. Question: in what sense does coxph() distinguish between ID's if there are repeated measures (as in your table in "Long" format)? Also, how is the ID variable referenced or read by coxph() in your example; e.g. does one need + 'cluster(ID)' in coxph()? Thanks in advance! $\endgroup$ Oct 26, 2015 at 19:08
  • $\begingroup$ @Ivan After much panicking over your question (I did this analysis for an MSc) I began to investigate. I couldn't find in any material I used an answer for your question. A colleague said Stata had an option that adjusted for multiple measures, so I ran the analysis in Stata instead to check if the results were the same as what I had got in R using the above commands. Thankfully they were the same, even though I hadn't added anything in the coxph to adjust for ID. I'm afraid that's where my answer runs out, I don't understand why they were the same when I hadn't added any cluster(ID) $\endgroup$
    – Lb93
    Oct 27, 2015 at 11:25
  • $\begingroup$ more input on this from another source would be great as I don't understand it. $\endgroup$
    – Lb93
    Oct 27, 2015 at 11:27
  • $\begingroup$ @Lb: thanks for your response. It seems that once the time variables, covariates, and event variable (i.e. censor variable) are selected, coxph() "knows" how to interpret the data. Hence, it seems that in your case that by not declaring the ID as a parameter, coxph() correctly interpreted repeated measures. I may post a question here on SE to confirm. $\endgroup$ Oct 27, 2015 at 20:43
  • $\begingroup$ stats.stackexchange.com/questions/178944/… $\endgroup$ Oct 27, 2015 at 21:34
coxph(formula = Surv(tstart, tstop, event) ~ Treatment + cluster(ID), data=data))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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