## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4
)

## ----setup--------------------------------------------------------------------
library(triageR)
library(mlbench)

## -----------------------------------------------------------------------------
data(BreastCancer)
bc <- BreastCancer

score_cols <- c("Cl.thickness", "Cell.size", "Cell.shape", "Marg.adhesion",
                 "Epith.c.size", "Bare.nuclei", "Bl.cromatin",
                 "Normal.nucleoli", "Mitoses")
bc[score_cols] <- lapply(bc[score_cols], function(x) as.numeric(as.character(x)))

bc_loaded <- tr_load_clinical(bc, id_col = "Id")

## -----------------------------------------------------------------------------
tr_check_missing(bc_loaded)

## ----message = FALSE, results = "hide"----------------------------------------
bc_for_impute <- bc_loaded[, setdiff(names(bc_loaded), "Id")]
bc_imputed <- tr_impute(bc_for_impute, method = "mice", m = 5)

## -----------------------------------------------------------------------------
set.seed(7)
n <- nrow(bc_imputed)
train_idx <- sample(seq_len(n), size = floor(0.7 * n))

bc_train <- bc_imputed[train_idx, ]
bc_test  <- bc_imputed[-train_idx, ]

bc_model_glm <- tr_fit(bc_train, outcome = "Class", engine = "logistic_reg")
bc_model_rf  <- tr_fit(bc_train, outcome = "Class", engine = "random_forest")
bc_model_xgb <- tr_fit(bc_train, outcome = "Class", engine = "boost_tree")

## -----------------------------------------------------------------------------
bc_val_glm <- tr_validate(bc_model_glm, newdata = bc_test)
bc_val_rf  <- tr_validate(bc_model_rf, newdata = bc_test)
bc_val_xgb <- tr_validate(bc_model_xgb, newdata = bc_test)

## -----------------------------------------------------------------------------
bc_comparison <- dplyr::bind_rows(
  dplyr::mutate(bc_val_glm$metrics, engine = "logistic_reg"),
  dplyr::mutate(bc_val_rf$metrics, engine = "random_forest"),
  dplyr::mutate(bc_val_xgb$metrics, engine = "boost_tree")
)

tidyr::pivot_wider(bc_comparison, names_from = .metric, values_from = .estimate)

## -----------------------------------------------------------------------------
bc_val_glm$roc_plot

## -----------------------------------------------------------------------------
tr_explain(bc_model_glm, method = "permutation")

## -----------------------------------------------------------------------------
bc_review <- tr_agent_review(bc_train, bc_model_glm, use_agent = FALSE)

## -----------------------------------------------------------------------------
bc_sensitivity <- tr_sensitivity(bc_train, bc_model_glm)

