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

# 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:  5s - 00:00:1.6
#> Cross Validation  ■■■■■■■■■■■■■■■■■■■■■■■           75% | ETA:  1s - 00:00:4.3
#> Cross Validation  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  100% | ETA:  0s - 00:00:5.6
#> 

# 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:8.8
#> Grid Search  ■■■■■■■■■■■■■■■■■■■               60% | ETA:  7s - 00:00:10.3
#> Grid Search  ■■■■■■■■■■■■■■■■■■■■■■■■■         80% | ETA:  4s - 00:00:16
#> Grid Search  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  100% | ETA:  0s - 00:00:16
#> 
output@results
#>     fc reg train_AUC  test_AUC   diff_AUC
#> 1  lqp   1 0.8644779 0.8527638 0.01171417
#> 2 lqph   1 0.8729872 0.8542633 0.01872400
#> 3  lqp   2 0.8613124 0.8503793 0.01093317
#> 4 lqph   2 0.8670053 0.8504928 0.01651250
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.8729872 0.8542633 0.01872400
#> 1  lqp   1 0.8644779 0.8527638 0.01171417
#> 4 lqph   2 0.8670053 0.8504928 0.01651250
#> 3  lqp   2 0.8613124 0.8503793 0.01093317

# 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"