# 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")
## Train a Maxent model
model <- train(method = "Maxent",
data = data,
fc = "l",
reg = 1.5,
iter = 700)
# Add samples to background. This should be done preparing the data before
# training the model without using
data <- addSamplesToBg(data)
model <- train("Maxent",
data = data)
## Train a Maxnet model
model <- train(method = "Maxnet",
data = data,
fc = "lq",
reg = 1.5)
## Cross Validation
# Create 4 random folds splitting only the presence data
folds <- randomFolds(data,
k = 4,
only_presence = TRUE)
model <- train(method = "Maxnet",
data = data,
fc = "l",
reg = 0.8,
folds = folds)
if (FALSE) {
# Run only if you have the package ENMeval installed
## Block partition using the ENMeval package
require(ENMeval)
block_folds <- get.block(occ = data@coords[data@pa == 1, ],
bg.coords = data@coords[data@pa == 0, ])
model <- train(method = "Maxnet",
data = data,
fc = "l",
reg = 0.8,
folds = block_folds)
## Checkerboard1 partition using the ENMeval package
cb_folds <- get.checkerboard1(occ = data@coords[data@pa == 1, ],
env = predictors,
bg.coords = data@coords[data@pa == 0, ],
aggregation.factor = 4)
model <- train(method = "Maxnet",
data = data,
fc = "l",
reg = 0.8,
folds = cb_folds)
## Environmental block using the blockCV package
# Run only if you have the package blockCV
require(blockCV)
# Create sf object
sf_df <- sf::st_as_sf(cbind(data@coords, pa = data@pa),
coords = c("X", "Y"),
crs = terra::crs(predictors,
proj = TRUE))
# Spatial blocks
spatial_folds <- cv_spatial(x = sf_df,
column = "pa",
rows_cols = c(8, 10),
k = 5,
hexagon = FALSE,
selection = "systematic")
model <- train(method = "Maxnet",
data = data,
fc = "l",
reg = 0.8,
folds = spatial_folds)}
## Train presence absence models
# Prepare presence and absence locations
p_coords <- virtualSp$presence
a_coords <- virtualSp$absence
# Create SWD object
data <- prepareSWD(species = "Virtual species",
p = p_coords,
a = a_coords,
env = predictors[[1:5]])
## Train an Artificial Neural Network model
model <- train("ANN",
data = data,
size = 10)
## Train a Random Forest model
model <- train("RF",
data = data,
ntree = 300)
## Train a Boosted Regression Tree model
model <- train("BRT",
data = data,
n.trees = 300,
shrinkage = 0.001)
## Multiple methods trained together with default arguments
output <- train(method = c("ANN", "BRT", "RF"),
data = data,
size = 10)
output$ANN
output$BRT
output$RF
## Multiple methods trained together passing extra arguments
output <- train(method = c("ANN", "BRT", "RF"),
data = data,
size = 10,
ntree = 300,
n.trees = 300,
shrinkage = 0.001)
output
Run the code above in your browser using DataLab