Skip to contents

Remove variables whose importance is less than the given threshold. The function removes one variable at time and after trains a new model to get the new variable contribution rank. If use_jk is TRUE the function checks if after removing the variable the model performance decreases (according to the given metric and based on the starting model). In this case the function stops removing the variable even if the contribution is lower than the given threshold.

Usage

reduceVar(
  model,
  th,
  metric,
  test = NULL,
  env = NULL,
  use_jk = FALSE,
  permut = 10,
  use_pc = FALSE,
  interactive = TRUE,
  verbose = TRUE
)

Arguments

model

SDMmodel or SDMmodelCV object.

th

numeric. The contribution threshold used to remove variables.

metric

character. The metric used to evaluate the models, possible values are: "auc", "tss" and "aicc", used only if use_jk is TRUE.

test

SWD object containing the test dataset used to evaluate the model, not used with aicc, and if use_jk = FALSE.

env

rast containing the environmental variables, used only with "aicc".

use_jk

Flag to use the Jackknife AUC test during the variable selection, if FALSE the function uses the percent variable contribution.

permut

integer. Number of permutations, used if use_pc = FALSE.

use_pc

logical. If TRUE and the model is trained using the Maxent method, the algorithm uses the percent contribution computed by Maxent software to score the variable importance.

interactive

logical. If FALSE the interactive chart is not created.

verbose

logical. If TRUE prints informative messages.

Value

The model trained using the selected variables.

Details

An interactive chart showing in real-time the steps performed by the algorithm is displayed in the Viewer pane.

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

# 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 Maxnet model
model <- train(method = "Maxnet",
               data = train,
               fc = "lq")

# Remove all variables with permuation importance lower than 2%
output <- reduceVar(model,
                    th = 2,
                    metric = "auc",
                    test = test,
                    permut = 1)
#>  The variables bio16, bio6, bio7, bio5, and bio17 have been removed

# Remove variables with permuation importance lower than 3% only if testing
# TSS doesn't decrease
if (FALSE) {
output <- reduceVar(model,
                    th = 3,
                    metric = "tss",
                    test = test,
                    permut = 1,
                    use_jk = TRUE)

# Remove variables with permuation importance lower than 2% only if AICc
# doesn't increase
output <- reduceVar(model,
                    th = 2,
                    metric = "aicc",
                    permut = 1,
                    use_jk = TRUE,
                    env = predictors)

# Train a Maxent model
model <- train(method = "Maxent",
               data = train,
               fc = "lq")

# Remove all variables with percent contribution lower than 2%
output <- reduceVar(model,
                    th = 2,
                    metric = "auc",
                    test = test,
                    use_pc = TRUE)}