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")The package supports multiple weighting schemes for weighted log-rank tests:
| 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*)) | Modest downweighting after cutoff time |
| Schemper | w(t) = S(t)/G(t) | Adjust for informative censoring |
| Xu-O’Quigley (XO) | w(t) = S(t)/Y(t) | 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
)
# 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)# 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 and
adjustedCurves packagesYou can also browse vignettes online at the CRAN package page.
Imports: - survival - stats - graphics
Suggests: - gt (for formatted tables - ggplot2 (for weight scheme visualization)
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.