Skip to contents

The function performs a random search in the hyperparameters space, creating a population of random models each one with a random combination of the provided hyperparameters values.

Usage

randomSearch(
  model,
  hypers,
  metric,
  test = NULL,
  pop = 20,
  env = NULL,
  interactive = TRUE,
  progress = TRUE,
  seed = NULL
)

Arguments

model

SDMmodel or SDMmodelCV object.

hypers

named list containing the values of the hyperparameters that should be tuned, see details.

metric

character. The metric used to evaluate the models, possible values are: "auc", "tss" and "aicc".

test

SWD object. Test dataset used to evaluate the model, not used with aicc and SDMmodelCV objects.

pop

numeric. Size of the population.

env

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

interactive

logical. If FALSE the interactive chart is not created.

progress

logical. If TRUE shows a progress bar.

seed

numeric. The value used to set the seed to have consistent results.

Value

SDMtune object.

Details

To know which hyperparameters can be tuned you can use the output of the function getTunableArgs. Hyperparameters not included in the hypers argument take the value that they have in the passed model.

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 [21ms]
#> 
#>  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 = "l")

# Define the hyperparameters to test
h <- list(reg = seq(0.2, 3, 0.2),
          fc = c("lqp", "lqph", "lh"))

# Run the function using as metric the AUC
output <- randomSearch(model,
                       hypers = h,
                       metric = "auc",
                       test = test,
                       pop = 10,
                       seed = 25)
#> Random Search  ■■■■■■                            18% | ETA: 10s - 00:00:2.2
#> Random Search  ■■■■■■■■■■■■■■■                   45% | ETA:  8s - 00:00:07
#> Random Search  ■■■■■■■■■■■■■■■■■                 55% | ETA:  8s - 00:00:9.3
#> Random Search  ■■■■■■■■■■■■■■■■■■■■■■■           73% | ETA:  4s - 00:00:11.5
#> Random Search  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■      91% | ETA:  1s - 00:00:14.5
#> Random Search  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  100% | ETA:  0s - 00:00:14.7
#> 
output@results
#>      fc reg train_AUC  test_AUC   diff_AUC
#> 1   lqp 0.2 0.8683369 0.8452413 0.02309562
#> 2   lqp 0.6 0.8667788 0.8447712 0.02200750
#> 3   lqp 1.2 0.8649231 0.8442637 0.02065938
#> 4   lqp 1.8 0.8626844 0.8433263 0.01935812
#> 5  lqph 0.6 0.8774013 0.8422513 0.03515000
#> 6  lqph 1.8 0.8686994 0.8387788 0.02992063
#> 7    lh 1.6 0.8671119 0.8378788 0.02923312
#> 8  lqph 2.0 0.8674688 0.8377488 0.02972000
#> 9  lqph 2.8 0.8639156 0.8366712 0.02724437
#> 10   lh 2.4 0.8626194 0.8358887 0.02673063
output@models
#> [[1]]
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqp"
#>reg: 0.2
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 320
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[2]]
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqp"
#>reg: 0.6
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 320
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[3]]
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqp"
#>reg: 1.2
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 320
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[4]]
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqp"
#>reg: 1.8
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 320
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[5]]
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqph"
#>reg: 0.6
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 320
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[6]]
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqph"
#>reg: 1.8
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 320
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[7]]
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lh"
#>reg: 1.6
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 320
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[8]]
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqph"
#>reg: 2
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 320
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[9]]
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqph"
#>reg: 2.8
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 320
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[10]]
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lh"
#>reg: 2.4
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 320
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 

# Order results by highest test AUC
output@results[order(-output@results$test_AUC), ]
#>      fc reg train_AUC  test_AUC   diff_AUC
#> 1   lqp 0.2 0.8683369 0.8452413 0.02309562
#> 2   lqp 0.6 0.8667788 0.8447712 0.02200750
#> 3   lqp 1.2 0.8649231 0.8442637 0.02065938
#> 4   lqp 1.8 0.8626844 0.8433263 0.01935812
#> 5  lqph 0.6 0.8774013 0.8422513 0.03515000
#> 6  lqph 1.8 0.8686994 0.8387788 0.02992063
#> 7    lh 1.6 0.8671119 0.8378788 0.02923312
#> 8  lqph 2.0 0.8674688 0.8377488 0.02972000
#> 9  lqph 2.8 0.8639156 0.8366712 0.02724437
#> 10   lh 2.4 0.8626194 0.8358887 0.02673063