survestimand() fits a survival model chosen by name,
checks the number of events per parameter, and returns a fit object that
print(), summary(), coef(),
sest_tidy() and sest_curve() read the same way
for every method.
This vignette covers fitting, prediction and the summaries of a single fit. The second vignette, Arm contrasts, competing risks and manifests, covers the comparison of two arms.
What can it fit?
The method registry is the catalogue. Each row is an estimator;
available reports whether its backend is installed.
sest_methods()
#> method kind backend available
#> 1 andersen_gill recurrent survival TRUE
#> 2 cause_specific competing survival TRUE
#> 3 cox cox survival TRUE
#> 4 cox_ridge cox survival TRUE
#> 5 cure cure flexsurvcure TRUE
#> 6 exponential parametric flexsurv TRUE
#> 7 fine_gray competing survival TRUE
#> 8 frailty frailty coxme TRUE
#> 9 gamma parametric flexsurv TRUE
#> 10 generalized_gamma parametric flexsurv TRUE
#> 11 gompertz parametric flexsurv TRUE
#> 12 interval_censored interval icenReg TRUE
#> 13 km nonparametric survival TRUE
#> 14 loglogistic parametric flexsurv TRUE
#> 15 lognormal parametric flexsurv TRUE
#> 16 spline spline flexsurv TRUE
#> 17 weibull parametric flexsurv TRUE
#> note
#> 1 recurrent events, Andersen-Gill
#> 2 competing risks, cause-specific hazard
#> 3 Cox proportional hazards
#> 4 ridge-penalised Cox (shrinkage for the few-events regime)
#> 5 mixture cure model (optional backend)
#> 6 parametric PH, flexsurvreg(dist = 'exp')
#> 7 competing risks, Fine-Gray subdistribution
#> 8 mixed-effects Cox / shared frailty
#> 9 parametric AFT, flexsurvreg(dist = 'gamma')
#> 10 parametric AFT, flexsurvreg(dist = 'gengamma')
#> 11 parametric PH, flexsurvreg(dist = 'gompertz')
#> 12 interval-censored regression (optional backend)
#> 13 Kaplan-Meier / Nelson-Aalen non-parametric baseline
#> 14 parametric AFT, flexsurvreg(dist = 'llogis')
#> 15 parametric AFT, flexsurvreg(dist = 'lnorm')
#> 16 Royston-Parmar flexible spline
#> 17 parametric AFT, flexsurvreg(dist = 'weibull')A first fit
A Weibull accelerated-failure-time model on the lung-cancer data (the parametric methods need the flexsurv package):
fit <- survestimand(survival::Surv(time, status) ~ age + sex, survival::lung,
method = "weibull")
fit
#> <sest_fit> method = weibull (parametric, via flexsurv)
#> n = 228 events = 165
#> power: ok (82.5 events/predictor)
#> coefficients:
#> shape scale age sex
#> 0.2823 6.2749 -0.0123 0.3821sest_tidy() gives the coefficient table with ratios and
confidence intervals; sest_curve() gives predicted survival
on a time grid.
sest_tidy(fit)
#> term component estimate std_error ratio_type ratio conf_low
#> 1 log(shape) auxiliary 0.28229534 0.061883272 <NA> NA NA
#> 2 log(scale) auxiliary 6.27485306 0.481366950 <NA> NA NA
#> 3 age covariate -0.01225703 0.006957472 TR 0.9878178 0.9744389
#> 4 sex covariate 0.38208514 0.127476840 TR 1.4653368 1.1413767
#> conf_high
#> 1 NA
#> 2 NA
#> 3 1.001380
#> 4 1.881248
sest_curve(fit, times = c(100, 300, 500))
#> time value profile
#> 1 100 0.8618418 1
#> 2 300 0.5282034 1
#> 3 500 0.2846043 1Switching method, same interface
The Cox proportional-hazards fit reads the same and tidies the same:
cox <- survestimand(survival::Surv(time, status) ~ age + sex, survival::lung,
method = "cox")
sest_tidy(cox)
#> term component estimate std_error ratio_type ratio conf_low
#> 1 age covariate 0.01704533 0.009223273 HR 1.017191 0.9989686
#> 2 sex covariate -0.51321852 0.167457962 HR 0.598566 0.4310936
#> conf_high
#> 1 1.0357467
#> 2 0.8310985The small-sample gate
The information in survival data comes from the events, not the rows. The events per variable, is checked against a floor of 10, a heuristic from simulations of the Cox model (Peduzzi et al. 1995) in which fewer events per parameter gave biased coefficients and inaccurate standard errors. On a small subset the gate flags the fit as under-powered:
small <- survival::lung[1:25, ]
sest_power(survival::Surv(time, status) ~ age + sex + ph.ecog, small)
#> $n
#> [1] 24
#>
#> $n_events
#> [1] 22
#>
#> $n_predictors
#> [1] 3
#>
#> $events_per_variable
#> [1] 7.333333
#>
#> $min_epv
#> [1] 10
#>
#> $underpowered
#> [1] TRUE
#>
#> $verdict
#> [1] "underpowered"
#>
#> $advice
#> [1] "only 7.3 events per predictor (< 10): the coefficients and their standard errors are unreliable. Use fewer predictors, a penalised fit (method = \"cox_ridge\"), or on_underpowered = \"abstain\"."survestimand() runs this check on every fit and warns
when the count is below the floor;
on_underpowered = "abstain" returns a refusal instead of a
fit.
Visualising a fit
The Kaplan-Meier curves show what a single hazard ratio or time ratio
would summarise. Here the lung-cancer patients are split at the median
age, and sest_curve() returns one curve per group, labelled
in its group column:
lung2 <- survival::lung
lung2$age_group <- factor(ifelse(lung2$age >= stats::median(lung2$age),
"older", "younger"),
levels = c("younger", "older"))
km_fit <- survestimand(survival::Surv(time, status) ~ age_group, lung2,
method = "km")
curves <- sest_curve(km_fit, times = seq(0, 900, by = 5))
cols <- c("#1b9e77", "#d95f02")
plot(NULL, xlim = c(0, 900), ylim = c(0, 1), xlab = "Days",
ylab = "Survival probability",
main = "Kaplan-Meier survival by age group")
for (j in 1:2) {
cj <- curves[curves$profile == j, ]
lines(cj$time, cj$value, type = "s", col = cols[j], lwd = 2)
}
legend("bottomleft", legend = unique(curves$group), col = cols, lwd = 2,
bty = "n")
Kaplan-Meier survival by age group in the lung-cancer data. The older group does worse in the first 150 days; from about 200 days the two curves run close together.
The older group’s survival falls faster in the first 150 days, after which the curves come together and cross. The log-rank test does not separate the groups:
sest_logrank(survival::Surv(time, status) ~ age_group, lung2)
#> <sest_logrank>
#> group n observed expected o_minus_e
#> younger 106 73 78.49 -5.49
#> older 122 92 86.51 5.49
#>
#> chi-squared = 0.7361 on 1 df, p = 0.3909A single hazard ratio for age group would average an early difference with a later absence of one.
Competing risks
With a multi-state response (the event is a factor whose first level is censoring), the Fine-Gray subdistribution hazard is one call:
mg <- survival::mgus2
mg$etime <- with(mg, ifelse(pstat == 1, ptime, futime))
mg$event <- with(mg, factor(ifelse(pstat == 1, "pcm",
ifelse(death == 1, "death", "censor")),
levels = c("censor", "pcm", "death")))
fg <- survestimand(survival::Surv(etime, event) ~ age + sex, mg,
method = "fine_gray", cause = "pcm")
sest_tidy(fg)
#> term component estimate std_error ratio_type ratio conf_low
#> 1 age covariate -0.01730171 0.005528135 HR 0.9828471 0.9722555
#> 2 sexM covariate -0.25975679 0.181707036 HR 0.7712391 0.5401567
#> conf_high
#> 1 0.9935541
#> 2 1.1011801Cumulative incidence
For competing risks the Aalen-Johansen cumulative incidence of each event is one call, and it accounts for the competing event rather than treating it as censoring:
ci <- sest_cuminc(survival::Surv(etime, event) ~ 1, mg)
head(ci)
#> time group cause cif cif_se
#> 1 1 all pcm 0.000000000 0.000000000
#> 2 2 all pcm 0.001446164 0.001021853
#> 3 3 all pcm 0.001446164 0.001021853
#> 4 4 all pcm 0.002169246 0.001251056
#> 5 5 all pcm 0.002892329 0.001444071
#> 6 6 all pcm 0.003615411 0.001613935Stacked, the two curves show how much of the probability of either event each cause accounts for at any time:
pcm <- ci[ci$cause == "pcm", c("time", "cif")]
death <- ci[ci$cause == "death", c("time", "cif")]
grid_t <- sort(unique(c(pcm$time, death$time)))
pcm_y <- stats::approx(pcm$time, pcm$cif, xout = grid_t, method = "constant",
rule = 2)$y
death_y <- stats::approx(death$time, death$cif, xout = grid_t,
method = "constant", rule = 2)$y
plot(grid_t, pcm_y + death_y, type = "s", col = "#7570b3", lwd = 2,
xlab = "Months", ylab = "Cumulative incidence",
main = "Stacked cumulative incidence: pcm and death",
ylim = c(0, max(pcm_y + death_y)))
lines(grid_t, pcm_y, type = "s", col = "#1b9e77", lwd = 2)
legend("topleft", legend = c("pcm", "pcm + death"),
col = c("#1b9e77", "#7570b3"), lwd = 2, bty = "n")
Aalen-Johansen cumulative incidence of plasma-cell malignancy (pcm) and death, stacked so the top line is the probability of either event. Most events are deaths; the cumulative incidence of pcm stays below 0.2.
Restricted mean survival time
The restricted mean survival time (RMST) to a horizon is the area under the survival curve on , a summary that stays defined when the median is never reached:
km <- survestimand(survival::Surv(time, status) ~ 1, survival::lung,
method = "km")
sest_rmst(km, tau = 365)
#> tau profile group rmst se conf_low conf_high
#> 1 365 1 <NA> 263.2219 7.798859 247.9364 278.5073Predictions can be scored for discrimination and calibration. The concordance index, the censoring-weighted (IPCW) Brier score, and its index of prediction accuracy are returned together. The Brier score at horizon weights each subject by the inverse probability of not being censored by , where is the predicted survival probability and is the Kaplan-Meier estimator of the censoring distribution (Graf et al. 1999); the index of prediction accuracy, , rescales against a covariate-free reference :
sest_score(cox, survival::lung, times = c(200, 400, 600))
#> time cindex brier ipa
#> 1 200 0.602853 0.2094573 0.03698539
#> 2 400 0.602853 0.2319303 0.01233122
#> 3 600 0.602853 0.1647980 0.01887124Adding a method
sest_register() adds a method under a new name:
sest_register("exp_survreg", kind = "parametric", backend = "survival",
fit = function(formula, data, ...) {
raw <- survival::survreg(formula, data = data, dist = "exponential", ...)
list(raw = raw, coef = coef(raw), vcov = vcov(raw),
loglik = as.numeric(logLik(raw)), n = nrow(data),
n_events = sum(data$status == 2), kind = "parametric",
backend = "survival", ratio_type = "TR", aux_pars = "(Intercept)")
})
"exp_survreg" %in% sest_methods()$method
#> [1] TRUEReferences
Peduzzi P, Concato J, Feinstein AR, Holford TR. Importance of events per independent variable in proportional hazards regression analysis II. Accuracy and precision of regression estimates. Journal of Clinical Epidemiology 1995; 48(12): 1503-1510. https://doi.org/10.1016/0895-4356(95)00048-8
