knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 6 )
Model calibration is one of the most computationally intensive processes automated in kuenm2. In this step, candidate models are trained and tested using a cross-validation approach as defined in the object prepared_data. Then, models are selected based on multiple criteria to warranty that the models used in later steps are the most robust among the candidates. This vignette guides users in running model calibration examples to explore and understand the options included and the results from the process.
At this point it is assumed that kuenm2 is installed (if not, see the Main guide). Load kuenm2 and any other required packages, and define a working directory (if needed).
Note: functions from other packages (i.e., not from base R or kuenm2) used in this guide will be displayed as package::function().
# Load packages library(kuenm2) library(terra) # Current directory getwd() # Define new directory #setwd("YOUR/DIRECTORY") # uncomment and modify if setting a new directory # Saving original plotting parameters original_par <- par(no.readonly = TRUE)
To start the calibration process, we need a prepared_data object. For more details in data preparation, please refer to the vignette prepare data for model calibration.
To start, let's create two prepared_data objects: one using the maxnet as the algorithm, and another with GLMs:
# Import occurrences data(occ_data, package = "kuenm2") # Import raster layers var <- rast(system.file("extdata", "Current_variables.tif", package = "kuenm2")) # Prepare data for maxnet model d_maxnet <- prepare_data(algorithm = "maxnet", occ = occ_data, x = "x", y = "y", raster_variables = var, species = "Myrcia hatschbachii", categorical_variables = "SoilType", partition_method = "kfolds", n_partitions = 4, n_background = 1000, features = c("l", "q", "lq", "lqp"), r_multiplier = c(0.1, 1, 2)) # Prepare data for glm model d_glm <- prepare_data(algorithm = "glm", occ = occ_data, x = "x", y = "y", raster_variables = var, species = "Myrcia hatschbachii", categorical_variables = "SoilType", partition_method = "bootstrap", n_partitions = 10, train_proportion = 0.7, n_background = 300, features = c("l", "q", "p", "lq", "lqp"), r_multiplier = NULL) # Not necessary with glms
The calibration() function fits and evaluates candidate models considering the follow metrics:
In summary, to calibrate and evaluate the models, the function requires a prepared_data object and the following definitions:
Optional arguments allow for modifications such as changing the delta AIC threshold for model selection (default is 2), determining whether to add presence samples to the background (default is TRUE), and whether to employ user-specified weights. For a comprehensive description of all arguments, refer to ?calibration.
In this example, we will evaluate the models considering two omission errors (5% and 10%), with model selection based on a 10% omission error. To accelerate the process, the argument parallel can be set to TRUE and specify the number of cores to utilize. To detect the number of available cores on your machine, run parallel::detectCores().
Let's calibrate the maxnet models:
m_maxnet <- kuenm2::calib_results_maxnet
#Calibrate maxnet models m_maxnet <- calibration(data = d_maxnet, error_considered = c(5, 10), omission_rate = 10, parallel = FALSE, # Set TRUE to run in parallel ncores = 1) # Define number of cores to run in parallel # Task 1/1: fitting and evaluating models: # |=====================================================================| 100% # # Model selection step: # Selecting best among 300 models. # Calculating pROC... # # Filtering 300 models. # Removing 0 model(s) because they failed to fit. # 135 models were selected with omission rate below 10%. # Selecting 2 final model(s) with delta AIC <2. # Validating pROC of selected models... # |=====================================================================| 100% # All selected models have significant pROC values.
The calibration() function returns a calibration_results object, a list containing various essential pieces of information from the calibration process. The elements of this object can be explored by printing the object or indexing them. All evaluation metrics are stored within the calibration_results element, see how to explore them below:
# See first rows of the summary of calibration results head(m_maxnet$calibration_results$Summary[, c("ID", "Omission_rate_at_10.mean", "AICc", "Is_concave")])
We can also examine the details of the selected models:
# See first rows of the summary of calibration results m_maxnet$selected_models[, c("ID", "Formulas", "R_multiplier", "Omission_rate_at_10.mean", "AICc", "Is_concave")]
When printed, the calibration_results object provides a summary of the model selection process. This includes the total number of candidate models considered, the number of models that failed to fit, and the number of models exhibiting concave curves (along with an indication of whether these were removed). Additionally, it reports the number of models excluded due to non-significant partial ROC (pROC) values, high omission error rates, or elevated AIC values. Finally, a summary of the metrics for up to five selected models is presented.
print(m_maxnet)
In this example, of the 300 candidate maxnet models fitted, two were selected based on a significant pROC value, a low omission error (<10%), and a low AIC score (<2).
Now, let's calibrate the GLM Models to see if different models factors are selected with this algorithm:
m_glm <- kuenm2::calib_results_glm
#Calibrate maxnet models m_glm <- calibration(data = d_glm, error_considered = c(5, 10), omission_rate = 10, parallel = FALSE, # Set TRUE to run in parallel ncores = 1) # Define number of cores to run in parallel # Task 1/1: fitting and evaluating models: # |=====================================================================| 100% # Model selection step: # Selecting best among 122 models. # Calculating pROC... # # Filtering 122 models. # Removing 0 model(s) because they failed to fit. # 21 models were selected with omission rate below 10%. # Selecting 1 final model(s) with delta AIC <2. # Validating pROC of selected models... # |=====================================================================| 100% # All selected models have significant pROC values.
Now, instead of two selected models, we have only one:
m_glm
It is worth noting that with both maxnet and glm algorithm, some models were identified as having concave response curves. Concave (or bimodal) curves indicate that higher suitability is found at variable values around a point of lower suitability. For example, as shown in the right panel of the figure below, higher suitability is observed in both the driest and wettest regions, with lower suitabilities occurring at intermediate precipitation levels.
knitr::include_graphics("vignettes_img/concave_curves.png")
In our example, none of the maxnet selected models have concave responses, but the GLM selected had at least one concave response:
#Selected maxnet models m_maxnet$selected_models[, c("ID", "Formulas", "Is_concave")] #Selected glm models m_glm$selected_models[, c("ID", "Formulas", "Is_concave")]
This shows that a model with concave responses could be selected if it has low omission rate and AIC values. To ensure that none of the selected models have concave curves, you can set remove_concave = TRUE within the calibration() function. Let's test it with the maxnet algorithm:
m_unimodal <- calibration(data = d_maxnet, remove_concave = TRUE, # Ensures concave models are not selected error_considered = c(5, 10), omission_rate = 10) # Task 1/2: checking for concave responses in models: # |=====================================================================| 100% # # Task 2/2: fitting and evaluating models with no concave responses: # |=====================================================================| 100% # # Model selection step: # Selecting best among 300 models. # Calculating pROC... # # Filtering 300 models. # Removing 0 model(s) because they failed to fit. # Removing 39 model(s) with concave curves. # 110 models were selected with omission rate below 10%. # Selecting 2 final model(s) with delta AIC <2. # Validating pROC of selected models... # |=====================================================================| 100% # All selected models have significant pROC values.
Note that the process is now divided into two tasks:
Task 1: Only candidate models that include quadratic terms are fitted. For GLMs, all models with quadratic terms are tested. For maxnet models, the version of the models with quadratic terms with the highest regularization multiplier is tested. This is because if a maxnet model produces a concave response at a high regularization value, it will do the same at lower values.
Task 2: In this step, the function fits and evaluates two groups of models: (1) models without quadratic terms; and (2) models with quadratic terms, but only those with formulas that did not produce concave responses in Task 1.
The model selection procedure is conducted internally during the calibration process. However, it is possible to re-select models by considering other omission rates (since these were calculated during calibration), model complexity (delta AIC), and removing or not models with concave responses.
To optimize computational time, calibration() calculates pROC values only for models pre-selected based on omission and complexity considerations (default). Consequently, pROC values for models that were not pre-selected are filled with NA.
# See first rows of the summary of calibration results (pROC values) head(m_maxnet$calibration_results$Summary[, c("ID", "Mean_AUC_ratio_at_10.mean", "pval_pROC_at_10.mean")]) # See pROC values of selected models m_maxnet$selected_models[, c("ID", "Mean_AUC_ratio_at_10.mean", "pval_pROC_at_10.mean")]
When pROC is not calculated for all models during calibration, the select_models() function requires to set compute_proc = TRUE to obtain the necessary results for selections with new criteria. Let's re-select maxnet models applying an omission rate of 5% instead 10%:
# Re-select maxnet models new_m_maxnet <- select_models(calibration_results = m_maxnet, compute_proc = TRUE, omission_rate = 5) # New omission rate print(new_m_maxnet)
If a calibration_results object is provided, select_models() will return a calibration_results output with the selected models and summary updated. Note that we now have different selected models with the maxnet algorithm:
new_m_maxnet$selected_models[,c("ID", "Formulas", "R_multiplier", "Omission_rate_at_5.mean", "Mean_AUC_ratio_at_5.mean", "AICc", "Is_concave")]
You can also provide a data.frame containing the evaluation metrics for each candidate model directly to select_models(). This data.frame is available in the output of the calibration() function under object$calibration_results$Summary. In this case, the function will return a list containing the selected models along with summaries of the model selection process.
#Re-select models using data.frame new_summary <- select_models(candidate_models = m_maxnet$calibration_results$Summary, data = d_maxnet, # Needed to compute pROC compute_proc = TRUE, omission_rate = 5) #Get class of object class(new_summary) #See selected models new_summary$selected_models[, c("ID", "Formulas", "R_multiplier", "Omission_rate_at_5.mean", "Mean_AUC_ratio_at_5.mean", "AICc", "Is_concave")]
After model calibration, selected models can be explored to understand the effect of leaving testing data out in every cross-validation process. This can help understand if leaving a testing partition out changes dramatically response curves compared to using other sets of data. These explorations can also be used to understand if the ability of models to predict testing records derives from extrapolation or not, and whether extrapolations are safe.
The function partition_response_curves() can be used to perform the explorations mentioned above for each of the selected models in the calibration_results object.
# ID of models that were selected m_maxnet$selected_models$ID # Response curves for model 192 partition_response_curves(calibration_results = m_maxnet, modelID = 192)
In the multi-panel plot produced above, we can see the response curves for each of the variables used in the model. Each panel shows:
The same can be done for each of the models selected. Below is a plot for the second model selected in this example.
# Response curves for model 219 partition_response_curves(calibration_results = m_maxnet, modelID = 219)
# Reset plotting parameters par(original_par)
After calibrating and selecting the best-performing models, we can proceed to fit the final models (see the vignette for model exploration) using the calibration_results object. As this object is essentially a list, users can save it to a local directory using saveRDS(). Saving the object facilitates loading it back into your R session later using readRDS(). See an example below:
# Set directory to save (here, in a temporary directory) dir_to_save <- file.path(tempdir()) # Save the data saveRDS(m_maxnet, file.path(dir_to_save, "Candidates_maxnet.rds")) # Import data m_maxnet <- readRDS(file.path(dir_to_save, "Candidates_maxnet.rds"))
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.