knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.align = "center") options(knitr.table.format = "html")
default_model <- SDMtune:::bm_maxent data <- SDMtune:::t files <- list.files(path = file.path(system.file(package = "dismo"), "ex"), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files)
In the previous articles you have learned how to prepare the data for the analysis, how to train a model, how to make predictions, how to evaluate a model and two different evaluation strategies using SDMtune. In this article you will learn how to display and plot the variable importance and how to plot the response curves.
First we load the SDMtune package:
library(SDMtune)
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:
default_model@model@results
kableExtra::kable(default_model@model@results) |> kableExtra::kable_styling(bootstrap_options = c("striped", "hover"), position = "center") |> kableExtra::scroll_box(height = "400px")
The function maxentVarImp()
extracts the variable importance values from the previous output and formats them in a more human readable way:
vi <- maxentVarImp(default_model) vi
vi <- maxentVarImp(default_model) kableExtra::kable(vi) |> kableExtra::kable_styling(bootstrap_options = c("striped", "hover"), position = "center", full_width = FALSE)
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:
plotVarImp(vi[, 1:2])
Plot the permutation importance as a bar chart. To see the solution highlight the following cell:
# The function accepts a data.frame with 2 columns: one with the variable name # and one with the values, so it is enough to select the first and the third # columns from the vi data.frame plotVarImp(vi[, c(1,3)])
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 use a Maxnet model and a training/testing validation strategy like in the previous article:
library(zeallot) # For unpacking assignment c(train, test) %<-% trainValTest(data, test = 0.2, only_presence = TRUE, seed = 25) maxnet_model <- train("Maxnet", data = train)
Now we can calculate the variable importance with the function varImp()
using 5 permutations:
vi_maxnet <- varImp(maxnet_model, permut = 5) vi_maxnet
vi_maxnet <- varImp(maxnet_model, permut = 5) kableExtra::kable(vi_maxnet) |> kableExtra::kable_styling(bootstrap_options = c("striped", "hover"), position = "center", full_width = FALSE)
And plot it with:
plotVarImp(vi_maxnet)
Use the varImp()
function to compute the permutation importance for the default_model
using 10 permutations and compare the results with the Maxent output. To see the solution highlight the following cell:
# Compute the permutation importance vi_maxent <- varImp(default_model, permut = 10) # Print it vi_maxent # Compare with Maxent output maxentVarImp(default_model)
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 maxnet_model
:
jk <- doJk(maxnet_model, metric = "auc", test = test) jk
jk <- doJk(maxnet_model, metric = "auc", test = test, progress = FALSE) kableExtra::kable(jk) |> kableExtra::kable_styling(bootstrap_options = c("striped", "hover"), position = "center", full_width = FALSE)
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:
plotJk(jk, type = "train", ref = auc(maxnet_model))
and the Jackknife test for the testing AUC:
plotJk(jk, type = "test", ref = auc(maxnet_model, test = test))
Repeat the Jackknife part using the TSS and the AICc as evaluation metric and using the dafult_model
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:
plotResponse(maxnet_model, var = "bio1", type = "cloglog", only_presence = TRUE, marginal = FALSE, rug = TRUE)
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:
plotResponse(maxnet_model, var = "biome", type = "logistic", only_presence = TRUE, marginal = TRUE, fun = mean, color = "blue")
Plot in green the univariate cloglog response curve of bio17 removing the rug and using the default_model
, to see the solution highlight the following cell:
plotResponse(default_model, var = "bio17", type = "cloglog", only_presence = TRUE, marginal = FALSE, color = "green")
In the case of an SDMmodelCV()
the response curve shows the averaged value of the prediction together with one Standard Deviation error interval. We use the cross validation model trained in the previous article:
cv_model <- SDMtune:::bm_maxnet_cv
plotResponse(cv_model, var = "bio1", type = "cloglog", only_presence = TRUE, marginal = TRUE, fun = mean, rug = TRUE)
All what you have learned till now con be saved and summarized calling the function modelReport()
. The function will:
.Rds
extension that can be loaded in R using the readRDS
function.The function is totally inspired by the default output of the MaxEnt Java software [@Phillips2006] and extends it to other methods. You can decide what to include in the report using dedicated function arguments, like response_curves
, jk
and 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:
modelReport(maxnet_model, type = "cloglog", folder = "virtual-sp", test = test, response_curves = TRUE, only_presence = TRUE, jk = TRUE, env = predictors)
The output is displayed in the browser and all the files are saved in the virtual-sp folder.
In this article you have learned:
In the next article you will learn how to perform data-driven variable selection.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.