The other vignettes are based on presence only methods. Here you will learn how to train a presence absence model. The following examples are based on the Artificial Neural Networks method (Venables and Ripley 2002), but you can adapt the code for any of the other supported methods. We use the first 8 environmental variables and the
virtualSp dataset selecting the absence instead of the background locations.
library(SDMtune) library(zeallot) # Prepare data files <- list.files(path = file.path(system.file(package = "dismo"), "ex"), pattern = "grd", full.names = TRUE) predictors <- raster::stack(files) p_coords <- virtualSp$presence a_coords <- virtualSp$absence data <- prepareSWD(species = "Virtual species", p = p_coords, a = a_coords, env = predictors[[1:8]]) # Split data in training and testing datasets c(train, test) %<-% trainValTest(data, test = 0.2, seed = 25) cat("# Training : ", nrow(train@data)) cat("\n# Testing : ", nrow(test@data)) # Create folds folds <- randomFolds(train, k = 4, seed = 25)
We first train the model with default settings and using 10 neurons:
Let’s check the training and testing AUC:
To check which hyperparameters can be tuned we use the function
We use the function
optimizeModel to tune the hyperparameters:
The best model is:
We now train a model with the same configuration as found by the function
optimizeModel, without cross validation, using all the train data, and we evaluate it using the held apart testing dataset:
Venables, W N, and B. D. Ripley. 2002. Modern Applied Statistics with S. Fourth Edi. New York, NY: Springer.