Skip to contents

A target-group background draws background points where records of a group of related or similarly recorded species were made, so that the background has the same sampling bias as the presences (Barber et al. 2021). gbif_target_group_sql() builds the GBIF SQL download of such a group: e.g. the other species of the genus or the family, without your own species. Run it with download_gbif_effort() and give the result as target in sample_background() (method = "target_group").

Usage

gbif_target_group_sql(
  extent,
  group,
  exclude_species = NULL,
  cell = NULL,
  years = NULL,
  max_uncertainty_m = 10000,
  basis = c("HUMAN_OBSERVATION", "PRESERVED_SPECIMEN", "OBSERVATION", "OCCURRENCE",
    "MATERIAL_SAMPLE")
)

Arguments

extent

Study extent c(xmin, xmax, ymin, ymax) in degrees.

group

The target group: a named value such as c(genus = "Dryas") or c(family = "Rosaceae") (any of kingdom, phylum, class, order, family, genus, or their ...Key), several values for one rank (c(genus = "Salix", genus = "Betula")), or an SQL condition.

exclude_species

Species names to leave out (your own species).

cell

Optional cell size (degrees) to count records per cell.

years

Optional range of years, e.g. c(1980, 2025).

max_uncertainty_m

Records with a larger stated uncertainty are left out.

basis

Accepted basisOfRecord values.

Value

The SQL query (a single string).

Details

  • cell = NULL (default): one row per record, with coordinates, year and recordedBy (GBIF stores observers as a list; the text is kept as written), for kernel smoothing and observer weighting in sample_background().

  • cell given: records counted per grid cell (like gbif_effort_sql()); turn the table into a raster with gbif_effort_rasters() and give its n_records layer as target.

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

Examples

cat(gbif_target_group_sql(c(5, 17, 43, 48), c(genus = "Dryas"),
                          exclude_species = "Dryas octopetala"))
#> SELECT gbifID, species, decimalLongitude, decimalLatitude, coordinateUncertaintyInMeters, "year", basisOfRecord, recordedBy FROM occurrence WHERE (genus IN ('Dryas')) AND (species IS NULL OR species NOT IN ('Dryas octopetala')) AND hasCoordinate = TRUE AND hasGeospatialIssues = FALSE AND occurrenceStatus = 'PRESENT' AND basisOfRecord IN ('HUMAN_OBSERVATION', 'PRESERVED_SPECIMEN', 'OBSERVATION', 'OCCURRENCE', 'MATERIAL_SAMPLE') AND (coordinateUncertaintyInMeters IS NULL OR coordinateUncertaintyInMeters <= 10000) AND decimalLatitude >= 43 AND decimalLatitude < 48 AND decimalLongitude >= 5 AND decimalLongitude < 17
cat(gbif_target_group_sql(c(5, 17, 43, 48), c(family = "Rosaceae"),
                          cell = 0.1, years = c(1980, 2025)))
#> SELECT FLOOR(decimalLatitude * 10) AS lat_bin, FLOOR(decimalLongitude * 10) AS lon_bin, COUNT(*) AS n_records, COUNT(DISTINCT speciesKey) AS n_species, 0 AS n_target FROM occurrence WHERE (family IN ('Rosaceae')) AND "year" >= 1980 AND "year" <= 2025 AND hasCoordinate = TRUE AND hasGeospatialIssues = FALSE AND occurrenceStatus = 'PRESENT' AND basisOfRecord IN ('HUMAN_OBSERVATION', 'PRESERVED_SPECIMEN', 'OBSERVATION', 'OCCURRENCE', 'MATERIAL_SAMPLE') AND (coordinateUncertaintyInMeters IS NULL OR coordinateUncertaintyInMeters <= 10000) AND decimalLatitude >= 43 AND decimalLatitude < 48 AND decimalLongitude >= 5 AND decimalLongitude < 17 GROUP BY FLOOR(decimalLatitude * 10), FLOOR(decimalLongitude * 10)