Skip to contents

Cross-validation is meant to test a model on data it has not seen. Data leakage, when information about the test data reaches the model during training, inflates the reported performance and is widespread in ecological machine learning (Stock et al. 2023). fold_report() checks a fold assignment, for each fold held out for testing against the other folds used for training:

Usage

fold_report(
  points,
  folds,
  occ = NULL,
  block_size = NULL,
  env = NULL,
  set = NULL,
  n_bins = 20,
  time = NULL,
  coords = NULL,
  points_crs = "EPSG:4326",
  crs = NULL
)

Arguments

points

The records that were split: a data.frame with coordinates, a CSV path, an sf object or a terra::SpatVector, one row per value of folds.

folds

Fold of each record (e.g. from spatial_folds() or environmental_folds()); NA = not used.

occ

Optional 0/1 vector (1 = presence): counts are given per class and spatial sorting bias is computed.

block_size

Optional block size (km) used to make the folds; taken from attr(folds, "block_size_km") when present.

env

Optional predictor rasters: shared cells and environmental separation are checked (values are read only at the records).

set

Optional vector of background sets or replicates: the checks are made within each set.

n_bins

Number of classes per predictor for the overlap.

time

Optional date or time of each record (numeric, e.g. year, or Date): the share of training records later than the test records is reported per fold.

coords

Optional names of the x/longitude and y/latitude columns when points are tabular. Detected automatically if NULL.

points_crs

CRS of tabular coordinates (default WGS84, "EPSG:4326").

crs

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

Value

A list of class fold_report: folds (one row per fold and set: n_test, n_train and, with occ, presences_test, presences_train; duplicates, shared_cells, min_dist_km, median_dist_km, within_block (share of test records closer than the block size to a training record), separation (median test-to-training distance divided by the median distance between neighbouring records), env_overlap, env_overlap_min, outside_range, ssb, later_train), suspect_layers (names of predictors that look like coordinates or time), leaks (one row per leaking record: index, fold, type, other_folds), leakage (TRUE/FALSE), overall and points (coordinates and folds, for plot_folds()).

Details

  • Leakage: records with the same coordinates in more than one fold, and records in the same raster cell split between folds (the model sees identical predictor values in training and testing). Either means the metrics of the affected folds are inflated.

  • Spatial separation: the distance from each test record to the nearest training record, compared with the block size and with the typical distance between neighbouring records. Folds whose test records are as close to training records as records are to each other are not spatially separated (as in random cross-validation), so spatial autocorrelation makes their metrics optimistic (Yoo and Koo 2025; Koldasbayeva and Zaytsev 2025).

  • Environmental separation (with env): the overlap of the distribution of each predictor between training and testing records (1 = identical, 0 = no overlap), and the share of test records outside the training range of at least one predictor (extrapolation).

  • Spatial sorting bias (with occ): Hijmans' (2012) ratio of the mean distance from test presences to the nearest training presence to that from test background points; see spatial_sorting_bias().

  • Time (with time): the share of training records dated after the earliest test record. If the model will be projected forward in time, training on later records than those tested is leakage of the future (Stock et al. 2023, Table 1); folds ordered in time (forward chaining) avoid it (Koldasbayeva and Zaytsev 2025). For a model of one period this does not matter, so it is reported, not called leakage.

  • Suspect predictors (with env): layers named like coordinates or time (lon, lat, x, y, year, month, doy, ...). Such predictors let a model learn where and when records were made rather than how the species responds to the environment, and the relation will not hold in a projection (Stock et al. 2023, Table 1). The check looks only at the layer names.

Duplicate coordinates and shared cells are definite leakage; the distances and overlaps describe how hard the test is and are reported with graded wording.

References

Stock, A., Gregr, E.J., Chan, K.M.A. (2023) Data leakage jeopardizes ecological applications of machine learning. Nature Ecology & Evolution 7, 1743-1745. doi:10.1038/s41559-023-02162-1

Yoo, M., Koo, H. (2025) Exploring the impact of spatial autocorrelation on optimistic bias in cross-validation and assessing the effectiveness of spatial cross-validation. Cartography and Geographic Information Science 52, 596-609. doi:10.1080/15230406.2024.2422593

Koldasbayeva, D., Zaytsev, A. (2025) Foundation for unbiased cross-validation of spatio-temporal models for species distribution modeling. Ecological Informatics 92, 103521. doi:10.1016/j.ecoinf.2025.103521

Hijmans, R.J. (2012) Cross-validation of species distribution models: removing spatial sorting bias and calibration with a null model. Ecology 93, 679-688. doi:10.1890/11-0826.1

Examples

sim <- simulate_enm_data(seed = 1)
pts <- rbind(cbind(sim$presence, occ = 1), cbind(sim$absence, occ = 0))
f <- spatial_folds(pts, k = 4, block_size = 100, occ = pts$occ, seed = 1)
fold_report(pts, f, occ = pts$occ, env = sim$env)
#> Cross-validation audit: 400 records in 4 folds
#> 
#> IN SHORT
#> - No leakage found: no record has the same coordinates as, or shares a
#>   raster cell with, a record in another fold.
#> - The folds are partly separated in space. Test records lie a median 37.6
#>   km from the nearest training record (closest pair 7.8 km); 100% of test
#>   records have a training record closer than the block size (100 km):
#>   blocks separate whole areas, not every pair of records.
#> - Training and testing environments overlap partly: mean overlap of the
#>   predictor distributions 0.60, lowest in fold 3 (0.54; least overlapping
#>   env2). No test record lies outside the training range of any predictor.
#> - Spatial sorting bias is strong: test presences lie closer to training
#>   presences than test background does (median SSB 0.48). AUC is inflated;
#>   use spatial_sorting_bias() for a distance-matched background.
#> 
#> Per fold:
#>  fold n_test n_train presences_test duplicates shared_cells median_dist_km
#>     1    108     292             29          0            0          47.28
#>     2    101     299             23          0            0          33.61
#>     3     95     305             27          0            0          38.52
#>     4     96     304             21          0            0          36.59
#>  within_block separation env_overlap outside_range  ssb
#>          0.98       3.29        0.60             0 0.48
#>          1.00       2.34        0.57             0 0.38
#>          1.00       2.68        0.54             0 0.47
#>          1.00       2.55        0.69             0 0.51
#> 
#> Leakage = the same coordinates or raster cell in training and testing.
#> separation = median distance from test to nearest training record / median
#> distance between neighbouring records; "not separated" (< 1.5), "partly" (<
#> 5) and "well" are preENM defaults, not from the literature. env_overlap =
#> overlap of the histograms of each predictor (1 = identical), averaged over
#> predictors; "similar" (>= 0.8) and "partly" (>= 0.5) are preENM defaults, not
#> from the literature. SSB = mean distance from test presences to the nearest
#> training presence / the same for test background (Hijmans 2012: 1 = no bias,
#> near 0 = extreme); the grades 0.8 and 0.5 are preENM defaults, not from the
#> literature.
# random folds with a duplicated record: leakage
pts2 <- rbind(pts, pts[1, ])
f2 <- c(sample(1:4, nrow(pts), replace = TRUE), 1 + f[1] %% 4)
fold_report(pts2, f2, occ = pts2$occ)
#> Cross-validation audit: 401 records in 4 folds
#> 
#> IN SHORT
#> - LEAKAGE FOUND in folds 1, 3: 2 records have the same coordinates as
#>   records in another fold. A model tested on these folds was trained on the
#>   same places and predictor values, so their metrics are inflated. Keep one
#>   record per cell (thin_by_cell()) before making folds, or assign folds by
#>   block (spatial_folds()).
#> - The folds are not separated: test records are about as close to training
#>   records as neighbouring records are to each other, as in random
#>   cross-validation, so spatial autocorrelation makes the metrics
#>   optimistic. Test records lie a median 15.7 km from the nearest training
#>   record (closest pair 0 km).
#> - Spatial sorting bias is strong: test presences lie closer to training
#>   presences than test background does (median SSB 0.33). AUC is inflated;
#>   use spatial_sorting_bias() for a distance-matched background.
#> 
#> Per fold:
#>  fold n_test n_train presences_test duplicates shared_cells median_dist_km
#>     1    102     299             32          1           NA          15.61
#>     2    107     294             26          0           NA          15.79
#>     3     99     302             24          1           NA          15.66
#>     4     93     308             19          0           NA          18.25
#>  within_block separation  ssb
#>            NA       1.09 0.29
#>            NA       1.10 0.34
#>            NA       1.10 0.32
#>            NA       1.28 0.44
#> 
#> Leakage = the same coordinates or raster cell in training and testing.
#> separation = median distance from test to nearest training record / median
#> distance between neighbouring records; "not separated" (< 1.5), "partly" (<
#> 5) and "well" are preENM defaults, not from the literature. env_overlap =
#> overlap of the histograms of each predictor (1 = identical), averaged over
#> predictors; "similar" (>= 0.8) and "partly" (>= 0.5) are preENM defaults, not
#> from the literature. SSB = mean distance from test presences to the nearest
#> training presence / the same for test background (Hijmans 2012: 1 = no bias,
#> near 0 = extreme); the grades 0.8 and 0.5 are preENM defaults, not from the
#> literature.