Run the Jackknife test for variable importance removing one variable at time.
Usage
doJk(
model,
metric,
variables = NULL,
test = NULL,
with_only = TRUE,
env = NULL,
return_models = FALSE,
progress = TRUE
)
Arguments
- model
SDMmodel or SDMmodelCV object.
- metric
character. The metric used to evaluate the models, possible values are: "auc", "tss" and "aicc".
- variables
vector. Variables used for the test, if not provided it takes all the variables used to train the model.
- test
SWD. If provided it reports the result also for the testing dataset. Not used for aicc and SDMmodelCV.
- with_only
logical. If
TRUE
it runs the test also for each variable in isolation.- env
rast containing the environmental variables, used only with "aicc".
- return_models
logical. If
TRUE
returns all the models together with the test result.- progress
logical If
TRUE
shows a progress bar.
Value
A data frame with the test results. If return_model = TRUE
it
returns a list containing the test results together with the models.
Examples
# Acquire environmental variables
files <- list.files(path = file.path(system.file(package = "dismo"), "ex"),
pattern = "grd",
full.names = TRUE)
predictors <- terra::rast(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")
#> ℹ Extracting predictor information for presence locations
#> ✔ Extracting predictor information for presence locations [20ms]
#>
#> ℹ Extracting predictor information for absence/background locations
#> ✔ Extracting predictor information for absence/background locations [47ms]
#>
# Split presence locations in training (80%) and testing (20%) datasets
datasets <- trainValTest(data,
test = 0.2,
only_presence = TRUE)
train <- datasets[[1]]
test <- datasets[[2]]
# Train a model
model <- train(method = "Maxnet",
data = train,
fc = "lq")
# Execute the Jackknife test only for the environmental variables "bio1" and
# "bio12", using the metric AUC
doJk(model,
metric = "auc",
variables = c("bio1", "bio12"),
test = test)
#> Variable Train_AUC_without Train_AUC_withonly Test_AUC_without
#> 1 bio1 0.8605459 0.8427728 0.8348900
#> 2 bio12 0.8628366 0.7484241 0.8367775
#> Test_AUC_withonly
#> 1 0.8270363
#> 2 0.7095675
# The same without testing dataset
doJk(model,
metric = "auc",
variables = c("bio1", "bio12"))
#> Variable Train_AUC_without Train_AUC_withonly
#> 1 bio1 0.8605459 0.8427728
#> 2 bio12 0.8628366 0.7484241
# Execute the Jackknife test only for the environmental variables "bio1" and
# "bio12", using the metric TSS but without running the test for one single
# variable
doJk(model,
metric = "tss",
variables = c("bio1", "bio12"),
test = test,
with_only = FALSE)
#> Variable Train_TSS_without Test_TSS_without
#> 1 bio1 0.628325 0.6206
#> 2 bio12 0.633950 0.6240
# Execute the Jackknife test only for the environmental variables "bio1" and
# "bio12", using the metric AICc but without running the test for one single
# variable
doJk(model,
metric = "aicc",
variables = c("bio1", "bio12"),
with_only = FALSE,
env = predictors)
#> Variable AICc_without
#> 1 bio1 5268.356
#> 2 bio12 5267.217
# Execute the Jackknife test for all the environmental variables using the
# metric AUC and returning all the trained models
jk <- doJk(model,
metric = "auc",
test = test,
return_models = TRUE)
#> Jk Test ■■■■■■■■■ 28% | ETA: 3s - 00:00:1.1
#> Jk Test ■■■■■■■■■■■■■ 39% | ETA: 2s - 00:00:1.5
#> Jk Test ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 100% | ETA: 0s - 00:00:3.7
#>
jk$results
#> Variable Train_AUC_without Train_AUC_withonly Test_AUC_without
#> 1 bio1 0.8605459 0.8427728 0.8348900
#> 2 bio12 0.8628366 0.7484241 0.8367775
#> 3 bio16 0.8633309 0.7604431 0.8364225
#> 4 bio17 0.8634928 0.6049622 0.8360250
#> 5 bio5 0.8627328 0.7281881 0.8369975
#> 6 bio6 0.8635784 0.8279953 0.8365125
#> 7 bio7 0.8635141 0.7356097 0.8370350
#> 8 bio8 0.8559647 0.8287769 0.8294275
#> 9 biome 0.8595491 0.7923491 0.8408100
#> Test_AUC_withonly
#> 1 0.8270363
#> 2 0.7095675
#> 3 0.7118025
#> 4 0.5964213
#> 5 0.7127687
#> 6 0.7958225
#> 7 0.6984638
#> 8 0.8274950
#> 9 0.7665000
jk$models_without
#> [[1]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and "bio8"
#> • Categorical: "biome"
#>
#> [[2]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio16", "bio17", "bio5", "bio6", "bio7", and "bio8"
#> • Categorical: "biome"
#>
#> [[3]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio17", "bio5", "bio6", "bio7", and "bio8"
#> • Categorical: "biome"
#>
#> [[4]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio5", "bio6", "bio7", and "bio8"
#> • Categorical: "biome"
#>
#> [[5]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio6", "bio7", and "bio8"
#> • Categorical: "biome"
#>
#> [[6]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio7", and "bio8"
#> • Categorical: "biome"
#>
#> [[7]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", and "bio8"
#> • Categorical: "biome"
#>
#> [[8]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", and "bio7"
#> • Categorical: "biome"
#>
#> [[9]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#> • Categorical: NA
#>
jk$models_withonly
#> [[1]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio1"
#> • Categorical: NA
#>
#> [[2]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio12"
#> • Categorical: NA
#>
#> [[3]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio16"
#> • Categorical: NA
#>
#> [[4]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio17"
#> • Categorical: NA
#>
#> [[5]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio5"
#> • Categorical: NA
#>
#> [[6]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio6"
#> • Categorical: NA
#>
#> [[7]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio7"
#> • Categorical: NA
#>
#> [[8]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: "bio8"
#> • Categorical: NA
#>
#> [[9]]
#>
#> ── Object of class: <SDMmodel> ──
#>
#> Method: Maxnet
#>
#> ── Hyperparameters
#> • fc: "lq"
#> • reg: 1
#>
#> ── Info
#> • Species: Virtual species
#> • Presence locations: 320
#> • Absence locations: 5000
#>
#> ── Variables
#> • Continuous: NA
#> • Categorical: "biome"
#>