Skip to contents

Trains a supervised classifier predicting either the sighting category or the therapy concept of a casualty from its card, through a full tidymodels path: a preprocessing recipe, stratified cross-validation, a tuning grid, and a final fit evaluated on a held-out split.

Usage

sdt_fit(
  data,
  target = c("triage", "therapy", "called_triage", "called_therapy"),
  engine = NULL,
  predictors = NULL,
  tune = TRUE,
  grid = 5L,
  folds = 5L,
  prop = 0.75,
  parallel = FALSE,
  workers = NULL,
  seed = NULL,
  verbose = TRUE
)

Arguments

data

A cohort or data frame of casualties.

target

Outcome to predict. "triage" and "therapy" are the true values on the card; "called_triage" and "called_therapy" are what a participant assigned, as produced by sdt_add_decisions(). The true values are close to a deterministic function of the card, so every engine scores near 1 on them and the comparison is uninformative. The participant's call is the target worth benchmarking.

engine

One of sdt_engines(). "multinom", "glmnet" and "tree" are glass-box; "mars" and "xgboost" are grey-box; "ranger", "svm_rbf" and "mlp" are black-box.

predictors

Character vector of predictor columns. Defaults to the card fields, excluding identifiers and every outcome column, so a model for the participant's call cannot simply read the true category.

tune

If TRUE, tune hyperparameters over a grid; if FALSE, fit the specification's defaults once.

grid

Number of candidate hyperparameter settings.

folds

Number of cross-validation folds, stratified on the target.

prop

Proportion of the data used for training in the initial split.

parallel

If TRUE, fit resamples and grid points in parallel. Defaults to FALSE: a package that claims every core without being asked is a bad guest on a shared machine, and a sequential run needs no thought about RNG streams. It pays off on a tuned fit or an engine screen and costs a little on a single untuned one. The backend is set up for the duration of the call and the caller's plan is restored on exit.

workers

Number of parallel workers, or NULL to choose one: a core short of the machine, capped at 8.

seed

Optional integer seed, applied with withr::local_seed().

verbose

If TRUE, report progress via cli.

Value

An S3 object of class sdt_model with elements workflow, engine, target, predictors, class_box, split, resamples, tuning, best and metrics.

Details

The returned object keeps the split, the resamples and the tuning results, not only the fitted workflow, so the numbers reported later can be traced back to what produced them.

See also

sdt_workflowset() to screen every engine at once, sdt_metrics(), sdt_roc(), sdt_explain().

Examples

co <- sdt_synthetic_cohort(n = 200, seed = 3)
needed <- c("parsnip", "recipes", "workflows", "rsample", "tune",
            "yardstick", "rpart")
if (all(vapply(needed, requireNamespace, logical(1), quietly = TRUE))) {
  fit <- sdt_fit(co, target = "triage", engine = "tree",
                 tune = FALSE, seed = 1, verbose = FALSE)
  fit
  sdt_metrics(fit)
}
#> # A tibble: 4 × 3
#>   .metric  .estimator .estimate
#>   <chr>    <chr>          <dbl>
#> 1 accuracy multiclass         1
#> 2 f_meas   macro              1
#> 3 kap      multiclass         1
#> 4 roc_auc  hand_till          1