Skip to contents

Reduces presence records to one per cell of the rasters you will model with (per species), and moves each kept record to the centre of its cell. This removes the sampling bias of many records in the same cell and makes every record match exactly one cell of the predictors. Records outside the rasters, or in cells where any layer has no value, are dropped.

Usage

thin_by_cell(
  x,
  raster,
  by = "species",
  priority = NULL,
  priority_col = "source",
  centre = TRUE,
  crs = "EPSG:4326",
  coords = NULL,
  points_crs = "EPSG:4326"
)

Arguments

x

Occurrence records: a data.frame with coordinates (e.g. from merge_occurrences() or kept()), a CSV path, an sf object or a terra::SpatVector.

raster

The predictor layers: a terra::SpatRaster (several layers allowed) or file paths. They define the grid; all layers must share extent, resolution and CRS.

by

Column with species names: one record per cell and species. Ignored if absent.

priority

Optional character vector of values of priority_col, from most to least preferred, e.g. c("sNMF", "GBIF").

priority_col

Column holding the source (default "source", as created by merge_occurrences()).

centre

Move kept records to the centre of their cell (default TRUE); the original coordinates are kept in lon_orig, lat_orig.

crs

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

coords

Optional names of the x/longitude and y/latitude columns of x; detected automatically if NULL.

points_crs

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

Value

A data.frame of the kept records with lon, lat (cell centres), cell (cell number), lon_orig, lat_orig and the other columns of x.

Details

When several records fall in one cell, the first one is kept. With priority, records from preferred sources win (e.g. your own verified samples over GBIF records); otherwise the input order decides.

This is geographic thinning to the raster resolution. It is not the same as environmental thinning (thinning in climate space).

References

Boria, R.A., Olson, L.E., Goodman, S.M., Anderson, R.P. (2014) Spatial filtering to reduce sampling bias can improve the performance of ecological niche models. Ecological Modelling 275, 73-77. doi:10.1016/j.ecolmodel.2013.12.012

Examples

r <- terra::rast(nrows = 10, ncols = 10, xmin = 0, xmax = 10, ymin = 40,
                 ymax = 50, vals = 1)
pts <- data.frame(lon = c(0.2, 0.7, 5.5, 20), lat = c(40.2, 40.6, 45.5, 45),
                  source = c("GBIF", "own", "GBIF", "GBIF"))
thin_by_cell(pts, r, priority = c("own", "GBIF"))
#> Outside the raster: 1; inside but without values in all layers: 0; remaining: 3
#> Records in a cell already taken (dropped): 1
#> Largest shift to a cell centre: ~20.24 km
#> Thinned records: 2
#>   lon  lat source cell lon_orig lat_orig
#> 1 0.5 40.5    own   91      0.7     40.6
#> 2 5.5 45.5   GBIF   46      5.5     45.5