# Umbral de Youden por modelo con las predicciones fuera de muestra de la CV
# (nunca con 2025). Se calcula aqui porque los objetos *_tuned no se guardan.
youden_threshold <- function(tuned, pars_fin = NULL, pos = "presence", n_grid = 501) {
preds <- if (is.null(pars_fin)) tune::collect_predictions(tuned) else
tune::collect_predictions(tuned, parameters = pars_fin)
prob <- preds[[paste0(".pred_", pos)]]
y <- as.integer(as.character(preds$class) == pos)
grid <- unique(stats::quantile(prob, probs = seq(0, 1, length.out = n_grid), names = FALSE))
j <- vapply(grid, function(th) {
pred <- prob >= th
mean(pred[y == 1]) + mean(!pred[y == 0]) - 1
}, numeric(1))
stats::median(grid[j >= max(j) - 1e-9])
}
metricas_cv_fold <- function(tuned, pars_fin = NULL, ml_title, threshold = NULL, pos = "presence") {
preds <- if (is.null(pars_fin)) tune::collect_predictions(tuned) else
tune::collect_predictions(tuned, parameters = pars_fin)
id_cols <- intersect(c("id", "id2"), names(preds))
preds |>
dplyr::group_by(dplyr::across(dplyr::all_of(id_cols))) |>
dplyr::group_modify(~ {
prob <- .x[[paste0(".pred_", pos)]]
y <- as.character(.x$class) == pos
auc <- yardstick::roc_auc_vec(.x$class, prob, event_level = "first")
s05 <- mean(prob[y] >= 0.5); e05 <- mean(prob[!y] < 0.5)
out <- tibble::tibble(.metric = c("auc", "sens_0.5", "spec_0.5", "TSS_0.5"),
.estimate = c(auc, s05, e05, s05 + e05 - 1))
if (!is.null(threshold)) {
sy <- mean(prob[y] >= threshold); ey <- mean(prob[!y] < threshold)
out <- dplyr::bind_rows(out, tibble::tibble(
.metric = c("sens_youden", "spec_youden", "TSS_youden"),
.estimate = c(sy, ey, sy + ey - 1)))
}
out
}) |>
dplyr::ungroup() |>
dplyr::mutate(modelo = ml_title, umbral = if (is.null(threshold)) NA_real_ else threshold)
}
tuned_models <- list(
"MaxNet" = list(tuned = maxnet_tuned, pars = maxnet_pars_fin),
"MaxNet (wdb)" = list(tuned = maxnet_tuned_wdb, pars = maxnet_pars_fin_wdb),
"Logistic Regression" = list(tuned = lr_tuned, pars = NULL),
"Logistic Regression (wdb)" = list(tuned = lr_tuned_wdb, pars = NULL),
"LRE" = list(tuned = lre_tuned, pars = lre_pars_fin),
"LRE (wdb)" = list(tuned = lre_tuned_wdb, pars = lre_pars_fin_wdb),
"GAM" = list(tuned = gam_tuned, pars = gam_pars_fin),
"GAM (wdb)" = list(tuned = gam_tuned_wdb, pars = gam_pars_fin_wdb),
"MARS" = list(tuned = mars_tuned, pars = mars_pars_fin),
"MARS (wdb)" = list(tuned = mars_tuned_wdb, pars = mars_pars_fin_wdb),
"Naive Bayes" = list(tuned = nbay_tuned, pars = nbay_pars_fin),
"Naive Bayes (wdb)" = list(tuned = nbay_tuned_wdb, pars = nbay_pars_fin_wdb),
"KNN" = list(tuned = knn_tuned, pars = knn_pars_fin),
"KNN (wdb)" = list(tuned = knn_tuned_wdb, pars = knn_pars_fin_wdb),
"Decision Tree" = list(tuned = dt_tuned, pars = dt_pars_fin),
"Decision Tree (wdb)" = list(tuned = dt_tuned_wdb, pars = dt_pars_fin_wdb),
"Random Forest" = list(tuned = rf_tuned, pars = rf_pars_fin),
"Random Forest (wdb)" = list(tuned = rf_tuned_wdb, pars = rf_pars_fin_wdb),
"LightGBM" = list(tuned = lgbm_tuned, pars = lgbm_pars_fin),
"LightGBM (wdb)" = list(tuned = lgbm_tuned_wdb, pars = lgbm_pars_fin_wdb),
"XGBoost" = list(tuned = xgb_tuned, pars = xgb_pars_fin),
"XGBoost (wdb)" = list(tuned = xgb_tuned_wdb, pars = xgb_pars_fin_wdb),
"MLP" = list(tuned = mlp_tuned, pars = mlp_pars_fin),
"MLP (wdb)" = list(tuned = mlp_tuned_wdb, pars = mlp_pars_fin_wdb)
)
# Un modelo que falle no detiene al resto (queda NA)
umbrales <- purrr::map_dbl(tuned_models, function(x)
tryCatch(youden_threshold(x$tuned, x$pars), error = function(e) NA_real_))
umbrales
metricas_cv_long <- purrr::imap_dfr(tuned_models, function(x, nombre)
tryCatch(metricas_cv_fold(x$tuned, x$pars, nombre,
threshold = if (is.na(umbrales[[nombre]])) NULL else umbrales[[nombre]]),
error = function(e) NULL))