Skip to contents

The grain (cell size) of an ENM is usually fixed by whatever resolution the predictor rasters happen to have, yet spatial scale and resolution can change the conclusions of species distribution models (Henderson et al. 2023), and how well suitability predicts local abundance depends on grain and on the scale of the covariates (Brambilla et al. 2023; Ferrer-Ferrando et al. 2025). grain_sensitivity() repeats the preparation steps of preENM at a series of coarser grains and reports how the design changes, so that the grain can be chosen, or at least reported, knowingly. It fits no model.

Usage

grain_sensitivity(
  records,
  env,
  factors = c(1, 2, 4, 8),
  method = c("average", "med", "mode", "near"),
  sizes = 3:6,
  size = NULL,
  thr_r = 0.8,
  thr_vif = 5,
  n_sample = 10000,
  env_thin = TRUE,
  uncertainty = NULL,
  max_rel = 0.1,
  max_radius_km = 20,
  stable = 1,
  thin_args = list(),
  env_thin_args = list(),
  sample_args = list(),
  select_args = list(),
  out_dir = NULL,
  coords = NULL,
  points_crs = "EPSG:4326",
  seed = NULL,
  verbose = TRUE
)

Arguments

records

Presence records: a data.frame with coordinates, a CSV path, an sf object or a terra::SpatVector. They may carry a coordinate-uncertainty column.

env

The predictor layers at their native grain: a terra::SpatRaster (or file paths), or a named list of them, one per data set (e.g. present, LGM, 2070), as for sample_environment(). The first data set is used for the thinning and the uncertainty.

factors

Aggregation factors (whole numbers, 1 = native grain).

method

GDAL resampling method for aggregation: "average" (default), "med" (median), "mode" (for categorical layers) or "near".

sizes, size

Set sizes searched by select_variables(), and the size of the selected set compared between grains (default: the largest size with a passing set at the finest grain).

thr_r, thr_vif

Collinearity limits of select_variables().

n_sample

Cells drawn per data set by sample_environment().

env_thin

Run thin_environment() at every grain.

uncertainty

Coordinate uncertainty for uncertainty_environment(): a column name (default: detected), one value per record or a single value; NA to skip this step.

max_rel, max_radius_km

Passed to uncertainty_environment().

stable

Lowest Jaccard index with the finest grain that still counts as the same selection.

thin_args, env_thin_args, sample_args, select_args

Further arguments (named lists) for thin_by_cell(), thin_environment(), sample_environment() and select_variables().

out_dir

Optional folder to keep the aggregated layers (one sub-folder per grain); by default they are temporary.

coords, points_crs

Coordinate columns and their CRS when records is a table.

seed

Optional random seed, used in the same way at every grain so that differences come from the grain, not from the sampling.

verbose

Print progress.

Value

An object of class grain_sensitivity: table (one row per grain: factor, res_x, res_y, cell_km (side of a square cell of the same area at the records' median latitude), records_cell, records_env, unc_examined, unc_lost, unc_lost_share, n_passing, selected, tied (passing sets sharing the top rank), jaccard, passing_jaccard, collinear_pairs, rho_change, stable), sets (the passing sets of each grain), rho (the Spearman matrices of the first data set), set_agreement, stable_factor, notes (steps that failed at a grain) and settings. See plot_grain_sensitivity() and write_grain_sensitivity().

Details

At each grain (the native resolution times each of factors):

  1. The layers are aggregated on disk with GDAL, file to file, one grain at a time (as in crop_rasters()): every coarse cell gets the mean (method = "average") of the native cells it covers, written as 32-bit floating point so that averages of integer layers are not rounded. Each grain's files are deleted before the next is made, unless out_dir is given.

  2. thin_by_cell(): one record per cell (and species).

  3. thin_environment() on those records (env_thin = TRUE). As the records now sit at cell centres, their positional accuracy is taken as one cell of that grain (radius_km), unless env_thin_args says otherwise.

  4. uncertainty_environment() on all records, if they have a coordinate uncertainty: the share of records whose uncertainty circle spans too much of the niche. The share can go either way: a coarse cell contains the circle more often, but a circle that crosses the edge of a coarse cell spans two cells whose values differ more.

  5. sample_environment() and select_variables() on every data set in env: the passing variable sets, and the selected set, i.e. the top-ranked set of size variables. With a single data set the only criterion is the VIF; give the periods or scenarios the model will be projected to as further data sets so that novelty and gradient stability count too, as in your own selection.

Agreement. The selected set at each grain is compared with the one at the finest grain by the Jaccard index (shared variables / all variables of the two sets; 1 = the same set). set_agreement is the mean Jaccard over all pairs of grains (1 when the same set is selected at every grain; a grain without a passing set counts as 0 against one with a set). The collections of passing sets are compared in the same way (passing_jaccard), which is less sensitive to near ties in the ranking. The correlation structure is summarised as the number of pairs with |Spearman rho| of at least thr_r in any data set, and the mean absolute change of rho from the finest grain.

Stable grain. The coarsest grain up to which the selected set agrees with the finest grain at every grain, with a Jaccard of at least stable (default 1, the same set). This is the practical answer: the grain can be coarsened that far without changing the predictors.

Memory use stays low: only the values at the records and random samples of cells are read (via terra::extract() and the samplers of preENM).

Status and limits. A preENM method. Averaging to coarser cells smooths extremes, so coarse grains lose the conditions of small habitats such as cold summits or wet hollows; the comparison is of the data design, not of model performance, which needs models fitted at each grain.

References

Henderson, A.F., Santoro, J.A., Kremer, P. (2023) Impacts of spatial scale and resolution on species distribution models of American chestnut (Castanea dentata) in Pennsylvania, USA. Forest Ecology and Management 529, 120741. doi:10.1016/j.foreco.2022.120741

Brambilla, M., Bazzi, G., Ilahiane, L. (2023) The effectiveness of species distribution models in predicting local abundance depends on model grain size. Ecology 105, e4224. doi:10.1002/ecy.4224

Ferrer-Ferrando, D., Tarroso, P., Telleria, J.L., Acevedo, P., Fernandez-Lopez, J. (2025) Disentangling the effect of the spatial scale and species spatial pattern on the abundance-suitability relationship. Ecography 2025, e07766. doi:10.1111/ecog.07766

Examples

# \donttest{
sim <- simulate_enm_data(seed = 1, n_presence = 200)
e <- sim$env
env <- c(e, e[[1]] * e[[2]], sqrt(e[[2]]))
names(env) <- c("t", "p", "txp", "sp")
g <- grain_sensitivity(sim$presence, env, factors = c(1, 2, 4),
                       sizes = 2:3, n_sample = 2000, env_thin = FALSE,
                       uncertainty = 5000, seed = 1, verbose = FALSE)
g
#> Grain sensitivity: 3 grain(s), x1 to x4 the native 0.125 x 0.1 degrees; 1 data set(s)
#> 
#> IN SHORT
#> - The selected set of 2 variables (t p) is the same at every grain tested,
#>   up to x4 (about 39 km), the coarsest tested; coarser grains were not
#>   examined.
#> - Over all pairs of grains, the selected sets agree with a mean Jaccard
#>   index of 1; the collections of passing sets with the finest grain, 1, 1.
#> - At x1, x2, x4, 2 passing sets share the top rank, so the selected set is
#>   only the first of equals there and its Jaccard index may change by
#>   chance; the agreement of the passing sets is the safer measure. Giving
#>   the data sets of the projection periods adds criteria that separate the
#>   sets.
#> - Cell thinning keeps 200 records at x1 (about 9.8 km) and 140, 70% of
#>   them, at x4 (about 39 km).
#> - Records too uncertain for the grain: 1% at x1, 12% at x4.
#> - Grain is a choice about the ecology (the scale at which the species
#>   responds) and the data (the positional accuracy of the records), not only
#>   about the variables: use the stable range as the room within which to
#>   choose, and report the grain.
#> 
#>  factor cell_km records_cell unc_lost_share n_passing selected tied jaccard
#>       1    9.81          200           0.01         5      t p    2       1
#>       2   19.63          184           0.03         5      t p    2       1
#>       4   39.25          140           0.12         5      t p    2       1
#>  passing_jaccard collinear_pairs rho_change
#>                1               1       0.00
#>                1               1       0.00
#>                1               1       0.02
#> 
#> factor = aggregation factor of the native cells (average); cell_km = side
#> of a square cell of the same area at the records' median latitude;
#> records_cell / records_env = records kept by thin_by_cell() /
#> thin_environment(); unc_lost_share = share of the examined records whose
#> uncertainty circle spans more than 0.1 of the niche breadth
#> (uncertainty_environment()); selected = top-ranked passing set of 2
#> variables (select_variables(), |rho| < 0.8, VIF <= 5); tied = passing sets
#> sharing the top rank; jaccard = shared / all variables of this and the
#> finest grain's set; passing_jaccard = the same for the collections of
#> passing sets; collinear_pairs = pairs with |rho| >= 0.8 in any data set;
#> rho_change = mean |change| of Spearman rho from the finest grain. Stable =
#> Jaccard >= 1 at this and every finer grain; this limit and the reading of
#> the Jaccard index (1 the same set, 0.5 or less mostly different) are preENM
#> defaults, not from the literature.
#> 
#> A preENM sensitivity analysis: how much the result depends on the grain of
#> the rasters. The others are listed in ?sensitivity_analyses.
plot_grain_sensitivity(g)

# }