Skip to contents

Draws background points from cells of the predictor rasters, one point per cell (without replacement), only from cells where all layers have values. Several independent sets can be drawn (e.g. for the set argument of evaluate_cv()); each uses its own seed (seed + set number). Restrict the rasters to the accessible area (M) beforehand.

Usage

sample_background(
  raster,
  n = 10000,
  n_sets = 1,
  exclude = NULL,
  seed = NULL,
  crs = "EPSG:4326",
  method = c("uniform", "envstrat", "effort", "enveffort", "target_group", "aggregation",
    "thickening"),
  effort = NULL,
  effort_floor = 1,
  effort_power = 1,
  target = NULL,
  target_bandwidth_km = 0,
  target_floor = 0,
  target_observer = NULL,
  presences = NULL,
  target_stratify = FALSE,
  aggregation_k = 1,
  aggregation_r_km = NULL,
  thickening_km = NULL,
  env_pool = 1e+06,
  env_cumvar = 0.95,
  env_axes = NULL,
  env_total_bins = 3000,
  env_min_bin = 5,
  log_vars = NULL
)

Arguments

raster

The predictor layers (terra::SpatRaster or file paths), clipped to the accessible area. For the environmental methods use the variables of your model.

n

Number of points per set (default 10000). If fewer cells are available, all are used, with a warning.

n_sets

Number of independent sets.

exclude

Optional presence records whose cells are excluded: a data.frame with a cell column (as returned by thin_by_cell()) or with coordinates, or a vector of cell numbers.

seed

Optional random seed.

crs

CRS of the output coordinates (default WGS84 longitude/latitude).

method

"uniform", "envstrat", "effort" or "enveffort".

effort

Recording-effort raster for "effort" and "enveffort" (any resolution and CRS; NA counts as 0), e.g. effort_n_species from gbif_effort_rasters().

effort_floor

Added to every cell's effort, so cells without records keep a small chance.

effort_power

Exponent of the effort (1 = proportional; 0.5 = a softer correction).

target

For "target_group": records of the target group (a data.frame with coordinates, a CSV path, sf or SpatVector; a column n, count or n_target gives the number of records at each point, e.g. from an aggregated GBIF table), or a raster of record counts. Leave your own species out of it.

target_bandwidth_km

Standard deviation of the Gaussian kernel (km); 0 = records per cell.

target_floor

Added to the target density of every cell (0: cells without target records are never drawn when the bandwidth is 0).

target_observer

Name of the observer column (e.g. recordedBy) in target and presences, for observer weighting.

presences

Records of your species: with the observer column, for the observer weights of "target_group"; their coordinates, for "aggregation".

target_stratify

Combine the target density with the environmental bins (as "enveffort" does with effort).

aggregation_k, aggregation_r_km

For "aggregation": the exponent of the geographic distance weighting (1 = aggregation background, 0 = similarity background) and the distance (km) added to it (default: one cell).

thickening_km

For "thickening": the radius (km) around each presence; NULL (default) = the mean semivariogram range of the layers.

env_pool

Size of the random pool of cells for the weighted methods.

env_cumvar, env_axes

PCs used for the bins: those explaining env_cumvar of the variance, or exactly env_axes PCs.

env_total_bins

Approximate total number of bins.

env_min_bin

Bins with fewer pool cells are down-weighted.

log_vars

Layers log-transformed for the PCA (default: bioclim precipitation amounts recognised by name).

Value

A data.frame with set, cell, lon, lat (cell centres) and, for the environmental methods, bin. Attribute "background" holds the method and, for the environmental methods, the PCA and bin settings.

Details

Methods

  • "uniform" (default): random cells; every cell equally likely.

  • "envstrat": uniform in environmental space. A PCA of the layers (precipitation amounts log-transformed) is cut into bins: all PCs up to env_cumvar of the variance, about env_total_bins bins in total (env_axes = 2, env_total_bins = 1600 gives the earlier PC1 x PC2, 40 x 40 version). Every occupied bin is equally likely, so rare climates are represented as well as common ones (bins with fewer than env_min_bin cells are down-weighted). It mirrors environmentally thinned presences (thin_environment()).

  • "effort": probability proportional to recording effort, e.g. the number of plant species recorded in GBIF per cell (see gbif_effort_sql()), or to the records of a target group of companion species (a "target-group background"). Corrects for the fact that "not recorded" means "absent" only where people recorded.

  • "enveffort": the envstrat bins with equal total probability per bin, but within a bin cells with more recording effort are preferred (their missing record of the species is a more reliable absence).

  • "target_group": probability proportional to the density of the records of a target group: species of the same genus, family or another group recorded by the same observers and methods, supplied as records in target (e.g. a GBIF download built with gbif_target_group_sql()). Target-group backgrounds corrected sampling bias best among four common methods, though they sometimes overcompensated (Barber et al. 2021). With target_bandwidth_km = 0 the density is the number of target records per cell (the classic target-group background); with a bandwidth it is a Gaussian kernel density estimate, which spreads the effort to neighbouring cells (TGOB+ of Balej et al. 2025, their recommendation when no independent data are available; no single bandwidth was best, so try a few, e.g. 1, 2 and 4 cell widths). With target_observer (the observer column of target and presences) each observer's density is normalised and weighted by that observer's share of the records of your species, as in the presence-weighted observer-oriented approach (PW-OOA) of Balej et al. (2025), which was best when checked against independent data. target_stratify = TRUE combines the density with the environmental bins of "enveffort": every occupied bin keeps the same total probability and the target density decides within it (bins without target records are not sampled unless target_floor > 0).

  • "aggregation": the "aggregation background" of Xu et al. (2024), which estimates the sampling bias from the presences themselves, for when no target group is available: each cell is weighted by the kernel density of the presences on each standardised layer (bandwidth sd * (4 / (3 n))^0.2), each presence weighted by 1 / d^aggregation_k, d its geographic distance from the cell (with aggregation_r_km, default one cell, added to avoid division by 0); the densities are averaged over the layers and rescaled to 0-1. With aggregation_k = 0 only environmental similarity counts (their "similarity background"). It outperformed the target-group background in their simulations and spatial filtering with up to about 70 presences; it puts background where presences are dense in both spaces, so read the models for overcompensation as with any bias correction.

  • "thickening": background thickening of Vollering et al. (2019): each cell is drawn with probability proportional to the number of presences within thickening_km of it, so the background is as clustered as the presences and no presence is discarded. By default the radius is the mean range of the semivariograms of the layers (as in the paper; ranges of hundreds of km are usual), estimated on a sample of cells; cells with no presence within the radius are not sampled. It beat presence thinning with 25 presences and lost to it with 250 in their simulations, so it suits small samples.

No method is best for every species and data set; compare the backgrounds on the same hold-out folds (and independent records, if you have them), with "uniform" as the reference.

For "envstrat", "effort" and "enveffort" the points are drawn from a random pool of env_pool valid cells (all cells if fewer), by weighted sampling without replacement.

Which reference distribution? The methods answer different questions and are not better or worse versions of one another. Uniform sampling represents the geographic availability of conditions in the area; effort weighting represents where people record, the sampling bias the presences share; environmental stratification gives each environmental class equal weight, which changes the distribution the presences are compared with rather than representing availability better. A target-group background assumes the target group shares the focal species' observation process (the same recorders, methods and periods); a group of herbarium specimens is a poor target for citizen-science records.

References

Barber, R.A., Ball, S.G., Morris, R.K.A., Gilbert, F. (2021) Target-group backgrounds prove effective at correcting sampling bias in Maxent models. Diversity and Distributions 28, 128-141. doi:10.1111/ddi.13442

Balej, P., Moudry, V., Prajzlerova, D., Gabor, L., Sillero, N., Rocchini, D., Simova, P. (2025) Species-observer link and kernel density estimation of background points allow for sampling bias correction in bird species distribution models. Ecography 2025, e08202. doi:10.1002/ecog.08202

Vollering, J., Halvorsen, R., Auestad, I., Rydgren, K. (2019) Bunching up the background betters bias in species distribution models. Ecography 42, 1717-1727. doi:10.1111/ecog.04503

Xu, Q., Wang, X., Yi, J., Wang, Y. (2024) Bias correction in species distribution models based on geographic and environmental characteristics. Ecological Informatics 81, 102604. doi:10.1016/j.ecoinf.2024.102604

Examples

r <- terra::rast(nrows = 20, ncols = 20, xmin = 0, xmax = 10, ymin = 40,
                 ymax = 50, vals = 1)
bg <- sample_background(r, n = 50, n_sets = 3, seed = 1)
#> Cells available for background: 400
table(bg$set)
#> 
#>  1  2  3 
#> 50 50 50 

# uniform in environmental space, with recording effort within bins
sim <- simulate_enm_data(seed = 1)
effort <- terra::init(sim$env[[1]], function(n) rpois(n, 5))
bg2 <- sample_background(sim$env, n = 500, method = "enveffort",
                         effort = effort, seed = 1)
#> Cells available for background: 4800
#> Environmental bins: PC1-PC2, 55 bins per axis, 3025 occupied
head(bg2)
#>   set cell     lon   lat   bin
#> 1   1 1337 21.0625 53.35 39 15
#> 2   1 2622 21.6875 51.75 43 30
#> 3   1 3464 16.9375 50.65 17 41
#> 4   1 4286 19.6875 49.65 32 50
#> 5   1 3402 19.1875 50.75 29 40
#> 6   1 2384 21.9375 52.05 44 28