Fits a single species distribution model (SDM) workflow using spatial cross-validation. Computes performance metrics for each fold and fits the final model to the entire dataset. Optionally calculates post-hoc metrics such as True Skill Statistic (TSS) and Boyce index.
Usage
h3sdm_fit_model(
sdm_workflow,
data_split,
presence_data = NULL,
truth_col = "presence",
pred_col = ".pred_1"
)
Arguments
- sdm_workflow
A
workflow
object fromh3sdm_workflow()
or manually created.- data_split
A resampling object (e.g., from
vfold_cv()
orh3sdm_spatial_cv()
) used for cross-validation.- presence_data
Optional
sf
or tibble object containing presence locations to compute Boyce index.- truth_col
Character string specifying the name of the column containing the true presence/absence values. Defaults to
"presence"
.- pred_col
Character string specifying the name of the column containing predicted probabilities. Defaults to
".pred_1"
.
Value
A list containing:
- cv_model
Cross-validation results (
tune_results
).- final_model
Fitted workflow on the full dataset.
- metrics
A tibble of performance metrics including ROC AUC, accuracy, sensitivity, specificity, F1-score, Kappa, TSS, and Boyce index.
Examples
if (FALSE) { # \dontrun{
library(h3sdm)
library(tidymodels)
library(sf)
# Simular datos espaciales
coords <- cbind(
x = runif(20, -100, 100),
y = runif(20, -100, 100)
)
dat_sf <- st_as_sf(
data.frame(
x1 = rnorm(20),
x2 = rnorm(20),
presence = factor(sample(0:1, 20, replace = TRUE))
),
coords = c("x", "y"),
crs = 4326
)
# Crear receta
rec <- recipe(presence ~ x1 + x2, data = dat_sf)
# Modelo simple
mod_spec <- logistic_reg() %>%
set_engine("glm") %>%
set_mode("classification")
# Crear workflow
my_workflow <- workflow() %>%
add_model(mod_spec) %>%
add_recipe(rec)
# Resampling espacial
my_cv <- vfold_cv(dat_sf, v = 3)
# Ajustar modelo
fitted <- h3sdm_fit_model(
sdm_workflow = my_workflow,
data_split = my_cv,
presence_data = dat_sf
)
} # }