Learn R Programming

SDMtune (version 0.1.0)

reduceVar: Reduce Variables

Description

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,
  parallel = FALSE, use_jk = FALSE, permut = 10, use_pc = FALSE)

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. Test dataset used to evaluate the model, not used with aicc, and if use_jk = FALSE, default is NULL.

env

stack containing the environmental variables, used only with "aicc", default is NULL.

parallel

logical, if TRUE it uses parallel computation, default is FALSE. 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, default is FALSE.

permut

integer. Number of permutations, used if use_pc is FALSE, default is 10.

use_pc

logical, use percent contribution. 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, default is FALSE.

Value

The model trained using the selected variables.

Examples

Run this code
# NOT RUN {
# Acquire environmental variables
files <- list.files(path = file.path(system.file(package = "dismo"), "ex"),
                    pattern = "grd", full.names = TRUE)
predictors <- raster::stack(files)

# Prepare presence locations
p_coords <- condor[, 1:2]

# Prepare background locations
bg_coords <- dismo::randomPoints(predictors, 5000)

# Create SWD object
presence <- prepareSWD(species = "Vultur gryphus", coords = p_coords,
                       env = predictors, categorical = "biome")
bg <- prepareSWD(species = "Vultur gryphus", coords = bg_coords,
                 env = predictors, categorical = "biome")

# Split presence locations in training (80%) and testing (20%) datasets
datasets <- trainValTest(presence, test = 0.2)
train <- datasets[[1]]
test <- datasets[[2]]

# Train a Maxnet model
model <- train(method = "Maxnet", p = train, a = bg, fc = "lq")

# Remove all variables with permuation importance lower than 2%
output <- reduceVar(model, th = 2, metric = "auc", test = test, permut = 1)

# Remove variables with permuation importance lower than 2% only if testing
# TSS doesn't decrease
output <- reduceVar(model, th = 2, 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", p = train, a = bg, fc = "lq")

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

Run the code above in your browser using DataLab