
Does the species respond to each predictor, or could it be chance?
Source:R/relevance.R
variable_relevance.RdA test to run before modelling. A set of uncorrelated predictors can still contain layers that mean nothing to the species (collinearity screening looks only at the predictors, not at the species), and models will use them anyway: records are few and spatially clustered, so any smooth layer separates them from the background to some degree, and gains some "importance". This function measures, for every layer, how much the conditions at the records differ from those available in the calibration area, and compares it with the same measure for random layers that have the spatial structure of real ones but no relation to the species.
Usage
variable_relevance(
points,
env,
null = c("field", "shift", "noise"),
n_null = 499,
alpha = 0.05,
n_bg = 10000,
min_shift = 0.1,
coords = NULL,
points_crs = "EPSG:4326",
seed = 1
)Arguments
- points
Records: a data.frame, CSV path,
sfor terra::SpatVector.- env
Predictor layers of the calibration area (terra::SpatRaster or file paths).
- null
The null model:
"field","shift"or"noise".- n_null
Number of random layers per predictor.
- alpha
Largest
p_valueof a relevant layer.- n_bg
Number of cells sampled to describe the calibration area.
- min_shift
With
null = "shift": smallest shift, as a share of the raster's rows and columns.- coords, points_crs
Coordinate columns and their CRS.
- seed
Seed.
Value
A variable_relevance object: a data.frame with one row per
layer, ordered from the most to the least distinct: D, null_mean,
null_95 (the 95th percentile of the null), p_value, relevant,
and two descriptions of the response, shift_sd (mean at the records
minus mean of the area, in standard deviations of the area) and
breadth_ratio (standard deviation at the records / in the area).
Details
Statistic. The Kolmogorov-Smirnov distance D between the values at
the records and the values in the calibration area: the largest
difference between the two cumulative distributions (0 = the species
uses the layer as it is available, 1 = it uses values the area hardly
offers). It responds to a shift (the species prefers high or low
values) and to a narrowing (it avoids both extremes).
Null model. D is recomputed n_null times at the same record
cells for a random layer:
"field"(default): a smooth random surface with the same range of spatial autocorrelation as the layer (estimated as insuggest_block_size()). Works for a calibration area of any shape."shift": the layer itself, moved by a random distance in both directions (wrapping around the edges) and randomly mirrored, which keeps its exact spatial structure (the torus-translation test of Harms et al. 2001). Needs a calibration area that fills most of the raster's rectangle: records that land on cells without values are left out, and a shift is redrawn if fewer than half remain."noise": the values of the layer shuffled among the cells, that is a random variable without any spatial structure (white noise, asrandom_variables()makes withmethod = "shuffle"). This is the classic random variable and an option for comparison, not a recommendation: real layers are smooth and records are clustered, so even a meaningless smooth layer, such as a painting, differs from the background at the records far more than noise does (Fourcade et al. 2018), and nearly every layer passes this test.
Reading the result. p_value is the share of random layers with a
D at least as large as the real one; relevant marks layers with
p_value <= alpha. Layers that are not distinguishable from random ones
are candidates for removal. The test is a screen, with three limits: it
looks at one layer at a time, so a layer that matters only in
combination with another can be missed; a layer correlated with a true
driver is relevant too; and recording bias along a layer (e.g. records
mostly from lowlands) looks like a response, so use thinned records.
The test is also deliberately cautious about layers that are a single
gradient across the whole area (e.g. temperature in a small flat
region): records of a species with a compact range differ from the
background on any such gradient, so the layer cannot be told apart from
geography. Then keep it on ecological grounds, or enlarge the area. To
test the layers inside your models, see random_variables() and
compare_to_random().
Cross-validation. The test uses the records, so choosing predictors
with it is feature selection on the response. If the models are then
cross-validated, run it on the training records of each fold (or on data
kept apart from the evaluation), not on all records: selection before
cross-validation lets the test records influence the model, a common
form of data leakage (Stock et al. 2023, Table 1). Collinearity
screening with select_variables() does not use the records and can be
done once.
Assumptions and limits. The distance compares the records with the whole calibration area, so it also measures sampling bias: a predictor correlated with recording effort differs at the records even if the species does not respond to it. It looks at one predictor at a time and cannot see interactions.
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
Harms, K.E., Condit, R., Hubbell, S.P., Foster, R.B. (2001) Habitat associations of trees and shrubs in a 50-ha neotropical forest plot. Journal of Ecology 89, 947-959. Fourcade, Y., Besnard, A.G., Secondi, J. (2018) Paintings predict the distribution of species, or the challenge of selecting environmental predictors and evaluation statistics. Global Ecology and Biogeography 27, 245-256. Konowalik, K. (2022) Phylogeography and colonization pattern of subendemic round-leaved oxeye daisy from the Dinarides to the Carpathians. Scientific Reports 12, 16443.
Examples
# three patchy layers; the species lives where the first one is high
sim <- simulate_enm_data(seed = 1)
env <- random_variables(sim$env, n = 3, range_km = 100, seed = 3)
names(env) <- c("temperature", "rainfall", "soil")
set.seed(1)
cells <- sample(terra::ncell(env), 150,
prob = terra::values(env$temperature)[, 1]^6)
records <- as.data.frame(terra::xyFromCell(env, cells))
names(records) <- c("lon", "lat")
variable_relevance(records, env, n_null = 199)
#> Relevance of the predictors: 150 occupied cells, 199 random layers each (null: field)
#> Not distinguishable from random (p > 0.05): rainfall, soil
#>
#> layer D null_mean null_95 p_value relevant shift_sd breadth_ratio
#> temperature 0.638 0.148 0.263 0.005 TRUE 1.290 0.366
#> rainfall 0.177 0.146 0.258 0.250 FALSE -0.346 0.960
#> soil 0.170 0.146 0.265 0.310 FALSE -0.210 0.883