# 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
Run the code above in your browser using DataLab