Tutorials › Joint Modeling of Longitudinal and Time-to-Event Data › Time-Varying Covariates in Cox Regression

Survival Models Review · Tutorial 109 of 697

Time-Varying Covariates in Cox Regression

Represent covariate histories correctly in Cox regression and calculate partial-likelihood contributions using only information available just before each event.

Intermediate 10 min read

What You'll Learn

  • Define start-stop intervals for time-varying covariates
  • Construct event-time risk sets from split follow-up records
  • Write and evaluate partial-likelihood contributions
  • Explain why covariate values must reflect information available before an event
  • Distinguish changing covariates from changing covariate effects
  • Recognize assumptions and common failure modes in time-varying Cox analyses

Why Covariate Timing Matters

In a Cox regression, a covariate need not remain fixed throughout follow-up. A patient may begin treatment, a laboratory measurement may change, or a participant’s exposure status may be updated. At each event time, the risk comparison must use the covariate values that apply to the individuals still at risk at that time. Assigning a value based on what happens later can put future information into an earlier risk set and distort the analysis.

This tutorial develops the start-stop representation of changing covariate histories and the corresponding Cox partial likelihood. The key discipline is temporal: the value used at time \(t\) must be available just before \(t\). The Cox proportional hazards model and event-time risk sets provide the starting point.

Definition: A time-varying covariate is a covariate whose value for an individual can change during follow-up. In a Cox model with covariate history \(X_i(t)\), the hazard for individual \(i\) at time \(t\), conditional on being at risk and on the covariate history available just before \(t\), is $$ \lambda_i(t\mid X_i(t))=\lambda_0(t)\exp\{\beta^\mathsf{T}X_i(t)\}. $$ Here \(\lambda_0(t)\) is an unspecified baseline hazard, and \(\beta\) is a vector of regression coefficients. This model allows the covariate value to change over time while keeping its coefficient \(\beta\) constant.

Start-Stop Records and Event-Time Risk Sets

Split each person’s follow-up into intervals over which the recorded covariate value is constant. Write an interval as \((a,b]\): the covariate value in that row applies after time \(a\) and through time \(b\). If a covariate is updated at time \(u\), the old row ends at \(u\), and the row with the new value starts at \(u\). Thus, at an event exactly at an update time \(u\), the value immediately before the update is used. This convention makes the covariate history predictable: it does not depend on information that becomes available at or after the event.

A subject contributes to the risk set at event time \(t\) if they have entered follow-up and have not yet had an event or been censored. In start-stop data, the active row must satisfy \(a_i<t\leq b_i\). With delayed entry at \(a_i\), this excludes the subject before entry. Although the data may contain several rows per person, a person contributes only once to any event-time risk set.

SubjectStartStopCovariate value in intervalEvent at stop?
A020Yes
B010No
B12.51Yes
C030No

At time 2, A, B, and C are at risk; B’s value is 1 because the update occurred at time 1. At time 2.5, A has already had an event, so only B and C remain at risk. C’s stop time of 3 is a censoring time, not an event.

Partial Likelihood With Changing Covariates

Suppose event times are distinct. Let \(D\) be the set of event times, \(i(t)\) the individual who experiences the event at time \(t\), and \(R(t)\) the risk set immediately before that event. The partial likelihood is

$$ L(\beta)=\prod_{t\in D} \frac{\exp\{\beta^\mathsf{T}X_{i(t)}(t)\}} {\displaystyle\sum_{k\in R(t)}\exp\{\beta^\mathsf{T}X_k(t)\}}. $$

The covariate values in both numerator and denominator are those active at that event time. The event individual’s contribution is their relative hazard among those at risk, with the common baseline hazard cancelling from the comparison. Taking logarithms turns the product into a sum of event-specific log contributions. If event times are tied, the tied-event methods used in Cox regression must be applied to the risk sets and covariate values at the tied time.

Justification: At an event time \(t\), conditional on one event occurring among the individuals in \(R(t)\), the model assigns individual \(i\) probability $$ \frac{\lambda_i(t)}{\sum_{k\in R(t)}\lambda_k(t)} = \frac{\lambda_0(t)\exp\{\beta^\mathsf{T}X_i(t)\}} {\sum_{k\in R(t)}\lambda_0(t)\exp\{\beta^\mathsf{T}X_k(t)\}} = \frac{\exp\{\beta^\mathsf{T}X_i(t)\}} {\sum_{k\in R(t)}\exp\{\beta^\mathsf{T}X_k(t)\}}. $$ Multiplying these conditional probabilities over event times gives the partial likelihood. This argument requires the hazard model to be correctly specified and the covariates used at \(t\) to be known just before \(t\); it also relies on censoring being noninformative conditional on the modeled covariate history.

Worked Example: Two Event Contributions With an Updated Covariate

Use the table above. Let \(X\) be a binary covariate and evaluate the partial likelihood at \(\beta=\log(2)\), so \(\exp(\beta)=2\). There are events at times 2 and 2.5.

At time 2, A has \(X=0\), while B has \(X=1\) and C has \(X=0\). The first contribution is $$ \frac{\exp\{(\log 2)(0)\}} {\exp\{(\log 2)(0)\}+\exp\{(\log 2)(1)\}+\exp\{(\log 2)(0)\}} = \frac{1}{1+2+1} = \frac14. $$

At time 2.5, B has the event and has \(X=1\); C, with \(X=0\), is the other person at risk. The second contribution is $$ \frac{\exp\{(\log 2)(1)\}} {\exp\{(\log 2)(1)\}+\exp\{(\log 2)(0)\}} = \frac{2}{2+1} = \frac23. $$ The partial likelihood at this specified coefficient is therefore \(L(\log 2)=(1/4)(2/3)=1/6\). This calculation evaluates the likelihood; it does not establish that \(\log 2\) is the fitted coefficient.

Constructing the Covariate History Carefully

Start-stop rows are a data representation, not a guarantee that the covariate values are valid. The analyst must decide when a value becomes available and how it applies between observations. For treatment initiation, a common definition is \(X_i(t)=0\) before treatment starts and \(X_i(t)=1\) after initiation. For intermittent measurements, carrying the last observed value forward may be reasonable if it matches the scientific definition and the measurement schedule; it is not automatically justified. The timing rule should be specified before fitting the model.

Worked Example: Respecting the Time of a Laboratory Measurement

Suppose three people have the following records: P enters at 0, has a laboratory value of 2 at time 0 and 3 at time 4, and experiences an event at time 6; Q enters at 0, has values 1 at time 0 and 2 at time 4, and is censored at time 8; R enters at time 2, has value 1 at entry, and is censored at time 8. Assume each recorded value applies until the next measurement.

The start-stop rows are P: \((0,4]\) with value 2 and \((4,6]\) with value 3; Q: \((0,4]\) with value 1 and \((4,8]\) with value 2; R: \((2,8]\) with value 1. At time 6, all three are at risk, with current values 3, 2, and 1, respectively. If the coefficient per one-unit increase is \(\beta=\log(2)\), P’s event contribution is $$ \frac{\exp\{3\log(2)\}} {\exp\{3\log(2)\}+\exp\{2\log(2)\}+\exp\{\log(2)\}} = \frac{8}{8+4+2} = \frac47. $$ The measurements recorded at time 4 are used; no measurement after time 6 is available in this example. If a value were measured exactly at the event time, the stated convention would use the value from just before that time.

Future Information and Other Failure Modes

A particularly serious error is to classify people at baseline by whether they will ever receive a treatment. That classification uses future information and can make a person appear exposed during time when they were not yet treated. It can also create an immortal-time problem: to enter the “ever treated” group, a person must remain under observation until treatment starts. The time-varying coding instead changes exposure status at the actual initiation time.

Worked Example: Why “Ever Treated” Is Not a Baseline Covariate

At time 2, A experiences an event. B is still at risk and will start treatment at time 4. C is also at risk and will never start treatment. At time 2, none of the three has yet started treatment. The correct treatment values are therefore 0 for A, B, and C, and A’s partial-likelihood contribution is $$ \frac{1}{1+1+1}=\frac13. $$ If B is incorrectly coded as treated from baseline because B will eventually receive treatment, the contribution becomes $$ \frac{1}{1+\exp(\beta)+1}. $$ At \(\beta=\log(2)\), this is \(1/4\), not \(1/3\). The altered contribution arises solely from using B’s future treatment status at an earlier event time; it is not evidence about treatment’s effect.

Other checks are important before interpreting a fitted model:

  • Check interval boundaries. Intervals should cover the intended follow-up without gaps or overlaps, and event indicators should appear only on the row ending at the event.
  • Check risk-set membership. At each event time, confirm that every included person has entered and has not already had an event or been censored.
  • Check update times. A value must not be assigned before it was measured or treatment was initiated. Clarify how values at event-time boundaries are handled.
  • Check the meaning of the covariate. Measurement error, irregular assessment, and informative measurement times can make the recorded history a poor representation of the underlying process.
  • Check model assumptions. The hazard must be adequately represented by the specified covariate history and a constant coefficient for each covariate. A changing covariate value is not the same as a changing coefficient.

Interpretation, Alternatives, and Limitations

For a one-unit increase in a covariate, holding the other modeled covariates fixed, \(\exp(\beta)\) is the hazard ratio at a given time for individuals whose current covariate values differ by one unit. For a binary treatment covariate, \(\exp(\beta)\) compares the modeled hazard while treated with the modeled hazard while untreated, conditional on the other covariates and on the model. It is an association under the model, not by itself a causal effect. In particular, time-varying confounding is not resolved merely by splitting the data into intervals.

A fixed-covariate Cox model is appropriate when the relevant covariates do not change during follow-up, or when a clearly defined baseline value is the scientific target. The start-stop form is needed when covariate history is part of the intended hazard model. If the scientific question concerns different effects at different follow-up times, that is a separate modeling issue: changing \(X_i(t)\) alone still uses a constant \(\beta\). A time-varying covariate model also does not automatically correct measurement error or account for an underlying unobserved longitudinal trajectory; those require additional modeling choices.

The central practical safeguard is to audit a few event times by hand: list who is at risk, identify each person’s active interval, and verify the covariate value available just before the event. If these checks fail, a numerically converged Cox fit can still answer the wrong question.

Check Your Understanding

Try each question, then compare your reasoning with the solutions below.

  1. A covariate changes at time 5, and an event occurs at time 5. Under the interval convention in this tutorial, which value enters that event’s risk-set calculation?
  2. At time 4, an event occurs for one of two people at risk. Both have a binary covariate value of 0 at time 4. Write the partial-likelihood contribution for this event.
  3. A person enters follow-up at time 3 and is still event-free at time 6. Is that person in the risk set at time 2? At time 4?
  4. Explain why coding a participant as treated from baseline because they start treatment later is invalid for an event occurring before treatment starts.

Solutions. (1) Use the value immediately before time 5: the old interval ends at 5, and the new value applies after 5. (2) Each person contributes \(\exp(0)=1\), so the event contribution is \(1/(1+1)=1/2\). (3) The person is not at risk at time 2 because entry has not occurred; they are at risk at time 4 because \(3<4\leq6\). (4) That coding assigns a future treatment status to an earlier risk set, although treatment had not begun. It changes the modeled covariate history and can introduce immortal-time bias.

Clinical Biostats