Skip to contents
knitr::opts_chunk$set(
  echo = TRUE, 
  message = FALSE, 
  warning = FALSE,
  fig.width = 10, 
  fig.height = 6
)
options(warn = -1)
library(survival)
library(ggplot2)
library(weightedsurv)
# formatting of the numbers quoted in the text
fmt <- function(x, digits = 2) formatC(x, format = "f", digits = digits)

Introduction

Martingale resampling (perturbation resampling) provides a powerful and flexible approach to inference in survival analysis. While asymptotic distributions are available for many common analysis methods, resampling-based inference

  • May have greater accuracy in small samples, and
  • Extends to complex processes where asymptotics are not readily available (Dobler et al. 2017).

This vignette demonstrates martingale resampling applications in the weightedsurv package using two breast cancer datasets:

  1. GBSG — a randomized clinical trial evaluating hormonal therapy (Schumacher et al. 1994).
  2. Rotterdam — an observational study where propensity-score weighting is used to adjust for confounding (Royston and Altman 2013).

The examples include:

  • Simultaneous confidence bands for survival (KM) arm differences
  • Cumulative RMST curves with pointwise and simultaneous bands across follow-up (Zhao et al. 2016)
  • Weighted analyses using propensity-score (IPTW) weights

Brief Review of Counting Processes

We observe (Xi,j,Δi,j,Zi,j)(X_{i,j}, \Delta_{i,j}, Z_{i,j}) where for subjects in group jj (Zi=jZ_i = j):

  • Xi=min⁡(Ti,Ci)X_i = \min(T_i, C_i) and Δi=I(Ti≤Ci)\Delta_i = I(T_i \leq C_i)
  • The at-risk process: Yi,j(t)=I(Xi≥t,Zi=j)Y_{i,j}(t) = I(X_i \geq t, Z_i = j)
  • The counting process: Ni,j(t)=I(Xi≤t,Δi=1,Zi=j)N_{i,j}(t) = I(X_i \leq t, \Delta_i = 1, Z_i = j)

The aggregate counting processes N‾j(t)=∑i=1njNi,j(t)\bar{N}_j(t) = \sum_{i=1}^{n_j} N_{i,j}(t) count the observed events through time tt for the control (j=0j=0) and experimental (j=1j=1) arms.

The Weighted Log-Rank Test

The weighted log-rank (WLR) statistic of class K+K^{+} is

WK=∫0∞{K(t)Y‾0(t)dN‾0(t)−K(t)Y‾1(t)dN‾1(t)}=∫0∞K(t)d{Λ̂0(t)−Λ̂1(t)},W_K = \int_0^{\infty} \left\{ \frac{K(t)}{\bar{Y}_0(t)} d\bar{N}_0(t) - \frac{K(t)}{\bar{Y}_1(t)} d\bar{N}_1(t) \right\} = \int_0^{\infty} K(t)\, d\left\{\hat\Lambda_0(t) - \hat\Lambda_1(t)\right\},

where K(t)=w(t)Y‾0(t)Y‾1(t)Y‾0(t)+Y‾1(t)K(t) = w(t)\, \frac{\bar{Y}_0(t)\, \bar{Y}_1(t)}{\bar{Y}_0(t) + \bar{Y}_1(t)}, as score_calculation() computes it (without a factor (n0+n1)/(n0n1)\sqrt{(n_0 + n_1)/(n_0 n_1)}; such a constant factor would multiply the statistic and its standard error alike, so the z-statistic is unchanged by it), with Fleming–Harrington weights w(t)=[F‾̂(t−)]ρ[1−F‾̂(t−)]γw(t) = [\hat{\bar{F}}(t^{-})]^{\rho}[1 - \hat{\bar{F}}(t^{-})]^{\gamma}, ρ≥0\rho \geq 0, γ≥0\gamma \geq 0.

Martingale Representation

Under the null, WKW_K can be written as a sum of martingale integrals:

WK=∑i=1n0∫0∞K(t)Y‾0(t)dNi,0(t)−∑i=1n1∫0∞K(t)Y‾1(t)dNi,1(t),W_K = \sum_{i=1}^{n_0} \int_0^{\infty} \frac{K(t)}{\bar{Y}_0(t)}\, dN_{i,0}(t) - \sum_{i=1}^{n_1} \int_0^{\infty} \frac{K(t)}{\bar{Y}_1(t)}\, dN_{i,1}(t),

where the Ni,j(t)N_{i,j}(t)’s can be replaced by the corresponding martingales Mi,j(t)=Ni,j(t)−∫0tYi,j(s)λj(s)dsM_{i,j}(t) = N_{i,j}(t) - \int_0^t Y_{i,j}(s)\lambda_j(s)\,ds under the null.

Resampling Recipe

The resampled statistic W†W^{\dagger} is constructed as:

W†=∑i=1n0∫0∞{K(t)Y‾0(t)dNi,0(t)}Gi,0−∑i=1n1∫0∞{K(t)Y‾1(t)dNi,1(t)}Gi,1,W^{\dagger} = \sum_{i=1}^{n_0} \int_0^{\infty} \left\{\frac{K(t)}{\bar{Y}_0(t)}\, dN_{i,0}(t)\right\} G_{i,0} - \sum_{i=1}^{n_1} \int_0^{\infty} \left\{\frac{K(t)}{\bar{Y}_1(t)}\, dN_{i,1}(t)\right\} G_{i,1},

where Gi,jG_{i,j} are nn i.i.d. N(0,1)N(0,1) random variables. The martingale increments dMi,jdM_{i,j} are represented by independent standard normal random variables (independent of the data) that are multiplied by the observable counting process increments dNi,jdN_{i,j}, with unknown parameters estimated by consistent estimators (Dobler et al. 2017).

GBSG Trial Data: Unweighted Analysis

Data Preparation

We use the GBSG randomized trial data from the survival package, converting follow-up from days to months.

df_gbsg <- gbsg
df_gbsg$tte <- df_gbsg$rfstime / 30.4375
df_gbsg$event <- df_gbsg$status
df_gbsg$treat <- df_gbsg$hormon
df_gbsg$grade3 <- ifelse(df_gbsg$grade == "3", 1, 0)

tte.name <- "tte"
event.name <- "event"
treat.name <- "treat"
arms <- c("treat", "control")

Counting Process Summary and Log-Rank Test

The df_counting() function prepares the survival data in counting-process format and computes the standard log-rank test (Fleming–Harrington ρ=0,γ=0\rho=0, \gamma=0), weighted log-rank statistics, and Cox model results.

dfcount_gbsg <- df_counting(
  df = df_gbsg, 
  tte.name = tte.name, 
  event.name = event.name, 
  treat.name = treat.name, 
  arms = arms, 
  by.risk = 12, 
  scheme = "fh", 
  scheme_params = list(rho = 0, gamma = 0), 
  lr.digits = 4, 
  cox.digits = 3
)

Kaplan-Meier Curves

plot_weighted_km(
  dfcount = dfcount_gbsg, 
  conf.int = TRUE, 
  show.logrank = TRUE, 
  ymax = 1.05, 
  xmed.fraction = 0.725
)
title(main = "GBSG (trial) data: unweighted")

Rotterdam Observational Data: Propensity-Score Weighted Analysis

Data Preparation and Propensity Score Estimation

The Rotterdam dataset is observational, so we use inverse probability of treatment weighting (IPTW) with stabilized propensity-score weights to adjust for confounding.

gbsg_validate <- within(rotterdam, {
  rfstime <- ifelse(recur == 1, rtime, dtime)
  t_months <- rfstime / 30.4375
  time_months <- t_months
  status <- pmax(recur, death)
  ignore <- (recur == 0 & death == 1 & rtime < dtime)
  status2 <- ifelse(recur == 1 | ignore, recur, death)
  rfstime2 <- ifelse(recur == 1 | ignore, rtime, dtime)
  time_months2 <- rfstime2 / 30.4375
  grade3 <- ifelse(grade == "3", 1, 0)
  treat <- hormon
  event <- status2
  tte <- time_months
  id <- as.numeric(1:nrow(rotterdam))
  SG0 <- ifelse(er <= 0, 0, 1)
})

# node-positive women, as in the GBSG trial
df_rotterdam_np <- subset(gbsg_validate, nodes > 0)

ps_weights() fits the logistic propensity score model and returns the weights; type = "stabilized" gives stabilized IPTW weights (the default is type = "ipw"). We first look at all node-positive women:

pw_np <- ps_weights(
  treat ~ age + meno + size + grade3 + nodes + pgr + chemo + er, 
  data = df_rotterdam_np, 
  type = "stabilized"
)
df_rotterdam_np$sw.weights <- pw_np$weights

ess_np <- df_counting(
  df = df_rotterdam_np, 
  tte.name = tte.name, 
  event.name = event.name, 
  treat.name = treat.name, 
  arms = arms, 
  weight.name = "sw.weights"
)$ess
knitr::kable(ess_np, digits = c(0, 0, 1, 3))
group n ess ratio
treat 339 105.2 0.310
control 1207 1162.9 0.964

df_counting()$ess gives, by arm, the number of subjects, the effective sample size (∑iwi)2/∑iwi2(\sum_i w_i)^2 / \sum_i w_i^2 and their ratio; a low ratio means that a few subjects carry most of the weight. Here the ratio of the treated arm is 0.31 and the largest weight is 12.9: hormonal therapy went mostly to postmenopausal women without chemotherapy (291 of the 339 treated women), and the few other treated women receive large weights. The weighted example is therefore restricted to postmenopausal women without chemotherapy, with the propensity model fitted on those rows:

df_rotterdam <- subset(df_rotterdam_np, meno == 1 & chemo == 0)

pw <- ps_weights(
  treat ~ age + size + grade3 + nodes + pgr + er, 
  data = df_rotterdam, 
  type = "stabilized"
)
pw
## Propensity-score weights (stabilized): n = 829, 8 coefficients in the logistic model
##        arm   n weight min weight max   ess ess / n
##  treat = 1 291     0.6809      2.709 270.7  0.9301
##  treat = 0 538     0.6621      1.329 529.3  0.9837
df_rotterdam$sw.weights <- pw$weights

The restricted population has 829 women (538 control, 291 treated). The object pw is passed below as weight_model together with weight_variance = "estimated", so that standard errors, intervals and bands account for the estimation of the weights (the default, "known", treats the weights as fixed numbers; no estimate differs between the two, and the weightedsurv_examples vignette shows them side by side).

Unweighted vs. Weighted KM Curves

dfcount_rotterdam_unwtd <- df_counting(
  df = df_rotterdam, 
  tte.name = tte.name, 
  event.name = event.name, 
  treat.name = treat.name, 
  arms = arms, 
  by.risk = 36, 
  risk.add = -50
)

dfcount_rotterdam_wtd <- df_counting(
  df = df_rotterdam, 
  tte.name = tte.name, 
  event.name = event.name, 
  treat.name = treat.name, 
  arms = arms, 
  by.risk = 36, 
  risk.add = -50, 
  weight.name = "sw.weights",
  weight_variance = "estimated", 
  weight_model = pw
)

par(mfrow = c(1, 2))
plot_weighted_km(
  dfcount = dfcount_rotterdam_unwtd, 
  xmed.fraction = 0.4, 
  arm.cex = 0.475, 
  risk.cex = 0.45, 
  risk_offset = 0.125
)
title(main = "Unweighted K-M curves")

plot_weighted_km(
  dfcount = dfcount_rotterdam_wtd, 
  xmed.fraction = 0.4, 
  arm.cex = 0.475, 
  risk.cex = 0.45, 
  risk_offset = 0.125
)
title(main = "Weighted K-M curves (stabilized PS)")

Effective sample size by arm in the restricted population:

knitr::kable(dfcount_rotterdam_wtd$ess, digits = c(0, 0, 1, 3))
group n ess ratio
treat 291 270.7 0.930
control 538 529.3 0.984

Both ratios are close to 1 (largest weight 2.7): the weights are spread evenly within each arm.

Comparison: GBSG Trial vs. Rotterdam PS-Weighted

par(mfrow = c(1, 2))
plot_weighted_km(
  dfcount = dfcount_gbsg, 
  conf.int = TRUE, 
  xmed.fraction = 0.4, 
  ymax = 1.05, 
  arm.cex = 0.475, 
  risk.cex = 0.45
)
title(main = "GBSG (trial) unweighted")

plot_weighted_km(
  dfcount = dfcount_rotterdam_wtd, 
  conf.int = TRUE, 
  xmed.fraction = 0.4, 
  arm.cex = 0.475, 
  risk.cex = 0.45
)
title(main = "Rotterdam PS-weighted")

Hazard ratios (FH(0,0), with asymptotic 95% intervals): in the restricted Rotterdam population the propensity-score weighted estimate is 0.66 (0.56, 0.78) and the unweighted estimate is 0.72 (0.60, 0.86); in the GBSG trial data the estimate is 0.69 (0.54, 0.89). The weighted Rotterdam estimate is below the unweighted one and below the GBSG estimate. The two datasets differ in population and follow-up, and weighting adjusts only for the covariates of the propensity model.

Simultaneous Confidence Bands for Survival Differences

Rotterdam Weighted Survival Difference Bands

The plotKM.band_subgroups() function computes KM survival difference estimates along with pointwise and simultaneous confidence bands using martingale resampling. The simultaneous band provides uniform coverage. With Δ̂(t)=Ŝ1(t)−Ŝ0(t)\hat{\Delta}(t) = \widehat{S}_1(t) - \widehat{S}_0(t), Δ(t)=S1(t)−S0(t)\Delta(t) = S_1(t) - S_0(t) and sê[Δ̂(t)]\widehat{\text{se}}[\hat{\Delta}(t)] the standard error of the difference, the critical value c1−αc_{1-\alpha} is the (1−α)(1-\alpha) quantile of the resampled supremum of the standardized process, so that

P(supt≤τ|Δ̂(t)−Δ(t)|sê[Δ̂(t)]≤c1−α)≈1−α,P\left(\sup_{t \leq \tau} \frac{\left|\hat{\Delta}(t) - \Delta(t)\right|}{\widehat{\text{se}}[\hat{\Delta}(t)]} \leq c_{1-\alpha}\right) \approx 1 - \alpha,

and the band is Δ̂(t)±c1−α⋅sê[Δ̂(t)]\hat{\Delta}(t) \pm c_{1-\alpha} \cdot \widehat{\text{se}}[\hat{\Delta}(t)], as in KM_diff(): wider where the standard error is larger.

par(mfrow = c(1, 1))
temp_rotterdam <- plotKM.band_subgroups(
  df = df_rotterdam,
  xlabel = "Months", 
  ylabel = "Survival difference",
  yseq_length = 5, 
  cex_Yaxis = 0.7, 
  risk_cex = 0.7,
  tau_add = 42, 
  by.risk = 24, 
  risk_delta = 0.075, 
  risk.pad = 0.03, 
  ymax.pad = 0.125,
  tte.name = tte.name, 
  treat.name = treat.name, 
  event.name = event.name, 
  weight.name = "sw.weights",
  draws.band = 1000, 
  show_resamples = TRUE,
  weight_variance = "estimated", 
  weight_model = pw
)

legend("topleft", 
  c("Difference estimate", "95% pointwise CIs"),
  lwd = c(2, 1), col = c("black"), lty = c(1, 2), bty = "n", cex = 0.7
)
title(main = "Rotterdam PS-weighted survival differences")

The first figure shows the first 20 resampled survival-difference curves, each divided by the standard error of the difference at that time (show_resamples = TRUE). They illustrate the perturbation-resampling distribution from which the critical value of the simultaneous band is computed: the critical value is the 95% quantile, over the draws.band resampled curves, of the largest absolute standardized value, and the band is the estimate plus and minus the critical value times the standard error. The supremum is taken over the whole time range of the call, up to the smaller of the two arms’ last event times (or tau_add, when that is later); no quantile range is trimmed. With subject weights that are not all equal the resampled curves are built from per-subject influence contributions (see the workflow summary below), here with the weights treated as estimated.

Cumulative RMST Curves Across Follow-Up

Combined Analysis: Trial and Observational Data

The restricted mean survival time (RMST) at time τ\tau is defined as μ(τ)=∫0τS(t)dt\mu(\tau) = \int_0^{\tau} S(t)\,dt, the area under the survival curve up to τ\tau. Rather than evaluating RMST at a single time point, we study the cumulative RMST curve τ↦μ̂1(τ)−μ̂0(τ)\tau \mapsto \hat\mu_1(\tau) - \hat\mu_0(\tau) across the entire follow-up period.

Simultaneous confidence bands for the RMST difference curve are constructed via martingale resampling, providing uniform coverage over the range of τ\tau values (Zhao et al. 2016).

The cumulative_rmst_bands() function takes the fitted object from plotKM.band_subgroups() and computes the cumulative RMST curves with both pointwise and simultaneous bands:

draws <- 1000
par(mfrow = c(2, 2))

# --- GBSG: KM difference bands ---
temp_gbsg <- plotKM.band_subgroups(
  df = df_gbsg, 
  Maxtau = NULL,
  xlabel = "Months", 
  ylabel = "Survival difference",
  yseq_length = 5, 
  cex_Yaxis = 0.7, 
  risk_cex = 0.7,
  by.risk = 6, 
  risk_delta = 0.075, 
  risk.pad = 0.03,
  tte.name = tte.name, 
  treat.name = treat.name, 
  event.name = event.name, 
  draws.band = draws, 
  show_resamples = FALSE
)
legend("topleft", 
  c("Difference estimate", "95% pointwise CIs"),
  lwd = c(2, 1), col = c("black"), lty = c(1, 2), bty = "n", cex = 0.75
)
title(main = "GBSG: KM survival differences")

# --- GBSG: Cumulative RMST bands ---
get_bands_gbsg <- cumulative_rmst_bands(
  df = df_gbsg, 
  fit = temp_gbsg$fit_itt, 
  tte.name = tte.name, 
  event.name = event.name, 
  treat.name = treat.name, 
  draws_sb = draws, 
  xlab = "months", 
  rmst_max_cex = 0.75
)
legend("topleft", 
  c("Cumulative RMST estimate", "95% pointwise CIs"),
  lwd = c(2, 1), col = c("black"), lty = c(1, 2), bty = "n", cex = 0.75
)

# --- Rotterdam weighted: KM difference bands ---
temp_rotterdam2 <- plotKM.band_subgroups(
  df = df_rotterdam,
  xlabel = "Months", 
  ylabel = "Survival difference",
  yseq_length = 5, 
  cex_Yaxis = 0.7, 
  risk_cex = 0.7,
  tau_add = 42, 
  by.risk = 12, 
  risk_delta = 0.075, 
  risk.pad = 0.03, 
  ymax.pad = 0.125,
  tte.name = tte.name, 
  treat.name = treat.name, 
  event.name = event.name, 
  weight.name = "sw.weights",
  draws.band = draws, 
  show_resamples = FALSE,
  weight_variance = "estimated", 
  weight_model = pw
)
legend("topleft", 
  c("Difference estimate", "95% pointwise CIs"),
  lwd = c(2, 1), col = c("black"), lty = c(1, 2), bty = "n", cex = 0.7
)
title(main = "Rotterdam PS-weighted: KM differences")

# --- Rotterdam weighted: Cumulative RMST bands ---
get_bands_rotterdam <- cumulative_rmst_bands(
  df = df_rotterdam, 
  fit = temp_rotterdam2$fit_itt, 
  tte.name = tte.name, 
  event.name = event.name, 
  treat.name = treat.name, 
  weight.name = "sw.weights", 
  draws_sb = draws, 
  xlab = "months", 
  rmst_max_cex = 0.7,
  weight_variance = "estimated", 
  weight_model = pw
)
legend("topleft", 
  c("Cumulative RMST estimate", "95% pointwise CIs"),
  lwd = c(2, 1), col = c("black"), lty = c(1, 2), bty = "n", cex = 0.7
)

Top row (GBSG trial): The KM survival difference (left) shows the instantaneous treatment gap at each time point, while the cumulative RMST difference (right) shows the accumulated treatment benefit in months gained. The RMST curve integrates the KM difference, providing a clinically interpretable time-scale summary.

Bottom row (Rotterdam weighted): The same pair of analyses using propensity-score weighted estimation from the observational Rotterdam data. The weighted cumulative RMST difference at the last time point of its grid is 15.6 months (pointwise 95% limits 9.7, 21.4) at 118.0 months; in the GBSG trial data (top row) it is 6.8 months (pointwise 95% limits 2.2, 11.4) at 77.9 months. The two values are taken at different time points and are not on a common horizon.

Workflow Summary

The typical workflow for martingale resampling analyses in weightedsurv follows two key steps.

Step 1: KM difference bands. Call plotKM.band_subgroups() to estimate survival differences and compute simultaneous confidence bands. This function returns a fitted object containing the KM estimates and resampling results.

Step 2: Cumulative RMST bands. Pass the fitted object from Step 1 to cumulative_rmst_bands(), which computes the cumulative RMST difference curve and its simultaneous bands using the same fitted survival estimates.

For weighted analyses (e.g., propensity-score adjustment), both functions accept a weight.name argument specifying the column of subject-specific weights in the data frame. When the weights are not all equal, a resampled curve of arm jj is no longer built from the event increments alone: it is −Ŝj(t)∑iGiwiψi(t)-\hat{S}_j(t) \sum_i G_i\, w_i\, \psi_i(t), where ψi(t)\psi_i(t) is subject ii’s influence contribution to the weighted Kaplan-Meier curve, computed from the weighted at-risk process Y‾jw(t)=∑iwiYi,j(t)\bar{Y}_j^w(t) = \sum_i w_i Y_{i,j}(t) and the weighted event counts (the formula is in the help page of KM_diff()), and GiG_i are the standard normal multipliers. The variance of these curves across draws is the robust variance of the weighted Kaplan-Meier curve. Without weights, or with all weights equal, the event increments are perturbed as described above.

By default the weights are treated as fixed, known numbers. When they come from a logistic propensity model fitted on the rows analysed, ps_weights() returns the weights together with what is needed to carry their estimation into the inference; passing that object as weight_model with weight_variance = "estimated" to both functions adds each subject’s contribution through the propensity model to the resampled curves, and so to the pointwise limits and the bands. No estimate changes.

# Optional, for a propensity-score weighted analysis: weights from a logistic model
pw <- ps_weights(treat ~ x1 + x2, data = mydata, type = "stabilized")
mydata$sw.weights <- pw$weights

# Step 1: KM survival difference with simultaneous bands
fit <- plotKM.band_subgroups(
  df = mydata,
  tte.name = "tte", treat.name = "treat", event.name = "event",
  weight.name = "sw.weights",   # optional: for weighted analysis
  weight_variance = "estimated", weight_model = pw,   # optional: weights treated as estimated
  draws.band = 1000
)

# Step 2: Cumulative RMST bands (uses fit from Step 1)
rmst_result <- cumulative_rmst_bands(
  df = mydata, 
  fit = fit$fit_itt,
  tte.name = "tte", event.name = "event", treat.name = "treat",
  weight.name = "sw.weights",   # must match Step 1
  weight_variance = "estimated", weight_model = pw,   # must match Step 1
  draws_sb = 1000
)

Summary

This vignette illustrated how martingale resampling provides flexible, nonparametric inference for survival analysis. Key advantages of this approach include:

  • Simultaneous coverage: Confidence bands maintain nominal coverage uniformly across all time points, unlike pointwise intervals.
  • Model-free: No proportional hazards or other parametric assumptions are required.
  • Weighted extension: The same multipliers apply to propensity-score weighted analyses of observational data, with the resampled curves built from per-subject influence contributions; the estimation of the weights can be carried into the bands (weight_variance = "estimated").
  • Complementary perspectives: KM survival differences capture the instantaneous treatment gap, while cumulative RMST differences summarize the cumulative treatment benefit in clinically interpretable units (months gained).

The weightedsurv package implements these methods via plotKM.band_subgroups() for KM difference bands and cumulative_rmst_bands() for RMST curves, with a shared workflow that ensures consistency between the two analyses.

References

Dobler, D., J. Beyersmann, and M. Pauly. 2017. “Non-Strange Weird Resampling for Complex Survival Data.” Biometrika 104 (3): 699–711.
Royston, Patrick, and Douglas G. Altman. 2013. “External Validation of a Cox Prognostic Model: Principles and Methods.” BMC Medical Research Methodology 13: 33. https://doi.org/10.1186/1471-2288-13-33.
Schumacher, M, G Bastert, H Bojar, et al. 1994. “Randomized 2 x 2 Trial Evaluating Hormonal Treatment and the Duration of Chemotherapy in Node-Positive Breast Cancer Patients. German Breast Cancer Study Group.” Journal of Clinical Oncology 12 (10): 2086–93. https://doi.org/10.1200/JCO.1994.12.10.2086.
Zhao, Lihui, Brian Claggett, Lu Tian, et al. 2016. “On the Restricted Mean Survival Time Curve in Survival Analysis.” Biometrics 72 (1): 215–21.