I am working on a project using Cox models with time varying covariates. My questions are:
- What are some good examples of conducting this analysis?
- What is the best R package to conduct this analysis?
Any suggestions are appreciated!
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
https://cran.r-project.org/web/packages/survival/vignettes/timedep.pdf
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.
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$
coxph(formula = Surv(tstart, tstop, event) ~ Treatment + cluster(ID), data=data))