biomod2 (Thuiller et al. 2009) fits and projects the models; preENM
prepares the data and judges the results. to_biomod2() turns the
outputs of preENM into the arguments of biomod2::BIOMOD_FormatingData()
and biomod2::BIOMOD_Modeling(), so that the records, background points
and folds chosen with preENM are the ones biomod2 uses:
Usage
to_biomod2(
presence,
env,
background = NULL,
absence = NULL,
folds = NULL,
resp_name = "species",
dir_name = ".",
coords = NULL,
points_crs = "EPSG:4326"
)Arguments
- presence
Presence records: a data.frame with coordinates, an
sfobject or a terra::SpatVector.- env
Predictor rasters (terra::SpatRaster or file paths).
- background
Optional background points, e.g. from
sample_background(); with asetcolumn, each set becomes one pseudo-absence set.- absence
Optional true absences (0 in
resp.var).- folds
Optional cross-validation folds: an integer vector with one fold per point in the order of
pointsin the result (presences, absences, background), or a function that takes that table (columnsx,y,resp,set) and returns the folds, e.g.function(p) spatial_folds(p, k = 4, block_size = 100, coords = c("x", "y"), points_crs = terra::crs(env)).- resp_name
Name of the species for biomod2.
- dir_name
Folder where biomod2 writes its files.
- coords, points_crs
Coordinate columns and their CRS when the points are tables.
Value
An object of class biomod2_input: formating (a list of
arguments for biomod2::BIOMOD_FormatingData()), modeling (a list
of the cross-validation arguments for biomod2::BIOMOD_Modeling(),
empty without folds), points (the points in the order of
resp.var, with x, y, resp and set) and summary.
Details
the presences (e.g. after
filter_occurrences(),thin_by_cell()orthin_environment()) and true absences, if any, asresp.var(1 and 0) with their coordinates inresp.xy;the background points of
sample_background()as pseudo-absences (NAinresp.var), one biomod2 pseudo-absence set per background set (setcolumn), given asPA.strategy = "user.defined"andPA.user.table;the predictors (e.g. cropped to M with
crop_to_m()and selected withselect_variables()) asexpl.var;the folds of
spatial_folds(),environmental_folds(),group_folds()ortemporal_folds()asCV.strategy = "user.defined"andCV.user.table, one cross-validation run per fold, repeated for every pseudo-absence set.
preENM fits nothing: the result is a list of arguments, passed to
biomod2 with do.call() (see Examples). The predictions of biomod2
(biomod2::get_predictions(), on a 0-1000 scale) go back into
evaluate_models(), ensemble_models(), thresholds() and
range_metrics(), which rescale them.
Status and limits. A preENM utility; the formats follow biomod2 4.x
(PA.user.table with columns PA1, PA2, ...; CV.user.table with
columns _PA1_RUN1, ... or _allData_RUN1, ..., TRUE for the
calibration rows). Coordinates are given in the CRS of env. Points
without values in all layers of env are dropped, as biomod2 would.
References
Thuiller, W., Lafourcade, B., Engler, R., Araujo, M.B. (2009) BIOMOD - a platform for ensemble forecasting of species distributions. Ecography 32, 369-373. doi:10.1111/j.1600-0587.2008.05742.x
Examples
sim <- simulate_enm_data(seed = 1, n_presence = 100)
bg <- sample_background(sim$env, n = 500, n_sets = 2, seed = 1)
#> Cells available for background: 4800
b2 <- to_biomod2(sim$presence, sim$env, background = bg,
folds = function(p) spatial_folds(p, k = 3,
block_size = 150, coords = c("x", "y"), seed = 1))
b2
#> Input for biomod2 (species)
#>
#> IN SHORT
#> - 100 presences, 1000 background points in 2 pseudo-absence set(s)
#> (PA.strategy = "user.defined"); 2 predictors.
#> - 3 cross-validation folds from preENM, for each of the 2 pseudo-absence
#> sets (CV.strategy = "user.defined").
#>
#> Use:
#> dat <- do.call(biomod2::BIOMOD_FormatingData, x$formating)
#> mod <- do.call(biomod2::BIOMOD_Modeling, c(list(bm.format = dat,
#> modeling.id = "preENM", models = c("GLM", "RF")), x$modeling))
names(b2$formating)
#> [1] "resp.name" "resp.var" "resp.xy" "expl.var"
#> [5] "dir.name" "PA.strategy" "PA.user.table"
head(b2$modeling$CV.user.table)
#> _PA1_RUN1 _PA1_RUN2 _PA1_RUN3 _PA2_RUN1 _PA2_RUN2 _PA2_RUN3
#> 1 FALSE TRUE TRUE FALSE TRUE TRUE
#> 2 TRUE TRUE FALSE TRUE TRUE FALSE
#> 3 FALSE TRUE TRUE FALSE TRUE TRUE
#> 4 TRUE FALSE TRUE TRUE FALSE TRUE
#> 5 TRUE FALSE TRUE TRUE FALSE TRUE
#> 6 TRUE FALSE TRUE TRUE FALSE TRUE
# \donttest{
# biomod2 also needs R.utils to format the data
if (requireNamespace("biomod2", quietly = TRUE) &&
requireNamespace("R.utils", quietly = TRUE)) {
b2$formating$dir.name <- tempdir()
dat <- do.call(biomod2::BIOMOD_FormatingData, b2$formating)
# mod <- do.call(biomod2::BIOMOD_Modeling,
# c(list(bm.format = dat, modeling.id = "preENM",
# models = c("GLM", "RF")), b2$modeling))
}
#>
#> -=-=-=-=-=-=-=-=-=-=-=-=-= [BIOMOD] Do Data Formating -=-=-=-=-=-=-=-=-=-=-=-=-=
#>
#> Checking arguments...
#>
#> + BIOMOD.formated.data.PA object
#>
#> Some data is located in the same raster cell. Please set 'filter.raster = TRUE' for automatic filtering.
#>
#>
#> -=-=-=--=-=-=- [bm] Pseudo-absences
#>
#> Checking arguments...
#>
#> + User-defined pseudo-absence selection
#>
#>
#> > Checking duplicated cells (calibration/validation)...
#>
#> Some data is located in the same raster cell. Please set 'filter.raster = TRUE' for automatic filtering.
#>
#> > Formating data (no evaluation)...
#>
#> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Done -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# }
