# NOT RUN {
# Acquire environmental variables
files <- list.files(path = file.path(system.file(package = "dismo"), "ex"),
pattern = "grd", full.names = TRUE)
predictors <- raster::stack(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
# The next line checks if Maxent is correctly configured but you don't need
# to run it in your script
if (checkMaxentInstallation(verbose = FALSE)) {
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)
# 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 spatial points data frame
library(raster)
sp_df <- SpatialPointsDataFrame(data@coords, data = as.data.frame(data@pa),
proj4string = crs(predictors))
e_folds <- envBlock(rasterLayer = predictors,
speciesData = sp_df,
species = "data@pa",
k = 4,
standardization = "standard",
rasterBlock = FALSE)
model <- train(method = "Maxnet", data = data, fc = "l", reg = 0.8,
folds = e_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