/
variable-importance.Rmd
266 lines (204 loc) · 9.75 KB
/
variable-importance.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
---
title: "Variable Importance"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE,
comment = "#>",
fig.align = "center")
options(knitr.table.format = "html")
```
```{r load-data, echo=FALSE, message=FALSE}
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)
```
## Intro
In the previous articles you have learned how to [prepare the data](./prepare-data.html) for the analysis, how to [train a model](./train-model.html), how to [make predictions](./make-predictions.html), how to [evaluate a model](./evaluate-model.html) and two different [evaluation strategies](./evaluation-strategies.html) using **SDMtune**. In this article you will learn how to display and plot the variable importance and how to plot the response curves.
## Variable importance for Maxent models
First we load the **SDMtune** package:
```{r load-SDMtune}
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:
```{r maxent-results, eval=FALSE}
default_model@model@results
```
```{r maxent results output, echo=FALSE}
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:
```{r maxent-var-importance, eval=FALSE}
vi <- maxentVarImp(default_model)
vi
```
```{r maxent-var-importance-output, echo=FALSE}
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:
```{r maxent-percent-contribution-plot}
plotVarImp(vi[, 1:2])
```
### Try yourself
Plot the permutation importance as a bar chart. To see the solution highlight the following cell:
```{r maxent-permutation-importance-plot, eval=FALSE, class.source='exercise'}
# 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.
## Permutation importance
For this example we use a **Maxnet** model and a training/testing validation strategy like in the [previous article](./evaluation-strategies.html#train-and-testing-datasets):
```{r train-test}
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:
```{r vi, eval=FALSE}
vi_maxnet <- varImp(maxnet_model,
permut = 5)
vi_maxnet
```
```{r vi -utput, echo=FALSE}
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:
```{r plot-var-imp}
plotVarImp(vi_maxnet)
```
### Try yourself
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:
```{r permut-exercise, eval=FALSE, class.source='exercise'}
# 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.
## Jackknife test for variable importance
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`:
```{r jk, eval=FALSE}
jk <- doJk(maxnet_model,
metric = "auc",
test = test)
jk
```
```{r jk-ouput, echo=FALSE}
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:
```{r plot-jk-train}
plotJk(jk,
type = "train",
ref = auc(maxnet_model))
```
and the Jackknife test for the testing AUC:
```{r plot-jk-test}
plotJk(jk,
type = "test",
ref = auc(maxnet_model, test = test))
```
### Try yourself
Repeat the Jackknife part using the TSS and the AICc as evaluation metric and using the `dafult_model`
## Response curves
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**:
```{r plot-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:
```{r plot-biome}
plotResponse(maxnet_model,
var = "biome",
type = "logistic",
only_presence = TRUE,
marginal = TRUE,
fun = mean,
color = "blue")
```
### Try yourself
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:
```{r plot-exercise, eval=FALSE,class.source='exercise'}
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](./evaluation-strategies.html#cross-validation):
```{r load-model, echo=FALSE}
cv_model <- SDMtune:::bm_maxnet_cv
```
```{r plot-cv-response}
plotResponse(cv_model,
var = "bio1",
type = "cloglog",
only_presence = TRUE,
marginal = TRUE,
fun = mean,
rug = TRUE)
```
## Model report
All what you have learned till now con be saved and summarized calling the function `modelReport()`. The function will:
* save the training, background and testing locations in separated csv files;
* train and evaluate the model;
* create a report in a html format with the ROC curve, threshold values, response curves, predicted map and Jackknife test;
* save the predicted distribution map;
* save all the curves in the plot folder;
* save the model with `.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:
```{r report, eval=FALSE}
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.
## Conclusion
In this article you have learned:
* how to get and plot the variable importance for **Maxent** models;
* how to compute and plot the permutation importance;
* how to perform the leave one out Jackknife test;
* how to plot the marginal and the univariate response curve;
* how to create a model report.
In the [next article](./variable-selection.html) you will learn how to perform data-driven variable selection.