Greenwood is a toolkit for survival analysis: the study of how long
it takes for an event to happen, when some of the times are still
unknown. This article walks through a typical analysis of the
lung data, a trial of patients with advanced lung cancer
that comes with the package.
library(greenwood)
head(lung)
#> inst time status age sex ph.ecog ph.karno pat.karno meal.cal wt.loss
#> 1 3 306 2 74 1 1 90 100 1175 NA
#> 2 3 455 2 68 1 0 90 90 1225 15
#> 3 3 1010 1 56 1 0 90 90 NA 15
#> 4 5 210 2 57 1 1 90 60 1150 11
#> 5 1 883 2 60 1 0 100 90 NA 0
#> 6 12 1022 1 74 1 1 50 80 513 0The survival response
time is the follow-up in days and status is
1 for a censored patient (alive at last contact) and 2 for a death.
Surv() combines them into a survival response, with a
+ marking each censored time.
Surv(lung$time[1:8], lung$status[1:8])
#> <Surv[8]: right>
#> [1] 306 455 1010+ 210 883 1022+ 310 361The estimators and models take the response on the left of a formula.
Survival curves
kaplan_meier() estimates the probability of surviving
past each time. With a grouping variable on the right of the formula, it
estimates one curve per group.
fit <- kaplan_meier(Surv(time, status) ~ sex, data = lung)
fit
#> <kaplan_meier> Surv(time, status) ~ sex
#>
#> n events median 0.95 LCL 0.95 UCL
#> sex=1 138 112 270 212 310
#> sex=2 90 53 426 348 550Every table of estimates starts with a strata column,
which is "all" for a fit without groups, so the layout
doesn’t depend on the number of curves.
head(as.data.frame(fit))
#> strata sex time n_risk n_event n_censor estimate std_error conf_low
#> 1 sex=1 1 11 138 3 0 0.9782609 0.01241392 0.9542301
#> 2 sex=1 1 12 135 1 0 0.9710145 0.01428117 0.9434235
#> 3 sex=1 1 13 134 2 0 0.9565217 0.01735977 0.9230952
#> 4 sex=1 1 15 132 1 0 0.9492754 0.01867953 0.9133612
#> 5 sex=1 1 26 131 1 0 0.9420290 0.01989290 0.9038355
#> 6 sex=1 1 30 130 1 0 0.9347826 0.02101829 0.8944820
#> conf_high
#> 1 1.0000000
#> 2 0.9994124
#> 3 0.9911586
#> 4 0.9866017
#> 5 0.9818365
#> 6 0.9768989
predict(fit, times = c(180, 365))
#> strata sex time estimate
#> 1 sex=1 1 180 0.6444650
#> 2 sex=1 1 365 0.3360878
#> 3 sex=2 2 180 0.8424017
#> 4 sex=2 2 365 0.5264630plot_survival() draws the curves with their confidence
bands and censoring marks, and can stack the numbers at risk
underneath.
plot_survival(fit, risk_table = TRUE, times = c(0, 250, 500, 750, 1000))
Comparing groups
The log-rank test asks whether the groups’ survival differs. The restricted mean survival time compares the average survival up to a horizon, which is easier to interpret.
logrank_test(Surv(time, status) ~ sex, data = lung)
#> <greenwood_test> Surv(time, status) ~ sex
#> Log-rank test
#>
#> n observed expected
#> sex=1 138 112 91.58174
#> sex=2 90 53 73.41826
#>
#> Chi-squared = 10.33 on 1 degree of freedom, p = 0.00131
rmst_test(Surv(time, status) ~ sex, data = lung, tau = 365)
#> <rmst_test> Surv(time, status) ~ sex
#> RMST difference (tau = 365)
#>
#> rmst std_error
#> sex=1 241.4951 10.35823
#> sex=2 297.4654 10.79132
#>
#> Difference (1 vs 2): -55.97, 95% CI [-85.29, -26.65]
#> z = -3.742, p = 0.000183Cox regression
cox_ph() models the hazard as a baseline hazard
multiplied by the exponential of a linear combination of covariates.
tidy() gives the coefficients as a data frame, and
glance() gives the model’s summary statistics in one
row.
cox <- cox_ph(Surv(time, status) ~ age + sex + ph.ecog, data = lung)
cox
#> <cox_ph> Surv(time, status) ~ age + sex + ph.ecog
#> Efron ties. n = 227, events = 164
#>
#> estimate hazard_ratio std_error statistic p_value
#> age 0.01107 1.0111 0.009267 1.194 2.324e-01
#> sex -0.55261 0.5754 0.167739 -3.294 9.861e-04
#> ph.ecog 0.46373 1.5900 0.113577 4.083 4.447e-05
#>
#> Likelihood ratio test: 30.5 on 3 df, p = 1.08e-06
#> Wald test: 29.9 on 3 df, p = 1.43e-06
#> Score test: 30.5 on 3 df, p = 1.08e-06
#> Concordance: 0.637 (std. error 0.0251)
#>
#> 1 row was dropped for missing values.
tidy(cox, exponentiate = TRUE)
#> term estimate std_error statistic p_value conf_low conf_high
#> 1 age 1.0111282 0.009267411 1.194159 2.324157e-01 0.9929281 1.0296620
#> 2 sex 0.5754446 0.167739054 -3.294477 9.860514e-04 0.4142130 0.7994351
#> 3 ph.ecog 1.5899912 0.113577266 4.082934 4.447067e-05 1.2726752 1.9864236
glance(cox)
#> n nevent loglik aic lr_statistic df lr_p_value frailty_theta
#> 1 227 164 -729.2301 1464.46 30.50067 3 1.082818e-06 NA
#> frailty_lrt_statistic frailty_lrt_p_value
#> 1 NA NAplot_forest() plots the hazard ratios.
plot_forest(cox, term_labels = c(age = "Age", sex = "Female", ph.ecog = "ECOG score"))
The model assumes that each hazard ratio is constant over time.
cox_zph() tests the assumption, and
plot_schoenfeld() shows the scaled Schoenfeld residuals: a
flat smooth supports it.
cox_zph(cox)
#> <cox_zph> Surv(time, status) ~ age + sex + ph.ecog
#> Transform: km
#>
#> statistic df p_value
#> age 0.188 1 0.6646
#> sex 2.305 1 0.1289
#> ph.ecog 2.054 1 0.1518
#> GLOBAL 4.464 3 0.2156
plot_schoenfeld(cox)
smooth_hr() checks whether a continuous covariate’s
effect is linear, by refitting the model with a spline in its place.
plot_smooth_hr(smooth_hr(cox, term = "age"), scale = "hr")
plot_predicted_survival() shows the model’s survival
curves for new patients.
patients <- data.frame(age = c(60, 60), sex = c(1, 2), ph.ecog = c(1, 1))
plot_predicted_survival(cox, patients, labels = c("Male, 60", "Female, 60"))
Competing risks
When one kind of event prevents another, the Kaplan-Meier estimate of
each overstates its probability. In the mgus2 data,
patients can progress to a plasma cell malignancy or die first. A factor
response with the censoring level first gives a competing-risks
response, and aalen_johansen() estimates each cause’s
cumulative incidence.
mgus <- data.frame(
time = with(mgus2, ifelse(pstat == 1, ptime, futime)),
event = factor(
with(mgus2, ifelse(pstat == 1, 1, 2 * death)),
levels = 0:2,
labels = c("censor", "progression", "death")
),
sex = mgus2$sex
)
cif <- aalen_johansen(Surv(time, event) ~ sex, data = mgus)
grays_test(Surv(time, event) ~ sex, data = mgus)
#> <greenwood_test> Surv(time, event) ~ sex
#> Gray's test (cause = "progression")
#>
#> n observed expected
#> sex=F 631 59 53.20504
#> sex=M 753 56 61.79496
#>
#> Chi-squared = 1.195 on 1 degree of freedom, p = 0.274
plot_cif(cif)
fine_gray() models the covariates’ effects on one
cause’s cumulative incidence.
Checking predictions
concordance_index() measures how well a risk score ranks
the patients: 0.5 is no better than chance, and 1 orders every
comparable pair correctly. On the data the model was fit to, it’s
optimistic. cross_validate() measures it on folds that the
model didn’t see.
complete <- na.omit(lung[c("time", "status", "age", "sex", "ph.ecog")])
cox <- cox_ph(Surv(time, status) ~ age + sex + ph.ecog, data = complete)
concordance_index(Surv(complete$time, complete$status), risk = predict(cox))
#> [1] 0.6371355
set.seed(1)
cross_validate(cox_ph, Surv(time, status) ~ age + sex + ph.ecog, data = complete, k = 5)
#> <cross_validate> Surv(time, status) ~ age + sex + ph.ecog
#> 5-fold cross-validation of cox_ph()
#>
#> metric mean sd
#> concordance 0.6336982 0.07163972brier_score(), time_dependent_auc(), and
calibration() assess predicted survival probabilities at
chosen times.