Skip to contents

Removes records that are unsuitable for niche modelling: cultivated plants, rounded or default coordinates, atlas-grid cell centres, points in towns, car parks or the sea, records far outside the known range and climatic or geographic outliers. It is designed especially for GBIF downloads (it uses GBIF columns such as issue, basisOfRecord, datasetKey and coordinateUncertaintyInMeters), but works on any table with coordinates: tests whose columns are missing are skipped.

Usage

filter_occurrences(
  x,
  settings = filter_settings(),
  layers = filter_layers(),
  exclude_ids = NULL,
  keep_ids = NULL,
  exclude_datasets = NULL,
  exclude_label_regex = NULL,
  cols = NULL,
  verbose = TRUE
)

Arguments

x

A data.frame of occurrences (e.g. from read_gbif()) or a path to a GBIF download.

settings

A filter_settings() object (default: the strict preset), or a list of them named by data type (the data_type column of merge_occurrences()), e.g. list(presence_only = filter_settings("strict"), presence_absence = filter_settings("lenient", drop_tests = "coarse_decimals")); an element default applies to the types not named. Each data type is then filtered on its own: a structured survey need not pass the coordinate-precision tests of a herbarium record.

layers

A filter_layers() object with the optional spatial layers.

exclude_ids, keep_ids

Record IDs (values of the id column, GBIF gbifID) to remove or to keep regardless of any test.

exclude_datasets

GBIF datasetKey values to remove.

exclude_label_regex

Regular expression on the dataset label (institution code or occurrenceID prefix), e.g. "SIVIM|INFOFLORA".

cols

Named character vector to override the column names, e.g. c(lon = "x", lat = "y", uncertainty = "unc_m"). Roles and their GBIF defaults: id (gbifID), dataset (datasetKey), occurrence_id, institution (institutionCode), issue, basis (basisOfRecord), establishment (establishmentMeans), country (countryCode, ISO2), uncertainty (coordinateUncertaintyInMeters), year, locality, habitat, remarks (occurrenceRemarks), species, lon (decimalLongitude), lat (decimalLatitude). Matching is case-insensitive.

verbose

Print progress and the number of records each test removes.

Value

An object of class occ_filter: a list with data (all records, with reason = NA for kept records and diagnostic columns such as climate, built_pct, dist_land_m), log (records removed per test, and skipped tests with the reason), datasets (one row per dataset with the gridded-data diagnostics) and settings. Use kept(), removed(), filter_report(), filter_sensitivity() and write_filter_results().

Details

Every test can be switched on or off and every threshold changed (see filter_settings()). Nothing is removed silently: each record keeps the first test it failed in the reason column, and is kept only if it passes every active test. Tests that need a layer (range polygon, shoreline, climate, land cover) run only when the layer is supplied through filter_layers().

Tests, in the order they run

Names (need the columns added by match_names(); skipped otherwise)

  • name_unmatched: the name does not resolve to the backbone, or only to a rank above the genus (optionally also fuzzy matches and low match confidence: reasons fuzzy_name, low_name_confidence).

  • name_synonym_conflict: the record resolves to an accepted name other than target_taxon (e.g. a synonym that now belongs to another species), or is a hybrid name (hybrid_name).

  • rank_conflict: identified only to a rank coarser than target_rank (e.g. the genus), or to an infraspecific taxon out of scope (infraspecific_excluded), or, with aggregates_in_scope = FALSE, an aggregate (aggregate_name).

Records and metadata

  • invalid_coordinates (always): missing or impossible coordinates.

  • duplicate: same species at the same location (rounded to dedup_digits decimals).

  • grid_cell_centre: coordinate uncertainty equal to half the diagonal of an atlas grid square (e.g. 707.1 m for 1 km squares, as in BSBI monads): the point is a cell centre, not a locality.

  • gbif_issue, basis_of_record, establishment_means: GBIF flags, living or fossil specimens, cultivated or introduced plants.

  • coord_uncertainty: uncertainty above max_coord_unc_m (and, with drop_na_uncertainty, records without one: no_coord_uncertainty).

  • coarse_decimals: both coordinates written with few decimals.

  • garden_text, settlement_text: gardens, car parks, campsites, etc. mentioned in locality, habitat or remarks.

  • locality_is_country: the locality is only a country name (a default position). Needs the countrycode package.

  • too_old: before min_year.

  • manual_exclude, excluded_dataset: your own lists.

Gridded datasets (computed per GBIF datasetKey, before duplicates are removed, because repeated coordinates are the signal)

  • grid_uncertainty_block, lattice_dataset, stacked_dataset: atlas or grid data recognised from a block of identical large uncertainties, a regular mesh of coordinates, or many records stacked on few locations.

CoordinateCleaner (needs the CoordinateCleaner package; please cite Zizka et al. 2019)

Layers

  • outside_range: farther than range_buffer_km from the range polygon.

  • at_sea: farther from land than allowed by the coordinate uncertainty, measured to detailed land polygons; with only a water raster, the coarse fallback reason is on_water.

  • no_climate_data: no value in the climate raster.

  • built_up, cropland, ghs_dense, ghs_building_nearby: settlements and fields.

Isolation and climate (run last, on records that passed everything else; records inside the range polygon are exempt from the isolation tests when protect_in_range = TRUE)

  • isolated_climate_outlier: beyond isolation_climate and with fewer than min_support other records within support_radius_km.

  • small_isolated_cluster: clusters of fewer than min_cluster_n records (chained closer than cluster_eps_km).

  • climate_limit: beyond the hard climate_limit.

  • climate_soft_limit: beyond climate_soft_limit with no cell within refuge_limit in refuge_radius_km (e.g. a warm valley floor is kept when cold mountains are nearby; a lowland park is not).

Absences. With a response column (from merge_occurrences()), records with response 0 (survey sites where the species was not found) are exempt from the tests that judge whether a presence is plausible (outside_range, the isolation and the climate tests), and do not count as support or cluster members for presences; a presence and an absence at the same place are not duplicates.

Finally, records in keep_ids are kept whatever they failed (the reason is moved to reason_overridden).

Status and limits. The individual tests follow the literature and CoordinateCleaner (Zizka et al. 2019); their combination, their order and the tests for atlas-grid data are preENM's. Each test trusts its column: a missing or wrong coordinateUncertaintyInMeters, basisOfRecord or issue lets errors through, and thresholds tuned for one region or group can remove good records elsewhere, e.g. distance-to-range tests for a species that is expanding. What to vary. The thresholds of the tests that remove most records: filter_sensitivity() shows how many records each value would remove; report the settings used.

References

Zizka, A. et al. (2019) CoordinateCleaner: standardized cleaning of occurrence records from biological collection databases. Methods in Ecology and Evolution 10, 744-751. doi:10.1111/2041-210X.13152

Examples

occ <- data.frame(
  gbifID = as.character(1:6), species = "Dryas octopetala",
  decimalLongitude = c("10.12345", "10.12345", "11.2", "12.54321",
                       "13.11111", "14.22222"),
  decimalLatitude = c("46.12345", "46.12345", "47.1", "46.54321",
                      "47.11111", "46.33333"),
  coordinateUncertaintyInMeters = c("30", "30", "50", "707.1", "5000", NA),
  basisOfRecord = c(rep("HUMAN_OBSERVATION", 5), "LIVING_SPECIMEN"))
# (the default CoordinateCleaner tests include "urban", which downloads
#  Natural Earth urban areas; here only offline tests are used)
st <- filter_settings("strict", cc_tests = c("centroids", "equal", "zeros"))
res <- filter_occurrences(occ, st)
#> Filtering 6 records (preset: strict)
#>   invalid_coordinates        removed:       0
#>   name_unmatched             skipped: no name columns: run match_names()
#>   name_synonym_conflict      skipped: no name columns: run match_names()
#>   rank_conflict              skipped: no name columns: run match_names()
#>   duplicate                  removed:       1
#>   grid_cell_centre           removed:       1
#>   gbif_issue                 skipped: no issue column
#>   basis_of_record            removed:       1
#>   establishment_means        skipped: no establishment column
#>   coord_uncertainty          removed:       1
#>   coarse_decimals            removed:       1
#>   garden_text                skipped: no locality, habitat or remarks column
#>   settlement_text            skipped: no locality, habitat or remarks column
#>   locality_is_country        skipped: no locality column
#>   too_old                    skipped: min_year is NA
#>   grid_uncertainty_block     skipped: no dataset column
#>   lattice_dataset            skipped: no dataset column
#>   stacked_dataset            skipped: no dataset column
#>   country_mismatch           skipped: no country column
#> Warning: [vect] guessed crs
#>   (CoordinateCleaner tests: centroids, equal, zeros)
#>   coordinatecleaner          removed:       0
#>   outside_range              skipped: no range polygon in layers
#>   at_sea                     skipped: no land polygons or water raster in layers
#>   no_climate_data            skipped: no climate layer
#>   built_up                   skipped: no built layer
#>   cropland                   skipped: no cropland layer
#>   ghs_dense                  skipped: no ghs_mean layer
#>   ghs_building_nearby        skipped: no ghs_max layer
#>   isolated_climate_outlier   skipped: no climate layer
#>   small_isolated_cluster     removed:       1
#>   climate_limit              skipped: no climate layer
#>   climate_soft_limit         skipped: no climate layer
#> Kept 0 of 6 records
res
#> Occurrence filtering (preset: strict): 6 records -> 0 kept (0%)
#> 
#> Removed, by the first test failed (in test order):
#>                  reason removed
#>               duplicate       1
#>        grid_cell_centre       1
#>         basis_of_record       1
#>       coord_uncertainty       1
#>         coarse_decimals       1
#>  small_isolated_cluster       1
#> 
#> Skipped tests:
#>  test                     note                                      
#>  name_unmatched           no name columns: run match_names()        
#>  name_synonym_conflict    no name columns: run match_names()        
#>  rank_conflict            no name columns: run match_names()        
#>  gbif_issue               no issue column                           
#>  establishment_means      no establishment column                   
#>  garden_text              no locality, habitat or remarks column    
#>  settlement_text          no locality, habitat or remarks column    
#>  locality_is_country      no locality column                        
#>  too_old                  min_year is NA                            
#>  grid_uncertainty_block   no dataset column                         
#>  lattice_dataset          no dataset column                         
#>  stacked_dataset          no dataset column                         
#>  country_mismatch         no country column                         
#>  outside_range            no range polygon in layers                
#>  at_sea                   no land polygons or water raster in layers
#>  no_climate_data          no climate layer                          
#>  built_up                 no built layer                            
#>  cropland                 no cropland layer                         
#>  ghs_dense                no ghs_mean layer                         
#>  ghs_building_nearby      no ghs_max layer                          
#>  isolated_climate_outlier no climate layer                          
#>  climate_limit            no climate layer                          
#>  climate_soft_limit       no climate layer                          
kept(res)
#>  [1] gbifID                        species                      
#>  [3] decimalLongitude              decimalLatitude              
#>  [5] coordinateUncertaintyInMeters basisOfRecord                
#>  [7] lat_decimals                  lon_decimals                 
#>  [9] grid_centre                   reason_overridden            
#> [11] reason                       
#> <0 rows> (or 0-length row.names)