Skip to contents

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.

Author

Sergio Vignali

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 [45ms]
#> 

# 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.8623625          0.8430191        0.8311688
#> 2    bio12         0.8648106          0.7419578        0.8338612
#>   Test_AUC_withonly
#> 1         0.8260513
#> 2         0.7354325

# The same without testing dataset
doJk(model,
     metric = "auc",
     variables = c("bio1", "bio12"))
#>   Variable Train_AUC_without Train_AUC_withonly
#> 1     bio1         0.8623625          0.8430191
#> 2    bio12         0.8648106          0.7419578

# 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.635100           0.6086
#> 2    bio12          0.641725           0.6147

# 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     5265.443
#> 2    bio12     5260.441

# 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  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  100% | ETA:  0s - 00:00:3.6
#> 

jk$results
#>   Variable Train_AUC_without Train_AUC_withonly Test_AUC_without
#> 1     bio1         0.8623625          0.8430191        0.8311688
#> 2    bio12         0.8648106          0.7419578        0.8338612
#> 3    bio16         0.8644281          0.7509947        0.8331363
#> 4    bio17         0.8652375          0.6066728        0.8336663
#> 5     bio5         0.8648625          0.7306206        0.8334712
#> 6     bio6         0.8653369          0.8244256        0.8342612
#> 7     bio7         0.8650681          0.7256463        0.8339212
#> 8     bio8         0.8556381          0.8365591        0.8342112
#> 9    biome         0.8622994          0.7897078        0.8322188
#>   Test_AUC_withonly
#> 1         0.8260513
#> 2         0.7354325
#> 3         0.7495962
#> 4         0.5895788
#> 5         0.7021563
#> 6         0.8025062
#> 7         0.7270950
#> 8         0.7960738
#> 9         0.7792925
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"
#>