Skip to contents

Test presences usually lie closer to the training presences than test background (or absence) points do, simply because records are clustered. A model then scores well by predicting high values near known records, and AUC is inflated: "spatial sorting bias" (Hijmans 2012). Its size is

Usage

spatial_sorting_bias(
  presence,
  background,
  train,
  tolerance = 0.33,
  n = 1,
  seed = NULL,
  coords = NULL,
  points_crs = "EPSG:4326",
  crs = NULL
)

Arguments

presence

Test presences (data.frame with coordinates, sf, terra::SpatVector or CSV path).

background

Test background or absence points.

train

Training presences.

tolerance

Largest relative difference between the distances of a presence and its matched background point.

n

Number of background points matched to each presence.

seed

Optional seed for the random order in which presences are matched.

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

A list of class spatial_sorting_bias: ssb, Dp, Da (km), presence_index and background_index (rows of presence and background to evaluate on; background_index[[i]] pairs with presence_index[i] when n = 1), ssb_matched (SSB of the matched sample) and n_dropped (presences without a match).

Details

SSB = Dp / Da,

where Dp is the mean distance from the test presences to the nearest training presence and Da the same for the test background points. SSB = 1 means no bias; values near 0 mean extreme bias (the background lies much farther from the training presences than the test presences do).

Pairwise distance sampling removes the bias: each test presence is paired with the unused background point whose distance to the nearest training presence is most similar; presences without a background point within tolerance (33%, as in Hijmans 2012) of their own distance are dropped, and each background point is used once. Evaluating on the returned presences and matched background gives an AUC free of spatial sorting bias (Hijmans 2012; dismo::pwdSample()).

Limits. Distance matching removes the bias only for points that can be matched; unmatched presences are dropped, which changes the test set.

References

Hijmans, R.J. (2012) Cross-validation of species distribution models: removing spatial sorting bias and calibration with a null model. Ecology 93, 679-688. doi:10.1890/11-0826.1

See also

fold_report(), which reports SSB per fold.

Examples

set.seed(1)
train <- data.frame(lon = runif(100, 10, 12), lat = runif(100, 50, 52))
test <- data.frame(lon = runif(50, 10, 12), lat = runif(50, 50, 52))
bg <- data.frame(lon = runif(500, 8, 16), lat = runif(500, 48, 54))
s <- spatial_sorting_bias(test, bg, train, seed = 1)
s
#> Spatial sorting bias: SSB = 0.07 (Dp 9.94 km / Da 153 km)
#> 
#> IN SHORT
#> - Spatial sorting bias is strong: test presences lie much closer to the
#>   training presences than the test background does, so AUC mostly measures
#>   distance to known records.
#> - The distance-matched sample keeps 46 of 50 test presences and 46
#>   background points; its SSB is 0.98. Evaluate on presence_index and
#>   background_index for an AUC free of this bias.
#> 
#> SSB = Dp / Da: mean distance from test presences (Dp) and from test
#> background (Da) to the nearest training presence; 1 = no bias, near 0 =
#> extreme (Hijmans 2012). Matching: pairwise distance sampling, tolerance 33%.
#> The grades 0.8 and 0.5 are preENM defaults, not from the literature.
# evaluate on test[s$presence_index, ] and bg[s$background_index, ]