View source: R/km_wlr_calculations_helpers.R
| KM_diff | R Documentation |
Calculates the difference in Kaplan-Meier curves between two groups, with confidence intervals and optional resampling-based simultaneous confidence bands.
KM_diff(
df,
tte.name = "tte",
event.name = "event",
treat.name = "treat",
weight.name = NULL,
at_points = sort(df[[tte.name]]),
alpha = 0.05,
seedstart = 8316951,
draws = 0,
risk.points = NULL,
draws.band = 0,
tau.seq = 0.25,
qtau = 0.025,
show_resamples = TRUE,
modify_tau = FALSE
)
df |
Data frame containing survival data. |
tte.name |
Character; name of time-to-event variable in |
event.name |
Character; name of event indicator variable in |
treat.name |
Character; name of treatment group variable in |
weight.name |
Character or NULL; name of weights variable in |
at_points |
Numeric vector; time points for calculation. Default: sorted unique event times. |
alpha |
Numeric; significance level for confidence intervals. Default: 0.05. |
seedstart |
Integer; random seed for reproducibility. Default: 8316951. |
draws |
Integer; number of draws for pointwise variance estimation. Default: 0. |
risk.points |
Numeric vector; time points for risk table display. |
draws.band |
Integer; number of draws for simultaneous confidence bands. Default: 0. |
tau.seq |
Numeric; step size for tau sequence when |
qtau |
Numeric; quantile for tau range restriction. Default: 0.025. |
show_resamples |
Logical; whether to plot resampled curves. Default: TRUE. |
modify_tau |
Logical; whether to restrict time range for simultaneous bands. Default: FALSE. |
This function computes the difference in Kaplan-Meier survival curves, delta(t) = S_1(t) - S_0(t),
along with variance estimates and confidence intervals.
When draws.band > 0, simultaneous confidence bands are constructed using
the supremum distribution of the standardized difference process. These bands
maintain the specified coverage probability across all time points simultaneously.
The variance is estimated using Greenwood's formula for unweighted data, or
resampling-based methods when draws > 0.
A list containing:
Time points used in calculations
Survival estimates for control and treatment groups
Variance estimates for survival curves
Survival difference (S1 - S0) at each time point
Variance of survival difference
Pointwise confidence limits (1 - alpha/2)
Simultaneous band limits (if draws.band > 0)
Critical value for simultaneous band (if draws.band > 0)
Matrix of resampled differences (if draws.band > 0)
Standardized resampled differences (if draws.band > 0)
Pointwise CIs (lower, upper): Cover the true difference at each time point with probability 1-alpha
Simultaneous bands (sb_lower, sb_upper): Cover the entire difference curve with probability 1-alpha
Treatment must be coded as 0=control, 1=experimental. Event must be binary (0/1).
df_counting for full survival analysis
plotKM.band_subgroups for visualization
cumulative_rmst_bands for RMST analysis
Other survival_analysis:
cox_rhogamma(),
df_counting(),
wt.rg.S()
library(survival)
str(veteran)
veteran$treat <- as.numeric(veteran$trt) - 1
# Basic KM difference
result <- KM_diff(
df = veteran,
tte.name = "time",
event.name = "status",
treat.name = "treat"
)
# Plot the difference
plot(result$at_points, result$dhat, type = "s",
xlab = "Time", ylab = "Survival Difference")
# With simultaneous confidence bands
result_band <- KM_diff(
df = veteran,
tte.name = "time",
event.name = "status",
treat.name = "treat",
draws.band = 1000,
modify_tau = TRUE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.