| Title: | Conditional Naive Bayes Survival Modelling for Right-Censored Data |
|---|---|
| Description: | Fits conditional naive Bayes survival models for right-censored outcomes using inverse-probability of censoring weighting. The package provides model fitting, prediction, resampling-based evaluation, cross-validation, hyper-parameter tuning, and permutation variable importance utilities for horizon-specific survival prediction. The model is the censored naive Bayes classifier of Wolfson et al. (2015) <doi:10.1002/sim.6526>, which combines the marginal Kaplan-Meier survivor function with horizon-specific class-conditional covariate densities and inverse-probability-of-censoring weights. Resampling evaluation uses the inverse-probability-of-censoring-weighted Brier score of Gerds and Schumacher (2006) <doi:10.1002/bimj.200610301>. |
| Authors: | Imad El Badisy [aut, cre] |
| Maintainer: | Imad El Badisy <[email protected]> |
| License: | GPL-3 |
| Version: | 0.5.1 |
| Built: | 2026-10-08 11:23:07 UTC |
| Source: | https://github.com/ielbadisy/nbsurv |
Assesses calibration of a nbsurv model at a single prediction horizon
by grouping observations into quantile bins of predicted event probability and
comparing the group mean prediction against the Kaplan-Meier observed event
probability within each bin.
calibration_plot_nbsurv(object, newdata, horizon, n_groups = 10, ...)calibration_plot_nbsurv(object, newdata, horizon, n_groups = 10, ...)
object |
A fitted |
newdata |
A data frame containing the outcome and predictors. |
horizon |
A single numeric prediction horizon. |
n_groups |
Number of quantile-based calibration groups. |
... |
Additional graphical arguments passed to |
Invisibly returns a data frame with columns mean_pred (mean predicted
event probability per group), observed (observed event probability from
Kaplan-Meier), and n (group size). The calibration plot is drawn as a
side effect.
library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv(Surv(time, status) ~ age + sex, data = lung) calibration_plot_nbsurv(fit, newdata = lung, horizon = 300)library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv(Surv(time, status) ~ age + sex, data = lung) calibration_plot_nbsurv(fit, newdata = lung, horizon = 300)
Provides resampling-based utilities for evaluating, cross-validating, and
tuning nbsurv survival models using horizon-specific Brier score,
integrated Brier score (IBS), and concordance.
The integrated Brier score summarises calibration over a range of horizons:
approximated by the trapezoidal rule over the supplied times.
cv_nbsurv( formula, data, folds = 5, times = NULL, seed = NULL, scale = TRUE, laplace = 1, min_sd = 0.05, time_grid = NULL, eps = 1e-06, cov_structure = c("diagonal", "full"), shrinkage = 0.2, time_smooth = FALSE, bandwidth = NULL ) evaluate_nbsurv(object, newdata, times, metrics = c("brier", "concordance"), ibs = FALSE) tune_nbsurv( formula, data, param_grid, folds = 5, times = NULL, metric = c("brier", "concordance"), maximize = NULL, seed = NULL, eps = 1e-06 )cv_nbsurv( formula, data, folds = 5, times = NULL, seed = NULL, scale = TRUE, laplace = 1, min_sd = 0.05, time_grid = NULL, eps = 1e-06, cov_structure = c("diagonal", "full"), shrinkage = 0.2, time_smooth = FALSE, bandwidth = NULL ) evaluate_nbsurv(object, newdata, times, metrics = c("brier", "concordance"), ibs = FALSE) tune_nbsurv( formula, data, param_grid, folds = 5, times = NULL, metric = c("brier", "concordance"), maximize = NULL, seed = NULL, eps = 1e-06 )
formula |
A model formula with a |
data |
A data frame containing outcome and predictors. |
folds |
Number of cross-validation folds. |
times |
Prediction horizons to evaluate. If |
seed |
Optional random seed for reproducible fold assignment. |
scale |
Logical; if |
laplace |
Laplace smoothing constant for categorical likelihoods. |
min_sd |
Lower bound used for Gaussian standard deviations. |
time_grid |
Optional prediction grid stored with each fitted model. |
eps |
Small numerical constant used in probability clipping and IPCW calculations. |
cov_structure |
Passed to each fold's |
shrinkage |
Passed to each fold's |
time_smooth |
Passed to each fold's |
bandwidth |
Passed to each fold's |
object |
A fitted |
newdata |
Evaluation data containing the same outcome and predictors as the training formula. |
metrics |
Performance metrics to compute. One or both of |
ibs |
Logical; if |
param_grid |
A data frame with columns |
metric |
The metric used to choose the best hyper-parameter setting. |
maximize |
Logical; if |
evaluate_nbsurv() returns a data frame of horizon-specific metrics. When
ibs = TRUE the integrated Brier score is attached via attr(result, "ibs").
cv_nbsurv() returns a list with fold-level and mean cross-validation summaries.
tune_nbsurv() returns a list with all candidate results and the best row.
library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv(Surv(time, status) ~ age + sex, data = lung) res <- evaluate_nbsurv(fit, newdata = lung[1:50, ], times = c(100, 200, 400), ibs = TRUE) res attr(res, "ibs") cv_nbsurv(Surv(time, status) ~ age + sex, data = lung, folds = 3, times = c(100, 200))library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv(Surv(time, status) ~ age + sex, data = lung) res <- evaluate_nbsurv(fit, newdata = lung[1:50, ], times = c(100, 200, 400), ibs = TRUE) res attr(res, "ibs") cv_nbsurv(Surv(time, status) ~ age + sex, data = lung, folds = 3, times = c(100, 200))
Fits a conditional naive Bayes model for right-censored survival data. The model combines the marginal Kaplan-Meier survival estimate with horizon-specific likelihood terms estimated among subjects who fail before the horizon and subjects known to survive beyond it.
nbsurv( formula, data, scale = TRUE, laplace = 1, min_sd = 0.05, time_grid = NULL, eps = 1e-06, cov_structure = c("diagonal", "full"), shrinkage = 0.2, time_smooth = FALSE, bandwidth = NULL )nbsurv( formula, data, scale = TRUE, laplace = 1, min_sd = 0.05, time_grid = NULL, eps = 1e-06, cov_structure = c("diagonal", "full"), shrinkage = 0.2, time_smooth = FALSE, bandwidth = NULL )
formula |
A model formula with a |
data |
A data frame containing the outcome and predictors. |
scale |
Logical; if |
laplace |
Laplace smoothing constant for categorical likelihoods. |
min_sd |
Lower bound used for Gaussian standard deviations. |
time_grid |
Optional numeric vector of prediction horizons stored with the fit. |
eps |
Small numerical constant used to clip probabilities away from 0 and 1. |
cov_structure |
|
shrinkage |
Shrinkage weight toward the diagonal covariance, in '[0, 1]', used only
when |
time_smooth |
Logical, default |
bandwidth |
Kernel bandwidth (in the same units as |
An object of class "nbsurv".
library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv( Surv(time, status) ~ age + sex, data = lung ) predict(fit, newdata = lung[1:3, ], times = c(100, 200, 300))library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv( Surv(time, status) ~ age + sex, data = lung ) predict(fit, newdata = lung[1:3, ], times = c(100, 200, 300))
nbsurv objects are created by nbsurv and store the training
data, outcome, feature types, scaling information, and the Kaplan-Meier
estimates required for prediction.
## S3 method for class 'nbsurv' print(x, ...) ## S3 method for class 'nbsurv' plot(x, times = NULL, n_curves = 5, ...)## S3 method for class 'nbsurv' print(x, ...) ## S3 method for class 'nbsurv' plot(x, times = NULL, n_curves = 5, ...)
x |
A fitted |
times |
Optional vector of horizons used for plotting. |
n_curves |
Number of training-set curves to display in the plot method. |
... |
Additional arguments passed to the underlying plotting functions. |
print.nbsurv is called for its side effect of printing a concise summary
of the fitted model (formula, number of training rows, predictors, prediction
grid size, covariance structure, and time-smoothing settings) to the console;
it returns its argument x invisibly.
plot.nbsurv is called for its side effect of drawing predicted
survival curves for the first n_curves training observations over
times; it returns, invisibly, the matrix of predicted survival
probabilities used for the plot (one row per displayed observation, one column
per element of times).
Base-graphics plot methods for the result objects returned by
varimp_nbsurv, cv_nbsurv, and tune_nbsurv.
plot.varimp_nbsurv() draws a horizontal bar chart of permutation importance,
sorted by increasing importance (largest bar at top).
plot.cv_nbsurv() draws the mean cross-validation metric against the evaluation
horizons.
plot.tune_nbsurv() draws the mean tuning metric for every candidate row of
param_grid, highlighting the selected best candidate in red.
## S3 method for class 'varimp_nbsurv' plot(x, ...) ## S3 method for class 'cv_nbsurv' plot(x, metric = c("brier", "concordance"), ...) ## S3 method for class 'tune_nbsurv' plot(x, ...)## S3 method for class 'varimp_nbsurv' plot(x, ...) ## S3 method for class 'cv_nbsurv' plot(x, metric = c("brier", "concordance"), ...) ## S3 method for class 'tune_nbsurv' plot(x, ...)
x |
A |
metric |
For |
... |
Additional arguments passed to the underlying base graphics function
( |
The input object, invisibly. Called for its plotting side effect.
library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv(Surv(time, status) ~ age + sex + ph.ecog, data = lung) vi <- varimp_nbsurv(fit, newdata = lung, times = c(100, 200, 400), n_repeats = 5, seed = 1) plot(vi) cvfit <- cv_nbsurv(Surv(time, status) ~ age + sex, data = lung, folds = 3, times = c(100, 200, 300)) plot(cvfit) grid <- expand.grid(scale = TRUE, laplace = c(0.5, 1, 2), min_sd = 0.05) tf <- tune_nbsurv( Surv(time, status) ~ age + sex, data = lung, param_grid = grid, folds = 3, times = c(100, 200) ) plot(tf)library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv(Surv(time, status) ~ age + sex + ph.ecog, data = lung) vi <- varimp_nbsurv(fit, newdata = lung, times = c(100, 200, 400), n_repeats = 5, seed = 1) plot(vi) cvfit <- cv_nbsurv(Surv(time, status) ~ age + sex, data = lung, folds = 3, times = c(100, 200, 300)) plot(cvfit) grid <- expand.grid(scale = TRUE, laplace = c(0.5, 1, 2), min_sd = 0.05) tf <- tune_nbsurv( Surv(time, status) ~ age + sex, data = lung, param_grid = grid, folds = 3, times = c(100, 200) ) plot(tf)
Computes horizon-specific survival or event probabilities from a fitted conditional naive Bayes survival model. The returned survival curves are forced to be non-increasing across increasing time horizons.
## S3 method for class 'nbsurv' predict(object, newdata, times, type = c("survival", "event"), ...) predictSurvProb(object, ...) ## S3 method for class 'nbsurv' predictSurvProb(object, newdata, times, ...)## S3 method for class 'nbsurv' predict(object, newdata, times, type = c("survival", "event"), ...) predictSurvProb(object, ...) ## S3 method for class 'nbsurv' predictSurvProb(object, newdata, times, ...)
object |
A fitted |
newdata |
A data frame of predictor values. |
times |
Numeric vector of prediction horizons. |
type |
Either |
... |
Unused additional arguments. |
A matrix with one row per observation in newdata and one column per
prediction horizon.
library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv(Surv(time, status) ~ age + sex, data = lung) predict(fit, newdata = lung[1:2, ], times = c(100, 200)) predict(fit, newdata = lung[1:2, ], times = c(100, 200), type = "event")library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv(Surv(time, status) ~ age + sex, data = lung) predict(fit, newdata = lung[1:2, ], times = c(100, 200)) predict(fit, newdata = lung[1:2, ], times = c(100, 200), type = "event")
Computes permutation-based variable importance: for each predictor, its values are independently
shuffled n_repeats times, evaluate_nbsurv is recomputed on the permuted data,
and importance is the resulting degradation in the chosen metric, averaged over times and
repeats. Predictors the model relies on more heavily degrade performance more when permuted.
varimp_nbsurv( object, newdata, times, metric = c("brier", "concordance"), n_repeats = 10, seed = NULL )varimp_nbsurv( object, newdata, times, metric = c("brier", "concordance"), n_repeats = 10, seed = NULL )
object |
A fitted |
newdata |
Evaluation data containing the same outcome and predictors as the training formula. |
times |
Prediction horizons averaged over when computing the metric. |
metric |
The metric to degrade under permutation. One of |
n_repeats |
Number of independent permutations per feature; importance is the mean degradation across repeats. |
seed |
Optional random seed for reproducible permutations. |
A data frame (class "varimp_nbsurv") with one row per predictor, sorted by decreasing
importance, with columns feature, baseline (the unpermuted metric, averaged over
times), permuted (the metric after permuting that feature, averaged over
times and n_repeats), and importance (the degradation: permuted -
baseline for "brier", baseline - permuted for "concordance", so higher is
always more important).
library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv(Surv(time, status) ~ age + sex + ph.ecog, data = lung) varimp_nbsurv(fit, newdata = lung, times = c(100, 200, 400), n_repeats = 5, seed = 1)library(survival) lung <- stats::na.omit(lung) lung$status <- as.integer(lung$status == 2) fit <- nbsurv(Surv(time, status) ~ age + sex + ph.ecog, data = lung) varimp_nbsurv(fit, newdata = lung, times = c(100, 200, 400), n_repeats = 5, seed = 1)