## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4,
  dpi = 110
)

## ----data-spec----------------------------------------------------------------
library(uniml)

data <- read.csv(
  system.file("extdata", "uniml-cvd-mortality.csv", package = "uniml")
)
set.seed(123)
data <- data[1:400, ]

spec <- uniml_spec(
  task_type = "binary",
  outcome = "died",
  positive = "yes",
  id = "id"
)

data <- as_uniml_data(data, spec)
names(data)

## ----preprocessing------------------------------------------------------------
spec <- set_preprocess(
  spec,
  impute = "median_mode",
  scale = "zscore",
  encode = "onehot"
)

preprocessor <- fit_preprocessor(data, spec = spec)
processed <- apply_preprocessor(preprocessor, data$data)

dim(processed)
head(names(processed), 8)

## ----selection----------------------------------------------------------------
spec <- set_feature_selection(
  spec,
  methods = c("univariate", "lasso"),
  combine = "intersect",
  p_threshold = 0.2,
  cor_threshold = 0.9
)

selection <- select_features(processed, spec)
selection$selected_features
head(selection$details, 4)

## ----training-----------------------------------------------------------------
features <- intersect(selection$selected_features, names(processed))
task <- make_task(processed[, c(features, "died"), drop = FALSE], spec)

spec <- set_learners(spec, "cart", tune = TRUE)
learners <- resolve_learners(spec, task = task, n_train = task$nrow)

tuned <- tune_model(
  task,
  learners[["cart"]],
  resampling = mlr3::rsmp("cv", folds = 3),
  tuner = "random_search",
  terminator = mlr3tuning::trm("evals", n_evals = 4),
  seed = 123
)

tuned$status
tuned$best_params
model <- train_model(task, tuned$learner, params = tuned$best_params)
model

## ----metrics------------------------------------------------------------------
prediction <- model$predict(task)
metrics <- calc_metrics(prediction, threshold = 0.5)
unlist(metrics[c("auc", "accuracy", "sensitivity", "specificity", "brier")])

ci <- calc_ci(
  prediction$prob[, "yes"],
  prediction$truth,
  method = "bootstrap",
  R = 50,
  threshold = 0.5,
  positive = "yes",
  seed = 123
)
ci[, c("metric", "mean", "lower", "upper")]

## ----calibration--------------------------------------------------------------
calibrated <- calibrate_probabilities(
  prediction$prob[, "yes"],
  labels = prediction$truth,
  method = "platt",
  positive = "yes"
)
calibrated$method
calibrated$metrics_before
calibrated$metrics_after

## ----roc, fig.cap = "ROC curve of a single mlr3 prediction."------------------
plot_roc(prediction)

## ----confusion, fig.cap = "Confusion matrix at a fixed threshold."------------
plot_confusion(prediction, threshold = 0.5)

