Skip to contents

This function combines cross-validation models by retraining a new model with all presence and absence/background locations and the same hyperparameters.

Usage

combineCV(model)

Arguments

model

SDMmodelCV object.

Value

An SDMmodel object.

Details

This is an utility function to retrain a model with all data after, for example, the hyperparameters tuning (gridSearch, randomSearch or optimizeModel) to avoid manual setting of the hyperparameters in the train function.

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

# Create 4 random folds splitting only the presence data
folds <- randomFolds(data,
                     k = 4,
                     only_presence = TRUE)

model <- train(method = "Maxnet",
               data = data,
               folds = folds)
#> Cross Validation  ■■■■■■■■■                         25% | ETA:  4s - 00:00:1.4
#> Cross Validation  ■■■■■■■■■■■■■■■■■■■■■■■           75% | ETA:  1s - 00:00:4.5
#> Cross Validation  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  100% | ETA:  0s - 00:00:6.2
#> 

# Define the hyperparameters to test
h <- list(reg = 1:2,
          fc = c("lqp", "lqph"))

# Run the function using the AUC as metric
output <- gridSearch(model,
                     hypers = h,
                     metric = "auc")
#> Grid Search  ■■■■■■■                           20% | ETA:  8s - 00:00:02
#> Grid Search  ■■■■■■■■■■■■■                     40% | ETA: 13s - 00:00:09
#> Grid Search  ■■■■■■■■■■■■■■■■■■■               60% | ETA:  7s - 00:00:10.4
#> Grid Search  ■■■■■■■■■■■■■■■■■■■■■■■■■         80% | ETA:  4s - 00:00:16.1
#> Grid Search  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  100% | ETA:  0s - 00:00:16.2
#> 
output@results
#>     fc reg train_AUC  test_AUC    diff_AUC
#> 1  lqp   1 0.8642979 0.8541523 0.010145667
#> 2 lqph   1 0.8736333 0.8552237 0.018409500
#> 3  lqp   2 0.8608411 0.8517622 0.009078833
#> 4 lqph   2 0.8668687 0.8535322 0.013336500
output@models
#> [[1]]
#> 
#> ── Object of class: <SDMmodelCV> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqp"
#>reg: 1
#> 
#> ── Info 
#>Species: Virtual species
#>Replicates: 4
#>Total presence locations: 400
#>Total absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[2]]
#> 
#> ── Object of class: <SDMmodelCV> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqph"
#>reg: 1
#> 
#> ── Info 
#>Species: Virtual species
#>Replicates: 4
#>Total presence locations: 400
#>Total absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[3]]
#> 
#> ── Object of class: <SDMmodelCV> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqp"
#>reg: 2
#> 
#> ── Info 
#>Species: Virtual species
#>Replicates: 4
#>Total presence locations: 400
#>Total absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"
#> 
#> [[4]]
#> 
#> ── Object of class: <SDMmodelCV> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqph"
#>reg: 2
#> 
#> ── Info 
#>Species: Virtual species
#>Replicates: 4
#>Total presence locations: 400
#>Total 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
#> 2 lqph   1 0.8736333 0.8552237 0.018409500
#> 1  lqp   1 0.8642979 0.8541523 0.010145667
#> 4 lqph   2 0.8668687 0.8535322 0.013336500
#> 3  lqp   2 0.8608411 0.8517622 0.009078833

# Combine cross validation models for output with highest test AUC
idx <- which.max(output@results$test_AUC)
combined_model <- combineCV(output@models[[idx]])
combined_model
#> 
#> ── Object of class: <SDMmodel> ──
#> 
#> Method: Maxnet
#> 
#> ── Hyperparameters 
#>fc: "lqph"
#>reg: 1
#> 
#> ── Info 
#>Species: Virtual species
#>Presence locations: 400
#>Absence locations: 5000
#> 
#> ── Variables 
#>Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and
#> "bio8"
#>Categorical: "biome"