Skip to contents

Random cross-validation of species distribution models is over-optimistic because training and test points lie next to each other and share the same environment (spatial autocorrelation). Spatial blocking splits the study area into square blocks and keeps each block entirely in one fold, so every model is tested on areas it has not seen.

Usage

spatial_folds(
  points,
  k = 5,
  block_size,
  occ = NULL,
  n_iter = 100,
  seed = NULL,
  coords = NULL,
  points_crs = "EPSG:4326",
  shift = FALSE,
  crs = NULL
)

Arguments

points

Points to split: a data.frame with coordinates, a CSV path, an sf object or a terra::SpatVector.

k

Number of folds.

block_size

Block side length in kilometres.

occ

Optional 0/1 vector (1 = presence) of the same length as points. When given, presences and absences are balanced separately.

n_iter

Number of random block-to-fold assignments tried.

seed

Optional random seed.

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").

shift

Shift the grid of blocks by a random offset (from seed) instead of starting it at the origin of the projection. Repeat with several seeds to see how much the evaluation depends on where the block boundaries fall (cv_sensitivity()).

crs

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

Value

An integer vector of fold numbers (1 to k), one per point, with attributes block (block id of each point) and block_size_km.

Details

Points are projected to an equal-area projection centred on the data (when they are in longitude/latitude), cut into square blocks of block_size km, and the blocks are assigned to k folds at random. Out of n_iter random assignments the one with the most even numbers of presences and of absences/background points per fold is kept.

Choosing block_size. Blocks should be larger than the range of spatial autocorrelation of the predictors, which can be estimated with blockCV::cv_spatial_autocor(). Values between 50 and 500 km usually suit regional to continental studies (a preENM suggestion, not from the literature). With few presences, use fewer folds (e.g. k = 3) or smaller blocks so that every fold still contains presences.

Limits. Blocks keep nearby points apart but not similar environments (see environmental_folds()). A block size from the predictors' autocorrelation is a starting point: the autocorrelation of the records or residuals, and the distance over which the model will be transferred, matter as much. Repeat with other seeds to see how much the result depends on the placement of the blocks.

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

sim <- simulate_enm_data(seed = 1)
pts <- rbind(cbind(sim$presence, occ = 1), cbind(sim$absence, occ = 0))
pts$fold <- spatial_folds(pts, k = 5, block_size = 100, occ = pts$occ,
                          seed = 1)
table(pts$fold, pts$occ)
#>    
#>      0  1
#>   1 58 19
#>   2 58 23
#>   3 56 24
#>   4 63 18
#>   5 65 16