| Title: | While-Alive Regression for Composite Endpoints with CRT-Robust Inference |
|---|---|
| Description: | Implements while-alive regression for composite endpoints with recurrent and terminal events. Supports flexible time bases (B-spline, natural spline, M-spline, piecewise polynomial, interval-local, etc.), cluster-robust inference for cluster randomized trials, prediction, plotting, and K-fold cross-validation. |
| Authors: | Xi Fang [aut, cre], Hajime Uno [aut], Fan Li [aut] |
| Maintainer: | Xi Fang <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.0 |
| Built: | 2026-05-12 09:12:37 UTC |
| Source: | https://github.com/fancy575/wareg |
A simulated dataset of clustered recurrent events with terminal/censoring outcomes and covariates, suitable for examples and tests.
data(crt_dt)data(crt_dt)
A data frame with the following columns:
Integer subject ID (within the whole sample).
Integer cluster ID.
Numeric event/censoring time.
Integer event type indicator:
0 = censored,
1 = recurrent type 1,
2 = recurrent type 2,
3 = death (terminal).
Cluster-level treatment indicator carried to subjects (e.g., 0/1).
Numeric covariate.
Numeric covariate.
Rows represent observed events (including censoring and death) for each subject.
Multiple rows per id indicate multiple recurrent events; terminal/censoring
rows mark the end of observation for that subject.
Generated by the package's simulation utilities.
data(crt_dt) head(crt_dt)data(crt_dt) head(crt_dt)
A simulated dataset of recurrent events with terminal/censoring outcomes and covariates, organized in long format.
data(irt_dt)data(irt_dt)
A data frame with the following columns:
Integer subject ID (within the whole sample).
Integer cluster ID.
Numeric event/censoring time.
Integer event type indicator:
0 = censored,
1 = recurrent type 1,
2 = recurrent type 2,
3 = death (terminal).
Cluster-level treatment indicator carried to subjects (e.g., 0/1).
Numeric covariate.
Numeric covariate.
Long-format events: each row is an event (or censoring/death) for a subject.
Generated by the package's simulation utilities.
data(irt_dt) head(irt_dt)data(irt_dt) head(irt_dt)
Plot while-alive trajectory or a covariate's time-varying effect
## S3 method for class 'WA' plot( x, newdata, t_seq, id = 1, mode = c("wa", "cov"), covariate = NULL, ylab_wa = "While-alive loss rate", ylab_cov = NULL, xlab = "Time", level = 0.95, smooth = FALSE, span = 0.3, ... )## S3 method for class 'WA' plot( x, newdata, t_seq, id = 1, mode = c("wa", "cov"), covariate = NULL, ylab_wa = "While-alive loss rate", ylab_cov = NULL, xlab = "Time", level = 0.95, smooth = FALSE, span = 0.3, ... )
x |
A |
newdata |
Data used to rebuild the RHS design (same columns as in the model). |
t_seq |
Times to plot over (numeric vector). |
id |
Row index of |
mode |
|
covariate |
Character; covariate name (must appear on RHS) when |
ylab_wa |
Y-axis label for while-alive plot. |
ylab_cov |
Y-axis label for covariate-effect plot; default
|
xlab |
X-axis label. |
level |
Confidence level for ribbons (default 0.95). |
smooth |
Logical; if |
span |
LOESS span used when |
... |
Unused. |
A ggplot2 object.
## Not run: plot.WA(fit, newdata = crt_dt, t_seq = seq(0, max(fit$tau_grid), length.out = 200), id = 1, mode = "wa", smooth = TRUE) plot.WA(fit, newdata = crt_dt, t_seq = seq(0, max(fit$tau_grid), length.out = 200), id = 1, mode = "cov", covariate = "numchron", ylab_cov = "Effect of numchron on linear predictor", smooth = TRUE) ## End(Not run)## Not run: plot.WA(fit, newdata = crt_dt, t_seq = seq(0, max(fit$tau_grid), length.out = 200), id = 1, mode = "wa", smooth = TRUE) plot.WA(fit, newdata = crt_dt, t_seq = seq(0, max(fit$tau_grid), length.out = 200), id = 1, mode = "cov", covariate = "numchron", ylab_cov = "Effect of numchron on linear predictor", smooth = TRUE) ## End(Not run)
Predict while-alive loss rates
## S3 method for class 'WA' predict(object, newdata, t_seq, level = 0.95, ...)## S3 method for class 'WA' predict(object, newdata, t_seq, level = 0.95, ...)
object |
A |
newdata |
Data frame with columns matching the RHS of the fitted model.
Predictions are computed for the rows of |
t_seq |
Numeric vector of times at which to evaluate predictions. |
level |
Confidence level for pointwise intervals (default 0.95). |
... |
Unused. |
A data frame with columns id (row index in newdata),
t, mu (predicted while-alive rate), and CI columns lb, ub.
## Not run: newdata_ex <- crt_dt |> dplyr::select(intv, demo_sex_male, numchron) |> dplyr::distinct() |> dplyr::slice(1:2) pred <- predict.WA(fit, newdata = newdata_ex, t_seq = seq(0, 3.5, by = 0.05)) head(pred) ## End(Not run)## Not run: newdata_ex <- crt_dt |> dplyr::select(intv, demo_sex_male, numchron) |> dplyr::distinct() |> dplyr::slice(1:2) pred <- predict.WA(fit, newdata = newdata_ex, t_seq = seq(0, 3.5, by = 0.05)) head(pred) ## End(Not run)
Summarize a WA object
## S3 method for class 'WA' summary(object, ...)## S3 method for class 'WA' summary(object, ...)
object |
A |
... |
Unused. |
An object of class "summary.WA" containing configuration and a
coefficient table with estimates, standard errors, and z-scores.
## Not run: fit <- WA_fit(Surv(time, status) ~ intv + demo_sex_male + numchron, crt_dt, id="screenid", cluster="practiceid", knots=seq(0,3.5,length.out=6), tau_grid=seq(0,3.5,length.out=6), basis="bz", degree=1, link="log", w_recur=c(1), w_term=2, ipcw="cox", ipcw_formula=~ demo_sex_male + numchron) summary(fit) ## End(Not run)## Not run: fit <- WA_fit(Surv(time, status) ~ intv + demo_sex_male + numchron, crt_dt, id="screenid", cluster="practiceid", knots=seq(0,3.5,length.out=6), tau_grid=seq(0,3.5,length.out=6), basis="bz", degree=1, link="log", w_recur=c(1), w_term=2, ipcw="cox", ipcw_formula=~ demo_sex_male + numchron) summary(fit) ## End(Not run)
Runs K-fold CV over a grid of basis types, degrees, interior-knot counts,
and link functions. For each configuration, fits the model on K-1 folds and
accumulates the prediction error (PE) on the held-out fold using
WA_PE() (IPCW computed on the training subjects).
WA_cv( formula, data, id, cluster = NULL, basis_set = c("il", "pl", "bz"), degree_vec = 1:2, n_int_vec = c(0, 2, 4), knot_scheme = c("equidist", "quantile"), link_set = c("log"), time_range = NULL, tau_grid = NULL, w_recur, w_term, ipcw = c("cox", "km"), ipcw_formula = ~1, K = 5, seed = 1L, verbose = TRUE )WA_cv( formula, data, id, cluster = NULL, basis_set = c("il", "pl", "bz"), degree_vec = 1:2, n_int_vec = c(0, 2, 4), knot_scheme = c("equidist", "quantile"), link_set = c("log"), time_range = NULL, tau_grid = NULL, w_recur, w_term, ipcw = c("cox", "km"), ipcw_formula = ~1, K = 5, seed = 1L, verbose = TRUE )
formula |
data, id, cluster See |
basis_set |
Character vector of candidate bases. |
degree_vec |
Integer vector of candidate degrees. |
n_int_vec |
Integer vector of interior-knot counts; 0 means boundaries only. |
knot_scheme |
|
link_set |
Character vector of candidate links (subset of |
time_range |
Optional numeric length-2 vector |
tau_grid |
Optional numeric vector; if |
w_recur |
w_term Event-type weights (see |
ipcw |
IPCW method ( |
ipcw_formula |
One-sided RHS formula for IPCW Cox model (if |
K |
Number of folds. |
seed |
RNG seed for fold assignment. |
verbose |
Logical; show a text progress bar and per-fold messages. |
A data frame with columns: basis, degree, n_int,
link, and aggregated PE. Lower PE is better.
## Not run: tmin <- 0 tmax <- max(crt_dt$time, na.rm = TRUE) cv_tab <- WA_cv( Surv(time, status) ~ intv + demo_sex_male + numchron, data = crt_dt, id = "screenid", cluster = "practiceid", basis_set = "bz", degree_vec = c(1,2,3,4), n_int_vec = c(0,2,3,4,5), knot_scheme = "equidist", link_set = "log", time_range = c(tmin, tmax), tau_grid = seq(0, 3.5, length.out = 6), w_recur = c(1), w_term = 2, ipcw = "cox", ipcw_formula = ~ demo_sex_male + numchron, K = 5, seed = 2025 ) head(cv_tab[order(cv_tab$PE), ], 10) ## End(Not run)## Not run: tmin <- 0 tmax <- max(crt_dt$time, na.rm = TRUE) cv_tab <- WA_cv( Surv(time, status) ~ intv + demo_sex_male + numchron, data = crt_dt, id = "screenid", cluster = "practiceid", basis_set = "bz", degree_vec = c(1,2,3,4), n_int_vec = c(0,2,3,4,5), knot_scheme = "equidist", link_set = "log", time_range = c(tmin, tmax), tau_grid = seq(0, 3.5, length.out = 6), w_recur = c(1), w_term = 2, ipcw = "cox", ipcw_formula = ~ demo_sex_male + numchron, K = 5, seed = 2025 ) head(cv_tab[order(cv_tab$PE), ], 10) ## End(Not run)
Fits the while-alive regression model targeting the while-alive loss rate
for composite endpoints with recurrent and terminal events. Time-varying
covariate effects are represented via user-chosen time bases (e.g., B-spline,
piecewise polynomial, interval-local). Robust inference supports
cluster-randomized trials (CRTs) via cluster-robust variance; if
cluster = NULL, IID (subject-as-cluster) variance is used.
WA_fit( formula, data, id, cluster = NULL, knots, tau_grid, basis = c("il", "pl", "bz", "ns", "ms", "st", "tl", "tf"), degree = 1, link = c("log", "identity"), w_recur, w_term, ipcw = c("km", "cox"), ipcw_formula = ~1 )WA_fit( formula, data, id, cluster = NULL, knots, tau_grid, basis = c("il", "pl", "bz", "ns", "ms", "st", "tl", "tf"), degree = 1, link = c("log", "identity"), w_recur, w_term, ipcw = c("km", "cox"), ipcw_formula = ~1 )
formula |
A |
data |
Long-format data frame with one row per event/checkpoint
per subject, containing |
id |
Character scalar; subject ID column name. |
cluster |
Optional character scalar; cluster column name for CRT-robust
inference. If |
knots |
Numeric vector (length |
tau_grid |
Numeric vector of evaluation times used to stack the
estimating equations. Independent of |
basis |
One of |
degree |
Integer degree for bases that use it (e.g., |
link |
Link function: |
w_recur |
Numeric vector of weights for each recurrent event type. Its
length must match the number of recurrent |
w_term |
Numeric scalar; weight for the terminal event. |
ipcw |
IPCW method: |
ipcw_formula |
A one-sided formula specifying RHS covariates for the IPCW Cox model
when |
The estimating equations solve
over tau_grid, where is the weighted composite loss
(recurrent+terminal), the while-alive loss rate under the chosen
link, , the at-risk/terminal indicator, and
the censoring survival modeled via ipcw.
An object of class "WA" with elements:
est: named coefficient vector.
vcov: cluster-robust variance matrix.
se: standard errors.
converged: logical.
basis, degree, link, Z_cols,
knots, tau_grid, id_var, cluster_var,
w_recur, w_term, status_codes, formula.
## Not run: # Example (non-running) using included 'crt_dt' long dataset fit <- WA_fit( Surv(time, status) ~ intv + demo_sex_male + numchron, data = crt_dt, id = "screenid", cluster = "practiceid", knots = seq(0, 3.5, length.out = 6), tau_grid = seq(0, 3.5, length.out = 6), basis = "bz", degree = 1, link = "log", w_recur = c(1), w_term = 2, ipcw = "cox", ipcw_formula = ~ demo_sex_male + numchron ) s <- summary(fit) ## End(Not run)## Not run: # Example (non-running) using included 'crt_dt' long dataset fit <- WA_fit( Surv(time, status) ~ intv + demo_sex_male + numchron, data = crt_dt, id = "screenid", cluster = "practiceid", knots = seq(0, 3.5, length.out = 6), tau_grid = seq(0, 3.5, length.out = 6), basis = "bz", degree = 1, link = "log", w_recur = c(1), w_term = 2, ipcw = "cox", ipcw_formula = ~ demo_sex_male + numchron ) s <- summary(fit) ## End(Not run)