
Thin presence records in environmental space, keeping their environmental coverage
Source:R/env_thin.R
thin_environment.RdDense sampling (e.g. the Alps, Scandinavia) puts many records into the same
part of environmental space, so models overweight those conditions.
thin_environment() keeps a set of records that represents the environment
of all records, without an arbitrary target number, and without
truncating the niche environmentally or geographically:
Usage
thin_environment(
points,
env,
axes = c("pca", "raw"),
log_vars = NULL,
pca_n = 2e+05,
pc_cumvar = 0.95,
pc_min = 2,
radius_km = 1,
window = 1,
width_quantile = 0.5,
width_mult = 1,
max_per_bin = 1,
pick = c("central", "random"),
protect = NULL,
protect_minmax = TRUE,
protect_tail = 0,
protect_hull = TRUE,
protect_rare = 0,
block_km = 50,
block_min = 2,
block_crs = NULL,
template = NULL,
coords = NULL,
points_crs = "EPSG:4326",
seed = NULL,
verbose = TRUE,
n_rep = 1
)Arguments
- points
Presence records: a data.frame with coordinates (e.g. from
thin_by_cell()), a CSV path, ansfobject or a terra::SpatVector.- env
Predictor rasters (terra::SpatRaster or file paths), clipped to the accessible area: the predictors the models will use, since thinning keeps the extremes and coverage only of the layers given.
- axes
"pca"(default) or"raw"(standardised layers).- log_vars
Layers log-transformed (
log1p) for the thinning space: names, orNULLto detect bioclim precipitation amounts (bio12, bio13, bio14, bio16, bio17, bio18, bio19) by name;character(0)for none.- pca_n
Random cells of the raster area used to fit the PCA.
- pc_cumvar, pc_min
Keep the first PCs explaining at least
pc_cumvarof the variance, but at leastpc_minof them.- radius_km
Positional accuracy of a record (km); the window of a record is every cell within this distance of its cell, and at least the 3 x 3 cells around it (a record's position within its cell is unknown, so on rasters coarser than the accuracy the window is one cell around it).
NAuses a fixed window ofwindowcells around the record's cell.- window
Window half-width in cells when
radius_km = NA(1 = 3 x 3).- width_quantile
Quantile of the window ranges used as bin width.
- width_mult
Multiplier of the bin widths (sensitivity runs only).
- max_per_bin
Records kept per occupied bin (protected ones count).
- pick
"central"or"random".- protect
Records always kept: a logical vector, or a named list of values per column, e.g.
list(source = "sNMF").- protect_minmax
Keep the minimum and maximum of every layer.
- protect_tail
Also keep this share of each layer's lowest and highest values (e.g. 0.005); 0 = off.
- protect_hull
Keep the convex-hull vertices of every pair of axes.
- protect_rare
Also keep this share of the records with the most isolated conditions in the full environmental space (largest distance, in bin widths on all axes at once, to their 5th nearest record), e.g. 0.05; 0 = off. It guards rare combinations of conditions that the ranges and the pairwise hulls can miss in more than two dimensions.
- block_km, block_min
Geographic quota: block size (km) and records kept per block (
block_min = 0switches it off).- block_crs
Equal-area CRS for the blocks (default: Lambert azimuthal equal-area centred on the records).
- template
Optional earlier
env_thinningwhose PCA, axes and bin widths are re-used (e.g. to thin lineages or subsets in the same space).- coords, points_crs
Coordinate columns and their CRS when
pointsis a table.- seed
Optional random seed.
- verbose
Print progress.
- n_rep
Number of replicate runs. With more than one, the result is the run with
pickas given, andreplicatesholds the records kept in each ofn_repfurther runs in the same space and bins with a random record per bin (seedsseed + 1,seed + 2, ...): fit a model to several and compare the predictions, e.g. withcoordinate_spread(), to see how much they depend on which records were kept.
Value
An object of class env_thinning: data (all records with their
layer values env_*, axis scores ax_*, bin, block, kept and
keep_reason: protected, extreme_minmax, extreme_tail, isolated, hull,
env_bin,
block_quota or NA = removed; and env_uncertainty, the record's
environmental uncertainty in bin widths), pca, bin_widths,
report (checks; uncertainty gives the share kept in each third of
the records ordered by their uncertainty), settings and, with
n_rep > 1, replicates (row numbers of the records kept in each
run). Use kept(), plot_env_thinning() and write_env_thinning().
Details
Environmental space. A PCA is fitted on a random sample of the whole raster area (the environment available to the species, e.g. M) and the records are placed in it; the first PCs explaining at least
pc_cumvarof the variance are used (axes = "raw"uses the standardised layers instead). Precipitation amounts (log_vars) are log-transformed for this space only, so that differences count in relative terms.Bin width from the data. Every record is accurate to
radius_km, so its true position may be any cell within that distance of its cell (the window spans more columns at high latitudes, where cells are narrow). The range of each axis within the window is the record's environmental uncertainty; the bin width of each axis is its median (width_quantile) over all records. Two records in one bin differ less than a single record's own uncertainty: they are environmental duplicates.One record per bin (
max_per_bin), the one closest to the bin's centre (pick = "central") or a random one. Every occupied bin keeps a record, so every removed record has a kept one less than one bin width away on every axis: only the density is evened out.Always kept: records you protect (e.g. genetic samples), the minimum and maximum of every layer (optionally the outer tails), and the convex hull vertices of every pair of axes (extreme combinations of conditions).
Geographic quota: the area is cut into
block_kmequal-area blocks and every block keeps at leastmin(block_min, records it had); missing records are added choosing the least represented environments first. Small or isolated areas and lone records are kept.
Run it on records already reduced to one per raster cell (see
thin_by_cell()), with the rasters you will model with, clipped to M.
What is kept. The thinned records retain the observed environmental
coverage of all records, by the rules above. That is not the species'
realised or fundamental niche, which the records only sample, and the
safeguards keep each variable's range and the hulls of PC pairs but cannot
guarantee every rare combination of conditions in higher dimensions. The
bin width follows from radius_km, the resolution of the layers and the
local heterogeneity: a record in heterogeneous mountains has a wider
environmental uncertainty than one in a uniform lowland, so thinning is
gentler there; the printout and report$uncertainty show how much.
Compare settings with bias_sensitivity(), e.g. env_thin_args = list(axes = "raw") or list(pc_cumvar = 0.99) against the defaults: a
variable with little overall variance can matter to the species
although the PCA gives it little weight.
Status and what to vary. A preENM method; filtering records in a
gridded environmental space was proposed by Varela et al. (2014), and
geographic filtering by Boria et al. (2014). Vary radius_km (the bin
width), pc_cumvar or axes = "raw", and compare the thinned sets with
bias_sensitivity().
References
Varela, S., Anderson, R.P., Garcia-Valdes, R., Fernandez- Gonzalez, F. (2014) Environmental filters reduce the effects of sampling bias and improve predictions of ecological niche models. Ecography 37, 1084-1091. doi:10.1111/j.1600-0587.2013.00441.x
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
sim <- simulate_enm_data(seed = 1, n_presence = 300)
th <- thin_environment(sim$presence, sim$env, radius_km = 20,
block_km = 100, seed = 1)
#> Environmental space: PC1-PC2 (100% of the variance of 4800 cells)
#> Bin widths (20 km window): PC1 = 0.173, PC2 = 0.231
#> Kept 168 of 300 records (56%); occupied bins: 166; added by the block quota: 2
th
#> Environmental thinning: kept 168 of 300 records (56%)
#> Space: PC1-PC2; bin widths: PC1 = 0.173, PC2 = 0.231
#> Geographic quota: 2 per 100 km block; blocks below quota: 0
#>
#> Records' environmental uncertainty varies 1-fold (10th to 90th percentile, 1
#> to 1 bin widths). Kept: 59% of the third in the most uniform surroundings,
#> 53% of the third in the most varied: varied places (e.g. mountains) are
#> thinned more gently.
#>
#> keep_reason Freq
#> block_quota 2
#> env_bin 150
#> extreme_minmax 4
#> hull 12
#> removed 132
#>
#> Ranges, all vs kept (must be identical):
#> layer min_all min_kept max_all max_kept sd_all sd_kept
#> env1 0.093750 0.093750 0.9938 0.9938 0.2131 0.2276
#> env2 0.008333 0.008333 0.9417 0.9417 0.2347 0.2425
plot_env_thinning(th)