The following steps are described in the basic-use vignette, refer to it if the following code is not clear:
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) data <- prepareSWD(species = "Virtual species", p = virtualSp$presence, a = virtualSp$background, env = predictors, categorical = "biome") c(train, test) %<-% trainValTest(data, test = 0.2, only_presence = TRUE, seed = 25) # Train model model <- train("Maxent", data = train) # Train cross validation model folds <- randomFolds(data, k = 4, only_presence = TRUE, seed = 25) cv_model <- train("Maxent", data = data, folds = folds)
For a Maxent model we can get the variable importance values from the output of the MaxEnt Java software. These values are stored in the model object and can be displayed using the following command:
maxentVarImp extracts the variable importance values from the previous output and formats them in a more human readable way:
As you can see the function returns a
data.frame with the variable name, the percent contribution and the permutation importance.
You can plot the variable importance as a bar chart using the function
plotVarImp. For example you can plot the percent contribution using:
or the permutation importance with:
SDMtune has its own function to compute the permutation importance that iterates through several permutations and return an averaged value together with the standard deviation. We will use this function to compute the permutation importance of a Maxnet model.
For this example we train a Maxnet model:
Now we can calculate the variable importance with the function
varImp() using 5 permutations:
And plot it with:
Next we compute the permutation importance for the Maxent model using 10 permutations and compare the results with the Maxent output:
The difference is probably due to a different shuffling of the presence and background locations during the permutation process and because in this example we performed 10 permutations and averaged the values.
Another method to estimate the variable importance is the leave one out Jackknife test. The test removes one variable at time and records the change in the chosen metric. We use the function
doJk, the AUC as evaluation metric and the
We can also plot the output using the function
plotJk. In the following example we plot the previous result and we add a line representing the AUC of the full model trained using all the variables. First we plot the Jackknife test for the training AUC:
and the Jackknife test for the testing AUC:
With the function
plotResponse is possible to plot the marginal and the univariate response curve. Let’s plot the cloglog univariate response curve of bio1:
On top is displayed the rug of the presence locations and on bottom the rug of the background locations. As another example we can plot the logistic marginal response curve of biome that is a categorical variable, keeping the other variables at the mean value:
In the case of an
SDMmodelCV the response curve shows the averaged value of the prediction together with one Standard Deviation error interval:
All what you have learned till now con be saved and summarized calling the function
modelReport. The function will:
.Rdsextension that can be loaded in R using the
The function is totally inspired by the default output of the MaxEnt Java software (Phillips, Anderson, and Schapire 2006) and extends it to other methods. You can decide what to include in the report using dedicated function arguments, like
env but the function cannot be used with
SDMmodelCV objects. Run the following code to create a report of the Maxnet model we trained before:
The output is displayed in the browser and all the files are saved in the virtual-sp folder.
To explore correlation among the variables we extract 10000 background locations using the function
randomPoints included in the
dismo package (we set the seed to have reproducible results). After we create an
SWD object using the
The environmental variables we downloaded have a coarse resolution and the function can extract a bit less than 10000 random locations (see the warning message).
With the function
plotCor you can create an heat map showing the degree of autocorrelation:
You can select a different correlation method or set a different correlation threshold. Another useful function is
corVar that instead of creating a heat map prints the pairs of correlated variables according to the given method and correlation threshold:
As you can see there are few variables that have a correlation coefficient greater than 0.7 in absolute value.
There are cases in which a model has some environmental variables ranked with very low contribution and you may want to remove some of them to reduce the model complexity. SDMtune offers two different strategies implemented in the function
reduceVar. We will use the Maxent model trained with all the variables. Let’s first check the permutation importance (we use only one permutation to save time):
In the first example we want to remove all the environmental variables that have a permutation importance lower than 6%, no matter if the model performance decreases. The function removes the last ranked environmental variable, trains a new model and computes a new rank. The process is repeated until all the remaining environmental variables have an importance greater than 6%:
In the second example we want to remove the environmental variables that have a permutation importance lower than 15% only if removing the variables the model performance does not decrease, according to the given metric. In this case the function performs a leave one out Jackknife test and remove the environmental variables in a step-wise fashion as described in the previous example, but only if the model performance doesn’t drop:
As you can see in this case several variables have been removed and the AUC in the testing dataset didn’t decrease.
Phillips, Steven J, Robert P Anderson, and Robert E Schapire. 2006. “Maximum entropy modeling of species geographic distributions.” Ecological Modelling 190: 231–59. https://doi.org/10.1016/j.ecolmodel.2005.03.026.