Package {survalis}


Type: Package
Title: Interpretable Survival Machine Learning Framework
Version: 2.4.1
Author: Imad El Badisy [aut, cre]
Maintainer: Imad El Badisy <elbadisyimad@gmail.com>
Description: A modular toolkit for interpretable survival machine learning with a unified interface for fitting, prediction, evaluation, and interpretation. It includes semiparametric, parametric, tree-based, ensemble, boosting, kernel, and deep-learning survival learners, together with benchmarking, scoring, calibration, and model-agnostic interpretation utilities. Representative methodological anchors include Cox (1972) <doi:10.1111/j.2517-6161.1972.tb00899.x>, Royston and Parmar (2002) <doi:10.1002/sim.1203>, Ishwaran et al. (2008) <doi:10.1214/08-AOAS169>, Jaeger et al. (2019) <doi:10.1214/19-AOAS1261>, Harrell et al. (1982) <doi:10.1001/jama.1982.03320430047030>, Graf et al. (1999) <doi:10.1002/(SICI)1097-0258(19990915/30)18:17/18%3C2529::AID-SIM274%3E3.0.CO;2-5>, Friedman (2001) <doi:10.1214/aos/1013203451>, Apley and Zhu (2020) <doi:10.1111/rssb.12377>, and other related methods for survival modeling, prediction, and interpretation. Clustering of individualized survival curves is described in El Badisy (2026) <doi:10.1093/bioadv/vbag218>.
License: MIT + file LICENSE
URL: https://elbadisyimad.com/survalis/, https://github.com/ielbadisy/survalis, https://CRAN.R-project.org/package=survalis
BugReports: https://github.com/ielbadisy/survalis/issues
Encoding: UTF-8
LazyData: true
Depends: R (≥ 4.4.0)
Imports: survival, ggplot2 (≥ 3.5.0), functionals, nnls, rpart, rsample, aftgee, aorsf, pec, party, ranger, unsurv, randomForestSRC, xgboost, fastgbm (≥ 0.7.0), densemlp, rpsurv, nbsurv, tvrmst, CoxBoost, BART, flexsurv, glmnet, mboost, timereg, partykit, gower, pracma, basetable (≥ 1.4.5), glue, cli, rlang, stats, utils
Suggests: testthat (≥ 3.0.0), knitr, rmarkdown, roxygen2, covr, timeROC, patchwork, cluster
RoxygenNote: 7.3.3
Config/testthat/edition: 3
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2026-11-14 10:50:15 UTC; imad-el-badisy
Repository: CRAN
Date/Publication: 2026-11-14 15:50:08 UTC

survalis: Interpretable Survival Machine Learning

Description

survalis provides a unified interface for fitting, predicting, evaluating, tuning, comparing, interpreting and standardising survival machine-learning models across 24 learners.

The seven verbs

The recommended interface, consistent with the funcml package:

fit()

fit a learner by id, returning a survalis_fit.

predict()

typed predictions on a recorded time grid ("survival", "risk", "chf", "hazard", "rmst", "quantile", "median").

evaluate()

resampled performance for a fit or a formula.

tune()

grid search, returning the refit best model.

compare()

paired multi-model leaderboard on shared folds.

interpret()

one entry point to the explanation methods.

estimate()

standardised binary-treatment contrast by G-computation.

Resampling is described by cv(), holdout(), groupcv() and bootstrap(). Discover components with listlearners(), listmetrics() and listmethods(). Custom learners plug into every verb as model = list(fit = , predict = ); see fit().

Author(s)

Maintainer: Imad El Badisy elbadisyimad@gmail.com

See Also

Useful links:


Wrap a legacy mlsurv_model as a survalis_fit

Description

Wrap a legacy mlsurv_model as a survalis_fit

Usage

.as_survalis_fit(mlobj, model_id, spec, formula, data, parsed)

Shared split-loop used by evaluate() and compare()

Description

Shared split-loop used by evaluate() and compare()

Usage

.evaluate_splits(
  splits,
  formula,
  data,
  model,
  spec,
  times,
  metrics,
  parsed,
  ncores = 1
)

Standardize learner prediction output to the survmat contract

Description

Internal utility used by ⁠predict_*()⁠ methods and evaluators to coerce raw predictions to a validated survival-probability matrix contract.

Usage

.finalize_survmat(S, times, clamp = TRUE, enforce_monotone = TRUE)

Arguments

S

Raw survival predictions as matrix, data.frame, or vector.

times

Numeric vector of evaluation times aligned with columns of S.

clamp

Logical; if TRUE, clamp values to ⁠[0, 1]⁠.

enforce_monotone

Logical; if TRUE, enforce non-increasing survival over increasing times via cumulative minima.

Value

A base data.frame with columns named ⁠t=<time>⁠.


0/1 event vector of a fit's training data

Description

0/1 event vector of a fit's training data

Usage

.fit_status_vector(fit)

Infer time points from survmat column names

Description

Internal helper that parses "t=<time>" column names when an explicit time vector is not supplied.

Usage

.infer_survmat_times(S)

Arguments

S

A survival-probability matrix or data frame.

Value

Numeric vector of inferred times.


Adaptors from interpret() method ids to compute_*() signatures

Description

Adaptors from interpret() method ids to compute_*() signatures

Usage

.interpret_dispatch()

Expand a resampling specification into concrete row-index splits

Description

Expand a resampling specification into concrete row-index splits

Usage

.make_splits(resampling, data, status_col = NULL)

Arguments

resampling

A survalis_resampling object.

data

The (already row-filtered) modelling data frame.

status_col

Name of the event-indicator column, used when params$strata == "status".

Value

A list of ⁠list(analysis = <int>, assessment = <int>, id = <chr>)⁠.


Parse a survival outcome from a model formula

Description

Internal helper that extracts the time and status components from a formula whose left-hand side is Surv(...) or pkg::Surv(...).

Usage

.parse_surv_formula(formula, data)

Arguments

formula

A model formula with a survival outcome on the left-hand side.

data

A data.frame used to resolve status recoding expressions such as status == 2.

Value

A list with time_col, status_col, event_value, and recode_status.


Reconstruct Cox-style survival curves from linear predictors

Description

Internal helper shared by Cox-family learners to produce comparable survival probabilities from a baseline cumulative hazard and subject-specific linear predictors.

Usage

.predict_cox_from_lp(lp_train, lp_new, y_train, times)

Arguments

lp_train

Numeric vector of linear predictors on the training data.

lp_new

Numeric vector of linear predictors on newdata.

y_train

A Surv object for the training data.

times

Numeric evaluation times.

Value

A standardized survival-probability data.frame.


Resolve a caller-supplied times argument against the default grid

Description

Resolve a caller-supplied times argument against the default grid

Usage

.resolve_times(times, time, status = NULL, n = 50L)

Arguments

times

NULL or a numeric vector.

time, status

Passed to default_times() when times is NULL.

n

Grid size for the default.

Value

A validated numeric vector (strictly increasing, non-negative; 0 is allowed).


Validate and standardize a survival-probability matrix (survmat)

Description

Internal utility to validate the basic structure of a surv_mat and coerce it to a numeric matrix. Optionally checks column names against times.

Usage

.survmat_as_matrix(S, times = NULL, strict_colnames = FALSE, clamp = FALSE)

Arguments

S

A surv_mat: numeric matrix or data.frame of survival probabilities.

times

Optional numeric vector of time points. If provided, requires length(times) == ncol(S) and enforces the column names "t=<time>".

strict_colnames

Logical; if TRUE and times is provided, checks that existing colnames are exactly "t=<time>" (after coercion to character). If FALSE, colnames are overwritten to match the contract.

clamp

Logical; if TRUE, clamps S into ⁠[0,1]⁠.

Value

A numeric matrix.


Tidy data frames from verb result objects

Description

as.data.frame() methods that return the most useful rectangular view of each result object, for downstream plotting or reporting:

Usage

## S3 method for class 'survmat'
as.data.frame(x, ..., format = c("wide", "long"))

## S3 method for class 'survalis_fit'
as.data.frame(x, ...)

## S3 method for class 'survalis_eval'
as.data.frame(x, ...)

## S3 method for class 'survalis_tune'
as.data.frame(x, ...)

## S3 method for class 'survalis_compare'
as.data.frame(x, ...)

## S3 method for class 'survalis_estimate'
as.data.frame(x, ...)

Arguments

x

A verb result object.

...

Ignored.

format

For survmat only: "wide" (default) or "long".

Details

Value

A data frame.


Compare several survival learners on shared resampling folds

Description

Evaluates each learner in models on the same set of resampling splits, so the per-fold values are paired across models. Returns a leaderboard plus the full per-split table.

Usage

compare(
  formula,
  data,
  models,
  specs = list(),
  times = NULL,
  resampling = cv(),
  metrics = c("cindex", "ibs"),
  tune = FALSE,
  ncores = 1,
  ...
)

Arguments

formula

A survival formula.

data

A data frame.

models

Character vector of learner ids, or a list mixing ids and custom learners (see fit()). List names, when given, label the learners.

specs

Named list of engine-argument lists, keyed by learner label (specs[["ranger"]] <- list(num.trees = 500)).

times

Evaluation grid; NULL picks up to 50 event-time quantiles (5th to 95th percentile).

resampling

A resampling spec (default cv()); the split set is drawn once and reused for every model.

metrics

Character vector from listmetrics()$metric.

tune

Logical. When TRUE, each tunable model is tuned once on the full data (its default grid) and the winning configuration is then evaluated on the shared folds. This is not nested CV, so the tuned results are optimistic; a message is emitted. Default FALSE.

ncores

Cores for the split loop (default 1).

...

Unused.

Value

A survalis_compare object: leaderboard (model x metric, mean and se), per_split (long), times, resampling, metrics, models, tuned.

Examples


cmp <- compare(Surv(time, status) ~ age + karno + celltype, veteran,
               models = c("coxph", "ranger"), times = c(90, 180, 365),
               resampling = cv(3))
cmp
plot(cmp)


Decision-weighted scores for survival predictions

Description

Scores predicted risks at one or more horizons with a decision-weighted proper score: the loss of the rule "act if predicted risk exceeds c", averaged over thresholds c with a weight w(c). The weight states which risk thresholds matter. Every such score equals a weighted area under the net benefit curve, with weight ⁠(1 - c) w(c)⁠, so a threshold range restricts model comparison to the decisions clinicians actually make.

Usage

dwscore(
  object,
  y,
  times = NULL,
  weight = c("range", "survacc", "brier", "log", "beta"),
  thresholds = c(0.05, 0.2),
  shape = c(2, 18)
)

Arguments

object

A numeric matrix of survival probabilities (rows = subjects, columns = times), for example from predict().

y

A survival::Surv() object with one entry per row of object.

times

Evaluation times. NULL reads the "times" attribute or column names of the form "t=<time>".

weight

Threshold weight:

"range"

uniform on thresholds (default); only decisions with a threshold in that range count.

"survacc"

spherical weight (c^2 + (1 - c)^2)^{-3/2}; the loss is 1 - SurvAcc.

"brier"

constant weight 2; the loss is the Brier score.

"log"

weight 1 / (c (1 - c)); the loss is the logarithmic score.

"beta"

Beta density with parameters shape.

thresholds

Length-2 numeric range c(lower, upper) in ⁠[0, 1]⁠ for weight = "range" (default c(0.05, 0.20)).

shape

Length-2 numeric Beta parameters for weight = "beta" (default c(2, 18), mean threshold 0.10).

Details

With risk r = 1 - S(t \mid X) and event status D = I(T \le t), the loss is L_w(r, 0) = \int_0^r w(c)\, c\, dc for a subject event free at t and L_w(r, 1) = \int_r^1 w(c) (1 - c)\, dc for a subject with an event by t. Right censoring is handled by inverse probability of censoring weights from the Kaplan-Meier estimate of the censoring distribution: subjects event free at t are weighted by 1 / G(t), subjects with an observed event at U \le t by 1 / G(U-), and subjects censored before t get weight zero. This assumes censoring is independent of the covariates. Range and Beta weights integrate to one, so those losses lie on the scale of an average regret per decision.

In evaluate(), compare() and tune(), use the metric ids "survacc" (SurvAcc, higher is better) and "range:<lower>-<upper>", for example "range:0.05-0.20" (loss, lower is better). Both need a single time.

Value

A data frame with columns time, weight and loss (lower is better for every weight).

Examples

m <- fit(Surv(time, status) ~ age + karno, veteran[1:100, ], "coxph")
S <- predict(m, veteran[-(1:100), ], times = c(90, 180))
y <- Surv(veteran$time[-(1:100)], veteran$status[-(1:100)])
dwscore(S, y)
dwscore(S, y, weight = "brier")
dwscore(S, y, weight = "range", thresholds = c(0.10, 0.30))

# the same scores inside the verbs (single time)
S180 <- predict(m, veteran[-(1:100), ], times = 180)
evaluate(S180, y = y, metrics = c("brier", "survacc", "range:0.05-0.20"))

Standardised causal-style contrast for a binary treatment

Description

Estimates a covariate-adjusted marginal contrast between the two levels of a binary treatment by G-computation (outcome standardisation): a survival model is fitted for Surv(time, status) ~ treatment * (confounders), every subject's outcome is predicted under both treatment levels, and the counterfactual mean survival curves are contrasted.

Usage

estimate(
  formula,
  data,
  model,
  estimand = c("RMST_diff", "RMST_ratio", "survival_diff", "survival_ratio"),
  treatment,
  tau,
  times = NULL,
  spec = list(),
  R = 200,
  conf = 0.95,
  refit = TRUE,
  seed = NULL,
  ...
)

Arguments

formula

A survival formula whose right-hand side lists the confounders (the treatment is added by the function, with treatment-by-confounder interactions).

data

A data frame.

model

A learner id (listlearners()$learner) or a custom learner (see fit()).

estimand

One of "RMST_diff" (default), "RMST_ratio", "survival_diff", "survival_ratio".

treatment

Name of a binary column in data. The larger value is the "treated" (A = 1) level.

tau

Horizon: the RMST restriction time for the RMST estimands, or the evaluation time for the survival estimands.

times

Prediction grid; NULL picks up to 50 event-time quantiles (5th to 95th percentile). Must reach tau.

spec

Engine arguments for the learner (named list).

R

Bootstrap replicates (default 200).

conf

Confidence level (default 0.95).

refit

Logical; refit the model inside each bootstrap replicate (default TRUE).

seed

Optional RNG seed.

...

Unused.

Details

Uncertainty is a nonparametric bootstrap over subjects; by default the model is refitted in each replicate (refit = TRUE), which propagates model estimation error. Set refit = FALSE for a faster interval that is conditional on the fitted model.

Value

A survalis_estimate object with estimand, estimate, conf_low, conf_high, conf, treatment, levels, tau, R, refit, boot (replicate estimates) and curves (counterfactual mean survival for each arm on times).

Interpretation

The estimate has a causal reading only under standard identification assumptions: consistency, no unmeasured confounding given the formula covariates, positivity, no interference, and a correctly specified outcome model. For a non-manipulable attribute it is a covariate-adjusted marginal contrast, not a causal effect. This is a plug-in G-formula estimator; it is not doubly robust.

Examples


est <- estimate(Surv(time, status) ~ age + karno + celltype, veteran,
                model = "coxph", estimand = "RMST_diff",
                treatment = "trt", tau = 300, R = 50, seed = 1)
est


Performance of a survival learner

Description

Scores survival predictions in one of three ways:

Resampled

evaluate(fit) or evaluate(formula, data, model) refits the learner on each training split of resampling and scores it on the held-out split (internal validation).

New data

evaluate(fit, newdata = test) scores a fitted learner on data it was not trained on (external or temporal validation).

Supplied predictions

evaluate(S, y = Surv(time, status)) scores an ⁠n x length(times)⁠ matrix of survival probabilities produced elsewhere, for example by another package.

Usage

evaluate(object, ...)

## S3 method for class 'survalis_fit'
evaluate(
  object,
  newdata = NULL,
  times = NULL,
  resampling = cv(),
  metrics = c("cindex", "ibs"),
  ncores = 1,
  ...
)

## S3 method for class 'formula'
evaluate(
  object,
  data,
  model,
  spec = list(),
  times = NULL,
  resampling = cv(),
  metrics = c("cindex", "ibs"),
  ncores = 1,
  ...
)

## S3 method for class 'matrix'
evaluate(object, y, times = NULL, metrics = c("cindex", "ibs"), ...)

Arguments

object

A survalis_fit from fit(), a survival formula, or a numeric matrix of survival probabilities (rows = subjects, columns = times).

...

Unused.

newdata

When object is a survalis_fit: optional data frame with the outcome columns. If given, the fit is scored on it once and resampling is ignored.

times

Evaluation grid. NULL picks a grid from the data quantiles and records it; for a matrix, NULL reads the "times" attribute or column names of the form "t=<time>".

resampling

A resampling spec from cv(), holdout(), groupcv() or bootstrap() (default cv()).

metrics

Character vector from listmetrics()$metric. "brier", "ece", "survacc" and range scores such as "range:0.05-0.20" require a single times value (see dwscore()).

ncores

Cores for the split loop (default 1).

data

When object is a formula: the data frame.

model

When object is a formula: a learner id (listlearners()$learner) or a custom learner (see fit()).

spec

When object is a formula: engine arguments (named list).

y

When object is a matrix: a survival::Surv() object with one entry per row of object.

Value

A survalis_eval object: per_split (long data frame), summary (mean / sd / se / 95% interval per metric), times, resampling (NULL when scored once), metrics, model.

Examples


m <- fit(Surv(time, status) ~ age + karno + celltype, veteran, "ranger",
         spec = list(num.trees = 100))
evaluate(m, times = c(90, 180, 365), resampling = cv(3))

# train / test split
idx <- seq_len(100)
m_tr <- fit(Surv(time, status) ~ age + karno, veteran[idx, ], "coxph")
evaluate(m_tr, newdata = veteran[-idx, ], times = c(90, 180))

# predictions from anywhere
S <- predict(m_tr, veteran[-idx, ], times = c(90, 180))
evaluate(S, y = Surv(veteran$time[-idx], veteran$status[-idx]))


Fit a survival learner

Description

The entry point of the survalis verb interface. fit() selects a learner by id, passes engine arguments through spec, and returns a survalis_fit object that the other verbs (predict(), evaluate(), tune(), interpret(), estimate()) consume.

Usage

fit(formula, data, model, spec = list(), seed = NULL, ...)

Arguments

formula

A survival formula with a Surv() outcome on the left-hand side, e.g. Surv(time, status) ~ age + karno.

data

A data frame containing the formula variables.

model

A learner id from listlearners()$learner (for example "coxph", "ranger", "densemlp"), or a custom learner: a list with ⁠fit = function(formula, data, ...)⁠ returning any fitted object and ⁠predict = function(object, newdata, times)⁠ returning an ⁠nrow(newdata) x length(times)⁠ matrix of survival probabilities. An optional name element labels it in printed output. A custom learner works with every verb.

spec

A named list of engine arguments forwarded to the learner's fitting routine (for example list(num.trees = 500) for "ranger").

seed

Optional integer seed set immediately before fitting, for learners with a stochastic training step.

...

Not used; engine arguments passed here give an error. Use spec.

Value

An object of class c("survalis_fit", "mlsurv_model") with elements model, learner, engine, formula, data, time, status, model_id, and spec.

See Also

predict.survalis_fit(), evaluate(), tune(), compare(), interpret(), estimate(), listlearners()

Examples


m <- fit(Surv(time, status) ~ age + karno + celltype, veteran,
         model = "ranger", spec = list(num.trees = 200))
m

# A custom learner: an exponential AFT model from survival::survreg
expo <- list(
  name = "exponential",
  fit = function(formula, data, ...) survival::survreg(formula, data, dist = "exponential"),
  predict = function(object, newdata, times) {
    rate <- 1 / predict(object, newdata, type = "response")
    exp(-outer(rate, times))
  }
)
m2 <- fit(Surv(time, status) ~ age + karno, veteran, model = expo)
predict(m2, veteran[1:3, ], times = c(90, 180))


Interpret a fitted survival learner

Description

A single entry point to the package's model-agnostic explanation and diagnostic methods. listmethods() lists the method ids with a one-line description of each.

Usage

interpret(fit, method, newdata = NULL, data = NULL, times = NULL, ...)

Arguments

fit

A survalis_fit (from fit()), a survalis_tune, or a legacy mlsurv_model.

method

One of "pdp", "ice", "ale", "shap", "shap_matrix", "varimp" / "permute", "interaction", "counterfactual", "surrogate", "tree_surrogate", "calibration", "roc", "dca", "survcluster".

newdata

Instances to explain (SHAP, ALE, counterfactual, surrogate, survcluster) or to assess (roc, dca; these need the outcome columns). Defaults to data, so roc and dca are apparent unless you pass held-out data.

data

Background data for the method. Defaults to the training data.

times

Evaluation grid; NULL picks one from the data quantiles. Methods that need a single time (calibration, roc, dca) take eval_time in ... or the sole times value.

...

Method-specific arguments, e.g. feature (pdp / ice / ale), target_time (counterfactual, surrogate), eval_time (calibration, roc, dca), thresholds (dca), K (survcluster). For "shap", the aggregation over time ("meanabs" or "integral") is passed as shap_method.

Value

A survalis_interpret object holding the method's result, with method, times, and a plot() method. as.data.frame() returns the result frame. For "survcluster" it is in long format, one row per observation and time: id (row of newdata), cluster, medoid (TRUE for the cluster's medoid curve), time and predicted survival surv. For "roc" and "dca" it is the curve with an eval_time column (and auc for "roc"); for "tree_surrogate" one row per time and feature with the number of splits on it and the tree's r_squared. For "pdp" / "ice" it is one row per curve point (feature, feature_value, id (NA on PDP rows), time, surv, type), for "ale" one row per feature value and time (feature, feature_value, time, ale), and for "calibration" the binned table with its eval_time. For "pdp" and "ale", as.data.frame(x, what = "integrated") returns the curve integrated over time instead (feature, feature_value, integrated).

See Also

For scalar summaries use evaluate(): metrics = "ece" summarises calibration and metrics = "auc" summarises the ROC curve.

Examples


m <- fit(Surv(time, status) ~ age + karno + celltype, veteran, "ranger",
         spec = list(num.trees = 100))
interpret(m, "pdp", feature = "karno", times = c(90, 180))
interpret(m, "varimp", times = c(90, 180))
plot(interpret(m, "roc", eval_time = 180))


Kaplan-Meier curves with a number-at-risk table

Description

Draws Kaplan-Meier curves in a publication style: step curves with censoring marks, the legend inside the panel, an optional log-rank p-value, and a number-at-risk table coloured by group under the plot. Besides the survival function, it draws the risk function, the cumulative hazard, the complementary log-log diagnostic of proportional hazards, and cumulative incidence under competing risks. A fitted survalis model adds its predicted or covariate-adjusted curves, dashed, over the Kaplan-Meier curves.

Usage

kmplot(
  formula,
  data,
  type = c("survival", "risk", "cumhaz", "cloglog"),
  event = NULL,
  model = NULL,
  model.curve = c("predicted", "adjusted"),
  title = NULL,
  xlab = "Time",
  ylab = NULL,
  breaks = NULL,
  pval = TRUE,
  risk.table = TRUE,
  conf.int = FALSE,
  colors = NULL,
  legend.pos = NULL,
  pval.pos = NULL,
  base_size = 10
)

Arguments

formula

A survival formula such as Surv(time, status) ~ 1 or Surv(time, status) ~ group, as in survival::survfit(). When the status is a factor whose first level is censoring and whose other levels are competing events, the plot shows the Aalen-Johansen cumulative incidence of event.

data

A data frame holding the variables in formula.

type

What to draw: "survival" (default) for S(t), "risk" for 1 - S(t), "cumhaz" for the cumulative hazard H(t) = -\log S(t), or "cloglog" for \log\{-\log S(t)\} against \log t, where parallel curves support proportional hazards. Competing-risks data take "risk" only, which is also their default.

event

Competing-risks data only: the event whose cumulative incidence is drawn. NULL (default) takes the first event level.

model

Optional fitted model from fit(). Its curves are drawn dashed over the Kaplan-Meier curves, so that the two can be compared. Not available for competing-risks data.

model.curve

"predicted" (default) averages the predicted survival of the subjects in each group, a calibration check of the model. "adjusted" sets the single grouping variable to each of its levels for every subject and averages the predictions (G-computation), which gives covariate-adjusted curves.

title

Panel title, for example "A. Overall survival". NULL (default) draws none.

xlab, ylab

Axis labels. ylab = NULL uses a label for type.

breaks

Time breaks for the x axis and the risk table. NULL (default) uses pretty() over the follow-up.

pval

Log-rank p-value when there are two or more groups: TRUE (default) computes it with survival::survdiff(), FALSE omits it, and a character string is printed as given. It is not computed for competing-risks data or for type = "cloglog", where only a string is printed.

risk.table

Logical; draw the number-at-risk table (default TRUE). Needs the 'patchwork' package. Not drawn for type = "cloglog", whose time axis is on the log scale.

conf.int

Logical; draw pointwise 95% confidence bands (default FALSE).

colors

Group colours. NULL (default) uses blue and red for the first two groups, then further distinct colours.

legend.pos

Legend position inside the panel, c(x, y) in ⁠[0, 1]⁠ units of the panel. NULL (default) chooses a corner away from the curves.

pval.pos

P-value position, c(time, y) in data units. NULL (default) chooses a corner away from the curves.

base_size

Base font size (default 10).

Value

A 'patchwork' object (curve over risk table) when the risk table is drawn, otherwise a 'ggplot2' object. Panels combine with patchwork operators, for example kmplot(...) | kmplot(...).

Examples

kmplot(Surv(time, status) ~ trt, data = veteran, risk.table = FALSE)
kmplot(Surv(time, status) ~ trt, data = veteran, type = "cloglog")

# Competing risks: progression versus death without progression
d <- survival::mgus2
d$etime <- ifelse(d$pstat == 1, d$ptime, d$futime)
d$event <- factor(ifelse(d$pstat == 1, 1, 2 * d$death), 0:2,
                  c("censored", "progression", "death"))
kmplot(Surv(etime, event) ~ sex, data = d, risk.table = FALSE)

if (requireNamespace("patchwork", quietly = TRUE)) {
  kmplot(Surv(time, status) ~ trt, data = veteran,
         title = "A. Overall survival", xlab = "Days",
         breaks = seq(0, 1000, 200))

  # Covariate-adjusted curves from a Cox model, over the Kaplan-Meier curves
  m <- fit(Surv(time, status) ~ trt + karno + age, data = veteran,
           model = "coxph")
  kmplot(Surv(time, status) ~ trt, data = veteran, model = m,
         model.curve = "adjusted", breaks = seq(0, 1000, 200))
}


List the survival learners

Description

The ids accepted by the model argument of fit(), evaluate(), compare(), tune() and estimate().

Usage

listlearners(has_tune = FALSE)

Arguments

has_tune

Logical (default FALSE). If TRUE, return only learners with a built-in tuning grid, so that tune() works without a grid.

Value

A data frame with columns learner (the id) and tunable (whether a built-in tuning grid exists). A learner with tunable = FALSE (for example "fastgbm") can still be tuned by passing a grid to tune().

Examples

listlearners()
listlearners(has_tune = TRUE)

List the interpretability methods

Description

The ids accepted by the method argument of interpret().

Usage

listmethods()

Value

A data frame with columns method and summary.

Examples

listmethods()

List the evaluation metrics

Description

The ids accepted by the metrics argument of evaluate() and compare() and the metric argument of tune().

Usage

listmetrics()

Details

Columns:

"range:<lower>-<upper>" is a template: write the thresholds into the id, for example "range:0.05-0.20".

Value

A data.frame with one row per metric.

Examples

listmetrics()

Predict from a fitted survival learner

Description

Produces predictions from a fit() object. Every matrix-valued type returns a survmat (an ⁠n x length(times)⁠ numeric matrix carrying its grid in the "times" attribute); the scalar-valued types ("rmst", "quantile", "median") return a numeric vector (or an ⁠n x length(probs)⁠ matrix for a vector probs).

Usage

## S3 method for class 'survalis_fit'
predict(
  object,
  newdata = NULL,
  times = NULL,
  type = c("survival", "risk", "chf", "hazard", "rmst", "quantile", "median"),
  ...
)

Arguments

object

A survalis_fit from fit().

newdata

A data frame of predictors. Defaults to the training data. Rows with missing values in model variables are rejected.

times

Numeric evaluation grid. When NULL, up to 50 event-time quantiles between the 5th and 95th percentiles are used and recorded on the result.

type

One of:

"survival"

predicted survival probability S(t) (default).

"risk"

cumulative risk 1 - S(t).

"chf"

cumulative hazard -\log S(t).

"hazard"

piecewise-constant hazard on the grid.

"rmst"

restricted mean survival time; upper limit tau (...), default max(times).

"quantile"

survival-time quantile(s); requires probs (...).

"median"

median survival time (probs = 0.5).

...

Type-specific arguments: tau for "rmst", probs for "quantile".

Value

A survmat for "survival", "risk", "chf", "hazard"; a numeric vector for "rmst", "median" and scalar-probs "quantile"; an ⁠n x length(probs)⁠ matrix for vector probs.

Examples


m <- fit(Surv(time, status) ~ age + karno, veteran, "ranger",
         spec = list(num.trees = 100))
predict(m, veteran[1:5, ], times = c(90, 180, 365))
predict(m, veteran[1:5, ], times = c(90, 180, 365), type = "risk")
predict(m, veteran[1:5, ], type = "rmst", tau = 365)


Re-export Surv from survival

Description

These objects are imported from other packages. Follow the links below to see their documentation.

survival

Surv

Examples

Surv(c(5, 8, 12), c(1, 0, 1))

Resampling specifications for evaluate(), tune() and compare()

Description

These constructors return a lightweight survalis_resampling description that the verb-level functions turn into concrete train/test splits. They do not touch the data themselves, so the same specification can be reused across models to obtain paired comparisons (identical folds via a shared seed).

Usage

cv(v = 5, repeats = 1, strata = "status", seed = NULL)

holdout(prop = 0.75, strata = "status", seed = NULL)

groupcv(group, v = 5, seed = NULL)

bootstrap(times = 25, seed = NULL)

Arguments

v

Integer number of folds (default 5).

repeats

Integer number of times the whole v-fold scheme is repeated with fresh random folds (default 1).

strata

Column name to stratify on, or NA to disable stratification. The default sentinel "status" means "use the event-indicator column of the model formula", whatever its name (for example cens in Surv(time, cens) ~ .). Any other name must be a column of the data; otherwise a warning is given and the resampling is not stratified.

seed

Optional integer seed. Set it (and keep it equal across models) for paired resampling.

prop

Proportion of rows assigned to the training set in holdout() (default 0.75).

group

Column name whose distinct values define the groups held out together in groupcv().

times

Integer number of bootstrap resamples (default 25).

Details

Value

An object of class survalis_resampling.

Examples

cv(5)
cv(v = 10, repeats = 3, seed = 1)
holdout(prop = 0.8)
bootstrap(times = 50)

The survmat prediction contract

Description

Internal constructor and validator for survmat, the single prediction container used by the verb-level predict() method. A survmat is an ⁠n x length(times)⁠ numeric matrix carrying its evaluation grid in the "times" attribute (column names "t=<time>" are kept for display and for backward compatibility with the ⁠*_survmat⁠ metric helpers) and the predicted quantity in the "quantity" attribute.

Usage

new_survmat(x, times, quantity = "survival")

validate_survmat(x)

as_survmat(x, times, quantity = "survival")

Arguments

x

A numeric matrix (or data frame / vector coercible to one) of predictions, one row per observation and one column per entry of times.

times

Numeric vector, strictly increasing and non-negative, of length ncol(x).

quantity

One of "survival", "risk", "chf", "hazard". Controls printing and the ⁠[0, 1]⁠ range check (applied to "survival" and "risk" only).

Details

new_survmat() builds the object without checks; validate_survmat() enforces the contract; as_survmat() is the checked constructor used internally after a learner's raw ⁠predict_*()⁠ output has been finalised.

Value

A survmat: a numeric matrix with class c("survmat", "matrix", "array"), attribute "times", and attribute "quantity".


Evaluation grid of a survmat

Description

Returns the numeric time grid of a survmat, reading the "times" attribute and falling back to parsing "t=<time>" column names for objects produced by the legacy ⁠predict_*()⁠ path.

Usage

survmat_times(x)

Arguments

x

A survmat or a legacy survival-probability matrix / data frame.

Value

A numeric vector.


Tune a survival learner over a hyperparameter grid

Description

Grid search over resampling folds. Learners listed by listlearners(has_tune = TRUE) have a built-in default grid; any other learner, including a custom one (see fit()), is tuned over the grid you supply, with every configuration scored on the same splits. The best configuration is refit on the full data and returned as a ready-to-use survalis_fit.

Usage

tune(
  formula,
  data,
  model,
  grid = NULL,
  times = NULL,
  resampling = cv(),
  metric = "ibs",
  ncores = 1,
  ...
)

Arguments

formula

A survival formula.

data

A data frame.

model

A learner id (listlearners()$learner) or a custom learner.

grid

A data frame of hyperparameter combinations (one row per configuration, for example from expand.grid()). NULL uses the learner's built-in default grid, if it has one. A grid need not name every tuned parameter: parameters it leaves out take the learner's default values (those of fit() with no spec).

times

Evaluation grid; NULL picks one from the data quantiles.

resampling

A resampling spec. The built-in tuner uses cv() only (its v and seed drive the inner cross-validation). It runs when grid is NULL or has exactly the columns of the built-in grid; any other grid is scored on shared splits and accepts any spec.

metric

A single metric to optimise (default "ibs").

ncores

Cores for the inner CV (default 1).

...

Fixed learner arguments: forwarded to the built-in tuner, or added to every configuration of a supplied grid.

Value

A survalis_tune object: fit (the refit survalis_fit at the best configuration), results (the scored grid), best (one-row best config), times, resampling, metric, model.

Examples


tuned <- tune(Surv(time, status) ~ age + karno, veteran, "ranger",
              grid = expand.grid(num.trees = c(100, 300), mtry = 2, min.node.size = c(5, 15)),
              times = c(90, 180), resampling = cv(3), metric = "ibs")
tuned
predict(tuned, veteran[1:3, ], times = c(90, 180))


Veteran's Administration Lung Cancer Trial Data

Description

This is the veteran dataset originally from the survival package, containing data from a randomized trial of lung cancer treatments.

Usage

veteran

Format

A data frame with 137 observations and 8 variables:

trt

Treatment: 1=standard, 2=test

celltype

Cell type: squamous, smallcell, adeno, large

time

Survival time in days

status

Censoring status: 1=dead, 0=alive

karno

Karnofsky performance score (higher = better)

diagtime

Months from diagnosis to randomization

age

Age in years

prior

Prior therapy: 0=no, 10=yes

Source

survival package, originally from Kalbfleisch and Prentice (1980) The Statistical Analysis of Failure Time Data.

Examples

head(veteran)
summary(veteran$time)