Skip to contents

cox_ph() fits the Cox proportional hazards model, in which the hazard of a subject with covariates \(x\) is \(h_0(t) \exp(x^\top \beta)\). The baseline hazard \(h_0\) is left unspecified, and the coefficients \(\beta\) are estimated by maximizing the partial likelihood with Newton-Raphson iterations (halving the step whenever it lowers the likelihood). Each coefficient is a log hazard ratio: the change in the log hazard for a one-unit increase in its covariate.

Usage

cox_ph(
  formula,
  data,
  weights,
  subset,
  na.action = stats::na.omit,
  ties = c("efron", "breslow"),
  robust = FALSE,
  cluster,
  frailty = NULL,
  frailty_cluster,
  frailty_theta = 0.5,
  frailty_max_iter = 30,
  conf_level = 0.95,
  max_iter = 30,
  tol = 1e-09
)

Arguments

formula

A formula with a Surv() response on the left and the covariates on the right, such as Surv(time, status) ~ age + sex. Factors are coded with treatment contrasts. A strata() term stratifies the baseline hazard, a cluster() term gives the clusters, and a frailty() term adds a shared frailty.

data

A data frame holding the variables in formula.

weights

Optional case weights, a column of data or a numeric vector.

subset

An optional expression selecting the rows of data to use.

na.action

What to do with rows that have missing values. The default drops them.

ties

The handling of tied event times: "efron" (the default) or "breslow".

robust

Use the robust (sandwich) variance. The default is FALSE, or TRUE when there are clusters.

cluster

Optional cluster identifiers, a column of data or a vector, for observations that are correlated within clusters. Implies robust = TRUE.

frailty

The distribution of a shared frailty: "gamma" or "lognormal". The default, NULL, gives none, unless the formula has a frailty() term.

frailty_cluster

The clusters of a shared frailty, a column of data or a vector, when they're not given by a frailty() term.

frailty_theta

The starting value of the frailty variance, above 0. The default is 0.5.

frailty_max_iter

The largest number of rounds of the frailty fit. The default is 30.

conf_level

The confidence level, between 0 and 1. The default is 0.95.

max_iter

The largest number of Newton-Raphson iterations. The default is 30.

tol

The convergence tolerance: iterations stop once the log partial likelihood changes by less than tol times its size. The default is 1e-9.

Value

A cox_ph object. Use as.data.frame() for the coefficient table, coef() and vcov() for the estimates and their variance, predict() for linear predictors, risk scores, and survival curves, residuals() for diagnostics, baseline_hazard() for the baseline cumulative hazard, and cox_zph() to check proportional hazards. logLik(), AIC(), and nobs() work as usual.

The object also holds loglik (the log partial likelihood at \(\beta = 0\) and at the estimate), the global tests lr_test, wald_test, and score_test (each with statistic, df, and p_value), concordance and its standard error concordance_se, n, n_event, iterations, and converged. With a shared frailty, frailty holds the distribution, the cluster variable, theta (the variance), effects (each cluster's frailty for a gamma frailty, or its log frailty for a lognormal one, named by cluster), the variance test, and the number of iterations of the frailty fit and whether it converged.

Details

Tied event times are handled with Efron's approximation by default, or with Breslow's. With case weights, Efron's approximation uses the mean weight of the tied events at each time.

A strata() term gives each stratum its own baseline hazard while the coefficients are shared, as in Surv(time, status) ~ age + strata(sex). Counting-process data, Surv(start, stop, event), handle delayed entry and time-varying covariates: each row is an interval over which a subject's covariates are constant, and the subject is at risk only within it.

With robust = TRUE, or a cluster (given as an argument or as a cluster() term), the variance is the robust sandwich estimate built from the score residuals, summed within each cluster. That variance is used for the standard errors, the confidence limits, and the Wald test. The likelihood ratio and score tests are unaffected.

A frailty() term, as in Surv(time, status) ~ age + frailty(inst), adds a shared frailty: a random effect for each cluster that multiplies the hazard of all its subjects, for clusters whose subjects are more alike than the covariates explain. frailty(inst) is a gamma frailty, with mean 1 and variance \(\theta\). frailty(inst, distribution = "gaussian") (or frailty.gaussian(inst)) is a lognormal frailty, whose logarithm is normal with mean 0 and variance \(\sigma^2\). The frailty and frailty_cluster arguments do the same as the term. The gamma fit alternates between fitting the coefficients with the clusters' current frailties, setting each frailty to its posterior mean, and choosing \(\theta\) on a grid of 80 values from 1e-8 to 1e3 to maximize the marginal likelihood of the clusters' event counts. The lognormal fit maximizes the partial likelihood over the coefficients and the log frailties together, penalized by their normal density, and updates \(\sigma^2\) from the log frailties and their variance. frailty_test() tests whether the variance is 0. A shared frailty needs right-censored data and ties = "breslow", and can't be combined with strata or a robust variance. Predictions are for a subject with a frailty of 1, and the residuals and the baseline hazard include the fitted frailties.

Examples

lung <- data.frame(
  time = c(306, 455, 1010, 210, 883, 1022, 310, 361, 218, 166, 170, 654, 728, 71, 567),
  status = c(2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  age = c(74, 68, 56, 57, 60, 74, 68, 71, 53, 61, 57, 68, 68, 60, 57),
  sex = c(1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1)
)

fit <- cox_ph(Surv(time, status) ~ age + sex, data = lung)
fit
#> <cox_ph> Surv(time, status) ~ age + sex
#> Efron ties. n = 15, events = 13
#> 
#>     estimate hazard_ratio std_error statistic p_value
#> age -0.03712       0.9636   0.04953   -0.7495  0.4536
#> sex  0.23114       1.2600   0.72224    0.3200  0.7489
#> 
#> Likelihood ratio test: 0.61 on 2 df, p = 0.737
#> Wald test:             0.568 on 2 df, p = 0.753
#> Score test:            0.578 on 2 df, p = 0.749
#> Concordance: 0.558 (std. error 0.0914)

# Hazard ratios with confidence limits
as.data.frame(fit, exponentiate = TRUE)
#>   term  estimate  std_error  statistic   p_value  conf_low conf_high
#> 1  age 0.9635579 0.04953093 -0.7494854 0.4535647 0.8744138  1.061790
#> 2  sex 1.2600308 0.72224460  0.3200247 0.7489496 0.3059183  5.189875

# Survival curves for new subjects
newdata <- data.frame(age = c(55, 70), sex = c(1, 2))
predict(fit, newdata = newdata, type = "survival", times = c(200, 400))
#>   subject strata time  estimate std_error  conf_low conf_high
#> 1       1    all  200 0.7624423 0.1358211 0.5377424         1
#> 2       1    all  400 0.3896045 0.1898316 0.1499281         1
#> 3       2    all  200 0.8221503 0.1190461 0.6190113         1
#> 4       2    all  400 0.5063175 0.1875519 0.2449728         1