Time-dependent covariates in the Cox model
What a time-dependent covariate is, how the data are laid out, how it avoids immortal time bias, and the pitfalls of covariates measured on the patient.
A time-dependent covariate is a variable whose value for a person can change during follow-up: whether they have started a treatment, a blood pressure reading, whether they have had a transplant, the dose they are on. In a Cox model the hazard of a person at time t depends on the value of the covariate at time t, , and the hazard ratio compares people whose current values differ by one unit at the same moment.
Used well, a time-dependent covariate lets a model follow what happens to people over time and avoids immortal time bias. Used carelessly, it can give biased estimates unless the relationship between the covariate and the outcome is understood (Fisher and Lin, 1999).
When you need one
- Exposure starts after baseline. Treatment, a transplant or a diagnosis happens during follow-up. Counting the person as exposed from the start gives them immortal time, a stretch in which they could not have had the event.
- A measurement is repeated. A biomarker, a score or a dose is measured at several visits and you want the hazard to depend on the latest value.
- An intermediate event changes risk. A relapse, an infection or a hospital admission changes what happens next.
How the data are set up
The usual layout follows the counting-process formulation of the Cox model (Andersen and Gill, 1982): each person has one row for every stretch of time over which their covariates are constant, with a start time, a stop time and an event indicator that is 1 only on the last row if the event happens there. Someone who starts treatment at month 4 and dies at month 10 has two rows: from 0 to 4, treatment 0, no event; from 4 to 10, treatment 1, event. In R and Stata, covariates that change at person-specific times need the data to be organised this way (Zhang et al., 2018).
At each event time the model compares the person who has the event with everyone still at risk at that moment, using each person's covariate value in force just before that moment. Each person's value must therefore be known at every event time at which they are at risk, which is why the rows should cover follow-up without unexplained gaps. In R, tmerge() in the survival package builds the rows, and coxph() takes the start, stop and event as Surv(tstart, tstop, event). In Stata, the rows are declared with stset using id() and failure(), with time0() if the records do not simply follow one another; stsplit can create the rows, and stcox then uses each record's covariate values. The tvc() option of stcox multiplies covariates by a function of time, which is how Stata fits time-varying coefficients (see below); it does not handle covariates whose values change at person-specific times.
What goes wrong without it: a simulated example
Take a simulated cohort of 200,000 people followed for up to 10 years. Everyone has a hazard of death of 0.1 per year. The time at which treatment would start is exponentially distributed with a mean of 4 years and is independent of everything else, so there is no confounding and only the handling of time is at issue. Treatment starts only if the person is still alive and under follow-up at that time, and from then on lowers the hazard by 30%, so the true hazard ratio is 0.7. In the cohort, 138,579 people start treatment.
The first analysis classes everyone who starts as treated from the beginning. Two things go wrong. Their pre-treatment time is counted as treated time in which, by construction, they could not die, because they had to survive to be treated, which dilutes the treated rate (0.048 deaths per person-year, against a true 0.070). And everyone who dies before they could start treatment is counted as never treated, which inflates the untreated rate (0.275 per person-year, against a true 0.100). The rate ratio, events per person-year in the treated against the untreated, is 0.18 (95% confidence interval 0.17 to 0.18), a benefit far larger than the true one. The hazards in the two groups are far from proportional, so no single hazard ratio describes this comparison. The second analysis counts each person as untreated until their own start and as treated afterwards, which is what a time-dependent covariate does. The rate ratio is 0.70 (0.69 to 0.71), close to the true 0.7, and a Cox model with the time-dependent covariate gives essentially the same answer, because the baseline hazard is constant. Suissa (2008) shows that this bias grows with the length of the immortal time. See also What is immortal time bias?
Pitfalls
- Internal covariates. A covariate generated by the patient and observed only while they are alive, such as a blood marker, a treatment-start indicator or a transplant, is internal (Kalbfleisch and Prentice, 2002; Zhang et al., 2018). One that is predetermined or defined, such as age or an air pollution level, is external. For an internal covariate the path up to time t exists only if the person survives to t, so the model cannot normally be used to predict survival over time (Fisher and Lin, 1999): the usual link between the hazard and the survival function no longer gives the probability of surviving to t.
- Reverse causation. A covariate can change because the event is coming, as with weight loss before death. Such relationships between the covariate and the outcome over time can lead to bias unless they are understood (Fisher and Lin, 1999).
- Between measurements. A common choice is to carry the last value forward, which treats a reading as true until the next one and ignores measurement error. The timing of visits can also depend on how ill the person is.
- Covariate or coefficient. A time-dependent covariate changes in value over time. A time-varying coefficient lets the effect of a covariate change over time, which is one remedy when the hazards are not proportional (Zhang et al., 2018). They are different tools, and a model can have both.
When a Cox model is not enough
A Cox model that includes a repeated measurement treats it as a predetermined process, a fixed value between visits observed without error, although it is measured with error and only intermittently and its path stops at death. A joint model describes the measurement process and the event process together, and gives survival predictions that account for them. If the covariate is itself changed by earlier treatment and also affects later treatment, adjusting for it in an ordinary Cox model is biased for a causal question, and g-methods such as marginal structural models are the usual answer. A landmark analysis is a simpler alternative when the question is about prediction from a given time. A multi-state model (What is a multi-state model?) also suits the case where the covariate is an intermediate event and you want transition-specific hazards.
What to report
- The definition of each time-dependent covariate, when its value can change, and how values between measurements were handled.
- The data structure: the number of rows, how time was split, and whether anyone's covariate history was incomplete.
- The hazard ratio read as a comparison of people with different current values at the same moment, with its confidence interval.
- For an internal covariate, a statement that the model does not give survival curves for fixed values of the covariate.
Want to learn more?
- What is the Cox model?
- What is immortal time bias?
- What is landmark analysis?
- What is the proportional hazards assumption?
- Simulating survival data with a continuous time-varying covariate
- An introduction to joint modelling of longitudinal and survival data
- Check out our Resources page
MethodSurvival analysis