10  Two-Sample Survival Analysis and the Cox Model

Survival-time patterns should be compared properly in their entirety rather than at isolated points only. (Mantel 1966)

Simple measures of association such as the risk ratio or odds ratio require the calculation of risks of disease over a specified time interval. Often, it is more efficient to compare times to events throughout an interval rather than at a single time point. Under the assumption of exponential times to events, the incidence rate allows us to calculate a rate that applies througout the observed time period. This is a step in the right direction, but calculations of incidence rates assume exponential times to events.

For one-sample inference, survival analysis allowed us to make more relaxed parametric assumptions (e.g., Weibull or log-logistic times to events) or to avoid any parametric assumption at all (e.g., the Kaplan-Meier and Nelson-Aalen estimators). Survival analysis plays a similar role in two-sample inference, allowing us to relax assumptions and compare survival time distributions in their entirety. The resulting hypothesis tests and measures of association can be more powerful and accurate than comparisons based on risks or incidence rates. All of the measures of association we discussed in Chapter 9 can be calculated using survival analysis. We focus on a single binary exposure as before, and we assume independent left truncation (i.e., delayed entry) and right censoring as described in Section 5.1.

10.1 Measures of association in survival analysis

Let \(\lambda_1\) be the rate parameter for the exposed and \(\lambda_0\) be the rate parameter for the unexposed, and assume (where needed) that both groups have the same shape parameter \(\alpha\). The rate ratio is just \(\lambda_1 / \lambda_0\). The hazard ratio at time \(t\) is \[ \HR(t) = \frac{h(t, \alpha, \lambda_1)}{h(t, \alpha, \lambda_0)}. \] The risk of disease onset in any interval \((0, t]\) can be calculated using the survival, cumulative hazard, or hazard functions. This allows us to calculate risk differences, risk ratios, or odds ratios similar to those in Chapter 9. In many cases, these measures of association depend on \(t\).

10.1.1 Rate ratios and hazard ratios

Suppose times to events have a Weibull(\(\alpha\), \(\lambda_1\)) distribution in the exposed and a Weibull(\(\alpha\), \(\lambda_0\)) in the unexposed. The rate ratio is \(\lambda_1 / \lambda_0\) at all times. However, the hazard ratio at time \(t\) is \[ \HR_\text{W} = \frac{\alpha \lambda_1^\alpha t^{\alpha - 1}}{\alpha \lambda_0^\alpha t^{\alpha - 1}} = \bigg(\frac{\lambda_1}{\lambda_0}\bigg)^\alpha. \] Therefore, the hazard ratio is constant and equal to \((\text{rate ratio})^\alpha\). Because the exponential distribution is a special case of the Weibull distribution with \(\gamma = 1\), the hazard ratio equals the rate ratio when both groups have exponential times to events.

If times to events have a log-logistic(\(\alpha\), \(\lambda_1\)) distribution in the exposed and a log-logistic(\(\alpha\), \(\lambda_0\)) in the unexposed, the rate ratio is still \(\lambda_1 / \lambda_0\) at all times. However, the hazard ratio is \[ \HR_\text{LL} = \frac{\alpha \lambda_1^\alpha t^{\alpha - 1}} {\alpha \lambda_0^\alpha t^{\alpha - 1}} \times \frac{1 + (\lambda_0 t)^\alpha}{1 + (\lambda_1 t)^\alpha} = \bigg(\frac{\lambda_1}{\lambda_0}\bigg)^\alpha \frac{1 + (\lambda_0 t)^\alpha}{1 + (\lambda_1 t)^\alpha}, \tag{10.1}\] which is not constant in time for any shape parameter \(\alpha > 0\). The hazard ratio is approximately \((\lambda_1 / \lambda_0)^\alpha\) just after \(t = 0\), and it approaches one as \(t \rightarrow \infty\). Figure 10.1 shows an example of this with shape \(\alpha = 2\) and rates \(\lambda_1 = 2\) and \(\lambda_1 = 1\).

Code
loglogistic-HR.R
## Rate ratios and hazard ratios for the log-logistic distribution

# log-logistic hazard function
hllog <- function(t, shape=1, rate=1) {
  shape * rate^shape * t^(shape - 1) / (1 + (rate * t)^shape)
}

# save old graphics parameters to restore them
mfrow_old <- par("mfrow")
mar_old <- par("mar")

# set two rows and one column and adjust margins between plots
par(mar = c(4, 4 ,1, 1), mfrow = c(2, 1))

# hazards with shape = 2 and rates = 2 and 1
t <- seq(0.01, 3, by = 0.01)
plot(t, hllog(t, shape = 2, rate = 2), type = "l", lty = "dashed",
     xlab = "", ylab = "Hazard")
lines(t, hllog(t, shape = 2, rate = 1))
grid()
text(2.5, 1.75, labels = "both shape = 2")
text(1.0, 0.75, labels = "rate = 1")
text(1.25, 1.75, labels = "rate = 2")

# hazard ratio
plot(t, hllog(t, 2, 2) / hllog(t, 2, 1), type = "l", ylim = c(0, 4),
     xlab = "Time", ylab = "Hazard ratio")
grid()
text(2.5, 3.5, labels = "rate ratio = 2")

# restore old graphics parameters
par(mar = mar_old, mfrow = mfrow_old)
Figure 10.1: Hazards (top) and hazard ratio (bottom) for log-logistic distributions with shape \(\alpha = 2\) and rates \(\lambda_1 = 2\) in the exposed and \(\lambda_0 = 1\) in the unexposed.

10.1.2 Risk differences, risk ratios, and odds ratios

All of the measures of association from Chapter 9 can be calculating using survival analysis because the estimated survival function can be used to calculate risks. For example: If the times to events have Weibull distributions in both groups, the risk of disease onset in \((0, t]\) given \(X = x\) is \[ 1 - S(t, \alpha, \lambda_x) = 1 - e^{-(\lambda_x t)^\alpha} \] where \(x = 1\) for the exposed and \(x = 0\) for the unexposed. Using these risks, we can calculate the risk difference, risk ratio, and odds ratio comparing the exposed to the unexposed over any time interval \((0, t]\). The odds of disease onset in \((0, t]\) given \(X = x\) is \[ \frac{1 - e^{-(\lambda_x t)^\alpha}}{e^{-(\lambda_x t)^\alpha}} = e^{(\lambda_x t)^\alpha} - 1, \] so the odds ratio comparing the exposed to the unexposed at time \(t\) is \[ \OR(t) = \frac{e^{(\lambda_1 t)^\alpha} - 1}{e^{(\lambda_0 t)^\alpha} - 1}. \] When \(\lambda_1 = \lambda_0\), this equals one at all \(t\). When \(\lambda_1 \neq \lambda_0\), it varies with \(t\). Figure 10.2 shows an example of this with shape \(\alpha = 2\) and rates \(\lambda_1 = 2\) and \(\lambda_1 = 1\). Similarly, the risk difference equals zero and the risk ratio equals one when \(\lambda_1 = \lambda_0\) but both vary with \(t\) otherwise.

Code
Weibull-OR.R
## Rate ratios and odds ratios for the Weibull distribution

# log-logistic odds function
odds_weib <- function(t, shape=1, rate=1) {
  p <- 1 - exp(-(rate * t)^shape)
  p / (1 - p)
}

# save old graphics parameters to restore them
mfrow_old <- par("mfrow")
mar_old <- par("mar")

# set two rows and one column and adjust margins between plots
par(mar = c(4, 4 ,1, 1), mfrow = c(2, 1))

# odds with shape = 2 and rates = 1 and 2
t <- seq(0.01, 0.6, by = 0.01)
plot(t, odds_weib(t, shape = 2, rate = 2), type = "l", lty = "dashed",
     xlab = "", ylab = "Odds")
lines(t, odds_weib(t, shape = 2, rate = 1))
grid()
text(0.1, 2.75, labels = "both shape = 2")
text(0.4, 0.4, labels = "rate = 1")
text(0.4, 1.3, labels = "rate = 2")

# odds ratio
plot(t, odds_weib(t, 2, 2) / odds_weib(t, 2, 1), type = "l", ylim = c(0, 8),
     xlab = "Time", ylab = "Odds ratio")
grid()
text(0.1, 7, labels = "rate ratio = 2")

# restore old graphics parameters
par(mar = mar_old, mfrow = mfrow_old)
Figure 10.2: Odds (top) and odds ratio (bottom) for Weibull distributions with shape \(\alpha = 2\) and rates \(\lambda_1 = 2\) in the exposed and \(\lambda_0 = 1\) in the unexposed.

If the failure time distributions are log-logistic with the same shape parameter \(\alpha\), the risk of disease onset in \((0, t]\) given \(X = x\) is \[ 1 - S(t, \alpha, \lambda_x) = 1 - \frac{1}{1 + (\lambda_1 t)^\alpha} = \frac{(\lambda_1 t)^\alpha}{1 + (\lambda_1 t)^\alpha}, \] and the corresponding odds of disease is \[ \frac{(\lambda_x t)^\alpha}{x + (\lambda_x t)^\alpha} \times \frac{x + (\lambda t)^\alpha}{1} = (\lambda_x t)^\alpha. \] When \(\lambda_1 \neq \lambda_0\), the risk difference and risk ratio are not constant in time. However, the odds ratio is \[ \frac{(\lambda_1 t)^\alpha}{(\lambda_0 t)^\alpha} = \bigg(\frac{\lambda_1}{\lambda_0}\bigg)^\alpha, \] which is constant in time and equal to \((\text{rate ratio})^\alpha\). Thus, the odds ratio for log-logistic times to events has the same form as the hazard ratio for Weibull times to events. The risk difference and risk ratio both vary with \(t\) when \(\lambda_1 \neq \lambda_0\).

10.2 Accelerated failure time models

Parametric regression models in survival analysis are based on rate parameters, which measure how quickly time passes relative to a baseline time-to-event distribution. In an accelerated failure time (AFT) regression model, predictors act multiplicatively on the random time to event \(T\): \[ T = e^{\beta_0 + \beta_1 x_1 + \ldots + \beta_k x_k} \times \tau, \] where \(\tau\) is a random sample from the baseline survival time distribution, which defines \(\lambda = 1\). The multiplier of \(\tau\) is the scale parameter \[ \sigma(\beta, x) = e^{\beta_0 + \beta_1 x_1 + \ldots + \beta_k x_k}, \] which measures how much the baseline failure time distribution is compressed (\(\sigma < 1\)) or stretched out (\(\sigma > 1\)). A one-unit increase in \(x_j\) with all other covariates held constant is associated with a survival time that is multiplied by \(\exp(\beta_j)\), which is called the acceleration factor. The rate parameter is \[ \lambda(\beta, x) = e^{-\big(\beta_0 + \beta_1 x_1 + \ldots + \beta_k x_k\big)}, \tag{10.2}\] which measures how quickly time passes relative to the baseline failure-time distribution. A one-unit increase in \(x_j\) with all other covariates held constant is associated with a rate ratio of \(\exp(-\beta_j)\). The null hypothesis of no association with \(X_j\) corresponds to \(\beta_j = 0\).

We will discuss AFT models that use exponential, Weibull, and log-logistic distributions with rate \(\lambda = 1\) has their baseline distributions. The gamma and log-normal distributions are also widely used, but they do not have simple closed forms for the hazard, cumulative hazard, or survival functions. All of these models can be fit using data with left truncation and right censoring, and the construction of the likelihood is the same. AFT models are a useful alternative to analyses based on incidence rates when the times to events are not exponential.

For simplicity, we will focus on AFT models with a single binary predictor \(X\). In these models, the rate parameter is \[ \lambda(x, \beta) = e^{-(\beta_0 + \beta_1 x)} = \begin{cases} e^{-\beta_0} & \text{in the unexposed,}\\ e^{-(\beta_0 + \beta_1)} & \text{in the exposed.} \end{cases} \tag{10.3}\] The rate ratio is \[ \frac{\lambda_1}{\lambda_0} = \frac{e^{-(\beta_0 + \beta_1)}}{e^{-\beta_0}} = e^{-\beta_1}. \] The relationship between this rate ratio and other measures of association depends on the underlying failure time distribution.

10.2.1 Exponential AFT model

An exponential distribution has a rate parameter but no shape parameter, so an exponential AFT model only has to estimate the coefficient vector \(\beta\) from Equation 10.3. In an exponential AFT model, the hazard function is \[ h(t, x, \beta) = \lambda(x, \beta), \] which is the exponential hazard function with \(\lambda = \lambda(x, \beta)\) from Equation 10.3. The corresponding cumulative hazard is \[ H(t, x, \beta) = \lambda(X, \beta) t. \] With left-truncated and right-censored data \[ (\tentry_i, \texit_i, \delta_i, x_i) \text{ for } i = 1, \ldots, n, \tag{10.4}\] the log likelihood for \(\beta\) is \[ \ell(\beta) = \sum_{i = 1}^n \Big(\delta_i \ln h(\texit_i, x_i, \beta) - H(\texit_i, x_i, \beta) + H(\tentry_i, x_i, \beta)\Big). \] Point and interval estimates of \(\beta\) can be obtained using maximum likelihood estimation (see Chapter 3) or Bayesian methods (see Chapter 12).

AFT-exponential.R
## Exponential accelerated failure time (AFT) model

# The rats data is from:
# Mantel, Bohidar, and Ciminera (1977). Cancer Research 37: 3863-3868.
library(survival)               # rats data and survreg()
library(lmtest)                 # coefci() for Wald confidence intervals

# exponential AFT model
# Default distribution is Weibull, so we must specify dist = "exponential".
# Coefficients are log scale parameters = -log rate parameters.
aft_exp <- survreg(Surv(time, status) ~ rx, data = rats, dist = "exponential")
exp(-coef(aft_exp))             # estimated rate (x = 0) and rate ratio
exp(-confint(aft_exp))          # log-transformed 95% Wald confidence intervals

# estimated rate in the exposed
lnrate1hat <- -sum(coef(aft_exp))
lnrate1var <- as.numeric(c(1, 1) %*% vcov(aft_exp) %*% c(1, 1))
lnrate1ci <- lnrate1hat + c(-1, 1) * qnorm(0.975) * sqrt(lnrate1var)
exp(lnrate1hat)                 # point estimate
exp(lnrate1ci)                  # log-transformed 95% Wald confidence interval

# exponential AFT results match Poisson GLM with log(rat-weeks) offset
IRRglm <- glm(status ~ rx  + offset(log(time)), family = poisson(), data = rats)
exp(coef(IRRglm))               # estimated incidence rate (x = 0) and IRR
exp(coefci(IRRglm))             # log-transformed Wald CIs matches above

# compare with nonparametric estimates
# Nelson-Aalen estimates (with Fleming-Harrington correction for ties)
# Formula ~ rx produces a separate estimate for each treatment group.
# Arguments stype = 2 and ctype = 2 produces NA estimate with FH correction.
ratsNA <- survfit(Surv(time, status) ~ rx, data = rats,
                  stype = 2, ctype = 2, conf.type =  "log-log")

# plot of Nelson-Aalen cumulative hazard curves in treated and untreated rats
plot(ratsNA, fun = "cumhaz", col = c("darkgray", "black"),
     xlab = "Weeks after treatment", ylab = "Cumulative hazard")
grid()
legend("topleft", bg = "white", lty = rep(c("solid", "dashed"), times = 2),
       col = rep(c("black", "darkgray"), each = 2),
       legend = c("Control (Nelson-Aalen)", "Control (Exponential AFT model)",
                  "Treated (Nelson-Aalen)", "Treated (Exponential AFT model)"))

# estimated cumulative hazards from exponential AFT model
aft_exp <- survreg(Surv(time, status) ~ rx, data = rats, dist = "exponential")
rate1_exp <- as.numeric(exp(-c(1, 1) %*% coef(aft_exp)))
rate0_exp <- exp(-coef(aft_exp)["(Intercept)"])
H_exp <- function(t, rate) rate * t

# add lines to plot
t <- seq(0, max(ratsNA$time), by = 0.1)
lines(t, H_exp(t, rate1_exp), lty = "dashed", col = "black")
lines(t, H_exp(t, rate0_exp), lty = "dashed", col = "darkgray")

10.2.2 Weibull AFT model

The Weibull AFT model generalizes the exponential model in the same way that the Weibull distribution generalizes the exponential distribution. As in the exponential AFT model, the rate parameter is determined by \(x\) and \(\beta\) as in Equation 10.3. The hazard function in a Weibull AFT model is \[ h(t, \alpha, \beta, x) = \alpha \lambda(\beta, x)^\alpha t^{\alpha - 1}, \] which is the hazard function of the Weibull(\(\alpha\), \(\lambda\)) distribution where \(\lambda = \lambda(\beta, X)\). The corresponding cumulative hazard function is \[ H(t, \alpha, \beta, x) = \big(\lambda(\beta, x) t\big)^\alpha. \] With left-truncated and right-censored data as in Equation 10.4, the log likelihood for \(\alpha\) and \(\beta\) is \[ \ell(\alpha, \beta) = \sum_{i = 1}^n \Big(\delta_i \ln h(\texit_i, x_i, \alpha, \beta) - H(t_i, x_i, \alpha, \beta) + H(\tentry_i, x_i, \alpha, \beta)\Big). \tag{10.5}\] Point and interval estimates of \(\alpha\) and \(\beta\) can be obtained using maximum likelihood estimation or Bayesian methods.

AFT-Weibull.R
## Weibull accelerated failure time (AFT) model

# The rats data is from:
# Mantel, Bohidar, and Ciminera (1977). Cancer Research 37: 3863-3868.
library(survival)               # rats data and survreg()

# Weibull accelerated failure time model
# The Weibull distribution is the default for survival::survreg().
# Coefficients are log scale parameters = -log rate parameters.
aft_weib <- survreg(Surv(time, status) ~ rx, data = rats)
exp(-coef(aft_weib)["rx"])      # estimated rate (x = 0) and rate ratio
exp(-confint(aft_weib)["rx", ]) # log-transformed 95% Wald confidence intervals
shapehat <- 1 / aft_weib$scale  # shape parameter point estimate
shapehat

# shape parameter Z-score and p-value
# The null hypothesis shape = 0 corresponds to an exponential distribution.
lnshapevar <- vcov(aft_weib)["Log(scale)", "Log(scale)"]
shapeZ <- log(shapehat) / sqrt(lnshapevar)
shapeZ
2 * pnorm(-abs(shapeZ))

# shape parameter 95% CI
exp(-log(aft_weib$scale) + c(-1, 1) * qnorm(.975) *
    sqrt(vcov(aft_weib)["Log(scale)", "Log(scale)"]))

# estimated rate in the exposed
lnrate1hat <- -sum(coef(aft_weib))
lnrate1var <- as.numeric(c(1, 1, 0) %*% vcov(aft_weib) %*% c(1, 1, 0))
lnrate1ci <- lnrate1hat + c(-1, 1) * qnorm(0.975) * sqrt(lnrate1var)
exp(lnrate1hat)                 # point estimate
exp(lnrate1ci)                  # log-transformed 95% Wald confidence interval

# compare with nonparametric estimates
# Nelson-Aalen estimates (with Fleming-Harrington correction for ties)
# Formula ~ rx produces a separate estimate for each treatment group.
# Arguments stype = 2 and ctype = 2 produces NA estimate with FH correction.
ratsNA <- survfit(Surv(time, status) ~ rx, data = rats,
                  stype = 2, ctype = 2, conf.type =  "log-log")

# plot of Nelson-Aalen cumulative hazard curves in treated and untreated rats
plot(ratsNA, fun = "cumhaz", col = c("darkgray", "black"),
     xlab = "Weeks after treatment", ylab = "Cumulative hazard")
grid()
legend("topleft", bg = "white", lty = rep(c("solid", "dashed"), times = 2),
       col = rep(c("black", "darkgray"), each = 2),
       legend = c("Control (Nelson-Aalen)", "Control (Weibull AFT model)",
                  "Treated (Nelson-Aalen)", "Treated (Weibull AFT model)"))

# estimated cumulative hazards from Weibull AFT model
aft_weib <- survreg(Surv(time, status) ~ rx, data = rats)
rate1_weib <- as.numeric(exp(-c(1, 1) %*% coef(aft_weib)))
rate0_weib <- exp(-coef(aft_weib)["(Intercept)"])
shape_weib <- 1 / aft_weib$scale
H_weib <- function(t, shape, rate) (rate * t)^shape

# add lines to plot
t <- seq(0, max(ratsNA$time), by = 0.1)
lines(t, H_weib(t, shape_weib, rate1_weib), lty = "dashed")
lines(t, H_weib(t, shape_weib, rate0_weib), lty = "dashed", col = "darkgray")

10.2.3 Log-logistic AFT model

The log-logistic AFT model has the same likelihood as the Weibull AFT model except that we replace the Weibull hazard and cumulative hazard functions with the log-logistic hazard and cumulative hazard functions. As in the exponential and Weibull AFT models, the rate parameter is determined by \(x\) and \(\beta\) as in Equation 10.3. The log-logistic hazard function is \[ h(t, x, \alpha, \beta) = \frac{\alpha \lambda(x, \beta)^\alpha t^{\alpha - 1}} {1 + \big(\lambda(x, \beta) t\big)^\alpha}, \] and the cumulative hazard function is \[ H(t, X, \beta, \alpha) = \ln\Bigl(1 + t^\alpha e^{-\alpha (\beta_0 + \beta_1 X)}\Bigr). \] With left-truncated and right-censored data as in Equation 10.4, the likelihood is given by Equation 10.5. Point and interval estimates of \(\alpha\) and \(\beta\) can be obtained using maximum likelihood estimation or Bayesian methods.

AFT-loglogistic.R
## Log-logistic accelerated failure time (AFT) model

# The rats data is from:
# Mantel, Bohidar, and Ciminera (1977). Cancer Research 37: 3863-3868.
library(survival)               # rats data and survreg()

# Log-logistic accelerated failure time model
# Default distribution is Weibull, so we must specify dist = "loglogistic".
# Coefficients are log scale parameters = -log rate parameters.
aft_llog <- survreg(Surv(time, status) ~ rx, data = rats, dist = "loglogistic")
exp(-coef(aft_llog)["rx"])      # estimated rate (x = 0) and rate ratio
exp(-confint(aft_llog)["rx", ]) # log-transformed 95% Wald confidence intervals
1 / aft_llog$scale              # shape parameter point estimate

# shape parameter 95% CI
exp(-log(aft_llog$scale) + c(-1, 1) * qnorm(.975) *
    sqrt(vcov(aft_llog)["Log(scale)", "Log(scale)"]))

# estimated rate in the exposed
lnrate1hat <- -sum(coef(aft_llog))
lnrate1var <- as.numeric(c(1, 1, 0) %*% vcov(aft_llog) %*% c(1, 1, 0))
lnrate1ci <- lnrate1hat + c(-1, 1) * qnorm(0.975) * sqrt(lnrate1var)
exp(lnrate1hat)                 # point estimate
exp(lnrate1ci)                  # log-transformed 95% Wald confidence interval

# compare with nonparametric estimates
# Nelson-Aalen estimates (with Fleming-Harrington correction for ties)
# Formula ~ rx produces a separate estimate for each treatment group.
# Arguments stype = 2 and ctype = 2 produces NA estimate with FH correction.
ratsNA <- survfit(Surv(time, status) ~ rx, data = rats,
                  stype = 2, ctype = 2, conf.type =  "log-log")

# plot of Nelson-Aalen cumulative hazard curves in treated and untreated rats
plot(ratsNA, fun = "cumhaz", col = c("darkgray", "black"),
     xlab = "Weeks after treatment", ylab = "Cumulative hazard")
grid()
legend("topleft", bg = "white", lty = rep(c("solid", "dashed"), times = 2),
       col = rep(c("black", "darkgray"), each = 2),
       legend = c("Control (Nelson-Aalen)", "Control (Log-logistic AFT model)",
                  "Treated (Nelson-Aalen)", "Treated (Log-logistic AFT model)"))

# estimated cumulative hazards from log-logistic AFT model
aft_llog <- survreg(Surv(time, status) ~ rx, data = rats, dist = "loglogistic")
rate1_llog <- as.numeric(exp(-c(1, 1) %*% coef(aft_llog)))
rate0_llog <- exp(-coef(aft_llog)["(Intercept)"])
shape_llog <- 1 / aft_llog$scale
H_llog <- function(t, shape, rate) log(1 + (rate * t)^shape)

# add lines to plot
t <- seq(0, max(ratsNA$time), by = 0.1)
lines(t, H_llog(t, shape_llog, rate1_llog), lty = "dashed")
lines(t, H_llog(t, shape_llog, rate0_llog), lty = "dashed", col = "darkgray")

10.3 Log-rank test

The log rank test (Mantel 1966) is a nonparametric score test for the null hypothesis that two groups have the same failure time distribution. Let \(t_1 < t_2 < \cdots < t_m\) be the distinct times where failures occur. Let \(d_i\) denote the number of failures at time \(t_i\), and let \(n_i\) denote the number of people in the risk set \(\mathcal{R}_i\) (i.e., the set of people at risk of failure at time \(t_i\)). Let \(X\) be a binary covariate. Let \(n_{1i}\) denote the number of people in \(R_i\) with \(X = 1\), and let \(d_{1i}\) denote the number of of these that fail at time \(t_i\). For now, we assume no tied failure times, so \(d_{1i} = 0\) or \(d_{1i} = 1\) for each \(i\).

10.3.1 Observed and expected failures among the exposed

The log-rank test is a score test, so it uses a test statistic calculated assuming the null hypothesis is true. Here, the null hypothesis is that the survival time distribution is the same among the exposed (\(X = 1\)) and the unexposed (\(X = 0\)). Under the null hypothesis, the \(d_i\) failures at time \(t_i\) occur in individuals randomly chosen from \(n_i\) individuals in the risk set \(\mathcal{R}(t_i)\). The number of exposed individuals \(D_{1i}\) among the randomly chosen \(d_i\) individuals has a hypergeometric distribution (see Section 7.1.1) with mean \[ \E(D_{1i}) = d_{i} p_{1i} \] where \[ p_{1i} = \frac{n_{1i}}{n_i} \] is the proportion of the risk set that is exposed. Its variance is \[ \Var(D_{1i}) = p_{1i} (1 - p_{1i}) \frac{d_i (n_i - d_i)}{n_i - 1}. \] In the special case where \(d_i = 1\), this hypergeometric distribution is a Bernoulli(\(p_{1i}\)) distribution with mean \(p_{1i}\) and variance \(p_{1i} (1 - p_{1i})\).

10.3.2 Chi-squared statistic

The numerator of the log-rank chi-squared statistic is total number of observed failures among the exposed minus the total number of expected failures among the exposed under the null hypothesis: \[ U = \sum_{i = 1}^m \Big(d_{1i} - d_i p_{1i}\Big). \tag{10.6}\] The denominator is the variance of \(U_\text{logrank}\), which is \[ \Var(U) = \sum_{i = 1}^m p_{1i} (1 - p_{1i}) \frac{d_i (n_i - d_i)}{n_i - 1} \] because the \(D_{1i}\) are independent. The log-rank test statistic is \[ \chi^2_\text{log-rank} = \frac{U^2}{\Var(U)}. \tag{10.7}\] When the number of observed failures is large, this has a chi-squared distribution with one degree of freedom under the null. We reject the null hypothesis for large values of \(\chi^2_\text{logrank}\). We get exactly the same chi-squared statistic and p-value if we use the observed and expected number of failures among the unexposed.

10.3.3 Weighted log-rank tests

Many nonparametric tests of differences between survival curves are weighted versions of the log-rank test. The weighted sum of the differences between the observed and expected numbers of events among the exposed is \[ U = \sum_{i = 1}^m w_i (d_{1i} - d_i p_{1i}), \] and the corresponding variance is \[ V = \sum_{i = 1}^m w_i^2 \bigg(p_{1i}(1 - p_{1i}) \frac{d_i (n_i - d_i)}{n_i - 1}\bigg). \] Table 10.1 shows the weights used by various tests. In the weights, \(\hat{S}(t)\) is the Kaplan-Meier estimate of the survival function, \(\hat{S}_0(t)\) is the Kaplan-Meier estimate among the unexposed, and \(\hat{S}_1(t)\) is the Kaplan-Meier estimate among the exposed. The Harrington-Fleming test is equivalent to the log-rank test when \(\rho = 0\), and it approximates the Peto-Prentice test when \(\rho = 1\). The weighted tests often give larger weights to earlier survival times, where there is usually more data.

Table 10.1: Weighted log-rank tests (Aalen, Borgan, and Gjessing 2008).
Test Weight (proportional to \(w_i\)) Reference
Log-rank \(1\) (Mantel 1966)
Gehan-Breslow \(n_i\) (Gehan 1965; N. Breslow 1970)
Tarone-Ware \(\sqrt{n_i}\) (Tarone and Ware 1977)
Harrington-Fleming \(\hat{S}(t_{i - 1})^\rho\) (Harrington and Fleming 1982)
Efron \(\hat{S}_0(t_{i - 1}) \hat{S}_1(t_{i - 1})\) (Efron 1967)
Peto-Prentice \(\prod_{i: t_i < t} \Big(1 - \frac{d_i}{n_i + 1}\Big)\) (Peto and Peto 1972; Prentice 1978)
logrank.R
## Log-rank test

# The log-rank test is done using survdiff() is in the survival package.
# The rats data is from:
# Mantel, Bohidar, and Ciminera (1977). Cancer Research 37: 3863-3868.
library(survival)               # rats data and survdiff
library(lmtest)                 # coeftest() for Wald test of coefficients

# log-rank test (rho = 0 by default)
# More tumor incidence observed than expected under the null in treated rats.
survdiff(Surv(time, status) ~ rx, data = rats)

# Peto-Prentice test (rho = 1)
# More tumor incidence observed than expected under the null in treated rats.
survdiff(Surv(time, status) ~ rx, data = rats, rho = 1)

# comparison with parametric AFT models
# exponential
aft_exp <- survreg(Surv(time, status) ~ rx, data = rats, dist = "exponential")
coeftest(aft_exp)["rx", "Pr(>|z|)"]
# Weibull
aft_weib <- survreg(Surv(time, status) ~ rx, data = rats, dist = "weibull")
coeftest(aft_weib)["rx", "Pr(>|z|)"]
# log-logistic
aft_llog <- survreg(Surv(time, status) ~ rx, data = rats, dist = "loglogistic")
coeftest(aft_llog)["rx", "Pr(>|z|)"]

10.4 Cox model

The Cox proportional hazards model [Cox (1972)} is a regression model that can estimate hazard ratios without making any other assumption about the underlying failure time distributions.1 It is a semiparametric model, which means that it has a parametric component and a nonparametric component. In the Cox model, the hazard function is \[ h(t, x, \beta) = e^{\beta_1 x_1 + \ldots + \beta_k x_k} h_0(t), \tag{10.8}\] where the first term on the right-hand side is the relative hazard function and \(h_0(t)\) is an unspecified baseline hazard function. The parametric component of the model is the relative hazard, where each predictor has a multiplicative effect on the hazard function. The nonparametric component of the model is the unspecified baseline hazard. There is no intercept because \(h_0(t)\) represents the hazard for an individual with all predictors equal to zero.

For simplicity, we will focus on Cox models with a single binary predictor \(X\). In these models, the relative hazard is \[ e^{\beta_1 x} = \begin{cases} 1 & \text{in the unexposed,}\\ e^{\beta_1} & \text{in the exposed.} \end{cases} \] Thus, the hazard ratio comparing the exposed to the unexposed is \(e^{\beta_1}\). The Cox model gives us a way of estimating the hazard ratio without making any assumption about the distributions of times to events in the exposed and unexposed except for proportional hazards. In a Cox model with a single binary covariate, the log-rank test from Section 10.3 is a score test of the null hypothesis that \(\beta = 0\).

10.4.1 Proportional hazards assumption

The proportional hazards assumption is much weaker than any assumption that the times to events in the exposed and unexposed come from a specific family, such as exponential, Weibull, or log-logistic distributions. However, it is still an assumption. The exponential and Weibull AFT models are special cases of the Cox model because the hazard ratio comparing the exposed to the unexposed is always \((\lambda_1 / \lambda_0)^\alpha\) where \(\lambda_1\) is the rate parameter for the exposed, \(\lambda_0\) is the rate parameter for the unexposed, and \(\alpha\) is the shape parameter. When the underlying failure times are truly exponential or Weibull, the corresponding AFT models will have slightly more power than the Cox model. The log-logistic AFT model is not a proportional hazards model because the hazard ratio changes over time, as shown in Equation 10.1.

10.4.2 Partial likelihood

The Cox model is fit using a partial likelihood, which leaves out parts of the full likelihood but behaves like a likelihood for the purposes of maximum likelihood estimation. The Cox partial likelihood retains critical information about the hazard ratios without placing constraints on the baseline hazard. To do this, it relies on a fundamental relationship between conditional probabilities and hazard functions.

Suppose \(T_1\) and \(T_2\) are independent failure times with survival functions \(S_1(t)\) and \(S_2(t)\), respectively. Let \(T = \min(T_1, T_2)\). Then the survival function of \(T\) is \[ S(t) = S_1(t)S_2(t) \] because \(T > t\) if and only if \(T_1 > t\) and \(T_2 > t\). The cumulative hazard function of \(T\) is \[ H(t) = -\ln S(t) = -\ln S_1(t) - \ln S_2(t) = H_1(t) + H_2(t), \] where \(H_1(t)\) an \(H_2(t)\) are the cumulative hazard functions of \(T_1\) and \(T_2\). Taking derivatives, we get the hazard function \[ h(t) = H'(t) = H_1'(t) + H_2'(t) = h_1(t) + h_2(t), \] where \(h_1(t)\) and \(h_2(t)\) are the hazard functions of \(T_1\) and \(T_2\).

This logic extends to the minimum of any finite set of survival times \(T_1, \ldots, T_n\). The survival function of \(T = \min(T_1, \ldots, T_n)\) is \[ S(t) = \prod_{i = 1}^n S_i(t), \tag{10.9}\] and the hazard function is \[ h(t) = \sum_{i = 1}^n h_i(t), \tag{10.10}\] where \(S_i(t)\) is the survival function and \(h_i(t)\) is the hazard function of \(T_i\).

The probability density function is the product of the hazard in Equation 10.10 and the survival in Equation 10.9. The probability density for a failure in any individual at time \(t\) is \[ \big(h_1(t) + \ldots + h_n(t)\big) \prod_{i = 1}^n S_i(t). \tag{10.11}\] If the first failure occurred in person \(k\), then the likelihood contribution would be \[ h_k(t) \prod_{i = 1}^n S_i(t). \tag{10.12}\] because the person who has an event contributes a hazard term and everyone (including the person who failed) contributes a survival term. To calculate the conditional probability that the failure occurred in person \(k\) given that there was a failure at time \(t\), we divide the likelihood in Equation 10.12 by the likelihood in Equation 10.11: \[ \frac{h_k(t) \prod_{i = 1}^n S_i(t)} {\big(h_1(t) + \ldots + h_n(t)\big) \prod_{i = 1}^n S_i(t)} = \frac{h_k(t)}{h_1(t) + \ldots + h_n(t)}. \] The survival functions cancel out, leaving only the hazards. Given that there is a failure at time \(t\), the probability that it occurred in person \(k\) is proportional to their hazard \(h_k(t)\).

The same logic applies to failures other than the first. As before, let \(\mathcal{R}(t)\) denote the risk set (i.e., the set of individuals at risk of an observed event) at time \(t\). Given that a failure occurs at time \(t\), the probability that it occurred in person \(k\) is \[ \frac{h_k(t)}{\sum_{j \in \mathcal{R}(t)} h_j(t)}. \] At any failure time \(t\), these probabilities add up to one. If we substitute the hazard function for the Cox model from Equation 10.8, we get \[ \frac{e^{\beta x_k} h_0(t)}{\sum_{j \in \mathcal{R}(t) e^{\beta x_j} h_0(t)}} = \frac{e^{\beta x_k}}{\sum_{j \in \mathcal{R}(t) e^{\beta x_j}}}. \] because the baseline hazard \(h_0(t)\) cancels out. When there are no tied failure times, let \(i\) be the index of the individual who has an event at time \(t_i\). Then the Cox partial likelihood is \[ L_\text{Cox}(\beta) = \prod_{i = 1}^m \frac{e^{\beta x_i}}{\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j}}. \] The risk sets are determined in exactly the same way as the Kaplan-Meier and Nelson-Aalen estimators in Chapter 6, so the Cox model can handle left truncation (i.e., delayed entry) and right censoring. It uses the same data from Equation 10.4 as the AFT models. For estimation of \(\beta\), the partial likelihood can be used just like a normal likelihood. This is a consequence of the fact that it can be derived as a profile likelihood where the likelihood for \(\beta\) is the full likelihood maximized over all possible baseline hazard functions (Johansen 1983).

10.4.3 Correction for ties*

There are three common methods for dealing with ties in a Cox model (in order of complexity): Breslow, Efron, and exact. The Breslow approximation (Peto 1972; N. Breslow 1974) is the simplest but least accurate. The exact method is accurate but computationally complex (Kalbfleisch and Prentice 2011). The Efron approximation (Efron 1977) is both accurate and computationally efficient. All of these methods assume that failures happen in continuous time and that ties are caused by rounding off of survival times (i.e., times measured to the day or week rather than hour, minute, and second).

10.4.3.1 Exact method

If there are tied survival times at time \(t_i\), then the exact method calculates the mean partial likelihood contribution at time \(t_i\) over all \(d_i!\) possible ways of breaking ties among the people who failed at time \(t_i\) (Kalbfleisch and Prentice 2011). These calculations get complex quickly—there are \(5! = 120\) ways to break ties among 5 events, \(10! = 3,628,800\) ways among 10 events, \(15! \approx 1.3\) trillion ways among 15 events, and so on. For simplicity, we illustrate the calculation for two events.

Let \(A\) and \(B\) denote the indices of the individuals who fail at time \(t_i\), with \(d_i = 2\). Let \(x_A\) and \(x_B\) denote their covariates. There are two ways to break the tie: \(A\) fails first or \(B\) fails first. If \(A\) fails first, then the partial likelihood contribution from the two failures is \[ \frac{e^{\beta x_A}}{\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j}} \times \frac{e^{\beta x_B}}{\Big(\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j}\Big) - e^{\beta x_A}}, \] where the denominator in the second term accounts for the fact that \(A\) is no longer in the risk set when \(B\) fails. If \(B\) fails first, the likelihood contribution from the two failures is \[ \frac{e^{\beta x_B}}{\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j}} \times \frac{e^{\beta x_A}}{\Big(\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j}\Big) - e^{\beta x_B}}, \] where the denominator in the second term accounts for the fact that \(B\) is no longer in the risk set when \(A\) fails. In both cases, the numerator is \(e^{\beta x_A + x_B}\), but the denominator is slightly different. If both possibilities are equally likely (which is true if \(\beta = 0\) or \(x_A = x_B\)), the average likelihood contribution is \[ \begin{aligned} &\frac{1}{2} \frac{e^{\beta (x_A + x_B)}} {\Big(\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j}\Big) \Big[\Big(\sum_{j \in \mathcal{R}(t_i)} e^{\beta X_j}\Big) - e^{\beta x_A}\Big]} \\ &\qquad + \frac{1}{2} \frac{e^{\beta (x_A + x_B)}} {\Big(\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j}\Big) \Big[\Big(\sum_{j \in \mathcal{R}(t_i)} e^{\beta X_j}\Big) - e^{\beta x_B}\Big]} \end{aligned} \tag{10.13}\] Extending this to larger numbers of ties is straightforward but tedious. When there are failure times with a large number of ties, this approach becomes computationally intractable.

10.4.3.2 Efron approximation

Let \(\mathcal{D}(t_i)\) denote the set of individuals who fail at time \(t_i\). The Efron approximation (Efron 1977) approximates the denominator of the exact estimate using the mean relative hazard among the \(d_i\) individuals in \(\mathcal{D}(t_i)\), which is \[ \frac{1}{d_i} \sum_{j \in \mathcal{D}(t_i)} e^{\beta x_j}. \] This is also the average amount taken out of the sum in the denominator for each of the first \(d_i - 1\) failures at time \(t_i\). The Efron approximation to the exact mean likelihood contribution is \[ \frac{\prod_{j \in \mathcal{D}(t_i)} e^{\beta x_j}} {\prod_{k = 1}^{d_i} \Big(\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j} - \frac{k - 1}{d_i} \sum_{j \in \mathcal{D}(t_i)} e^{\beta x_j}\Big)} \] This is easy to calculate and is a good approximation to the exact mean likelihood contribution even when there are many ties.

In our example with two tied failures, the Efron approximation to the likelihood contribution in equation~[eq:exact] is \[\frac{e^{\beta (x_A + x_B)}}{\Big(\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j}\Big) \Big(\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j} - \frac{1}{2} \big(e^{\beta x_A} + e^{\beta x_B}\big)\Big)}.\] In the second term in the denominator, we have taken out the average of individuals \(A\) and \(B\).

10.4.3.3 Breslow approximation

The Breslow approximation (Peto 1972; N. E. Breslow 1972) simply ignores the fact that failures are removed from the risk set, giving the following approximation to the exact likelihood contribution: \[ \frac{\prod_{j \in D_i} e^{\beta X_j}}{\Big(\sum_{j \in R_i} e^{\beta X_j}\Big)^{d_i}}. \] In our example with tied failure times in \(y\) and \(z\), the Breslow approximation to the likelihood contribution in Equation 10.13 is \[ \frac{e^{\beta (x_A + x_B)}}{\Big(\sum_{j \in \mathcal{R}(t_i)} e^{\beta x_j}\Big)^2}. \] This leaves the first person to fail (\(A\) or \(B\)) in the risk set when the second person fails. When the size of the risk set is large compared to the number of failures, this is not a terrible approximation. However, the Efron approximation stays accurate when ties become more severe.

10.4.4 Estimation of baseline survival and cumulative hazard*

Given an estimate of \(\hat{\beta}\) of \(\beta_\true\), we can estimate the baseline cumulative hazard \[ H_0(t) = \int_0^t h(u) \,\dif u \] and the baseline survival \[ S_0(t) = e^{-H_0(t)}. \] This allows us to estimate the cumulative hazard or survival for any given combination of covariates \(x\):We can also estimate the cumulative hazard function for any given covariate \(x\): \[ \hat{H}(t, x) = e^{\hat{\beta} x} \hat{H}_0(t) \] and \[ \hat{S}(t, x) = e^{-\hat{H}(t, x)}. \] Thus, a Cox model can be used to estimate risks, odds, and measures of association such as the risk difference, risk ratio, or odds ratio. As in Section 10.1, these measures of association will often depend on \(t\).

One way to estimate the baseline cumulative hazard is to calculate a Nelson-Aalen estimate in the subset with \(X = 0\). However, it is much more efficient to use all of the data. Here, we look at three methods that do this. Two of them are generalizations of the Nelson-Aalen estimator, and one is a generalization of the Kaplan-Meier estimator. Variance estimation for these baseline hazard estimators accounts for two sources of uncertainty: the uncertainty in our estimate of \(\beta_\true\) and the uncertainty that we would have in the baseline cumulative hazard or survival even if we knew \(\beta_\true\) (similar to the variance of the Nelson-Aalen estimator in Section 6.3 or Kaplan-Meier estimator in Section 6.2). For simplicity, we will focus only on the point estimates.

10.4.4.1 Breslow estimate of the baseline cumulative hazard

The Breslow estimator of \(H_0(t)\) is \[ \hat{H}_0(t) = \sum_{i: t_i \leq t} \frac{d_i}{\sum_{j \in \mathcal{R}(t_i)} e^{\hat{\beta} x_j}}, \] where \(\hat{\beta}\) is the maximum partial likelihood estimate of \(\beta\) (N. E. Breslow 1972). When \(\hat{\beta} = 0\), this reduces to the Nelson-Aalen estimator from Section 6.3.

10.4.4.2 Efron estimate of the baseline cumulative hazard

The Efron estimate of \(H_0(t)\) handles ties in the same way as the Efron approximation for the partial likelihood. Whenever there are \(d_i > 1\) failures at time \(t_i\) in risk set \(R_i\), the contribution to the cumulative hazard estimate is \[ \sum_{k = 1}^{d_i} \frac{1}{\sum_{j \in \mathcal{R}(t_i)} e^{\hat{\beta} x_j} - \frac{k - 1}{d_i} \sum_{j \in \mathcal{D}(t_i)} e^{\hat{\beta} X_j}}, \] where \(\mathcal{D}(t_i)\) represents the set of \(d_i\) individuals who failed. The Efron estimate of \(H_0(t)\) is \[ \hat{H}_0(t) = \sum_{i : t_i \leq t} \sum_{k = 1}^{d_i} \frac{1}{\sum_{j \in \mathcal{R}(t_i)} e^{\hat{\beta} x_j} - \frac{k - 1}{d_i} \sum_{j \in \mathcal{D}(t_i)} e^{\hat{\beta} x_j}}. \] When \(\hat{\beta} = 0\), this reduces to the Nelson-Aalen estimator with the Fleming-Harrington correction for ties from Section 6.3.1.

10.4.4.3 Kalbfleisch-Prentice estimate of the baseline survival

The Kalbfleisch-Prentice estimate of the survival function is based on treating the observed failure times as discrete time points where the probability of failure at time \(t_i\) is \(1 - s_i\) when \(X = 0\). Let \(\mathbf{s} = (s_1, \ldots, s_m)\). Then the likelihood for the data is \[ L(\mathbf{s}) = \prod_{i = 1}^m \bigg(\prod_{j \in \mathcal{D}(t_i)} \Big(1 - s_i^{\exp(\hat{\beta} x_j)}\Big) \prod_{j \in \mathcal{R}(t_i) \setminus \mathcal{D}(t_i)} s_i^{\exp(\hat{\beta} X_j)}\bigg), \] where \(\mathcal{R}(t_i) \setminus \mathcal{D}(t_i)\) is the set of people in the risk set at time \(t_i\) who did not fail. The corresponding log likelihood is \[ \ell(\mathbf{s}) = \sum_{i = 1}^m \bigg(\sum_{j \in \mathcal{D}(t_i)} \ln\Big(1 - s_i^{\exp(\hat{\beta} x_j)}\Big) + \sum_{j \in \mathcal{R}(t_i) \setminus \mathcal{D}(t_i)} e^{\hat{\beta} x_j} \ln s_i\bigg). \] Differentiating \(\ell(\mathbf{s})\) with respect to \(s_i\) and rearranging, we find that \(s_i\) solves the equation \[ \sum_{j \in \mathcal{D}(t_i)} \frac{e^{\hat{\beta} x_j}}{1 - s_i^{\exp(\hat{\beta} x_j)}} = \sum_{j \in \mathcal{R}(t_i)} e^{\hat{\beta} X_j}. \tag{10.14}\] When \(d_i = 1\) and person \(i\) fails at time \(t_i\), this is solved by \[ \hat{s}_i = \bigg(1 - \frac{e^{\hat{\beta} x_i}}{\sum_{j \in \mathcal{R}(t_i)} e^{\hat{\beta} x_j}}\bigg)^{\exp(-\beta x_i)}. \] When \(d_i > 1\), Equation 10.14 must be solved numerically. The Kalbfleisch-Prentice estimate of the survival function is \[ \hat{S}_0(t) = \prod_{i : t_i \leq t} \hat{s}_i. \] When \(\hat{\beta} = 0\), this reduces to the Kaplan-Meier survival estimate from Section 6.2.

Cox-model.R
## Cox proportional hazards regression model

# The rats data is from:
# Mantel, Bohidar, and Ciminera (1977). Cancer Research 37: 3863-3868.
library(survival)               # rats data and survreg()

# Cox model with Efron correction for ties (the default)
# With a single binary predictor, the global score test is the log-rank test.
cox_efron <- coxph(Surv(time, status) ~ rx, data = rats)
names(cox_efron)
summary(cox_efron)

# coefficient point estimates, confidence intervals, and covariance matrix
coef(cox_efron)
confint(cox_efron)
vcov(cox_efron)

# hazard ratio and confidence intervals
exp(coef(cox_efron))
exp(confint(cox_efron))

# Breslow correction for ties
table(rats$time[rats$status == 1])  # table of number of tumor onsets
cox_breslow <- coxph(Surv(time, status) ~ rx, data = rats,
                     ties = "breslow")
summary(cox_breslow)
cox_breslow$method

# exact correction for ties
cox_exact <- coxph(Surv(time, status) ~ rx, data = rats,
                   ties = "exact")
summary(cox_exact)
cox_exact$method

# predicted survival in both treatment groups
# Method for estimating survival mirrors method used to correct for ties.
S_pred <- survfit(cox_efron, newdata = data.frame(rx = c(0, 1)),
                  conf.type = "log-log")
S_summary <- summary(S_pred, times = c(20, 40, 60, 80))
S_summary
names(S_summary)
S_summary$newdata     # order of predicted survival probabilities, etc.
S_summary$conf.int    # confidence level
S_summary$lower       # lower 95% confidence limits
S_summary$upper       # upper 95% confidence limits

# predicted cumulative hazard and confidence limits
# The lower bound for H(t) comes from the upper bound for S(t) and vice versa.
S_summary$cumhaz
-log(S_summary$surv)
-log(S_summary$upper)
-log(S_summary$lower)

# plot of predicted cumulative hazards
# Uses data from both groups for each predicted cumulative hazard function.
plot(S_pred, fun = "cumhaz", lty = c("dashed", "solid"),
     xlab = "Weeks since treatment", ylab = "Cumulative hazard")
grid()
legend("topleft", bg = "white", lty = rep(c("solid", "dashed"), each = 2),
       col = rep(c("black", "darkgray"), 2),
       legend = c("Treated rats (Cox model)", "Treated rats (Nelson-Aalen)",
                  "Untreated rats (Cox model)", "Unreated rats (Nelson-Aalen)"))
# add Nelson-Aalen curves
NAest <- survfit(Surv(time, status) ~ rx, data = rats, stype = 2, ctype = 2,
                 conf.type = "log-log")
lines(NAest, fun = "cumhaz", col = "darkgray", lty = c("dashed", "solid"))

# predicted survival in the untreated rats
S1_pred <- survfit(cox_efron, newdata = data.frame(rx = 1),
                   conf.type = "log-log")
summary(S1_pred, times = c(20, 40, 60, 80))

# predicted survival in the treated rats
S0_pred <- survfit(cox_efron, newdata = data.frame(rx = 0),
                   conf.type = "log-log")
summary(S0_pred, times = c(20, 40, 60, 80))

# plot of risk ratio, hazard ratio, and odds ratio over time
# Risks are zero until time[3], which is 3.
# Negative indices remove elements from vectors.
odds <- function(p) p / (1 - p)
t_indices <- -(1:2)
times <- S0_pred$time[t_indices]
R1_pred <- 1 - S1_pred$surv[t_indices]
R0_pred <- 1 - S0_pred$surv[t_indices]
plot(times, R1_pred / R0_pred, type = "l", lty = "dashed",
     ylim = c(1.7, 2.3), xlab = "Weeks since treatment",
     ylab = "Estimated tumor incidence risk ratio")
lines(times, odds(R1_pred) / odds(R0_pred), lty = "dotted")
abline(h = exp(coef(cox_efron)["rx"]))
grid()
legend("bottomleft", lty = c("dotted", "solid", "dashed"), bg = "white",
       legend = c("Odds ratio", "Hazard ratio", "Risk ratio"))

  1. Sir David Roxbee Cox (1924–2022) is a British statistician who helped develop logistic regression (Cox 1958) and proportional hazards regression. He worked at Imperial College London and Oxford University. As of 6 February 2025, Cox (1972) has 63,224 citations on Google Scholar.↩︎