Skip to contents

This vignette analyses two public data sets from the survival package. The first, veteran, is a randomised trial of two chemotherapies in advanced lung cancer (137 patients). The second, mgus2, follows 1384 people with monoclonal gammopathy for two competing events: progression to a plasma-cell malignancy (PCM) and death without it. Its comparison by sex is descriptive, not causal, because sex is not assigned.

library(survestimand)
v <- survival::veteran
v$trt <- factor(v$trt, levels = 1:2, labels = c("standard", "test"))

The randomised comparison

Log-rank test and proportional hazards

sest_logrank(survival::Surv(time, status) ~ trt, v)
#> <sest_logrank>
#>     group  n observed expected o_minus_e
#>  standard 69       64     64.5   -0.5002
#>      test 68       64     63.5    0.5002
#> 
#>   chi-squared = 0.008227 on 1 df, p = 0.9277

A hazard ratio from a Cox model is a single number only if the hazards stay proportional. sest_ph_check() tests that for each term and shows the hazard ratio in separate time windows.

cx <- survestimand(survival::Surv(time, status) ~ trt + karno + celltype, v,
                   method = "cox")
ph <- sest_ph_check(cx, cuts = 90)
ph$test
#>              term      chisq df      p_value
#> trt           trt  0.2063074  1 0.6496771412
#> karno       karno 12.8133256  1 0.0003441593
#> celltype celltype 14.5317104  3 0.0022638903
#> GLOBAL     GLOBAL 22.8596853  5 0.0003590645
ph$hr_by_window[ph$hr_by_window$term %in% c("trttest", "karno"), ]
#>   window start end events    term       log_hr          se        hr  conf_low
#> 1      1     0  90     73 trttest  0.369932922 0.254685575 1.4476375 0.8787616
#> 2      1     0  90     73   karno -0.044447401 0.006214177 0.9565259 0.9449465
#> 6      2    90 Inf     55 trttest -0.331809828 0.359719838 0.7176238 0.3545706
#> 7      2    90 Inf     55   karno  0.004944119 0.011966053 1.0049564 0.9816613
#>   conf_high
#> 1 2.3847814
#> 2 0.9682472
#> 6 1.4524156
#> 7 1.0288042

The treatment term passes the test, but karno (the Karnofsky performance score) and celltype fail it: the effect of karno is strong in the first 90 days and close to nothing afterwards. A Cox model with these covariates is therefore misspecified. The differences below do not depend on proportional hazards.

Survival and restricted mean survival by arm

The restricted mean survival time (RMST) to a horizon is the average number of days alive within that horizon. With randomisation, the difference between arms in survival at a time, or in RMST, is the effect of assigning the test treatment.

The Kaplan-Meier contrast uses no covariates.

km <- survestimand(survival::Surv(time, status) ~ trt, v, method = "km")
sest_contrast(km, v, arm = "trt", times = 180, tau = 365)$contrasts
#>   estimand time  arm reference    estimate          se    conf_low  conf_high
#> 1 survival  180 test  standard  0.02042615  0.07376006  -0.1241409  0.1649932
#> 2     rmst  365 test  standard -6.56740839 19.76838186 -45.3127249 32.1779081

A model-based contrast predicts every patient’s curve under each arm and averages them (standardisation). Its interval comes from a seeded bootstrap.

sest_contrast(cx, v, arm = "trt", times = 180, tau = 365,
              n_boot = 50L, seed = 1L)$contrasts
#>   estimand time  arm reference     estimate          se    conf_low  conf_high
#> 1 survival  180 test  standard  -0.06049355  0.03702867  -0.1255059 0.01603817
#> 2     rmst  365 test  standard -20.44849329 12.60920703 -41.4631336 5.21548479

The pseudo-observation estimator regresses each patient’s pseudo-value for the RMST on arm and covariates. It needs neither proportional hazards nor a correct covariate model; it assumes that censoring does not depend on the covariates.

po <- sest_pseudo(survival::Surv(time, status) ~ trt + karno + celltype,
                  v, arm = "trt", type = "rmst", time = 365)
po$contrasts
#>   estimand time  arm reference  estimate       se  conf_low conf_high
#> 1     rmst  365 test  standard -4.117423 16.01744 -35.51102  27.27617

Which parametric model fits best?

sest_ic() compares the parametric fits by AIC.

f <- survival::Surv(time, status) ~ trt + karno
sest_ic(weibull = survestimand(f, v, method = "weibull"),
        lognormal = survestimand(f, v, method = "lognormal"),
        loglogistic = survestimand(f, v, method = "loglogistic"))
#>           fit      method likelihood   n n_events    loglik k      aic      bic
#> 1     weibull     weibull       full 137      128 -725.7921 4 1459.584 1471.264
#> 2   lognormal   lognormal       full 137      128 -721.3610 4 1450.722 1462.402
#> 3 loglogistic loglogistic       full 137      128 -720.1613 4 1448.323 1460.003
#>   delta_aic
#> 1 11.261646
#> 2  2.399379
#> 3  0.000000

A piecewise-exponential model

Cutting follow-up into intervals turns survival into a Poisson model with one rate per interval. Draws of its coefficients (here from the large-sample normal approximation; a Bayesian fit would supply posterior draws) give draws of each arm’s standardised RMST.

cuts <- c(30, 90, 180)
sp <- sest_split(survival::Surv(time, status) ~ trt + karno, v, cuts = cuts)
g <- stats::glm(event ~ 0 + interval + trt + karno + offset(log_exposure),
                family = stats::poisson(), data = sp)
set.seed(2L)
dr <- matrix(stats::rnorm(500 * length(stats::coef(g))), 500) %*%
  chol(stats::vcov(g))
dr <- sweep(dr, 2L, stats::coef(g), `+`)
colnames(dr) <- names(stats::coef(g))
sest_from_hazard_draws(dr, cuts, ~ trt + karno, v, arm = "trt",
                       times = 180, tau = 365)$contrasts
#>   estimand time  arm reference     estimate          se    conf_low   conf_high
#> 1 survival  180 test  standard  -0.03824797  0.04638268  -0.1296042  0.04530291
#> 2     rmst  365 test  standard -12.89104444 15.56072326 -43.0076885 15.09211691

Competing risks

mg <- survival::mgus2
mg$etime <- with(mg, ifelse(pstat == 1, ptime, futime))
mg$event <- factor(ifelse(mg$pstat == 1, "pcm",
                          ifelse(mg$death == 1, "death", "censor")),
                   levels = c("censor", "pcm", "death"))
mg <- mg[stats::complete.cases(mg[, c("etime", "event", "sex", "age")]), ]

The cumulative incidence of PCM at 10 years (120 months) and the time lost to PCM within 20 years, by sex, from the Aalen-Johansen estimator:

sest_cr_contrast(survival::Surv(etime, event) ~ sex, mg, arm = "sex",
                 cause = "pcm", times = 120, tau = 240)$contrasts
#>    estimand time arm reference    estimate         se    conf_low   conf_high
#> 1       cif  120   M         F -0.01857542 0.01380988 -0.04564229 0.008491441
#> 2 time_lost  240   M         F -3.10308068 2.69534502 -8.38585985 2.179698492

Standardised over age with cause-specific Cox models, and by pseudo-values:

sest_cr_contrast(survival::Surv(etime, event) ~ sex + age, mg, arm = "sex",
                 cause = "pcm", times = 120, tau = 240,
                 method = "cause_specific_cox", n_boot = 30L)$contrasts
#>    estimand time arm reference    estimate         se    conf_low  conf_high
#> 1       cif  120   M         F -0.01066983 0.01185566 -0.02766151 0.01007063
#> 2 time_lost  240   M         F -2.70416245 2.64835406 -6.45748496 1.86202395
sest_pseudo(survival::Surv(etime, event) ~ sex + age, mg, arm = "sex",
            type = "time_lost", time = 240, cause = "pcm")$contrasts
#>    estimand time arm reference  estimate      se  conf_low conf_high
#> 1 time_lost  240   M         F -3.256616 2.69951 -8.547558  2.034325

Checking an analysis against planted truths

sest_simulate() replaces the outcomes of a data set with recurrence and death drawn from rates you choose (here per year), keeping its arms and covariates. It returns the true value of each estimand, computed two ways that must agree. Here the test arm lowers the recurrence rate by a hazard ratio of 0.6 for the first year only.

sim <- sest_simulate(v, "trt", cuts = 1,
  rates = list(recurrence = c(0.6, 0.3), death = c(0.1, 0.2),
               death_after = c(1, 0.8)),
  arm_effects = list(recurrence = c(test = log(0.6))),
  covariate_effects = list(death = c(karno = -0.02)),
  fade_time = 1, censor_time = 3, times = 2, tau = 2, seed = 3L, n_mc = 1e5)
sim$truth[!is.na(sim$truth$reference), ]
#>         estimand time  arm reference       exact          mc        mc_se
#> 9    os_survival    2 test  standard  0.06463745  0.06456000 0.0009301230
#> 10       os_rmst    2 test  standard  0.08274000  0.08440161 0.0006764282
#> 11       rec_cif    2 test  standard -0.11107069 -0.11094000 0.0009931430
#> 12 rec_time_lost    2 test  standard -0.21067262 -0.21258656 0.0009313406
#>              z
#> 9  -0.08326322
#> 10  2.45644618
#> 11  0.13159668
#> 12 -2.05503265

The planted data can then be analysed like the real data, and each estimate compared with its truth.

sest_cr_contrast(survival::Surv(efs_time, efs_event) ~ trt, sim$data,
                 arm = "trt", cause = "recurrence", times = 2,
                 tau = 2)$contrasts
#>    estimand time  arm reference   estimate         se   conf_low   conf_high
#> 1       cif    2 test  standard -0.1815857 0.08337464 -0.3449970 -0.01817439
#> 2 time_lost    2 test  standard -0.2702403 0.13064289 -0.5262957 -0.01418498

Results as orchestra manifests

When the orchestraManifest package is installed, every result can be written as a manifest with a hash of its numbers. A contrast is a treatment_effects manifest; a refusal becomes a manifest that records why no number was given.

m <- orchestraManifest::as_orchestra_manifest(po)
m@inferential_target
#> [1] "treatment_effects"
m@params
#>   estimand time  arm reference  estimate       se  conf_low conf_high
#> 1     rmst  365 test  standard -4.117423 16.01744 -35.51102  27.27617
orchestraManifest::verify_manifest(m)$ok
#> [1] TRUE
r <- tryCatch(sest_contrast(km, v, arm = "trt", tau = 2000),
              error = function(e) e)
orchestraManifest::as_orchestra_manifest(r)@summary$abstain_reason
#> [1] "survestimand:horizon_beyond_data: the horizon 2000 is past the last observed time (553) in at least one arm; choose `times` and `tau` no later than 553."