---
title: "Ecological Niche Model of Confirmed Imported Dengue Cases in the Metropolitan Area of Mexico City using Machine Learning (2023-2024)"
format: 
  html:
      self-contained: true
      theme: flatly
      code-fold: true
      toc: true
      toc-depth: 4
      number-sections: true
title-block-banner: true
editor: visual
#bibliography: references.bib
vignette: >
  %\VignetteIndexEntry{Machine-learning workflow: dengue cases}
  %\VignetteEngine{quarto::html}
  %\VignetteEncoding{UTF-8}
execute:
  eval: false
---

## Objective

Predict the probability of the presence of confirmed imported dengue cases in the Mexico City Metropolitan Area using Machine Learning

## Load the pkg

```{r setup, warning=FALSE, message=FALSE, error=TRUE}
## R
library(tidyverse) 
library(magrittr) 
library(tidymodels) 
library(skimr) 
library(bonsai) 
## Balanceo
library(themis) 
library(parallel)
library(doParallel)
library(discrim)
library(kknn)
source("metrics_class.R")
load("dengue_2025_layers_sfa_2k.RData")
data_2025 <- data_lyr; rm(data_lyr)
data_2025 <- data_2025 |>
  dplyr::select(-temperatura,  -tmin, -elevation)
```

## Load the dengue dataset and the area of interes

```{r load_dataset, warning=FALSE, message=FALSE, error=TRUE}

# Step 1.1 load the dataset ####
load("dengue_2023_2024_layers_sfa_2k.RData")
data <- data_lyr; rm(data_lyr)
data <- data |>
  dplyr::select(-temperatura, -tmin, -elevation)
# Step 1.2 load the area of interes ####
load("aoi.RData")
aoi <- mzmv; rm(mzmv)
```

### map of the study area and the dataset

```{r map_dengue}
#| warning: False
#| message: False
#| echo: true
#| output: true
#| out.width: "100%"
library(mapgl)
maplibre(style = carto_style("positron"),
         zoom = 10,        # Initial zoom level
  minZoom = 5,      # Prevent zooming out too far
  maxZoom = 18,
         bounds = data) |>
    add_circle_layer(id = "dengue",
                     source = data,
                     circle_color = match_expr(
                         column = "class",
                         values = c("presence", "pseudoabs"), # reemplaza por tus valores
                         stops = c("#E01A59", "#0F9D58")),
                     circle_stroke_color = "white", # color = "white"
                     circle_stroke_width = 1, # lwd = 0.5
                     circle_radius = 5) |>
    #    Capa 2 — polígono aoi (sin leyenda)
    add_fill_layer(id = "aoi",
                   source = aoi,
                   fill_color = "#ECB32D",
                   fill_opacity = 0.3, # ajusta al gusto
                   fill_outline_color = "black") |>
    add_line_layer(id           = "aoi-borde",
                   source       = aoi,      # misma fuente
                   line_color   = "black",  # color = "black"
                   line_width   = 2,        # lwd = 2
                   line_opacity = 1) |>
    add_categorical_legend(legend_title = "Dengue",
                           position = "bottom-left",
                           values = c("presence", "pseudoabs"), 
                           colors = c("#E01A59", "#0F9D58")) |>
    add_fullscreen_control(position = "top-left") |> 
    add_navigation_control()
```

## Exploratory Data Analysis

### glimpse of your data

```{r glimse_data,  warning=FALSE, message=FALSE, error=TRUE }
data |>
    dplyr::glimpse()
```

### Univariate EDA

```{r uni_EDA,  warning=FALSE, message=FALSE, }
# EDA Univariado ####
skimr::skim(data |> sf::st_drop_geometry())
```

```{r}
gtExtras::gt_plt_summary(data |> sf::st_drop_geometry())
```

### Multivariate EDA

```{r multi_EDA}
library(corrplot)
library(ggplot2)
#library(dplyr)
data |>
    sf::st_drop_geometry() |>
  dplyr::select_if(is.numeric) |>
  cor() |>
  corrplot(method= "number", 
           type="upper",
           order="hclust", 
           tl.col="black")
```

![](corrmat_data_dengue_2023_2024_sfa.png)

```{r}
library(see) # for plotting
library(ggraph) # needs to be loaded

dat <- data |> 
    sf::st_drop_geometry() |>
    dplyr::select_if(is.numeric) 
plot(correlation::correlation(dat,
                              partial = TRUE)) +
    ggraph::scale_edge_color_continuous(low = "#E01A59", 
                                        high = "#0F9D58")
```

### Response Variable

#### Table

```{r}
# Balanceo
data |>
    sf::st_drop_geometry() |>
    dplyr::group_by(class) |> 
    dplyr::count(name = 'frec') |>
    dplyr::ungroup() |>
    dplyr::mutate( Porc= frec/sum(frec)) |>
    gt::gt()
```

#### Graph

```{r}
data |>
    sf::st_drop_geometry() |>
    dplyr::group_by(class) |> 
    dplyr::count(name = 'frec') |>
    dplyr::ungroup() |>
    dplyr::mutate(Porc= frec/sum(frec)) |>
    ggplot2::ggplot(ggplot2::aes(x= class, 
                                 y= Porc)) +
    ggplot2::geom_segment(ggplot2::aes(xend = class, 
                                       y = 0, 
                                       yend=Porc), 
                          color= c("#0F9D58","#E01A59"), 
                          linewidth= 1) +
    ggplot2::geom_point(size=5, 
                        color= c("#0F9D58","#E01A59")) +
    ggplot2::coord_flip() +
    ggplot2::scale_y_continuous(labels = scales::percent_format()) +
    ggplot2::labs(title= 'Porcentaje de Registros de casos de dengue', 
                  y = "Porcentaje", 
                  x = "") +
    ggplot2::theme_bw()
```

## Modeling

### Train-Test Split

Initial split

```{r train_test, warning=FALSE, message=FALSE, error=TRUE}
# . Train-Test Split ####
set.seed(12345) # Semilla para aleatorios
split <- data |>
    rsample::initial_split(prop = 0.8, 
                           strata = class)
```

Train

```{r}
train <- rsample::training(split)
dim(train)
```

Test

```{r}
test <- rsample::testing(split)
dim(test)
```

### Preprocessing

#### Without Balance

```{r preprocesamiento, warning=FALSE, message=FALSE,error=TRUE }
receta <- train |>
    sf::st_drop_geometry() |>
    #dplyr::select(class, dplyr::starts_with("bio")) |>
    recipes::recipe(class ~ . ) |>## Crea la receta 
    ## Eliminar variables que no usaremos
    # step_rm() |>
    ## Crear nuevas variables (insight desde el EDA)
    #recipes::step_mutate(temperature = temperature/1000) |>
    ## Imputar los datos 
    # step_impute_mean()
    recipes::step_impute_knn(recipes::all_predictors() ) |>
    ## Estandarizacion/Normalizacion de numericas
    recipes::step_normalize(recipes::all_numeric(),
                            -recipes::all_outcomes()) |>
    ## Crear una categoría "otros" que agrupe a categorias pequeñas
    recipes::step_other(recipes::all_nominal(), 
                        -recipes::all_outcomes() , 
                        threshold = 0.07, 
                        other = "otros") |>
    ## Crear una categoría "new" para observaciones con labels "no muestreados"
    recipes::step_novel(recipes::all_nominal(), 
                        -recipes::all_outcomes(), 
                        new_level = "new") |>
    ## Crear variables indicadoras para cada categoría
    recipes::step_dummy(recipes::all_nominal(), 
                        -recipes::all_outcomes()) |>
  #recipes::step_pca(recipes::all_numeric(), threshold = 0.8) |>
    ## Eliminar automáticamente variables con alta correlacion 
    ## para evitar la multicolinealidad xi ~ xj
    recipes::step_corr(recipes::all_numeric(),
                       -recipes::all_outcomes(), 
                       threshold = 0.80) |>
    recipes::step_nzv(all_predictors())
```

```{r receta, warning=FALSE, message=FALSE, }
receta 
receta  |>
    recipes::prep() |>
    recipes::bake(new_data = NULL) |>
    dplyr::glimpse()
```

#### Balance using Upsampling

```{r preprocesamiento_upsampling, warning=FALSE, message=FALSE,error=TRUE }
receta_upsamping <- train |>
    sf::st_drop_geometry() |>
    #dplyr::select(class, dplyr::starts_with("bio")) |>
    recipes::recipe(class ~ . ) |>## Crea la receta 
    ## Eliminar variables que no usaremos
    # step_rm() |>
    ## Crear nuevas variables (insight desde el EDA)
    #recipes::step_mutate(temperature = temperature/1000) |>
    ## Imputar los datos 
    # step_impute_mean()
    recipes::step_impute_knn(recipes::all_predictors() ) |>
    ## Estandarizacion/Normalizacion de numericas
    recipes::step_normalize(recipes::all_numeric(),
                            -recipes::all_outcomes()) |>
    ## Crear una categoría "otros" que agrupe a categorias pequeñas
    recipes::step_other(recipes::all_nominal(), 
                        -recipes::all_outcomes() , 
                        threshold = 0.07, 
                        other = "otros") |>
    ## Crear una categoría "new" para observaciones con labels "no muestreados"
    recipes::step_novel(recipes::all_nominal(), 
                        -recipes::all_outcomes(), 
                        new_level = "new") |>
    ## Crear variables indicadoras para cada categoría
    recipes::step_dummy(recipes::all_nominal(), 
                        -recipes::all_outcomes()) |>
  #recipes::step_pca(recipes::all_numeric(), threshold = 0.8) |>
    ## Eliminar automáticamente variables con alta correlacion 
    ## para evitar la multicolinealidad xi ~ xj
    recipes::step_corr(recipes::all_numeric(),
                       -recipes::all_outcomes(), 
                       threshold = 0.80) |>
    recipes::step_nzv(all_predictors()) |> # near zero variance
    ## Balancear usando upsampling
    ## over_ratio implica que vamos a llevar a la clase minoritaria a 
    ## alrededor de 90% de filas que la mayoritaria
    themis::step_upsample(class, 
                          over_ratio= 0.90, 
                          skip= TRUE, 
                          seed = 345)
```

```{r receta_upsampling, warning=FALSE, message=FALSE, error=TRUE}
receta_upsamping 

receta_upsamping  |>
    recipes::prep() |>
    recipes::bake(new_data = NULL) |>
    dplyr::glimpse()
```

#### Balance with importance_weights

```{r balanceo_iw, warning=FALSE, message=FALSE, error=TRUE}
# peso de las presencias = razon de clases del conjunto de entrenamiento
n_pres <- sum(train$class == "presence")
n_abs  <- sum(train$class == "pseudoabs")
w_pres <- n_abs / n_pres
w_pres

train_iw <- train |> 
    dplyr::mutate(## crear la variable con los pesos
        case_wts = ifelse(class == "presence", w_pres, 1),
        ## crea el vector de importancia ponderada
        case_wts = recipes::importance_weights(case_wts))
```

```{r preprocesamiento_iw, warning=FALSE, message=FALSE, }
receta_iw <- train_iw |>
  sf::st_drop_geometry() |>
  recipes::recipe(class ~ . ) |>## Crea la receta
  #recipes::step_indicate_na(urban, tree, ia) |>
    ## Eliminar variables que no usaremos
    # step_rm() |>
  
    ## Crear nuevas variables (insight desde el EDA)
    #recipes::step_mutate(temperature = temperature/1000) |>
    ## Imputar los datos 
    # step_impute_mean()
  recipes::step_impute_knn(recipes::all_predictors() ) |>
    ## Estandarizacion/Normalizacion de numericas
  recipes::step_normalize(recipes::all_numeric(), 
                             -recipes::all_outcomes()) |>
    ## Crear una categoría "otros" que agrupe a categorias pequeñas
  recipes::step_other(recipes::all_nominal(), 
                        -recipes::all_outcomes() , 
                        threshold = 0.07, 
                        other = "otros") |>
    ## Crear una categoría "new" para observaciones con labels "no muestreados"
  recipes::step_novel(recipes::all_nominal(), 
                        -recipes::all_outcomes() , 
                        new_level = "new") |>
  ## Crear variables indicadoras para cada categoría
    recipes::step_dummy(recipes::all_nominal(), 
                        -recipes::all_outcomes()) |>
  #recipes::step_pca(recipes::all_numeric(), threshold = 0.8) |>
    ## Crear variables indicadoras para cada categoría
    # recipes::step_dummy(all_nominal(), -all_outcomes() ) |># Dummy
    ## Eliminar automáticamente variables con alta correlacion 
    ## para evitar la multicolinealidad xi ~ xj
  recipes::step_corr(recipes::all_numeric(),
                       -recipes::all_outcomes(), 
                       threshold = 0.8) |>
  recipes::step_nzv(all_predictors())
    ## Tambien podemos eliminar variables con multicolinealidad "a mano"
    #recipes::step_rm(suhi_night, suhi_day,wordcover)
```

```{r receta_iw, warning=FALSE, message=FALSE, error=TRUE}
receta_iw

receta_iw |>
    recipes::prep() |>
    recipes::bake(new_data = NULL) |>
    dplyr::glimpse()
```

### Hyperparameter Optimization and Training

#### Resamping (Cross Validation)

##### upsampling

```{r remuestreo_upsampling, warning=FALSE, message=FALSE, error=TRUE }
set.seed(123456789)
library(tidysdm)
sp_block_cv  <- TRUE
if(sp_block_cv == TRUE){
  # Resampling with spatial block cross validation
cv <- spatialsample::spatial_block_cv(train,
                                      method = "random",
                                      repeats = 4,
                                      v = 5)
autoplot(cv) +
    ggplot2::geom_sf(data = aoi,
                     fill = NA,
                     col = "black",
                     lwd = 0.5)
} else{
  # # Resampling with stratify cross validation
cv <- rsample::vfold_cv(train |> sf::st_drop_geometry(), 
                        v = 5, 
                        repeats = 4, 
                        strata = class)
}

```

##### Importancie Weigths

```{r remuestreo_iw, warning=FALSE, message=FALSE, error=TRUE}
set.seed(123456789)

library(tidysdm)
sp_block_cv  <- TRUE
if(sp_block_cv == TRUE){
  # Resampling with spatial block cross validation
cv_iw <- spatialsample::spatial_block_cv(train_iw,
                                      method = "random",
                                      repeats = 4,
                                      v = 5)
autoplot(cv_iw) +
    ggplot2::geom_sf(data = aoi,
                     fill = NA,
                     col = "black",
                     lwd = 0.5)
} else{
  # # Resampling with stratify cross validation
cv_iw <- rsample::vfold_cv(train_iw |> sf::st_drop_geometry(), 
                        v = 5, 
                        repeats = 4, 
                        strata = class)
}

```

#### Metrics

```{r metricas,  warning=FALSE, message=FALSE, error=TRUE}
library(yardstick)
library(tidysdm)
metricas <- yardstick::metric_set(yardstick::sens, # 1a metrica: la que optimiza tune_bayes
                                  roc_auc,
                                  yardstick::spec,
                                  bal_accuracy,
                                  pr_auc, 
                                  accuracy, 
                                  boyce_cont, 
                                  tss_max, 
                                  kap)
metricas
```

#### Activate Parallelization

```{r paralelizacion, warning=FALSE, message=FALSE, error=TRUE}
parallel::detectCores(logical=FALSE)

cl <- parallel::makePSOCKcluster(as.integer(Sys.getenv("N_CORES", "25")))
doParallel::registerDoParallel(cl)
```

### Model specification

#### Mexnet

```{r maxmet_ms}
maxnet_ms <- tidysdm::maxent(feature_classes = tune::tune(),
                              regularization_multiplier = tune::tune())
```

#### Logistic Regresion

```{r lr_ms}
lr_ms <- parsnip::logistic_reg() |> 
  parsnip::set_engine("glm") |> 
  parsnip::set_mode("classification")
```

#### Logistic Regresion With ElasticNet

```{r lre_ms}
lre_ms <- parsnip::logistic_reg(penalty = tune(), 
                                 mixture = tune() ) |> 
  parsnip::set_engine("glmnet", 
             family = "binomial") |> 
  parsnip::set_mode("classification")
```

#### Generalized Additive Model

```{r gam_ms}
gam_ms <- parsnip::gen_additive_mod(select_features = FALSE,
                                     adjust_deg_free = tune::tune()) |>
  parsnip::set_engine("mgcv") |>
  parsnip::set_mode("classification")
```

#### MARS

```{r mars_ms}
mars_ms <- parsnip::mars(num_terms = tune(), 
                         prod_degree = tune(), 
                         prune_method= tune()) |>
  parsnip::set_engine("earth") |>
  parsnip::set_mode("classification")
```

#### Naive Bayes

```{r nb_ms}
nbay_ms <- parsnip::naive_Bayes(smoothness = tune::tune())  |>
  parsnip::set_engine("naivebayes") |>
  parsnip::set_mode("classification")
```

#### K Nearest Neighbor

```{r knn_ms, warning=FALSE, message=FALSE, error=TRUE}
knn_ms <- parsnip::nearest_neighbor(neighbors = tune(),
                                    weight_func = tune(),
                                    dist_power = tune()) |>
  parsnip::set_engine("kknn") |>
  parsnip::set_mode("classification")
```

#### Decision Tree

```{r dt_ms, warning=FALSE, message=FALSE, error=TRUE}
dt_ms <- parsnip::decision_tree(cost_complexity = parsnip::tune(),
                                tree_depth = parsnip::tune(),
                                min_n = parsnip::tune()) |>
  parsnip::set_engine("rpart") |>
  parsnip::set_mode("classification")

dt_ms
```

#### Random Forest

https://www.tidymodels.org/find/parsnip/ https://parsnip.tidymodels.org//reference/details_rand_forest_ranger.html

```{r rf_ms, warning=FALSE, message=FALSE, error=TRUE}
rf_ms <- parsnip::rand_forest(mtry = tune(), 
                              trees = tune(), 
                              min_n = tune() ) |>
    parsnip::set_engine("ranger", 
                        # opciones específicas del engine 
                        importance = "impurity") |>
    parsnip::set_mode("classification")

rf_ms
```

#### LightGBM

```{r lgbm_ms, warning=FALSE, message=FALSE,error=TRUE }
library(bonsai)
lgbm_ms <- parsnip::boost_tree(mtry = tune::tune(), 
                               trees = tune::tune(), 
                               tree_depth = tune::tune(),
                               loss_reduction = tune::tune(), 
                               learn_rate= tune::tune()) |>
    parsnip::set_engine("lightgbm") |>
    parsnip::set_mode("classification")
```

#### XGBoost

```{r xgboost_ms, warning=FALSE, message=FALSE, error=TRUE}
xgb_ms <- parsnip::boost_tree(mtry = tune::tune(), 
                              trees = tune::tune(), 
                              tree_depth = tune::tune(),
                              loss_reduction = tune::tune(), 
                              learn_rate= tune::tune()) |>
    parsnip::set_engine("xgboost",
                        nthread= 2) |>
    parsnip::set_mode("classification")
```

#### Multilayer Perceptron (Neural Network)

```{r mlp_ms, warning=FALSE, message=FALSE,error=TRUE }
library(nnet)
mlp_ms <- parsnip::mlp(hidden_units = tune::tune(),
                       penalty = tune::tune(),
                       epochs = tune::tune()) |>
    parsnip::set_engine("nnet") |>
    parsnip::set_mode("classification")
```

### Workflow

#### Without data balancing

##### MaxNet

```{r worflow_maxnet,  warning=FALSE, message=FALSE, error=TRUE}
maxnet_wflow    <- workflows::workflow() |> 
  workflows::add_recipe(receta) |> 
  workflows::add_model(maxnet_ms)
```

##### Logistic Regression

```{r worflow_lr,  warning=FALSE, message=FALSE, error=TRUE}
lr_wflow    <- workflows::workflow() |> 
  workflows::add_recipe(receta) |> 
  workflows::add_model(lr_ms)
```

##### Logistic Regresion With ElasticNet

```{r worflow_rle,  warning=FALSE, message=FALSE, error=TRUE}
rle_wflow <- workflows::workflow() |>
    workflows::add_recipe(receta) |>
    workflows::add_model(lre_ms)
rle_wflow
```

##### Generalized Additive Model

```{r worflow_gam,  warning=FALSE, message=FALSE, error=TRUE}
pred_vars   <- recipes::prep(receta) |> 
  recipes::bake(new_data = NULL) |>
  dplyr::select(-class) |> 
  names()
gam_formula <- reformulate(paste0("s(", pred_vars, ", k = 3)"), 
                           response = "class")
gam_wflow <- workflows::workflow() |> 
  workflows::add_recipe(receta) |> 
  workflows::add_model(gam_ms, formula = gam_formula)
```

##### MARS

```{r worflow_mars,  warning=FALSE, message=FALSE, error=TRUE}
mars_wflow <- workflows::workflow() |>
    workflows::add_recipe(receta) |>
    workflows::add_model(mars_ms)
mars_wflow
```

##### Naive Bayes

```{r workflow_nbay,  warning=FALSE, message=FALSE, error=TRUE}
nbay_wflow <- workflows::workflow() |>
    workflows::add_recipe(receta) |>
    workflows::add_model(nbay_ms)
nbay_wflow
```

##### K Nearest Neighbor

```{r workflow_knn,  warning=FALSE, message=FALSE, error=TRUE}
knn_wflow <- workflows::workflow() |>
    workflows::add_recipe(receta |> 
                              step_sample(size = 10000)) |>
    workflows::add_model(knn_ms)
knn_wflow
```

##### Decision Tree

```{r workflow_dt,  warning=FALSE, message=FALSE, error=TRUE}
dt_wflow <- workflows::workflow() |>
  workflows::add_recipe(receta) |>
  workflows::add_model(dt_ms) 
dt_wflow
```

##### Random Forest

```{r workflow_rf,  warning=FALSE, message=FALSE,error=TRUE }
rf_wflow <- workflows::workflow() |>
  workflows::add_recipe(receta) |>
  workflows::add_model(rf_ms) 
rf_wflow 
```

##### LightGBM

```{r workflow_lgbm,  warning=FALSE, message=FALSE, error=TRUE}
lgbm_wflow <- workflows::workflow() |>
    workflows::add_recipe(receta) |>
    workflows::add_model(lgbm_ms) 
lgbm_wflow
```

##### XGBoost

```{r workflow_xgb,  warning=FALSE, message=FALSE, error=TRUE}
xgb_wflow <- workflows::workflow() |> 
    workflows::add_recipe(receta) |> 
    workflows::add_model(xgb_ms) 

xgb_wflow
```

##### Multilayer Perceptron (Neural Network)

```{r workflow_mlp,  warning=FALSE, message=FALSE, error=TRUE}
mlp_wflow <- workflows::workflow() |>
    workflows::add_recipe(receta) |>
    workflows::add_model(mlp_ms)
mlp_wflow
```

#### With data balancing

##### MaxNet

```{r worflow_maxnet_up,  warning=FALSE, message=FALSE,error=TRUE }
maxnet_wflow_up    <- workflows::workflow() |> 
  workflows::add_recipe(receta_upsamping) |> 
  workflows::add_model(maxnet_ms)
```

##### Logistic Regresion

```{r worflow_lr_up,  warning=FALSE, message=FALSE, error=TRUE}
lr_wflow_up    <- workflows::workflow() |> 
  workflows::add_recipe(receta_upsamping) |> 
  workflows::add_model(lr_ms)
```

##### Logistic Regresion With ElasticNet

```{r worflow_lre_up,  warning=FALSE, message=FALSE, error=TRUE}
rle_wflow_up <- workflows::workflow() |>
    workflows::add_recipe(receta_upsamping) |>
    workflows::add_model(lre_ms)
rle_wflow_up
```

##### Generalized Additive Model

```{r worflow_gam_up,  warning=FALSE, message=FALSE, error=TRUE}
pred_vars   <- recipes::prep(receta) |> 
  recipes::bake(new_data = NULL) |>
  dplyr::select(-class) |> 
  names()
gam_formula <- reformulate(paste0("s(", pred_vars, ", k = 3)"), 
                           response = "class")
gam_wflow_up <- workflows::workflow() |>
  workflows::add_recipe(receta_upsamping) |> 
  workflows::add_model(gam_ms, formula = gam_formula)
```

##### MARS

```{r worflow_mars_up,  warning=FALSE, message=FALSE, error=TRUE}
mars_wflow_up <- workflows::workflow() |>
    workflows::add_recipe(receta_upsamping) |>
    workflows::add_model(mars_ms)
mars_wflow_up
```

##### Naive Bayes

```{r workflow_nbay_up,  warning=FALSE, message=FALSE, error=TRUE}
nbay_wflow_up <- workflows::workflow() |>
    workflows::add_recipe(receta_upsamping) |>
    workflows::add_model(nbay_ms)
nbay_wflow_up
```

##### K Nearest Neighbor

```{r workflow_knn_up,  warning=FALSE, message=FALSE, error=TRUE}
knn_wflow_up <- workflows::workflow() |>
    workflows::add_recipe(receta_upsamping |> 
                              step_sample(size = 10000)) |>
    workflows::add_model(knn_ms)
knn_wflow_up
```

##### Decision Tree

```{r workflow_dt_iw,  warning=FALSE, message=FALSE, error=TRUE}
dt_wflow_iw <- workflows::workflow() |>
  workflows::add_recipe(receta_iw) |>
  workflows::add_model(dt_ms) |>
  workflows::add_case_weights(case_wts)
dt_wflow_iw
```

##### Random Forest

```{r workflow_rf_iw,  warning=FALSE, message=FALSE, error=TRUE}
rf_wflow_iw <- workflows::workflow() |>
  workflows::add_recipe(receta_iw) |>
  workflows::add_model(rf_ms) |>
  workflows::add_case_weights(case_wts)
rf_wflow_iw 
```

##### LightGBM

```{r workflow_lgbm_up,  warning=FALSE, message=FALSE, error=TRUE}
lgbm_wflow_up <- workflows::workflow() |>
    workflows::add_recipe(receta_upsamping) |>
    workflows::add_model(lgbm_ms) 
lgbm_wflow_up
```

##### XGBoost

```{r workflow_xgb_iw, message=FALSE, warning=FALSE, error=TRUE}
xgb_wflow_iw <- workflows::workflow() |> 
    workflows::add_recipe(receta_iw) |> 
    workflows::add_model(xgb_ms) |> 
    workflows::add_case_weights(case_wts) ## Aquí agregamos los pesos

xgb_wflow_iw 
```

##### Multilayer Perceptron (Neural Network)

```{r workflow_mlp_up,  warning=FALSE, message=FALSE,error=TRUE }
mlp_wflow_up <- workflows::workflow() |>
    workflows::add_recipe(receta_upsamping) |>
    workflows::add_model(mlp_ms)
mlp_wflow_up
```

### Hyperparameter tuning

#### Search Grid

##### MaxNet

```{r mesh_grid_maxnet,  message=FALSE, warning=FALSE, error=TRUE}
set.seed(12345789)
maxnet_grid <- maxnet_ms |>
  workflowsets::extract_parameter_set_dials() |>
  recipes::update(feature_classes = tidysdm::feature_classes(values = c("l",
                                                                        "lq",
                                                                        "lqp",
                                                                        "lqph",
                                                                        "lqpht")),
                  regularization_multiplier = tidysdm::regularization_multiplier(range = c(0.5,4))) |>
  dials::grid_space_filling(size = 100)

```

##### Logistic Regresion

##### Logistic Regresion with Elasticnet (LRE)

```{r mesh_grid_lre,  message=FALSE, warning=FALSE, error=TRUE}
set.seed(12345789)
lre_grid <- crossing(penalty = c(0.1, 0.01, 0.001),
                     mixture = c(0.1, 0.3, 0.6, 0.9))

lre_grid 
```

##### Generalized Additive Model

```{r mesh_grid_gam,  message=FALSE, warning=FALSE, error=TRUE}
set.seed(12345789)
gam_grid <- gam_ms |>
  workflowsets::extract_parameter_set_dials() |>
  dials::grid_space_filling(size = 100)
```

##### Multivariate Adaptative Regresion Splines (MARS)

```{r mesh_grid_mars,  message=FALSE, warning=FALSE, error=TRUE}
set.seed(12345789)
mars_grid <- tidyr::crossing(num_terms= 3:7, 
                             prod_degree= 1:2, 
                             prune_method= c('backward', 'forward'))


mars_grid 
```

##### Naive Bayes

```{r mesh_grid_nbay,  message=FALSE, warning=FALSE, error=TRUE}
set.seed(12345789)
library(discrim)
nbay_grid <- tibble::tibble(smoothness= seq(from = 0.5, to = 2, by = 0.1))

nbay_grid 

```

##### K Nearest Neighbor

```{r mesh_search_knn, warning=FALSE, message=FALSE,error=TRUE }
set.seed(12345789)
knn_grid <- knn_ms |>
    workflowsets::extract_parameter_set_dials() |>
    recipes::update(weight_func = dials::weight_func(c("biweight",
                                                       "rectangular"))) |>
    dials::grid_space_filling(size = 100)

knn_grid 
```

##### Decision Tree

```{r mesh_search_dt, warning=FALSE, message=FALSE,error=TRUE }
set.seed(12345789)
dt_grid <- dt_ms |>
  workflowsets::extract_parameter_set_dials() |>
  recipes::update(
                  #tree_depth = dials::tree_depth(range= c(1, 8)),
                  # 
                  #tree_depth= dials::tree_depth(range= c(3, 8)),
                  # porcentajes de un dígito (1% o 2% a 5%)
                  min_n = dials::min_n(range = c(3, 15))
                  ) |>
  dials::grid_space_filling(size = 100)
dt_grid
```

##### Random Forest

```{r mesh_search_rf, warning=FALSE, message=FALSE, error=TRUE}
set.seed(12345789)
rf_grid <- rf_ms |>
  workflowsets::extract_parameter_set_dials() |>
  recipes::update(# porcentajes de las filas (0.5% min y 5% max)
                  min_n = dials::min_n(range =  c(3, 15)),
                  ## 18 columnas sqrt(10, 11) = 3 aprox 4, min = 3-1 = 2, max = 3 +3
                  mtry = dials::mtry(range = c(2, 6)),
                  ##
                  trees = dials::trees(range = c(100, 1000))) |>
  dials::grid_space_filling(size = 100)
rf_grid
```

##### LightGBM

```{r mesh_search_lgbm, warning=FALSE, message=FALSE,error=TRUE }
set.seed(12345789)
lgbm_grid <- lgbm_ms |>
    ## preguntamos los parametros tuneables del modelo
    workflowsets::extract_parameter_set_dials() |>
    ## Vamos a definir un rango para el min_n y mtry
    recipes::update(
        ## 17 columnas sqrt(10, 11) = 4 aprox 4, min = 3-1 = 2, max = 3 +3
        mtry = dials::mtry(range= c(2, 6)),
        ## Cantidad de árboles en el proceso de boosting
        ### Cantidad de arboles (suele ser cantidades grandes)
        trees= dials::trees(range = c(100, 1000)),
        ## Profundidad de cada arbol
        tree_depth= dials::tree_depth(range= c(3, 6))
        ## Los otros hiperparam los dejamos por default
    ) |>
    ## En Boosting se suelen tener mallas grandes, porque estos modelos
    ## son muy sensibles a los hiperparametros y ademas tienen una 
    ## gran cantidad de hiperparametros
    #grid_latin_hypercube(size = 40)
    dials::grid_space_filling(size = 100)

lgbm_grid 
```

##### XGBoost

```{r mesh_search_xgboost, warning=FALSE, message=FALSE, error=TRUE}
set.seed(12345)
xgb_grid <- xgb_ms |>
    ## preguntamos los parametros tuneables del modelo
    workflowsets::extract_parameter_set_dials() |>
    ## Vamos a definir un rango para el min_n y mtry
    recipes::update(
        ## 18 columnas sqrt(10, 11) = 3 aprox 4, min = 3-1, max = 3 +3
        mtry = dials::mtry(range= c(2, 6)),
        ## Cantidad de árboles en el proceso de boosting
        trees= dials::trees(range = c(100, 1000)),
        ## Profundidad de cada arbol
        tree_depth= dials::tree_depth(range= c(3, 6))
        ## Los otros hiperparam los dejamos por default
    ) |> 
    ## En Boosting se suelen tener mallas grandes, porque estos modelos
    ## son muy sensibles a los hiperparametros y ademas tienen una 
    ## gran cantidad de hiperparametros
    #grid_latin_hypercube(size = 40)
    dials::grid_space_filling(size = 100)

xgb_grid 
```

##### Multilayer Perceptron (Neural Network)

```{r mesh_search_mlp, warning=FALSE, message=FALSE, error=TRUE}
set.seed(12345789)
mlp_grid <- mlp_ms |>
    workflowsets::extract_parameter_set_dials() |>
    ## mismo rango que en el modelo de Ae. aegypti
    recipes::update(
        hidden_units = dials::hidden_units(range = c(1, 8)),
        penalty = dials::penalty(range = c(-4, 0)),
        epochs = dials::epochs(range = c(50, 300))
    ) |>
    dials::grid_space_filling(size = 100)

mlp_grid 
```

#### Search Mesh Training with Cross Validation

##### Without data balancing

###### Maxnet

```{r entrenamiento_mesh_search_maxnet, warning=FALSE, message=FALSE, error=TRUE}
maxnet_pset <- maxnet_ms |>
    workflowsets::extract_parameter_set_dials() |>
    recipes::update(
        feature_classes = tidysdm::feature_classes(values = c("l","lq",
                                                              "lqp",
                                                              "lqph","lqpht")),
        regularization_multiplier = tidysdm::regularization_multiplier(range = c(0.5, 4))
    )

tictoc::tic(); set.seed(12345)
maxnet_tuned <- tune::tune_bayes(maxnet_wflow, 
                                 resamples = cv, 
                                 param_info = maxnet_pset,
                                 initial = 15,
                                 iter = 25,
                                 metrics = metricas, 
                                 control = tune::control_bayes(allow_par = T, 
                                                               save_pred = T,
                                                               no_improve = 10, 
                                                               verbose = FALSE))
tictoc::toc()
```

###### Logistic Regresion

```{r entrenamiento_mesh_search_lr, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic(); set.seed(12345)
#lr_res <- tune::tune_grid(lr_wflow, 
#                              resamples = cv, 
#                              metrics = metricas,
#                              control = tune::control_resamples(
#                                                                 save_pred = T))
lr_tuned <- tune::fit_resamples(lr_wflow, 
                              resamples = cv, 
                              metrics = metricas,
                              control = tune::control_resamples(save_pred = T))
tictoc::toc()

```

###### Logistic Regresion ElasticNet

```{r entrenamiento_mesh_search_lre, warning=FALSE, message=FALSE, error=TRUE}
## penalty en escala log10: 0.001-0.1 -> range log10 = c(-3, -1)
lre_pset <- dials::parameters(
    dials::penalty(range = c(-3, -1)),
    dials::mixture(range = c(0.1, 0.9))
)

tictoc::tic()
set.seed(12345)
lre_tuned <- tune::tune_bayes(rle_wflow, ## Modelo
                              resamples = cv, ## Crossvalidation
                              param_info = lre_pset, ## Espacio de búsqueda
                              initial = 15,
                              iter = 25,
                              metrics = metricas, ## Metricas
                              control = tune::control_bayes(allow_par = T, save_pred = T,
                                                            no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### Generalized Additive Model

```{r entrenamiento_mesh_search_gam, warning=FALSE, message=FALSE, error=TRUE}
gam_pset <- gam_ms |>
    workflowsets::extract_parameter_set_dials()

tictoc::tic(); set.seed(12345)
gam_tuned <- tune::tune_bayes(gam_wflow, 
                              resamples = cv, 
                              param_info = gam_pset,
                              initial = 15,
                              iter = 25,
                              metrics = metricas, 
                              control = tune::control_bayes(allow_par = T, save_pred = T,
                                                            no_improve = 10, verbose = FALSE))
tictoc::toc()
```

###### Multivariate Adaptative Regresion Splines (MARS)

```{r entrenamiento_mesh_search_mars, warning=FALSE, message=FALSE, error=TRUE}
mars_pset <- dials::parameters(
    dials::num_terms(range = c(3, 12)),
    dials::prod_degree(range = c(1, 2)),
    dials::prune_method(values = c('backward', 'forward'))
)

tictoc::tic()
set.seed(12345)
mars_tuned <- tune::tune_bayes(mars_wflow, ## Modelo
                               resamples = cv, ## Crossvalidation
                               param_info = mars_pset, ## Espacio de búsqueda
                               initial = 15,
                               iter = 25,
                               metrics = metricas, ## Metricas
                               control = tune::control_bayes(allow_par = T, save_pred = T,
                                                             no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### Naive Bayes

```{r entrenamiento_mesh_search_nbay, warning=FALSE, message=FALSE, error=TRUE}
#nbay_pset <- dials::parameters(dials::Laplace(range = c(0.5, 3)))
nbay_pset <- dials::parameters(dials::smoothness(range = c(0.5, 2)))
tictoc::tic()
set.seed(12345)
nbay_tuned <- tune::tune_bayes(nbay_wflow, ## Modelo
                               resamples = cv, ## Crossvalidation
                               param_info = nbay_pset, ## Espacio de búsqueda
                               initial = 15,
                               iter = 25,
                               metrics = metricas, ## Metricas
                               control = tune::control_bayes(allow_par = T, save_pred = T,
                                                             no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### K Nearest Neighbor

```{r entrenamiento_mesh_search_knn, warning=FALSE, message=FALSE, error=TRUE}
knn_pset <- knn_ms |>
    workflowsets::extract_parameter_set_dials() |>
    recipes::update(weight_func = dials::weight_func(c("biweight", "rectangular")))

tictoc::tic()
set.seed(12345)
library(kknn)
knn_tuned <- tune::tune_bayes(knn_wflow, ## Modelo
                              resamples = cv, ## Crossvalidation
                              param_info = knn_pset, ## Espacio de búsqueda
                              initial = 15,
                              iter = 25,
                              metrics = metricas, ## Metricas
                              control = tune::control_bayes(allow_par = T, save_pred = T,
                                                            no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### Decision Tree

```{r entrenamiento_mesh_search_dt, warning=FALSE, message=FALSE, error=TRUE}
dt_pset <- dt_ms |>
    workflowsets::extract_parameter_set_dials() |>
    recipes::update(min_n = dials::min_n(range = c(2, 15)))

tictoc::tic()
set.seed(12345)
dt_tuned <- tune::tune_bayes(dt_wflow, ## Modelo
                             resamples = cv, ## Crossvalidation
                             param_info = dt_pset, ## Espacio de búsqueda
                             initial = 15,
                             iter = 25,
                             metrics = metricas, ## Metricas
                             control = tune::control_bayes(allow_par = T, save_pred = T,
                                                           no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### Random Forest

```{r entrenamiento_mesh_search_rf, warning=FALSE, message=FALSE, error=TRUE}
rf_pset <- rf_ms |>
    workflowsets::extract_parameter_set_dials() |>
    recipes::update(
        min_n = dials::min_n(range = c(2, 20)),
        mtry = dials::mtry(range = c(2, 6)),
        trees = dials::trees(range = c(100, 1000))
    )

tictoc::tic()
set.seed(12345)
rf_tuned <- tune::tune_bayes(rf_wflow, ## Modelo
                             resamples = cv, ## Crossvalidation
                             param_info = rf_pset, ## Espacio de búsqueda
                             initial = 15,
                             iter = 25,
                             metrics = metricas, ## Metricas
                             control = tune::control_bayes(allow_par = T, save_pred = T,
                                                           no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### LightGBM

```{r entrenamiento_mesh_search_lgbm, warning=FALSE, message=FALSE,error=TRUE }
lgbm_pset <- lgbm_ms |>
    workflowsets::extract_parameter_set_dials() |>
    recipes::update(
        mtry = dials::mtry(range = c(2, 9)),
        trees = dials::trees(range = c(100, 1000)),
        tree_depth = dials::tree_depth(range = c(1, 8))
    )

tictoc::tic()
set.seed(12345)
lgbm_tuned <- tune::tune_bayes(lgbm_wflow, ## Modelo
                               resamples = cv, ## Crossvalidation
                               param_info = lgbm_pset, ## Espacio de búsqueda
                               initial = 15,
                               iter = 25,
                               metrics = metricas, ## Metricas
                               control = tune::control_bayes(allow_par = T, save_pred = T,
                                                             no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### XGBoost

```{r entrenamiento_mesh_search_xgboost, warning=FALSE, message=FALSE,error=TRUE }
xgb_pset <- xgb_ms |>
    workflowsets::extract_parameter_set_dials() |>
    recipes::update(
        mtry = dials::mtry(range = c(2, 6)),
        trees = dials::trees(range = c(20, 1000)),
        tree_depth = dials::tree_depth(range = c(1, 6)),
        learn_rate = dials::learn_rate(range = c(-3, 0))
    )

tictoc::tic()
set.seed(12345)
xgb_tuned <- tune::tune_bayes(xgb_wflow, ## Modelo
                              resamples = cv, ## Crossvalidation
                              param_info = xgb_pset, ## Espacio de búsqueda
                              initial = 15,
                              iter = 25,
                              metrics = metricas, ## Metricas
                              control = tune::control_bayes(allow_par = T, save_pred = T,
                                                            no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### Multilayer Perceptron (Neural Network)

```{r entrenamiento_mesh_search_mlp, warning=FALSE, message=FALSE, error=TRUE}
## Espacio de búsqueda (mismo rango que mlp_grid, sin fijar la malla)
mlp_pset <- mlp_ms |>
    workflowsets::extract_parameter_set_dials() |>
    recipes::update(
        hidden_units = dials::hidden_units(range = c(1, 8)),
        penalty = dials::penalty(range = c(-4, 0)),
        epochs = dials::epochs(range = c(50, 300))
    )

tictoc::tic()
set.seed(12345)
mlp_tuned <- tune::tune_bayes(mlp_wflow, ## Modelo
                              resamples = cv, ## Crossvalidation
                              param_info = mlp_pset, ## Espacio de búsqueda
                              initial = 15, ## Puntos iniciales (space-filling)
                              iter = 25, ## Iteraciones de optimización bayesiana
                              metrics = metricas, ## Metricas
                              control = tune::control_bayes(allow_par = T,
                                                            save_pred = T,
                                                            no_improve = 10,
                                                            verbose = FALSE))
tictoc::toc()

```

##### With data balancing

###### Maxnet

```{r entrenamiento_mesh_search_maxnet_wdb, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic(); set.seed(12345)
maxnet_tuned_wdb <- tune::tune_bayes(maxnet_wflow_up, 
                                     resamples = cv, 
                                     param_info = maxnet_pset,
                                     initial = 15,
                                     iter = 25,
                                     metrics = metricas, 
                                     control = tune::control_bayes(allow_par = T, save_pred = T,
                                                                   no_improve = 10, verbose = FALSE))
tictoc::toc()
```

###### Logistic Regresion

```{r entrenamiento_mesh_search_lr_wdb, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic(); set.seed(12345)
lr_tuned_wdb <- tune::fit_resamples(lr_wflow_up, 
                                  resamples = cv,
                                  metrics = metricas,
                                  control = tune::control_resamples(save_pred = T))
tictoc::toc()
```

###### Generalized Additive Model

```{r entrenamiento_mesh_search_gam_wbd, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic(); set.seed(12345)
gam_tuned_wdb <- tune::tune_bayes(gam_wflow_up, 
                                  resamples = cv, 
                                  param_info = gam_pset,
                                  initial = 15,
                                  iter = 25,
                                  metrics = metricas, 
                                  control = tune::control_bayes(allow_par = T, save_pred = T,
                                                                no_improve = 10, verbose = FALSE))
tictoc::toc()
```

###### Logistic Regresion ElasticNet

```{r entrenamiento_mesh_search_lre_wdb, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic()
set.seed(12345)
lre_tuned_wdb <- tune::tune_bayes(rle_wflow_up, ## Modelo
                                  resamples = cv, ## Crossvalidation
                                  param_info = lre_pset, ## mismo espacio de búsqueda
                                  initial = 15,
                                  iter = 25,
                                  metrics = metricas, ## Metricas
                                  control = tune::control_bayes(allow_par = T, save_pred = T,
                                                                no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### Multivariate Adaptative Regresion Splines (MARS)

```{r entrenamiento_mesh_search_mars_wdb, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic()
set.seed(12345)
mars_tuned_wdb <- tune::tune_bayes(mars_wflow_up, ## Modelo
                                   resamples = cv, ## Crossvalidation
                                   param_info = mars_pset, ## mismo espacio de búsqueda
                                   initial = 15,
                                   iter = 25,
                                   metrics = metricas, ## Metricas
                                   control = tune::control_bayes(allow_par = T, save_pred = T,
                                                                 no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### Naive Bayes

```{r entrenamiento_mesh_search_nbay_wdb, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic()
set.seed(12345)
nbay_tuned_wdb <- tune::tune_bayes(nbay_wflow_up, ## Modelo
                                   resamples = cv, ## Crossvalidation
                                   param_info = nbay_pset, ## mismo espacio de búsqueda
                                   initial = 15,
                                   iter = 25,
                                   metrics = metricas, ## Metricas
                                   control = tune::control_bayes(allow_par = T, 
                                                                 save_pred = T,
                                                                 no_improve = 10, 
                                                                 verbose = FALSE))
tictoc::toc()

```

###### K Nearest Neighbor

```{r entrenamiento_mesh_search_knn_wdb, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic()
set.seed(12345)
library(kknn)
knn_tuned_wdb <- tune::tune_bayes(knn_wflow_up, ## Modelo
                                  resamples = cv, ## Crossvalidation
                                  param_info = knn_pset, ## mismo espacio de búsqueda
                                  initial = 15,
                                  iter = 25,
                                  metrics = metricas, ## Metricas
                                  control = tune::control_bayes(allow_par = T, save_pred = T,
                                                                no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### Decision Tree

```{r entrenamiento_mesh_search_dt_wdb, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic()
set.seed(12345)
dt_tuned_wdb <- tune::tune_bayes(dt_wflow_iw, ## Modelo
                                 resamples = cv_iw, ## Crossvalidation
                                 param_info = dt_pset, ## mismo espacio de búsqueda
                                 initial = 15,
                                 iter = 25,
                                 metrics = metricas, ## Metricas
                                 control = tune::control_bayes(allow_par = T, save_pred = T,
                                                               no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### Random Forest

```{r entrenamiento_mesh_search_rf_wdb, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic()
set.seed(12345)
rf_tuned_wdb <- tune::tune_bayes(rf_wflow_iw, ## Modelo
                                 resamples = cv_iw, ## Crossvalidation
                                 param_info = rf_pset, ## mismo espacio de búsqueda
                                 initial = 15,
                                 iter = 25,
                                 metrics = metricas, ## Metricas
                                 control = tune::control_bayes(allow_par = T, save_pred = T,
                                                               no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### LightGBM

```{r entrenamiento_mesh_search_lgbm_wdb, warning=FALSE, message=FALSE,error=TRUE }
tictoc::tic()
set.seed(12345)
lgbm_tuned_wdb <- tune::tune_bayes(lgbm_wflow_up, ## Modelo
                                   resamples = cv, ## Crossvalidation
                                   param_info = lgbm_pset, ## mismo espacio de búsqueda
                                   initial = 15,
                                   iter = 25,
                                   metrics = metricas, ## Metricas
                                   control = tune::control_bayes(allow_par = T, save_pred = T,
                                                                 no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### XGBoost

```{r entrenamiento_mesh_search_xgboost_wdb, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic()
set.seed(12345)
xgb_tuned_wdb <- tune::tune_bayes(xgb_wflow_iw, ## Modelo
                                  resamples = cv_iw, ## Crossvalidation
                                  param_info = xgb_pset, ## mismo espacio de búsqueda
                                  initial = 15,
                                  iter = 25,
                                  metrics = metricas, ## Metricas
                                  control = tune::control_bayes(allow_par = T, save_pred = T,
                                                                no_improve = 10, verbose = FALSE))
tictoc::toc()

```

###### Multilayer Perceptron (Neural Network)

```{r entrenamiento_mesh_search_mlp_wdb, warning=FALSE, message=FALSE, error=TRUE}
tictoc::tic()
set.seed(12345)
mlp_tuned_wdb <- tune::tune_bayes(mlp_wflow_up, ## Modelo
                                  resamples = cv, ## Crossvalidation
                                  param_info = mlp_pset, ## mismo espacio de búsqueda
                                  initial = 15, ## Puntos iniciales (space-filling)
                                  iter = 25, ## Iteraciones de optimización bayesiana
                                  metrics = metricas, ## Metricas
                                  control = tune::control_bayes(allow_par = T,
                                                                save_pred = T,
                                                                no_improve = 10,
                                                                verbose = FALSE))
tictoc::toc()

```

#### Best Metrics

##### MaxNet

```{r best_metrics_maxnet}
tune::show_best(maxnet_tuned, 
                metric = 'spec', n = 10)
```

```{r best_metrics_maxnet_wbd}
tune::show_best(maxnet_tuned_wdb, 
                metric = 'spec', 
                n = 10)
```

##### Logistic Regresion

```{r best_metrics_lr}
tune::show_best(lr_tuned)
```

```{r best_metrics_lr_wbd}
tune::show_best(lr_tuned_wdb)
```

##### Logistic Regresion with ElasticNet

```{r best_metrics_lre}
tune::show_best(lre_tuned, metric = 'spec', n = 10)
```

```{r best_metrics_lre_wbd}
tune::show_best(lre_tuned_wdb, metric = 'spec', n = 10)
```

##### Generalized Additive Model

```{r best_metrics_gam}
tune::show_best(gam_tuned, metric = 'spec', n = 10)
```

```{r best_metrics_gam_wdb}
tune::show_best(gam_tuned_wdb, metric = 'spec', n = 10)
```

##### Multivariate Adaptative Regresion Spline

```{r best_metrics_mars}
tune::show_best(mars_tuned, metric = 'spec', n = 10)
```

```{r best_metrics_mars_wbd}
tune::show_best(mars_tuned_wdb, metric = 'spec', n = 10)
```

##### Naive Bayes

```{r best_metrics_nbay}
tune::show_best(nbay_tuned, metric = 'spec', n = 10)
```

```{r best_metrics_nbay_wbd}
tune::show_best(nbay_tuned_wdb, metric = 'spec', n = 10)
```

##### K Nearest Neighbor

```{r best_metrics_knn}
tune::show_best(knn_tuned, metric = 'spec', n = 10)
```

```{r best_metrics_knn_wbd}
tune::show_best(knn_tuned_wdb, metric = 'spec', n = 10)
```

##### Decision Tree

```{r best_metrics_dt}
tune::show_best(dt_tuned, metric = 'spec', n = 10)
```

```{r best_metrics_dt_wbd}
tune::show_best(dt_tuned_wdb, metric = 'spec', n = 10)
```

##### Random Forest

```{r best_metrics_rf}
tune::show_best(rf_tuned, metric = 'spec', n = 10)
```

```{r best_metrics_rf_wbd}
tune::show_best(rf_tuned_wdb, metric = 'spec', n = 10)
```

##### LightGBM

```{r best_metrics_lgbm}
tune::show_best(lgbm_tuned, metric = 'spec', n = 10)
```

```{r best_metrics_lgbm_wbd}
tune::show_best(lgbm_tuned_wdb, metric = 'spec', n = 10)
```

##### XGBoost

```{r best_metrics_xgb}
tune::show_best(xgb_tuned, metric = 'spec', n = 10)
```

```{r best_metrics_xgb_wbd}
tune::show_best(xgb_tuned_wdb, metric = 'spec', n = 10)
```

##### Multilayer Perceptron (Neural Network)

```{r best_metrics_mlp}
tune::show_best(mlp_tuned, metric = 'spec', n = 10)
```

```{r best_metrics_mlp_wbd}
tune::show_best(mlp_tuned_wdb, metric = 'spec', n = 10)
```

## Final Model

### Defining the Best Hyperparameter Combination

In this workflow, the best hyperparameter combination was defined using sensitivity, since the goal is to predict more accurately where confirmed imported dengue cases occur.

#### MaxNet

```{r hyperparametros_maxnet, warning=FALSE, message=FALSE, error=TRUE}
maxnet_pars_fin <- tune::select_best(maxnet_tuned, 
                                   metric = "sens")
maxnet_pars_fin
```

```{r hyperparametros_maxnet_wbd, warning=FALSE, message=FALSE, error=TRUE}
maxnet_pars_fin_wdb <- tune::select_best(maxnet_tuned_wdb, 
                                         metric = "sens")
maxnet_pars_fin_wdb
```

#### Logistic Regresion

```{r hyperparametros_lr, warning=FALSE, message=FALSE, error=TRUE}
lr_pars_fin <- tune::select_best(lr_tuned, 
                                   metric = "sens")
lr_pars_fin
```

```{r hyperparametros_lr_wbd, warning=FALSE, message=FALSE,error=TRUE }
lr_pars_fin_wdb <- tune::select_best(lr_tuned_wdb, 
                                   metric = "sens")
lr_pars_fin_wdb
```

#### Logistic Regresion ElasticNet

```{r hyperparametros_lre, warning=FALSE, message=FALSE, error=TRUE}
lre_pars_fin <- tune::select_best(lre_tuned, 
                                   metric = "sens")
lre_pars_fin
```

```{r hyperparametros_lre_wbd, warning=FALSE, message=FALSE, error=TRUE}
lre_pars_fin_wdb <- tune::select_best(lre_tuned_wdb, 
                                   metric = "sens")
lre_pars_fin_wdb
```

#### Generalized Additive Model

```{r hyperparametros_gam, warning=FALSE, message=FALSE, error=TRUE}
gam_pars_fin <- tune::select_best(gam_tuned, 
                                   metric = "sens")
gam_pars_fin
```

```{r hyperparametros_gam_wbd, warning=FALSE, message=FALSE,error=TRUE }
gam_pars_fin_wdb <- tune::select_best(gam_tuned_wdb, 
                                   metric = "sens")
gam_pars_fin_wdb
```

#### Multivariate Adaptative Regresion Splines (MARS)

```{r hyperparametros_mars, warning=FALSE, message=FALSE, error=TRUE}
mars_pars_fin <- tune::select_best(mars_tuned, 
                                   metric = "sens")
mars_pars_fin
```

```{r hyperparametros_mars_wdb, warning=FALSE, message=FALSE,error=TRUE }
mars_pars_fin_wdb <- tune::select_best(mars_tuned_wdb, 
                                   metric = "sens")
mars_pars_fin_wdb
```

#### Naive Bayes

```{r hyperparametros_nbay, warning=FALSE, message=FALSE, error=TRUE}
nbay_pars_fin <- tune::select_best(nbay_tuned, 
                                   metric = "sens")
nbay_pars_fin
```

```{r hyperparametros_nbay_wdb, warning=FALSE, message=FALSE, error=TRUE}
nbay_pars_fin_wdb <- tune::select_best(nbay_tuned_wdb, 
                                   metric = "sens")
nbay_pars_fin_wdb
```

#### K Nearest Neighbor

```{r hyperparametros_knn, warning=FALSE, message=FALSE, error=TRUE}
knn_pars_fin <- tune::select_best(knn_tuned, 
                                   metric = "sens")
knn_pars_fin
```

```{r hyperparametros_knn_wdb, warning=FALSE, message=FALSE, error=TRUE}
knn_pars_fin_wdb <- tune::select_best(knn_tuned_wdb, 
                                   metric = "sens")
knn_pars_fin_wdb
```

#### Decision Tree

```{r hyperparametros_dt, warning=FALSE, message=FALSE, error=TRUE}
dt_pars_fin <- tune::select_best(dt_tuned, 
                                 metric = "sens")
dt_pars_fin
```

```{r hyperparametros_dt_wdb, warning=FALSE, message=FALSE, error=TRUE}
dt_pars_fin_wdb <- tune::select_best(dt_tuned_wdb, 
                                 metric = "sens")
dt_pars_fin_wdb
```

#### Random Forest

```{r hyperparametros_rf, warning=FALSE, message=FALSE, error=TRUE}
rf_pars_fin <- tune::select_best(rf_tuned, 
                                 metric = "sens")
rf_pars_fin
```

```{r hyperparametros_rf_wdb, warning=FALSE, message=FALSE,error=TRUE }
rf_pars_fin_wdb <- tune::select_best(rf_tuned_wdb, 
                                     metric = "sens")
rf_pars_fin_wdb
```

#### LightGBM

```{r hyperparametros_lgbm, warning=FALSE, message=FALSE, error=TRUE}
lgbm_pars_fin <- tune::select_best(lgbm_tuned, 
                                   metric = "sens")
lgbm_pars_fin
```

```{r hyperparametros_lgbm_wdb, warning=FALSE, message=FALSE, error=TRUE}
lgbm_pars_fin_wdb <- tune::select_best(lgbm_tuned_wdb, 
                                   metric = "sens")
lgbm_pars_fin_wdb
```

#### XGBoost

```{r hyperparametros_xgboost, warning=FALSE, message=FALSE, error=TRUE}
xgb_pars_fin <- tune::select_best(xgb_tuned, 
                                   metric = "sens")
xgb_pars_fin
```

```{r hyperparametros_xgboost_wdb, warning=FALSE, message=FALSE, error=TRUE}
xgb_pars_fin_wdb <- tune::select_best(xgb_tuned_wdb, 
                                   metric = "sens")
xgb_pars_fin_wdb
```

#### Multilayer Perceptron (Neural Network)

```{r hyperparametros_mlp, warning=FALSE, message=FALSE, error=TRUE}
mlp_pars_fin <- tune::select_best(mlp_tuned, 
                                   metric = "sens")
mlp_pars_fin
```

```{r hyperparametros_mlp_wdb, warning=FALSE, message=FALSE, error=TRUE}
mlp_pars_fin_wdb <- tune::select_best(mlp_tuned_wdb, 
                                   metric = "sens")
mlp_pars_fin_wdb
```

### Finalize (assign values to tunable parameters) the workflow

#### Maxnet

```{r, warning=FALSE, message=FALSE, error=TRUE}
maxnet_wflow_fin <- maxnet_wflow |>
    tune::finalize_workflow(maxnet_pars_fin)
maxnet_wflow_fin
```

```{r, warning=FALSE, message=FALSE, error=TRUE}
maxnet_wflow_fin_wdb <- maxnet_wflow_up |>
    tune::finalize_workflow(maxnet_pars_fin_wdb)
maxnet_wflow_fin_wdb
```

#### Logistic Regresion

```{r, warning=FALSE, message=FALSE, error=TRUE}
lr_wflow_fin <- lr_wflow |>
    tune::finalize_workflow(lr_pars_fin)
lr_wflow_fin
```

```{r, warning=FALSE, message=FALSE, error=TRUE}
lr_wflow_fin_wdb <- lr_wflow_up |>
    tune::finalize_workflow(lr_pars_fin_wdb)
lr_wflow_fin_wdb
```

#### Generalized Additive Model

```{r, warning=FALSE, message=FALSE, error=TRUE}
gam_wflow_fin <- gam_wflow |>
    tune::finalize_workflow(gam_pars_fin)
gam_wflow_fin
```

```{r, warning=FALSE, message=FALSE, error=TRUE}
gam_wflow_fin_wdb <- gam_wflow_up |>
    tune::finalize_workflow(gam_pars_fin_wdb)
gam_wflow_fin_wdb
```

#### Logistic Regresion ElasticNet

```{r, warning=FALSE, message=FALSE, error=TRUE}
lre_wflow_fin <- rle_wflow |>
    tune::finalize_workflow(lre_pars_fin)
lre_wflow_fin
```

```{r, warning=FALSE, message=FALSE, error=TRUE}
lre_wflow_fin_wdb <- rle_wflow_up |>
    tune::finalize_workflow(lre_pars_fin_wdb)
lre_wflow_fin_wdb
```

#### Multivariate Adaptative Regresion Splines (MARS)

```{r, warning=FALSE, message=FALSE, error=TRUE}
mars_wflow_fin <- mars_wflow |>
    tune::finalize_workflow(mars_pars_fin)
mars_wflow_fin
```

```{r, warning=FALSE, message=FALSE,error=TRUE }
mars_wflow_fin_wdb <- mars_wflow_up |>
    tune::finalize_workflow(mars_pars_fin_wdb)
mars_wflow_fin_wdb
```

#### Naive Bayes

```{r, warning=FALSE, message=FALSE, error=TRUE}
nbay_wflow_fin <- nbay_wflow |>
    tune::finalize_workflow(nbay_pars_fin)
nbay_wflow_fin
```

```{r, warning=FALSE, message=FALSE, error=TRUE}
nbay_wflow_fin_wdb <- nbay_wflow_up |>
    tune::finalize_workflow(nbay_pars_fin_wdb)
nbay_wflow_fin_wdb
```

#### K Nearest Neighbor

```{r, warning=FALSE, message=FALSE, error=TRUE}
knn_wflow_fin <- knn_wflow |>
    tune::finalize_workflow(knn_pars_fin)
knn_wflow_fin
```

```{r, warning=FALSE, message=FALSE, error=TRUE}
knn_wflow_fin_wdb <- knn_wflow_up |>
    tune::finalize_workflow(knn_pars_fin_wdb)
knn_wflow_fin_wdb
```

#### Decision Tree

```{r, warning=FALSE, message=FALSE, error=TRUE}
dt_wflow_fin <- dt_wflow |>
    tune::finalize_workflow(dt_pars_fin)
dt_wflow_fin
```

```{r, warning=FALSE, message=FALSE, error=TRUE}
dt_wflow_fin_wdb <- dt_wflow_iw |>
    tune::finalize_workflow(dt_pars_fin_wdb)
dt_wflow_fin_wdb
```

#### Random Forest

```{r, warning=FALSE, message=FALSE, error=TRUE}
rf_wflow_fin <- rf_wflow |>
    tune::finalize_workflow(rf_pars_fin)
rf_wflow_fin
```

```{r, warning=FALSE, message=FALSE,error=TRUE }
rf_wflow_fin_wdb <- rf_wflow_iw |>
    tune::finalize_workflow(rf_pars_fin_wdb)
rf_wflow_fin_wdb
```

#### LightGBM

```{r, warning=FALSE, message=FALSE, error=TRUE}
lgbm_wflow_fin <- lgbm_wflow |>
    tune::finalize_workflow(lgbm_pars_fin)
lgbm_wflow_fin
```

```{r, warning=FALSE, message=FALSE, error=TRUE}
lgbm_wflow_fin_wdb <- lgbm_wflow_up |>
    tune::finalize_workflow(lgbm_pars_fin_wdb)
lgbm_wflow_fin_wdb
```

#### XGBoost

```{r, warning=FALSE, message=FALSE, error=TRUE}
xgb_wflow_fin <- xgb_wflow |>
    tune::finalize_workflow(xgb_pars_fin)
xgb_wflow_fin
```

```{r, warning=FALSE, message=FALSE, error=TRUE}
xgb_wflow_fin_wdb <- xgb_wflow_iw |>
    tune::finalize_workflow(xgb_pars_fin_wdb)
xgb_wflow_fin_wdb
```

#### Multilayer Perceptron (Neural Network)

```{r, warning=FALSE, message=FALSE, error=TRUE}
mlp_wflow_fin <- mlp_wflow |>
    tune::finalize_workflow(mlp_pars_fin)
mlp_wflow_fin
```

```{r, warning=FALSE, message=FALSE, error=TRUE}
mlp_wflow_fin_wdb <- mlp_wflow_up |>
    tune::finalize_workflow(mlp_pars_fin_wdb)
mlp_wflow_fin_wdb
```

### Train the final model

#### MaxNet

```{r ent_mod_maxnet, warning=FALSE, message=FALSE,error=TRUE }
maxnet_fitted <- fit(maxnet_wflow_fin, train)
maxnet_fitted
```

```{r ent_mod_maxnet_wdb, warning=FALSE, message=FALSE, error=TRUE}
maxnet_fitted_wdb <- fit(maxnet_wflow_fin_wdb, train)
maxnet_fitted_wdb
```

#### Logistic Regresion

```{r ent_mod_lr, warning=FALSE, message=FALSE, error=TRUE}
lr_fitted <- fit(lr_wflow_fin, train)
lr_fitted
```

```{r ent_mod_lr_wdb, warning=FALSE, message=FALSE, error=TRUE}
lr_fitted_wdb <- fit(lr_wflow_fin_wdb, train)
lr_fitted_wdb
```

#### Logistic Regresion ElasticNet

```{r ent_mod_lre, warning=FALSE, message=FALSE, error=TRUE}
lre_fitted <- fit(lre_wflow_fin, train)
lre_fitted
```

```{r ent_mod_lre_wdb, warning=FALSE, message=FALSE, error=TRUE}
lre_fitted_wdb <- fit(lre_wflow_fin_wdb, train)
lre_fitted_wdb
```

#### Generalized Additive Model

```{r ent_mod_gam, warning=FALSE, message=FALSE,error=TRUE }
gam_fitted <- fit(gam_wflow_fin, train)
gam_fitted
```

```{r ent_mod_gam_wdb, warning=FALSE, message=FALSE, error=TRUE}
gam_fitted_wdb <- fit(gam_wflow_fin_wdb, train)
gam_fitted_wdb
```

#### Multivariate Adaptative Regresion Splines (MARS)

```{r ent_mod_mars, warning=FALSE, message=FALSE, error=TRUE}
mars_fitted <- fit(mars_wflow_fin, train)
mars_fitted
```

```{r ent_mod_mars_wdb, warning=FALSE, message=FALSE, error=TRUE}
mars_fitted_wdb <- fit(mars_wflow_fin_wdb, train)
mars_fitted_wdb
```

#### Naive Bayes

```{r ent_mod_nbay, warning=FALSE, message=FALSE, error=TRUE}
nbay_fitted <- fit(nbay_wflow_fin, train)
nbay_fitted
```

```{r ent_mod_nbay_wdb, warning=FALSE, message=FALSE, error=TRUE}
nbay_fitted_wdb <- fit(nbay_wflow_fin_wdb, train)
nbay_fitted_wdb
```

#### K Nearest Neighbor

```{r ent_mod_knn, warning=FALSE, message=FALSE, error=TRUE}
knn_fitted <- fit(knn_wflow_fin, train)
knn_fitted
```

```{r ent_mod_knn_wdb, warning=FALSE, message=FALSE, error=TRUE}
knn_fitted_wdb <- fit(knn_wflow_fin_wdb, train)
knn_fitted_wdb
```

#### Decision Tree

```{r ent_mod_dt, warning=FALSE, message=FALSE, error=TRUE}
dt_fitted <- fit(dt_wflow_fin, train_iw)
dt_fitted
```

```{r ent_mod_dt_wdb, warning=FALSE, message=FALSE, error=TRUE}
dt_fitted_wdb <- fit(dt_wflow_fin_wdb, train_iw)
dt_fitted_wdb
```

#### Random Forest

```{r ent_mod_rf, warning=FALSE, message=FALSE, error=TRUE}
rf_fitted <- fit(rf_wflow_fin, train_iw)
rf_fitted
```

```{r ent_mod_rf_wdb, warning=FALSE, message=FALSE, error=TRUE}
rf_fitted_wdb <- fit(rf_wflow_fin_wdb, train_iw)
rf_fitted_wdb
```

#### LightGBM

```{r ent_mod_lgbm,warning=FALSE, message=FALSE, error=TRUE}
lgbm_fitted <- fit(lgbm_wflow_fin, train)
lgbm_fitted
```

```{r ent_mod_lgbm_wdb,warning=FALSE, message=FALSE, error=TRUE}
lgbm_fitted_wdb <- fit(lgbm_wflow_fin_wdb, train)
lgbm_fitted_wdb
```

#### XGBoost

```{r ent_mod_xgboost,warning=FALSE, message=FALSE, error=TRUE}
xgb_fitted <- fit(xgb_wflow_fin, train_iw)
xgb_fitted
```

```{r ent_mod_xgboost_wdb,warning=FALSE, message=FALSE, error=TRUE}
xgb_fitted_wdb <- fit(xgb_wflow_fin_wdb, train_iw)
xgb_fitted_wdb
```

#### Multilayer Perceptron (Neural Network)

```{r ent_mod_mlp, warning=FALSE, message=FALSE, error=TRUE}
mlp_fitted <- fit(mlp_wflow_fin, train)
mlp_fitted
```

```{r ent_mod_mlp_wdb, warning=FALSE, message=FALSE, error=TRUE}
mlp_fitted_wdb <- fit(mlp_wflow_fin_wdb, train)
mlp_fitted_wdb
```

### Evaluate the model

#### MaxNet

##### Internal

```{r evaluation_maxnet_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              ml = maxnet_fitted,
              train = train,
              ml_title = "MaxNet",
              metrics_data = FALSE)
```

```{r evaluation_maxnet_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = maxnet_fitted_wdb,
              train = train,
              ml_title = "Logistic Regresion ElasticNet (WDB)")
```

##### External 2025

```{r evaluation_maxnet_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = maxnet_fitted,
              train = train,
              ml_title = "MaxNet 2023-2024 versus 2025")
```

```{r evaluation_maxnet_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test =  data_2025,
              metrics_data = FALSE,
              ml = maxnet_fitted_wdb,
              train = train,
              ml_title = "MaxNet 2023-2024 (WDB) versus 2025")
```

#### Logistic Regresion

##### Internal

```{r evaluation_lr_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = lr_fitted,
              train = train,
              ml_title = "Logistic Regresion")
```

```{r evaluation_lr_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = lr_fitted_wdb,
              train = train,
              ml_title = "Logistic Regresion (WDB)")
```

##### External 2025

```{r evaluation_lr_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = lr_fitted,
              train = train,
              ml_title = "Logistic Regresion 2023-2024 versus 2025")
```

```{r evaluation_lr_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = lr_fitted_wdb,
              train = train,
              ml_title = "Logistic Regresion 2023-2024 (WDB) versus 2025")
```

#### Logistic Regresion ElasticNet

##### Internal

```{r evaluation_lre_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = lre_fitted,
              train = train,
              ml_title = "Logistic Regresion ElasticNet")
```

```{r evaluation_lre_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = lre_fitted_wdb,
              train = train,
              ml_title = "Logistic Regresion ElasticNet (WDB)")
```

##### External 2025

```{r evaluation_lre_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = lre_fitted,
              train = train,
              ml_title = "LR ElasticNet 2023-2024 versus 2025")
```

```{r evaluation_lre_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = lre_fitted_wdb,
              train = train,
              ml_title = "LR ElasticNet 2023-2024 (WDB) versus 2025")
```

#### Generalized Additive Model

##### Internal

```{r evaluation_gam_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = gam_fitted,
              train = train,
              ml_title = "Generalized Additive Model")
```

```{r evaluation_gam_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = gam_fitted_wdb,
              train = train,
              ml_title = " Generalized Additive Model (WDB)")
```

##### External 2025

```{r evaluation_gam_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = gam_fitted,
              train = train,
              ml_title = "Generalized Additive Model 2023-2024 versus 2025")
```

```{r evaluation_gam_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = gam_fitted_wdb,
              train = train,
              ml_title = "Generalized Additive Model 2023-2024 (WDB) versus 2025")
```

#### Multivariate Adaptative Regresion Splines (MARS)

##### Internal

```{r evaluation_mars_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = mars_fitted,
              train = train,
              ml_title = "Multivariate Adaptative Regresion Splines (MARS)")
```

```{r evaluation_mars_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = mars_fitted_wdb,
              train = train,
              ml_title = "Multivariate Adaptative Regresion Splines (MARS) WDB")
```

##### External 2025

```{r evaluation_mars_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = mars_fitted,
              train = train,
              ml_title = "MARS 2023-2024 versus 2025")
```

```{r evaluation_mars_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = mars_fitted_wdb,
              train = train,
              ml_title = "MARS 2023-2024 (WDB) versus 2025")
```

#### Naive Bayes

##### Internal

```{r evaluation_nbay_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = nbay_fitted,
              train = train,
              ml_title = "Naive Bayes")
```

```{r evaluation_nbay_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = nbay_fitted_wdb,
              train = train,
              ml_title = "Naive Bayes (wdb)")
```

##### External 2025

```{r evaluation_nbay_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = nbay_fitted,
              train = train,
              ml_title = "Naive Bayes 2023-2024 versus 2025")
```

```{r evaluation_nbay_wdb_external_b, warning=FALSE, message=FALSE,error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = nbay_fitted_wdb,
              train = train,
              ml_title = "Naive Bayes 2023-2024 (WDB) versus 2025")
```

#### K Nearest Neighbor

##### Internal

```{r evaluation_knn_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = knn_fitted,
              train = train,
              ml_title = "K Nearest Neighbor")
```

```{r evaluation_knn_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = knn_fitted_wdb,
              train = train,
              ml_title = "K Nearest Neighbor (wdb)")
```

##### External 2025

```{r evaluation_knn_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = knn_fitted,
              train = train,
              ml_title = "K Nearest Neighbor 2023-2024 versus 2025")
```

```{r evaluation_knn_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = knn_fitted_wdb,
              train = train,
              ml_title = "K Nearest Neighbor 2023-2024 (WDB) versus 2025")
```

#### Decision Tree

##### Internal

```{r evaluation_dt_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = dt_fitted,
              train = train_iw,
              ml_title = "Decision Tree")
```

```{r evaluation_dt_wdb_internal, warning=FALSE, message=FALSE,error=TRUE }
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = dt_fitted_wdb,
              train = train_iw,
              ml_title = "Decision Tree (wdb)")
```

##### External 2025

```{r evaluation_dt_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = dt_fitted,
              train = train,
              ml_title = "Decision Tree 2023-2024 versus 2025")
```

```{r evaluation_dt_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = dt_fitted_wdb,
              train = train,
              ml_title = "Decision Tree 2023-2024 (WDB) versus 2025")
```

#### Random Forest

##### Internal

```{r evaluation_rf_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = rf_fitted,
              train = train_iw,
              ml_title = "Random_Forest")
```

```{r evaluation_rf_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = rf_fitted_wdb,
              train = train_iw,
              ml_title = "Random_Forest (wdb)")
```

##### External 2025

```{r evaluation_rf_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = rf_fitted,
              train = train,
              ml_title = "Random Forest 2023-2024 versus 2025")
```

```{r evaluation_rf_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = rf_fitted_wdb,
              train = train,
              ml_title = "Random Forest 2023-2024 (WDB) versus 2025")
```

#### LightGBM

##### Internal

```{r evaluation_lgbm_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = lgbm_fitted,
              train = train,
              ml_title = "LigthGBM")
```

```{r evaluation_lgbm_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = lgbm_fitted_wdb,
              train = train,
              ml_title = "LigthGBM (wdb)")
```

##### External 2025

```{r evaluation_lgbm_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = lgbm_fitted,
              train = train,
              ml_title = "LigthGBM 2023-2024 versus 2025")
```

```{r evaluation_lgbm_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = lgbm_fitted_wdb,
              train = train,
              ml_title = "LigthGBM 2023-2024 (WDB) versus 2025")
```

#### XGBoost

##### Internal

```{r evaluacion_xgboost_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = xgb_fitted,
              train = train_iw,
              ml_title = "XGBoost")

```

```{r evaluacion_xgboost_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = xgb_fitted_wdb,
              train = train_iw,
              ml_title = "XGBoost (wdb)")

```

##### External 2025

```{r evaluation_xgboost_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)




metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = xgb_fitted,
              train = train,
              ml_title = "XGBoost 2023-2024 versus 2025")
```

```{r evaluation_xgboost_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = xgb_fitted_wdb,
              train = train,
              ml_title = "XGBoost 2023-2024 (WDB) versus 2025")
```

#### Multilayer Perceptron (Neural Network)

##### Internal

```{r evaluation_mlp_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = mlp_fitted,
              train = train,
              ml_title = "Multilayer Perceptron (Neural Network)")
```

```{r evaluation_mlp_wdb_internal, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = test,
              metrics_data = FALSE,
              ml = mlp_fitted_wdb,
              train = train,
              ml_title = "Multilayer Perceptron (Neural Network) (WDB)")
```

##### External 2025

```{r evaluation_mlp_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)
metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = mlp_fitted,
              train = train,
              ml_title = "Neural Network 2023-2024 versus 2025")
```

```{r evaluation_mlp_wdb_external_b, warning=FALSE, message=FALSE, error=TRUE}
library(magrittr)


metrics_class(test = data_2025,
              metrics_data = FALSE,
              ml = mlp_fitted_wdb,
              train = train,
              ml_title = "Neural Network 2023-2024 (WDB) versus 2025")
```

### Stop the parallelization

```{r}
parallel::stopCluster(cl)
```

## Youden thresholds and fold metrics (out-of-fold)

```{r umbrales_youden, warning=FALSE, message=FALSE, error=TRUE}
# 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))
```

## Save the results

```{r save_results}
save(train, test, train_iw, umbrales, metricas_cv_long,
     maxnet_fitted,
     maxnet_fitted_wdb,
     maxnet_pars_fin,
     maxnet_pars_fin_wdb,
     lr_fitted,
     lr_fitted_wdb,
     lre_fitted,
     lre_fitted_wdb,
     lre_pars_fin,
     lre_pars_fin_wdb,
     gam_fitted,
     gam_fitted_wdb,
     gam_pars_fin,
     gam_pars_fin_wdb,
     mars_fitted,
     mars_fitted_wdb,
     mars_pars_fin,
     mars_pars_fin_wdb,
     nbay_fitted,
     nbay_fitted_wdb,
     nbay_pars_fin, 
     nbay_pars_fin_wdb,
     knn_fitted_wdb,
     knn_fitted,
     knn_pars_fin,
     knn_pars_fin_wdb,
     dt_fitted,
     dt_fitted_wdb,
     dt_pars_fin,
     dt_pars_fin_wdb,
     rf_fitted,
     rf_fitted_wdb, rf_pars_fin_wdb, rf_pars_fin,
     lgbm_fitted,
     lgbm_fitted_wdb,
     lgbm_pars_fin,
     lgbm_pars_fin_wdb,
     xgb_fitted,
     xgb_fitted_wdb,
     xgb_pars_fin,
     xgb_pars_fin_wdb,
     mlp_fitted,
     mlp_fitted_wdb,
     mlp_pars_fin,
     mlp_pars_fin_wdb, 
     file = "enm_ml_dengue_2023_2024_tune_bayes_scv5_sfa_2k_sin_cir.RData")

```

```{r ensemble_models_list, warning=FALSE, message=FALSE}
ensemble_models <- list(
  "MaxNet"                     = list(wflow = maxnet_wflow_fin,     data = train),
  "MaxNet (wdb)"                = list(wflow = maxnet_wflow_fin_wdb, data = train),
  "Logistic Regression"         = list(wflow = lr_wflow,             data = train),
  "Logistic Regression (wdb)"   = list(wflow = lr_wflow_up,          data = train),
  "LRE"                         = list(wflow = lre_wflow_fin,        data = train),
  "LRE (wdb)"                   = list(wflow = lre_wflow_fin_wdb,    data = train),
  "GAM"                         = list(wflow = gam_wflow_fin,        data = train),
  "GAM (wdb)"                   = list(wflow = gam_wflow_fin_wdb,    data = train),
  "MARS"                        = list(wflow = mars_wflow_fin,       data = train),
  "MARS (wdb)"                  = list(wflow = mars_wflow_fin_wdb,   data = train),
  "Naive Bayes"                 = list(wflow = nbay_wflow_fin,       data = train),
  "Naive Bayes (wdb)"           = list(wflow = nbay_wflow_fin_wdb,   data = train),
  "KNN"                         = list(wflow = knn_wflow_fin,        data = train),
  "KNN (wdb)"                   = list(wflow = knn_wflow_fin_wdb,    data = train),
  "Decision Tree"               = list(wflow = dt_wflow_fin,         data = train),
  "Decision Tree (wdb)"         = list(wflow = dt_wflow_fin_wdb,     data = train_iw),
  "Random Forest"               = list(wflow = rf_wflow_fin,         data = train),
  "Random Forest (wdb)"         = list(wflow = rf_wflow_fin_wdb,     data = train_iw),
  "LightGBM"                    = list(wflow = lgbm_wflow_fin,       data = train),
  "LightGBM (wdb)"              = list(wflow = lgbm_wflow_fin_wdb,   data = train),
  "XGBoost"                     = list(wflow = xgb_wflow_fin,        data = train),
  "XGBoost (wdb)"               = list(wflow = xgb_wflow_fin_wdb,    data = train_iw),
  "MLP"                         = list(wflow = mlp_wflow_fin,        data = train),
  "MLP (wdb)"                   = list(wflow = mlp_wflow_fin_wdb,    data = train)
)

save(ensemble_models,
     file = "ensemble_models_enm_ml_dengue_2023_2024_tune_bayes_scv5_sfa_2k_sin_cir.RData")
```

## Session Info

```{r}
sessioninfo::session_info()
```
