Spatial blocks keep nearby records apart, but folds can still share the
same environment; environmental blocking instead tests each model on
environmental conditions it was not trained on, which matters when the
model will be transferred to new conditions (Roberts et al. 2017; Valavi
et al. 2019; Koldasbayeva and Zaytsev 2025). The records are placed in
the same environmental space as thin_environment() (a PCA of the
standardised predictors, precipitation amounts log-transformed, from a
sample of cells) and clustered into k groups by k-means; each cluster is
a fold. Folds therefore differ in size, and a fold may lack presences
(a warning says so), in which case use fewer folds. Check the result with
fold_report().
Usage
environmental_folds(
points,
env,
k = 5,
occ = NULL,
log_vars = NULL,
pca_n = 2e+05,
pc_cumvar = 0.95,
pc_min = 2,
n_start = 25,
seed = NULL,
coords = NULL,
points_crs = "EPSG:4326",
crs = NULL
)Arguments
- points
Points to split: a data.frame with coordinates, a CSV path, an
sfobject or a terra::SpatVector.- env
Predictor rasters (terra::SpatRaster or file paths); values are read only at the points and at a sample of
pca_ncells.- k
Number of folds.
- occ
Optional 0/1 vector (1 = presence) of the same length as
points. When given, presences and absences are balanced separately.- log_vars, pca_n, pc_cumvar, pc_min
Environmental space, as in
thin_environment().- n_start
Number of random starts of k-means.
- 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").- crs
Deprecated: the former name of
points_crs, still accepted with a warning.
Value
An integer vector of fold numbers (1 to k; NA for points
without predictor values), with attributes centers (cluster centres
on the PCA axes), size and env_space (axes and variance explained).
Details
Limits. k-means clusters differ in size and some may lack presences; the separation depends on the predictors and their scaling. Environmental blocks test transfer to new conditions, so their scores are lower than those of spatial blocks and not comparable with them.
References
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
Roberts, D.R. et al. (2017) Cross-validation strategies for data with temporal, spatial, hierarchical, or phylogenetic structure. Ecography 40, 913-929.
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.
Examples
sim <- simulate_enm_data(seed = 1)
pts <- rbind(cbind(sim$presence, occ = 1), cbind(sim$absence, occ = 0))
f <- environmental_folds(pts, sim$env, k = 4, occ = pts$occ, seed = 1)
table(f, pts$occ)
#>
#> f 0 1
#> 1 81 25
#> 2 109 20
#> 3 35 31
#> 4 75 24
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 well separated in space. Test records lie a median 179 km
#> from the nearest training record (closest pair 20.6 km).
#> - Training and testing environments differ strongly: mean overlap of the
#> predictor distributions 0.34, lowest in fold 3 (0.29; least overlapping
#> env1). 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 106 294 25 0 0 162.91
#> 2 129 271 20 0 0 153.96
#> 3 66 334 31 0 0 224.44
#> 4 99 301 24 0 0 194.52
#> within_block separation env_overlap outside_range ssb
#> NA 11.34 0.34 0 0.50
#> NA 10.72 0.39 0 0.41
#> NA 15.62 0.29 0 0.54
#> NA 13.54 0.32 0 0.46
#>
#> 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.
