This function combines cross-validation models by retraining a new model with all presence and absence/background locations and the same hyperparameters.
Arguments
- model
SDMmodelCV object.
Value
An SDMmodel 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.
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")
#> ℹ Extracting predictor information for presence locations
#> ✔ Extracting predictor information for presence locations [37ms]
#>
#> ℹ Extracting predictor information for absence/background locations
#> ✔ Extracting predictor information for absence/background locations [62ms]
#>
# 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)
#> Cross Validation ■■■■■■■■■ 25% | ETA: 6s - 00:00:1.9
#> Cross Validation ■■■■■■■■■■■■■■■■ 50% | ETA: 5s - 00:00:4.9
#> Cross Validation ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 100% | ETA: 0s - 00:00:9.4
#>
# 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")
#> Grid Search ■■■■■■■ 20% | ETA: 13s - 00:00:3.3
#> Grid Search ■■■■■■■■■■■■■ 40% | ETA: 20s - 00:00:13.4
#> Grid Search ■■■■■■■■■■■■■■■■■■■■■■■■■ 80% | ETA: 7s - 00:00:27
#> Grid Search ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 100% | ETA: 0s - 00:00:27
#>
output@results
#> fc reg train_AUC test_AUC diff_AUC
#> 1 lqp 1 0.8649126 0.8516457 0.01326683
#> 2 lqph 1 0.8734156 0.8525413 0.02087433
#> 3 lqp 2 0.8613268 0.8498438 0.01148300
#> 4 lqph 2 0.8672273 0.8497767 0.01745050
output@models
#> [[1]]
#>
#> ── Object of class: <SDMmodelCV> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lqp"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Replicates: 4
#> • Total presence locations: 400
#> • Total absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#> • Categorical: "biome"
#>
#> [[2]]
#>
#> ── Object of class: <SDMmodelCV> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lqph"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Replicates: 4
#> • Total presence locations: 400
#> • Total absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#> • Categorical: "biome"
#>
#> [[3]]
#>
#> ── Object of class: <SDMmodelCV> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lqp"
#> • reg: 2
#>
#> ── Info
#> • Species: Virtual species
#> • Replicates: 4
#> • Total presence locations: 400
#> • Total absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#> • Categorical: "biome"
#>
#> [[4]]
#>
#> ── Object of class: <SDMmodelCV> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lqph"
#> • reg: 2
#>
#> ── Info
#> • Species: Virtual species
#> • Replicates: 4
#> • Total presence locations: 400
#> • Total absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#> • Categorical: "biome"
#>
# Order results by highest test AUC
output@results[order(-output@results$test_AUC), ]
#> fc reg train_AUC test_AUC diff_AUC
#> 2 lqph 1 0.8734156 0.8525413 0.02087433
#> 1 lqp 1 0.8649126 0.8516457 0.01326683
#> 3 lqp 2 0.8613268 0.8498438 0.01148300
#> 4 lqph 2 0.8672273 0.8497767 0.01745050
# 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
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lqph"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 400
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#> • Categorical: "biome"