Skip to contents

The first step of the variable selection in select_variables(). Draws random cells from each data set, i.e. the same predictors for different periods (present, Last Glacial Maximum, ...) or climate scenarios (e.g. 2070 SSP5-8.5), and reads their values. Collinearity among predictors can change between periods; a set chosen only on present-day data may be collinear, or extrapolated, in the period a model is projected to.

Usage

sample_environment(
  datasets,
  vars = NULL,
  n = 1e+05,
  reference = NULL,
  regions = NULL,
  region_col = NULL,
  anomalies = NULL,
  ratio_vars = NULL,
  ratio_limits = c(0.1, 10),
  bounds = NULL,
  check_scale = TRUE,
  fix_scale = FALSE,
  area_weighted = TRUE,
  exclude = NULL,
  keep = NULL,
  seed = NULL,
  verbose = TRUE
)

Arguments

datasets

Named list of data sets; each a terra::SpatRaster with one layer per variable, a vector of raster files or a folder. The first is the reference (the calibration period) unless reference says otherwise.

vars

Variables to use. Layers are matched by name: exactly, or as a whole part of the layer name between _, - or . (so bio01 finds CHELSA_TraCE21k_bio01_-100_V1.0). Default: the layer names of the reference.

n

Number of cells drawn per data set (and for the common cells).

reference

Name of the reference data set (default: the first).

regions

Optional regions: polygons (sf, terra::SpatVector or a path) with names in region_col, or a list of tile breaks in the raster coordinates, e.g. list(x = c(-30, 5, 30, 90), y = c(30, 60, 90)).

region_col

Column with the region names (default: the first text column).

anomalies

Optional named list of anomaly data sets, each c(current = , past = , model_present = ) naming data sets.

ratio_vars, ratio_limits

Variables that use a ratio anomaly (e.g. precipitation) and its limits; see anomaly_climate().

bounds

Optional limits per variable applied to anomaly values, e.g. list(scd = c(0, 365)) for snow-cover days.

check_scale

Warn about variables whose spread (interquartile range) differs from the reference by a factor of about 10, i.e. stored x 10 in one data set (as bio07 in some CHELSA-TraCE21k files).

fix_scale

Divide or multiply such variables by 10 to match the reference.

area_weighted

Area-proportional sampling for longitude/latitude rasters.

exclude, keep

Optional masks, to leave cells out of a data set (exclude, e.g. the ice sheet of that period) or to restrict it to some cells (keep, e.g. the land of that period). One mask for all data sets, or a list named by data set, e.g. exclude = list(LGM = "ice_21ka.gpkg"). A mask is polygons (sf, terra::SpatVector or a file; the cells whose centre is inside) or a raster (object or file; the cells with a value other than NA and 0), in any CRS.

seed

Optional random seed.

verbose

Print progress.

Value

An object of class env_sample for select_variables().

Details

Cells. Cells with values in all variables are drawn at random from each data set ("own" cells). For longitude/latitude rasters the sample is area-proportional (cells are accepted with probability proportional to cos(latitude)), so correlations are not dominated by the many small cells of high latitudes. When all data sets share one grid, cells valid in all of them ("common" cells) are drawn too: they compare the same places between periods (gradient stability, agreement between data sources, anomalies).

Reading. Rasters are read block by block, sequentially, so memory use stays low and compressed files are not decompressed once per cell. Clip them to the accessible area first (e.g. crop_rasters()).

Anomaly ("delta") data sets combine a high-resolution present climate with a climate model's change, at the common cells only (no maps needed): anomalies = list(LGM_delta = c(current = "CHELSA", past = "LGM", model_present = "TraCE_present")) (see anomaly_climate()).

Regions are the areas within which correlations are also checked: polygons (e.g. the Alps, the Carpathians, Scandinavia) or a grid of tiles.

Ice and sea in past periods. Palaeoclimate layers (e.g. CHELSA-TraCE21k) usually have values under the ice sheets and over what was sea, and those cells are sampled like any other. They do not belong to the area a species could live in, and a region that was under the ice is then screened as if it were land. exclude and keep leave such cells out, per data set: for example the ice extent of each period as polygons (such as the DATED-1 reconstruction of the Eurasian ice sheets, Hughes et al. 2016) or as a raster (such as the glacier layer glz that comes with CHELSA-TraCE21k; look at it first, a raster mask takes every cell with a value other than NA and 0), and a land raster of the period for keep. The common cells are then those unmasked in every data set. Without a mask, data sets whose names or files look like past periods give a warning (switch it off with options(preENM.past_warning = FALSE) when the layers are already cut to the land of each period).

References

Hughes, A.L.C., Gyllencreutz, R., Lohne, O.S., Mangerud, J., Svendsen, J.I. (2016) The last Eurasian ice sheets: a chronological database and time-slice reconstruction, DATED-1. Boreas 45, 1-45.

Examples

sim <- simulate_enm_data(seed = 1)
now <- c(sim$env, sim$env[[1]] * sim$env[[2]])
names(now) <- c("t", "p", "tp")
past <- now
past[["t"]] <- past[["t"]] - 0.3
es <- sample_environment(list(present = now, past = past), n = 2000,
                         seed = 1)
#> Finding cells with values in all 3 variables ...
#> Reading values ...
#> Sample: present = 2000, past = 2000; common cells = 2000
es
#> Environmental sample: 3 variables (t, p, tp)
#> Data sets (reference: present): present = 2000, past = 2000 cells
#> Common cells: 2000; area-proportional: TRUE