24  Cox Model Diagnostics

24.1 Overview

After fitting a Cox model, we should check two key assumptions:

  1. Proportional hazards: the hazard ratio between any two individuals is constant over time.
  2. Linearity: continuous explanatory variables enter the log-hazard linearly.

Several diagnostic tools are available.

24.2 Graphical check: log-cumulative hazard plots

The simplest PH check for a categorical explanatory variable is the log-cumulative hazard plot (introduced in Chapter 21): plot \(\log\widehat{H}(t)\) from the KM estimator against \(\log t\) for each group. Under PH, the curves should be parallel.

Code
library(survival)

# Hypernephroma data (used throughout this chapter)
h <- read.table("https://www.richardpmann.com/MATH3701/Datasets/hypernephroma.dat", header = TRUE)
h$age         <- as.factor(h$age)
levels(h$age)  <- c("<60", "60-70", ">70")
h$nephrectomy <- as.factor(h$nephrectomy)
levels(h$nephrectomy) <- c("no", "yes")

km_g <- survfit(Surv(time, status) ~ nephrectomy, data = h)
plot(km_g, fun = "cloglog",          # log H(t) vs log(t)
     col = c("blue", "red"), lwd = 2,
     xlab = "log(t)", ylab = "log H(t)")
legend("topleft", legend = c("No nephrectomy", "Nephrectomy"),
       col = c("blue", "red"), lwd = 2, bty = "n")
Figure 24.1: Log-cumulative hazard plot for the nephrectomy data, by nephrectomy status. Roughly parallel curves support the proportional hazards assumption.

24.3 Schoenfeld residuals

NoteDefinition: Schoenfeld residuals

At each event time \(t_{(j)}\), the Schoenfeld residual for explanatory variable \(k\) is the observed value of that variable minus the risk-set-weighted expected value: \[ r_{jk} = x_{(j)k} - \frac{\sum_{\ell \in \mathcal{R}(t_{(j)})} x_{\ell k} \exp(\hat{\bbeta}^T\mathbf{x}_\ell)} {\sum_{\ell \in \mathcal{R}(t_{(j)})} \exp(\hat{\bbeta}^T\mathbf{x}_\ell)}. \]

Under the PH assumption, the Schoenfeld residuals should have mean zero and no trend with time. A plot of \(r_{jk}\) against event time \(t_{(j)}\) that shows a systematic trend (increasing, decreasing, or non-linear) suggests that the PH assumption is violated for explanatory variable \(k\).

The cox.zph() function in R tests formally for a time trend in each explanatory variable’s Schoenfeld residuals.

Code
library(survival)

cox_fit <- coxph(Surv(time, status) ~ age + nephrectomy, data = h,
                 ties = "breslow")

# Schoenfeld residual test for PH
ph_test <- cox.zph(cox_fit)
print(ph_test)   # p-values for each explanatory variable + global test
            chisq df     p
age          2.96  2 0.228
nephrectomy  3.58  1 0.059
GLOBAL       6.95  3 0.073
Code
# Plot Schoenfeld residuals: one panel per explanatory variable
par(mfrow = c(1, 3), mar = c(4, 4, 2, 1))
plot(ph_test)
Figure 24.2: Scaled Schoenfeld residuals against time for each explanatory variable (with a smooth fit and confidence band). A flat smooth indicates no evidence against proportional hazards.

A small \(p\)-value from cox.zph() suggests the PH assumption is violated for that explanatory variable.

24.4 Martingale residuals

NoteDefinition: Martingale residuals

\[ M_i = \delta_i - \widehat{H}_0(t_i)\exp(\hat{\bbeta}^T\mathbf{x}_i), \] where \(\widehat{H}_0(t)\) is the Breslow estimate of the baseline cumulative hazard.

Martingale residuals lie in \((-\infty, 1]\) and sum to zero. Plotting them against a continuous explanatory variable that was assumed to enter linearly can reveal non-linearity: a non-linear trend in the residuals suggests that a transformation of the explanatory variable (or a smooth term) would improve the fit.

Code
library(survival)

# The hypernephroma explanatory variables are all categorical, so we
# illustrate the linearity check with the lung cancer data (continuous age)
mydata <- lung
mydata$delta <- as.numeric(mydata$status == 2)   # 1 = death, 0 = censored

# Martingale residuals from a null (intercept-only) model
null_fit <- coxph(Surv(time, delta) ~ 1, data = mydata, ties = "breslow")
mart_res  <- residuals(null_fit, type = "martingale")

# Plot vs continuous explanatory variable to check linearity
plot(mydata$age, mart_res,
     xlab = "Age", ylab = "Martingale residual")
lines(lowess(mydata$age, mart_res), col = "red", lwd = 2)
abline(h = 0, lty = 2)
Figure 24.3: Martingale residuals from the null model plotted against age for the lung data, with a lowess smooth (red). Curvature in the smooth would suggest a non-linear effect of age.

24.5 Deviance residuals

Deviance residuals are a transformation of the martingale residuals designed to be more symmetric around zero for a well-fitting model: \[ d_i = \text{sign}(M_i)\sqrt{-2\bigl(M_i + \delta_i\log(\delta_i - M_i)\bigr)}. \]

Large absolute deviance residuals (say, \(|d_i| > 3\)) may indicate influential observations or outliers.

Code
dev_res <- residuals(cox_fit, type = "deviance")
plot(predict(cox_fit), dev_res,
     xlab = "Linear predictor", ylab = "Deviance residual")
abline(h = c(-2, 0, 2), lty = c(2, 1, 2))
Figure 24.4: Deviance residuals against the linear predictor for the nephrectomy model. Dashed lines mark \(\pm 2\).

24.6 Complete diagnostics workflow

Code
library(survival)

# Fit final model (data `h` loaded above)
fit_final <- coxph(Surv(time, status) ~ age + nephrectomy,
                   data = h, ties = "breslow")

par(mfrow = c(2, 3), mar = c(4, 4, 2, 1))

# 1. PH test (3 panels: one per explanatory variable)
ph_test <- cox.zph(fit_final)
print(ph_test)
            chisq df     p
age          2.96  2 0.228
nephrectomy  3.58  1 0.059
GLOBAL       6.95  3 0.073
Code
plot(ph_test)

# 2. Martingale residuals (all explanatory variables are categorical here)
mart <- residuals(fit_final, type = "martingale")
plot(predict(fit_final), mart,
     xlab = "Fitted log-hazard", ylab = "Martingale residual")

# 3. Deviance residuals
dev <- residuals(fit_final, type = "deviance")
plot(seq_along(dev), dev, type = "h",
     xlab = "Observation index", ylab = "Deviance residual")
abline(h = c(-2, 2), lty = 2)
Figure 24.5: Complete diagnostics for the final nephrectomy model: Schoenfeld residuals (top row), martingale residuals against the fitted log-hazard (bottom left) and deviance residuals by observation index (bottom middle).

Exercises

24.1. After fitting the Cox proportional hazards model from Exercise 23.1 to the Drug A/B data, a researcher wishes to check the model assumptions. Without using R, describe the diagnostic approach for each of the following.

Think about log–log survival plots (\(\log(-\log\hat{S}(t))\) vs \(\log t\) for each group), Schoenfeld residuals plotted against time, and dfbeta or deviance residuals for influence assessment.

  1. Checking the proportional hazards assumption. What plot(s) would you produce, what would you look for, and what would departures from PH look like?

  2. Identifying influential observations. What residual-based diagnostics would you use, and what pattern in the plot would flag an observation as influential?

24.2. Using the robot surgery data from Exercise 20.1, perform a full set of Cox model diagnostics in R.

Use coxph() to fit the model, cox.zph() to test PH, plot(cox.zph(fit)) to visualise Schoenfeld residuals, and residuals(fit, type="deviance") for deviance residuals.

  1. Fit a Cox model with group as the explanatory variable. Run cox.zph() and interpret the output.

  2. Plot the Schoenfeld residuals against time. What pattern would confirm that the PH assumption is satisfied?

  3. Plot the deviance residuals against the linear predictor. Identify any potential outliers.