weightedsurv provides tools for weighted and stratified survival analysis, including Cox proportional hazards models, weighted log-rank tests, Kaplan-Meier curves, and Restricted Mean Survival Time (RMST) calculations with flexible weighting schemes.
The package is particularly useful for clinical trial analyses where treatment effects may vary over time, such as in oncology trials with delayed treatment effects or crossing hazards.
Install the released version from CRAN:
install.packages("weightedsurv")Or install the development version from GitHub:
# install.packages("devtools")
devtools::install_github("larry-leon/weightedsurv")ps_weights(), with standard errors, bands and tests that
account for the estimation of the weights
(weight_variance = "estimated"), and the effective sample
size by arm (df_counting()$ess)The package supports multiple weighting schemes for weighted log-rank
tests (S is the pooled Kaplan-Meier curve, S(t-) its left-continuous
version; see wt.rg.S()):
| Scheme | Description | Use Case |
|---|---|---|
| Fleming-Harrington (fh) | w(t) = S(t-)^ρ × (1-S(t-))^γ | Emphasize early (ρ>0) or late (γ>0) differences |
| Magirr-Burman (MB) | w(t) = 1/max(S(t-), S(t*)) | Weight rises until the cutoff time t* and is constant after it |
| Schemper | w(t) = S(t-)/G(t-), G the censoring K-M curve | Average hazard ratio approximately free of the censoring distribution |
| Xu-O’Quigley (XO) | w(t) = S(t-)/Y(t), Y the number at risk | Adjust for risk set size |
| Custom time-based | Step function weights | Pre-specified time-based weighting |
library(weightedsurv)
library(survival)
# Prepare data (treatment must be coded 0/1)
data(veteran)
veteran$treat <- as.numeric(veteran$trt) - 1
# Comprehensive survival analysis
result <- df_counting(
df = veteran,
tte.name = "time",
event.name = "status",
treat.name = "treat",
arms = c("Treatment", "Control")
)
# View results
print(result$cox_results$cox_text)
print(result$zlogrank_text)
print(result$quantile_results)# Fleming-Harrington (0,1) weights - emphasize late differences
result_fh <- df_counting(
df = veteran,
tte.name = "time",
event.name = "status",
treat.name = "treat",
scheme = "fh",
scheme_params = list(rho = 0, gamma = 1)
)
# Magirr-Burman weights with 6-month cutoff
result_mb <- df_counting(
df = veteran,
tte.name = "time",
event.name = "status",
treat.name = "treat",
scheme = "MB",
scheme_params = list(mb_tstar = 6)
)# Stratified by cell type
result_strat <- df_counting(
df = veteran,
tte.name = "time",
event.name = "status",
treat.name = "treat",
strata.name = "celltype"
)
print(result_strat$z.score_stratified)# Plot KM curves with risk table
plot_weighted_km(result,
arms = c("Treatment", "Control"),
show.med = TRUE,
risk.set = TRUE)# Calculate KM difference with simultaneous confidence bands
km_diff <- KM_diff(
df = veteran,
tte.name = "time",
event.name = "status",
treat.name = "treat",
draws.band = 1000,
show_resamples = TRUE
)
# Plot difference curve
plot(km_diff$at_points, km_diff$dhat, type = "s",
xlab = "Time", ylab = "Survival Difference (Treatment - Control)")
lines(km_diff$at_points, km_diff$sb_lower, lty = 2)
lines(km_diff$at_points, km_diff$sb_upper, lty = 2)
abline(h = 0, col = "grey")# Weighted Cox model with resampling-based inference
cox_fit <- cox_rhogamma(
dfcount = result,
scheme = "fh",
scheme_params = list(rho = 0, gamma = 0.5),
draws = 1000,
verbose = TRUE
)
# Results with resampling-based CI
print(cox_fit$cox_text_star)
print(cox_fit$hr_ci_star)# Cumulative RMST with confidence bands
rmst_result <- cumulative_rmst_bands(
df = veteran,
fit = km_diff,
tte.name = "time",
event.name = "status",
treat.name = "treat",
draws_sb = 1000
)
print(rmst_result$rmst_text)ps_weights() fits a logistic propensity model and
returns the weights. Passed as weight_model with
weight_variance = "estimated", it carries the estimation of
the weights into standard errors, intervals, bands and tests (the
default, "known", treats the weights as fixed numbers).
df_counting()$ess gives the effective sample size by arm.
The lines below are from the main vignette, where
df_rotterdam (the restricted Rotterdam population) and the
column names are defined; see the article Estimated
propensity-score weights for the method.
pw <- ps_weights(treat ~ age + size + grade3 + nodes + pgr + er,
data = df_rotterdam, type = "stabilized")
pw
df_rotterdam$sw.weights <- pw$weights
dfcount_rotterdam_wtd <- get_dfcounting(df=df_rotterdam, tte.name=tte.name, event.name=event.name, treat.name=treat.name, arms=arms, by.risk=24,
weight.name="sw.weights", weight_variance = "estimated", weight_model = pw)
knitr::kable(dfcount_rotterdam_wtd$ess, digits = c(0, 0, 1, 3))# Create publication-ready baseline table
baseline_table <- create_baseline_table(
data = veteran,
treat_var = "treat",
vars_continuous = c("age", "karno"),
vars_categorical = "celltype",
vars_binary = "prior",
var_labels = c(
age = "Age (years)",
karno = "Karnofsky Score",
celltype = "Cell Type",
prior = "Prior Therapy"
)
)
print(baseline_table)df_counting() for comprehensive
survival analysisplot_weighted_km() for
Kaplan-Meier curvesKM_diff()
for treatment effect visualizationcox_rhogamma() for hazard ratio estimationcumulative_rmst_bands() for absolute treatment effectsFor detailed examples and methodological background, see the package vignette:
# View the main vignette
vignette("weightedsurv_examples", package = "weightedsurv")The vignette demonstrates:
survival package (weighted
Kaplan-Meier standard errors agree with survfit()) and a
comparison with the adjustedCurves package (the curves
agree; its iptw_km standard errors differ)You can also browse vignettes online at the CRAN package page.
Imports: - ggplot2 - graphics - grDevices - rlang - stats - survival - utils
Suggests: - adjustedCurves - data.table - DiagrammeR - dplyr - gt - knitr - pammtools - rmarkdown - testthat (>= 3.1.7)
The website articles need further packages, listed in the
Config/Needs/website field of DESCRIPTION.
Fleming, T. R. and Harrington, D. P. (1991). Counting Processes and Survival Analysis. Wiley.
Magirr, D. and Burman, C. F. (2019). Modestly weighted logrank tests. Statistics in Medicine, 38(20), 3782-3790.
Schemper, M., Wakounig, S., and Heinze, G. (2009). The estimation of average hazard ratios by weighted Cox regression. Statistics in Medicine, 28(19), 2473-2489.
Contributions are welcome! Please feel free to submit issues or pull requests.
This package is licensed under the MIT License. See the LICENSE file for details.