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 [44ms]
#> 
#>  Extracting predictor information for absence/background locations
#>  Extracting predictor information for absence/background locations [66ms]
#> 

# 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.8615253          0.8431000        0.8381525
#> 2    bio12         0.8646128          0.7507094        0.8372200
#>   Test_AUC_withonly
#> 1         0.8278400
#> 2         0.7004263

# The same without testing dataset
doJk(model,
     metric = "auc",
     variables = c("bio1", "bio12"))
#>   Variable Train_AUC_without Train_AUC_withonly
#> 1     bio1         0.8615253          0.8431000
#> 2    bio12         0.8646128          0.7507094

# 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.63095           0.6049
#> 2    bio12           0.63980           0.6086

# 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     5270.823
#> 2    bio12     5257.704

# 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  ■■■■■■■■                          22% | ETA:  4s - 00:00:1.1
#> Jk Test  ■■■■■■■■■■■■■                     39% | ETA:  3s - 00:00:2.1
#> Jk Test  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■     94% | ETA:  0s - 00:00:4.8
#> Jk Test  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  100% | ETA:  0s - 00:00:05
#> 

jk$results
#>   Variable Train_AUC_without Train_AUC_withonly Test_AUC_without
#> 1     bio1         0.8615253          0.8431000        0.8381525
#> 2    bio12         0.8646128          0.7507094        0.8372200
#> 3    bio16         0.8638516          0.7582772        0.8373400
#> 4    bio17         0.8646478          0.6151031        0.8379325
#> 5     bio5         0.8636816          0.7394456        0.8376125
#> 6     bio6         0.8646297          0.8164175        0.8369925
#> 7     bio7         0.8646666          0.7186294        0.8374800
#> 8     bio8         0.8567178          0.8312512        0.8300225
#> 9    biome         0.8618691          0.7900763        0.8357625
#>   Test_AUC_withonly
#> 1         0.8278400
#> 2         0.7004263
#> 3         0.7204662
#> 4         0.5558575
#> 5         0.6681212
#> 6         0.8285187
#> 7         0.7446375
#> 8         0.8174625
#> 9         0.7780738
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"
#>