Tutorials › Joint Modeling of Longitudinal and Time-to-Event Data › Testing and Modeling Time-Varying Effects

Survival Models Review · Tutorial 111 of 697

Testing and Modeling Time-Varying Effects

You will learn to diagnose and test a time-varying covariate effect, choose an interaction with time, and interpret its hazard ratios at selected follow-up times.

Intermediate 12 min read

What You'll Learn

  • Distinguish a time-varying covariate from a covariate whose effect changes over time.
  • Specify a Cox model with a covariate-by-time interaction and a clear time reference.
  • Test the proportional-hazards restriction using a Wald test or a likelihood-ratio test.
  • Calculate and interpret hazard ratios at selected follow-up times.
  • Use diagnostics to assess whether the chosen time function is plausible.
  • Recognize when stratification or another model form answers a different question.

Why Let a Covariate Effect Change With Time?

A Cox proportional hazards model represents a covariate’s effect as constant over follow-up: for two individuals who differ by one unit in that covariate but otherwise have the same modeled covariates, their hazard ratio does not depend on time. This can be too restrictive. For example, a treatment might be associated with a larger hazard difference soon after enrollment and a smaller difference later. A single hazard ratio would conceal that pattern.

The key modeling distinction is between a time-varying covariate, whose value changes during follow-up, and a time-varying effect, in which the coefficient multiplying a covariate changes with time. A covariate can be fixed while its effect changes. Conversely, a covariate can change over time while its coefficient remains constant. The earlier material on time-varying covariates treats the first issue; here the focus is on the second.

A time-by-covariate interaction is one way to model a changing effect. It makes the hazard ratio an explicit function of time, so the effect can be reported at meaningful follow-up times rather than summarized by one constant number.

Model and Interpretation

Let \(X_i\) be a covariate for individual \(i\), and let \(Z_i\) denote other covariates whose effects are modeled as constant over time. A Cox model with a time-varying effect of \(X_i\) is

$$ h_i(t)=h_0(t)\exp\left\{\beta X_i+\gamma X_i g(t)+\boldsymbol{\theta}^{\mathsf T}Z_i\right\}. $$

Here \(h_0(t)\) is an unspecified baseline hazard, \(g(t)\) is a specified function of follow-up time, and \(\gamma\) is the interaction coefficient. For two individuals with the same \(Z_i\), the hazard ratio comparing \(X=x+1\) with \(X=x\) at time \(t\) is

$$ HR_X(t)=\exp\{\beta+\gamma g(t)\}. $$

This formula follows by subtracting the two log hazards. The baseline term and the contribution of \(Z_i\) cancel, leaving \(\beta+\gamma g(t)\) for a one-unit difference in \(X\). For a difference of \(d\) units, the hazard ratio is \(\exp\{d[\beta+\gamma g(t)]\}\).

If \(g(t)=0\) at a chosen reference time \(t_0\), then \(\beta\) is the log hazard ratio at \(t_0\), and \(\gamma\) describes how that log hazard ratio changes with \(g(t)\). A convenient choice is \(g(t)=\log(t/t_0)\), which is zero at \(t=t_0\) and compares time on a multiplicative scale. Time must be positive for this choice. Centering time this way also makes the main-effect coefficient interpretable at a useful reference time.

Definition: If \(g(t)\) is nonconstant over the relevant follow-up times and the interaction is identifiable, the proportional-hazards restriction for \(X\) is \(H_0:\gamma=0\). If \(\gamma=0\), the hazard ratio for a one-unit difference in \(X\) is \(\exp(\beta)\), constant over time. If \(\gamma\ne0\), it varies with \(g(t)\) over those times.

This model relies on the usual Cox-model conditions: the event and censoring information are recorded appropriately; censoring is independent of the event time conditional on the modeled information; individuals contribute independent event histories; and the specified covariates and functional form are adequate for the intended comparison. It also assumes that effects represented by \(\boldsymbol{\theta}\) are proportional over time. The interaction only relaxes proportionality for \(X\); it does not automatically fix other model misspecifications.

Worked Example: Hazard Ratios at Three Follow-Up Times

Suppose a hypothetical Cox fit uses a binary treatment indicator \(X\) and \(g(t)=\log(t/1\text{ year})\). The fitted coefficients are \(\hat{\beta}=0.10\) and \(\hat{\gamma}=0.20\). Assume the other modeled covariates are held equal. Calculate the treatment hazard ratio at 1, 2, and 4 years.

At 1 year, \(g(1)=\log(1)=0\). Thus the estimated log hazard ratio is \(0.10+0.20(0)=0.10\), giving

$$ \widehat{HR}_X(1)=\exp(0.10)\approx1.105. $$

At 2 years, \(g(2)=\log(2)\approx0.693147\). Therefore,

$$ \widehat{\log HR}_X(2)=0.10+0.20\log(2) =0.10+0.1386294=0.2386294, \qquad \widehat{HR}_X(2)=\exp(0.2386294)\approx1.270. $$

At 4 years, \(g(4)=\log(4)\approx1.386294\), so

$$ \widehat{\log HR}_X(4)=0.10+0.20(1.386294)=0.3772588, \qquad \widehat{HR}_X(4)=\exp(0.3772588)\approx1.458. $$

The fitted hazard ratio is larger at later times because the estimated interaction coefficient is positive. At 2 years, for example, the fitted hazard for the treated group is about 1.270 times the hazard for the untreated group, conditional on the other modeled covariates. This is a comparison of instantaneous hazards at that time, not a ratio of survival probabilities and not a causal conclusion by itself.

Testing for a Time-Varying Effect

For a single interaction coefficient, the null hypothesis of proportional hazards for \(X\) is \(H_0:\gamma=0\), against \(H_a:\gamma\ne0\). A Wald test uses the estimated coefficient and its standard error:

$$ Z=\frac{\hat{\gamma}}{SE(\hat{\gamma})}. $$

Under \(H_0\), and with the usual large-sample approximation, \(Z\) is approximately standard normal. A two-sided p-value is \(2P(N\geq |Z|)\), where \(N\sim N(0,1)\). For multiple time-interaction terms, test their coefficients jointly; for example, a Wald test uses the estimated coefficient vector and its covariance matrix. A likelihood-ratio test instead compares the maximized partial likelihoods of nested models, with and without the interaction terms. The test should match the intended restriction, and both models must be fitted to the same analysis data.

Worked Example: Wald Test of the Interaction

In a hypothetical analysis, the fitted coefficient for \(X\log(t/t_0)\) is \(\hat{\gamma}=0.35\), with \(SE(\hat{\gamma})=0.14\). Test for a time-varying effect at \(\alpha=0.05\), using a two-sided alternative.

The hypotheses are \(H_0:\gamma=0\) and \(H_a:\gamma\ne0\). The test statistic is

$$ Z=\frac{0.35}{0.14}=2.50. $$

The two-sided standard normal p-value is approximately \(0.0124\). Since \(0.0124<0.05\), reject \(H_0\) at the 5% significance level. In this fitted model, the data provide evidence that the log hazard ratio changes with \(\log(t/t_0)\); the p-value is not the probability that \(H_0\) is true.

The corresponding approximate 95% confidence interval for \(\gamma\) is

$$ 0.35\pm1.96(0.14)=0.35\pm0.2744=(0.0756,\ 0.6244). $$

Because this interval excludes zero, it agrees with the two-sided test. If time is measured in years and \(g(t)=\log(t/t_0)\), a doubling of time changes the fitted hazard ratio by a factor \(\exp\{\gamma\log(2)\}\). Using the estimate, this factor is \(\exp\{0.35(0.693147)\}=\exp(0.242601)\approx1.275\). This describes how the hazard ratio itself changes as time doubles, not the hazard ratio comparing the covariate groups at any one time.

Uncertainty for a Hazard Ratio at a Selected Time

Testing \(\gamma=0\) answers whether the specified interaction is supported by the data. It does not by itself give the uncertainty for the hazard ratio at a particular time. At time \(t\), the estimated log hazard ratio is \(\hat{\beta}+\hat{\gamma}g(t)\). Its variance must include the covariance between the estimates:

$$ \widehat{\operatorname{Var}}\{\hat{\beta}+\hat{\gamma}g(t)\} = \widehat{\operatorname{Var}}(\hat{\beta}) +g(t)^2\widehat{\operatorname{Var}}(\hat{\gamma}) +2g(t)\widehat{\operatorname{Cov}}(\hat{\beta},\hat{\gamma}). $$

An approximate confidence interval is formed on the log hazard-ratio scale, then exponentiated at both endpoints. This procedure uses the estimated covariance matrix from the fitted model. Simply combining separate confidence intervals for \(\beta\) and \(\gamma\) does not generally give the correct interval.

Worked Example: Confidence Interval at 18 Months

Consider a hypothetical binary covariate with \(g(t)=\log(t/6\text{ months})\), \(\hat{\beta}=0.40\), and \(\hat{\gamma}=-0.15\). Suppose the estimated variances are \(0.04\) and \(0.01\), respectively, and their estimated covariance is \(-0.006\). Find the fitted hazard ratio and approximate 95% confidence interval at 18 months.

At 18 months, \(g(18)=\log(18/6)=\log(3)\approx1.098612\). The estimated log hazard ratio is

$$ 0.40+(-0.15)(1.098612)=0.235208. $$

Thus the fitted hazard ratio is \(\exp(0.235208)\approx1.265\). Its log-scale variance is

$$ 0.04+(1.098612)^2(0.01)+2(1.098612)(-0.006) =0.04+0.012070-0.013183=0.038886. $$

The standard error is \(\sqrt{0.038886}\approx0.1972\). The approximate 95% interval on the log scale is \(0.235208\pm1.96(0.1972)\), or approximately \((-0.1513,\ 0.6217)\). Exponentiating gives an interval of approximately \((0.860,\ 1.862)\) for the hazard ratio. At 18 months, the point estimate is above 1, but the interval includes 1, so this interval does not rule out equal hazards at that time. The interval is an uncertainty statement for the hazard ratio at 18 months under the fitted model.

Choosing the Time Function and Checking the Model

The interaction is only as flexible as the chosen \(g(t)\). A log-time interaction is useful when a gradual change with follow-up time is scientifically plausible. A linear-time interaction gives a different pattern: it makes the log hazard ratio linear in elapsed time rather than in log time. The choice should be made with the time scale and scientific question in mind, and should not be selected solely because it produces a small p-value.

Diagnostics help assess whether the selected form is credible. The earlier material on Schoenfeld residuals provides a way to examine evidence of nonproportionality. In this setting, inspect whether the residual pattern for \(X\) is consistent with the proposed time trend, and consider whether the trend is approximately represented by \(g(t)\). Also check that the time variable is defined consistently, that the reference time is meaningful, and that the fitted interaction is not driven by very few individuals at late follow-up. Sparse risk sets can make late-time estimates unstable even when the model converges.

A useful comparison is with stratification. Stratifying on a categorical variable allows its baseline hazard to differ across strata but does not estimate a hazard ratio for that variable. It is suitable when adjustment is needed but its effect is not the target. By contrast, an interaction model estimates a time-dependent hazard ratio for the covariate of interest. A time-varying covariate model addresses changing covariate values, not necessarily a changing coefficient. These approaches answer different questions and are not interchangeable.

A single interaction also has limitations. If the actual effect changes sharply or nonlinearly, a linear function of time or log time may misrepresent it. Adding many time functions can overfit, especially with few events, and makes interpretation less direct. In such cases, carefully chosen piecewise effects or more flexible functions may be considered, with the extra modeling complexity justified by the data and scientific aims. Whatever form is used, report the hazard ratio at selected times with uncertainty, and specify the covariate contrast and time units.

Common Pitfalls

  • Confusing changing values with changing effects. A covariate measured repeatedly is not automatically a time-varying effect. The interaction term specifically allows the coefficient to depend on time.
  • Interpreting \(\exp(\beta)\) as the hazard ratio at all times. With an interaction, \(\exp(\beta)\) is the hazard ratio only where \(g(t)=0\); elsewhere use \(\exp\{\beta+\gamma g(t)\}\).
  • Ignoring the time scale or reference. With log time, changing units without adjusting the reference can change coefficient interpretation. State the units and reference time explicitly.
  • Claiming that a significant interaction proves a particular biological mechanism. The test concerns the specified statistical form and does not establish causation or explain why the effect changes.
  • Omitting covariance when constructing an interval. Use the full estimated variance of \(\hat{\beta}+\hat{\gamma}g(t)\), including its covariance term.
Key takeaway: A covariate-by-time interaction replaces one constant hazard ratio with \(HR_X(t)=\exp\{\beta+\gamma g(t)\}\). Test the interaction to assess the proportional-hazards restriction, but interpret and report the fitted hazard ratio at specified times, with its uncertainty and time scale clearly stated.

Check Your Understanding

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

  1. A model uses \(g(t)=\log(t/2\text{ years})\). At what time does \(\beta\) give the log hazard ratio?
  2. For \(\hat{\beta}=0.20\), \(\hat{\gamma}=-0.10\), and \(g(6)=\log(3)\), calculate the hazard ratio at 6 years when the reference time is 2 years.
  3. A fitted interaction estimate is \(0.12\) with standard error \(0.08\). Calculate the Wald statistic and state the approximate two-sided p-value and decision at \(\alpha=0.05\).
  4. Why is stratification not a substitute for a time-varying-effect model when the goal is to estimate a covariate’s hazard ratio over time?

Solutions. (1) At \(t=2\) years, \(g(t)=\log(1)=0\), so \(\beta\) is the log hazard ratio at 2 years. (2) Here \(g(6)=\log(6/2)=\log(3)\approx1.098612\). The log hazard ratio is \(0.20-0.10(1.098612)=0.090139\), so the hazard ratio is \(\exp(0.090139)\approx1.094\). (3) \(Z=0.12/0.08=1.50\), giving a two-sided p-value of approximately \(0.134\). Since \(0.134>0.05\), do not reject \(H_0:\gamma=0\); this is not proof that the effect is constant. (4) Stratification allows different baseline hazards between strata but does not estimate a hazard ratio for the stratifying variable. An interaction model estimates the covariate’s hazard ratio as a function of time.

Clinical Biostats