# 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")
# Split presence locations in training (80%) and testing (20%) datasets
datasets <- trainValTest(data,
test = 0.2,
only_presence = TRUE)
train <- datasets[[1]]
test <- datasets[[2]]
# Train a model
model <- train(method = "Maxnet",
data = train,
fc = "l")
# Prepare background locations to test autocorrelation, this usually gives a
# warning message given that less than 10000 points can be randomly sampled
bg_coords <- terra::spatSample(predictors,
size = 9000,
method = "random",
na.rm = TRUE,
xy = TRUE,
values = FALSE)
bg <- prepareSWD(species = "Virtual species",
a = bg_coords,
env = predictors,
categorical = "biome")
if (FALSE) {
# Remove variables with correlation higher than 0.7 accounting for the AUC,
# in the following example the variable importance is computed as permutation
# importance
vs <- varSel(model,
metric = "auc",
bg4cor = bg,
test = test,
cor_th = 0.7,
permut = 1)
vs
# Remove variables with correlation higher than 0.7 accounting for the TSS,
# in the following example the variable importance is the MaxEnt percent
# contribution
# Train a model
model <- train(method = "Maxent",
data = train,
fc = "l")
vs <- varSel(model,
metric = "tss",
bg4cor = bg,
test = test,
cor_th = 0.7,
use_pc = TRUE)
vs
# Remove variables with correlation higher than 0.7 accounting for the aicc,
# in the following example the variable importance is the MaxEnt percent
# contribution
vs <- varSel(model,
metric = "aicc",
bg4cor = bg,
cor_th = 0.7,
use_pc = TRUE,
env = predictors)
vs}
Run the code above in your browser using DataLab