combineCV: Combine Cross Validation models

View source: R/combineCV.R

combineCVR Documentation

Combine Cross Validation models

Description

This function combines cross-validation models by retraining a new model with all presence and absence/background locations and the same hyperparameters.

Usage

combineCV(model)

Arguments

model

SDMmodelCV object.

Details

This is an utility function to retrain a model with all data after, for example, the hyperparameters tuning (gridSearch, randomSearch or optimizeModel) to avoid manual setting of the hyperparameters in the train function.

Value

An SDMmodel object.

Author(s)

Sergio Vignali

Examples

# Acquire environmental variables
files <- list.files(path = file.path(system.file(package = "dismo"), "ex"),
                    pattern = "grd",
                    full.names = TRUE)

predictors <- terra::rast(files)

# Prepare presence and background locations
p_coords <- virtualSp$presence
bg_coords <- virtualSp$background

# Create SWD object
data <- prepareSWD(species = "Virtual species",
                   p = p_coords,
                   a = bg_coords,
                   env = predictors,
                   categorical = "biome")

# Create 4 random folds splitting only the presence data
folds <- randomFolds(data,
                     k = 4,
                     only_presence = TRUE)

model <- train(method = "Maxnet",
               data = data,
               folds = folds)

# Define the hyperparameters to test
h <- list(reg = 1:2,
          fc = c("lqp", "lqph"))

# Run the function using the AUC as metric
output <- gridSearch(model,
                     hypers = h,
                     metric = "auc")
output@results
output@models

# Order results by highest test AUC
output@results[order(-output@results$test_AUC), ]

# Combine cross validation models for output with highest test AUC
idx <- which.max(output@results$test_AUC)
combined_model <- combineCV(output@models[[idx]])
combined_model

SDMtune documentation built on July 9, 2023, 6:03 p.m.