fit: Fit a survival learner

View source: R/verb-fit.R

fitR Documentation

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))


survalis documentation built on Nov. 14, 2026, 5:10 p.m.