Skip to contents

Summarises spatial (or any) cross-validation results per model or algorithm. You supply, for every cross-validation run, the predictions of the model on the points it was not trained on; the function computes AUC, MAE, bias, TSS and the Boyce index for each run and averages them.

Usage

evaluate_cv(
  data,
  model = "model",
  fold = "fold",
  set = NULL,
  obs = "obs",
  pred = "pred",
  threshold = NULL,
  metrics = NULL,
  coords = NULL,
  points_crs = "EPSG:4326",
  env = NULL,
  block_size = NULL,
  time = NULL,
  audit = NULL,
  rescale = TRUE,
  data_type = c("background", "absence", "pseudo_absence"),
  crs = NULL
)

Arguments

data

A data.frame in long format with one row per held-out point and model run, containing the columns named by model, fold, obs and pred (and set, if used). Predictions from biomod2, ENMeval, Maxent, tidymodels or your own code can all be brought into this form (see the README).

model

Column with the model or algorithm name (results are summarised per value of this column).

fold

Column with the fold that was held out.

set

Optional column with the background set / replicate.

obs

Column with observations (1 = presence, 0 = absence or background).

pred

Column with predicted suitability (0-1).

threshold

Optional column with a fixed threshold per run (e.g. the TSS-optimal or 10th-percentile threshold found on that run's calibration data, as biomod2 does for its validation TSS). Without it, TSS uses the best threshold on the held-out data, which is slightly optimistic.

metrics

Optional extra metrics: a named list of functions function(obs, pred) and/or names of optional metrics ("calibration_slope", "calibration_intercept", "brier", "Boyce_smooth"), computed per run and summarised like the built-in ones (see evaluate_models()).

coords

Optional names of the coordinate columns of data (e.g. c("lon", "lat")): the folds are then audited with fold_report() (within each set, on the rows of the first model), the audit is printed with the metrics and a warning is given when leakage is found.

points_crs

Coordinate reference system of coords.

env, block_size

Optional predictor rasters and block size (km) for the audit; see fold_report().

time

Optional column of data with the date or year of each record, for the time check of the audit.

audit

Alternatively, a ready fold_report().

rescale

Bring the predictions of each model to the 0-1 scale (models on 0-100 or 0-1000 are divided by 100 or 1000, with a message); see extract_predictions().

data_type

What the zeros are: "background" (default), "absence" or "pseudo_absence"; see evaluate_models(). It does not change the numbers, only what the printout says they mean.

crs

Deprecated: the former name of points_crs, still accepted with a warning.

Value

An object of class enm_cv: a data.frame with one row per model, the mean of each metric over runs, their SDs (AUC_sd, ...), n_runs and, when available, AUC_sd_folds and AUC_sd_sets. Per-run metrics are in attr(x, "runs") and mean AUC per fold in attr(x, "by_fold") (a fold that is low for every model points to a region that is hard to predict, not to a bad model). With coords or audit, the fold_report() is in attr(x, "audit").

Details

Runs can also be repeated over several background (pseudo-absence) sets via set. The output then shows whether the background sets or the spatial folds cause more variation (AUC_sd_sets vs AUC_sd_folds): if the sets vary much less than the folds, adding more background sets gains little.

Limits. Averaging over runs treats folds as independent replicates although they share training data, so the standard deviations understate the uncertainty, and folds of very different sizes count equally.

References

Roberts, D.R., Bahn, V., Ciuti, S., et al. (2017) Cross- validation strategies for data with temporal, spatial, hierarchical, or phylogenetic structure. Ecography 40, 913-929. doi:10.1111/ecog.02881

Valavi, R., Elith, J., Lahoz-Monfort, J.J., Guillera-Arroita, G. (2019) blockCV: an R package for generating spatially or environmentally separated folds for k-fold cross-validation of species distribution models. Methods in Ecology and Evolution 10, 225-232. doi:10.1111/2041-210X.13107

Examples

# see the README for a full example with spatial folds and two GLMs
set.seed(1)
d <- data.frame(model = rep(c("good", "poor"), each = 200),
                fold = rep(1:4, 100), obs = rep(rbinom(200, 1, 0.3), 2))
d$pred <- ifelse(d$model == "good", plogis(3 * d$obs - 1.5 +
                 rnorm(400)), runif(400))
evaluate_cv(d)
#> Cross-validated evaluation: 2 model(s), 4 folds
#> Data: presences against background points. AUC measures how well the models
#>   separate presences from the background; MAE, bias, the specificity in TSS
#>   and the calibration metrics treat background points as absences, so they
#>   compare models evaluated on the same background, not absolute errors of
#>   occurrence probability.
#> 
#>  model n_runs   AUC   MAE MAEbal   bias   TSS Boyce AUC_sd MAE_sd MAEbal_sd
#>   good      4 0.989 0.216  0.211 -0.087 0.912 0.425  0.009  0.023     0.019
#>   poor      4 0.551 0.475  0.475 -0.166 0.229 0.131  0.118  0.035     0.058
#>  bias_sd TSS_sd Boyce_sd AUC_sd_folds
#>    0.059  0.050    0.059        0.009
#>    0.086  0.143    0.329        0.118
#> 
#> Mean hold-out AUC per fold:
#>  model fold_1 fold_2 fold_3 fold_4
#>   good  0.993  0.977  0.989  0.998
#>   poor  0.494  0.727  0.472  0.512