Suggests block_size for spatial_folds() from the distance over which
the predictors are spatially autocorrelated: blocks at least that large
make the held-out fold nearly independent of the training data (Roberts
et al. 2017; Valavi et al. 2019). For each layer the empirical
semivariogram is computed from a sample of cells, and its range is
taken as the shortest distance at which the semivariance reaches
sill_share (default 95%) of the layer's variance. The suggestion is
the median range over the layers, as in blockCV.
Usage
suggest_block_size(
env,
n = 2000,
max_km = NULL,
n_bins = 30,
sill_share = 0.95,
seed = 1,
points = NULL,
value = NULL,
transfer_km = NULL,
n_null = 19,
coords = NULL,
points_crs = "EPSG:4326"
)Arguments
- env
Predictor rasters (terra::SpatRaster or file paths).
- n
Number of cells sampled (all pairs of them are compared).
- max_km
Longest distance examined (default: half the largest distance between sampled cells).
- n_bins
Number of distance classes.
Share of the variance that defines the range.
- seed
Seed for the sample.
- points
Optional records (data.frame with coordinates, CSV path,
sforSpatVector); at most 1000 are used (a random sample).- value
Optional name of a numeric column of
points, e.g. model residuals, whose semivariogram range is reported instead of the clustering of the records.- transfer_km
Optional distance (km) over which the model will be transferred (e.g. from the calibration area to the projection area).
- n_null
Number of random point sets for the clustering envelope.
- coords, points_crs
Coordinate columns and CRS of
points.
Value
A block_size object: a list with block_size_km (the
suggestion), predictor_km (the median range of the predictors),
distances (one row per distance considered: source, km),
ranges (one row per layer: range_km, reached) and variogram
(the semivariance of each layer per distance class, in units of its
variance).
Details
Layers that change steadily across the whole area (a trend, such as
temperature from north to south) have no patch size: their range comes
out large, of the order of the size of the area, or is not reached
within the distances examined (reached = FALSE; the longest distance
is then reported). If the suggested size leaves too few blocks for the
folds, blocks cannot be made independent in this area, and the largest
size that still gives each fold enough records is the practical choice
(see the warnings of spatial_folds()).
Three distances, not one. The autocorrelation of the predictors is a
starting point, but the records and the model's errors can be
autocorrelated over other distances (dispersal limitation, history,
biotic interactions, sampling), and the folds should also reflect the
distance over which the model will be transferred. With points the
function adds the scale of the records: without value, the largest
distance up to which the records are more clustered than random points
in the same area (the share of record pairs in each distance class is
compared with the highest share among n_null sets of random points
from the cells with values, as in a pair-correlation function); with value (e.g. model residuals at the
points), the range of the semivariogram of that column. transfer_km is
the distance you intend to transfer the model over. The suggested
block_size_km is then the largest of the distances available, so that
blocks are independent on every account; predictor_km keeps the
range of the predictors alone.
Status and limits. The variogram range follows blockCV (Valavi et
al. 2019); the comparison of three distances is preENM's, after the
review of Roberts et al. (2017). The clustering scale depends on the
area of the layers (random points are drawn there) and on sampling
bias, which also clusters records.
References
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)
b <- suggest_block_size(sim$env, n = 1000)
b
#> Suggested block size: 395 km
#> Predictors: median range 395 km over 2 layers (1000 cells, distances up to 464 km)
#> layer range_km reached
#> env1 395 TRUE
#> env2 395 TRUE
# folds <- spatial_folds(points, k = 5, block_size = b$block_size_km)
