trainESM {enmSdmX} | R Documentation |
Calibrate an ensemble of small models
Description
This function calibrates a set of "ensembles of small models" (ESM), which are designed for modeling species with few occurrence records. In the original formulation, each model has two covariates interacting additively. Models are calibrated using all possible combinations of covariates. By default, this function does the same, but can also include univariate models, models with two covariates plus their interaction term, and models with quadratic and corresponding linear terms. This function will only train generalized linear models. Extending the types of algorithms is planned!
Usage
trainESM(
data,
resp = names(data)[1],
preds = names(data)[2:ncol(data)],
univariate = FALSE,
quadratic = FALSE,
interaction = FALSE,
interceptOnly = FALSE,
method = "glm.fit",
scale = NA,
w = TRUE,
family = stats::binomial(),
...,
verbose = FALSE
)
Arguments
data |
Data frame or matrix. Response variable and environmental predictors (and no other fields) for presences and non-presence sites. |
resp |
Character or integer. Name or column index of response variable. Default is to use the first column in |
preds |
Character vector or integer vector. Names of columns or column indices of predictors. Default is to use the second and subsequent columns in |
univariate , quadratic , interaction |
|
interceptOnly |
If |
method |
Character: Name of function used to solve the GLM. For "normal" GLMs, this can be |
scale |
Either |
w |
Weights. Any of:
|
family |
Character or function. Name of family for data error structure (see |
... |
Arguments to pass to |
verbose |
Logical. If |
Value
A list object with several named elements:
-
models
: A list with each ESM model. -
tuning
: Adata.frame
with one row per model, in the order as they appear in$models
.
References
Breiner, F.T., Guisan, A., Bergamini, A., and Nobis, M.P. 2015. Overcoming limitations of modeling rare species by using ensembles of small models. Methods in Ecology and Evolution 6:1210-1218.. doi:10.1111/2041-210X.12403 Lomba, A., L. Pellissier, C. Randin, J. Vicente, J. Horondo, and A. Guisan. 2010. Overcoming the rare species modeling complex: A novel hierarchical framework applied to an Iberian endemic plant. Biological Conservation 143:2647-2657. doi:10.1016/j.biocon.2010.07.007
See Also
trainBRT
, trainGAM
, trainGLM
, trainMaxEnt
, trainMaxNet
, trainNS
, trainRF
, trainByCrossValid
Examples
# NB: The examples below show a very basic modeling workflow. They have been
# designed to work fast, not produce accurate, defensible models. They can
# take a few minutes to run.
library(terra)
set.seed(123)
### setup data
##############
# environmental rasters
rastFile <- system.file('extdata/madClim.tif', package='enmSdmX')
madClim <- rast(rastFile)
# coordinate reference system
wgs84 <- getCRS('WGS84')
# lemur occurrence data
data(lemurs)
occs <- lemurs[lemurs$species == 'Eulemur fulvus', ]
occs <- vect(occs, geom=c('longitude', 'latitude'), crs=wgs84)
occs <- elimCellDuplicates(occs, madClim)
occEnv <- extract(madClim, occs, ID = FALSE)
occEnv <- occEnv[complete.cases(occEnv), ]
# create 10000 background sites (or as many as raster can support)
bgEnv <- terra::spatSample(madClim, 20000)
bgEnv <- bgEnv[complete.cases(bgEnv), ]
bgEnv <- bgEnv[1:min(10000, nrow(bgEnv)), ]
# collate occurrences and background sites
presBg <- data.frame(
presBg = c(
rep(1, nrow(occEnv)),
rep(0, nrow(bgEnv))
)
)
env <- rbind(occEnv, bgEnv)
env <- cbind(presBg, env)
predictors <- c('bio1', 'bio12')
### calibrate models
####################
# "traditional" ESMs with just 2 linear predictors
# just one model in this case because we have just 2 predictors
esm1 <- trainESM(
data = env,
resp = 'presBg',
preds = predictors,
family = stats::binomial(),
scale = TRUE,
w = TRUE
)
str(esm1, 1)
esm1$tuning
# extended ESM with other kinds of terms
esm2 <- trainESM(
data = env,
resp = 'presBg',
preds = predictors,
univariate = TRUE,
quadratic = TRUE,
interaction = TRUE,
interceptOnly = TRUE,
family = stats::binomial(),
scale = TRUE,
w = TRUE,
verbose = TRUE
)
str(esm2, 1)
esm2$tuning
### make a set of predictions to rasters
########################################
# center environmental rasters and divide by their SD
madClimScaled <- scale(madClim, center = esm2$scale$mean, scale = esm2$scale$sd)
# make one raster per model
predictions <- list()
for (i in 1:length(esm2$models)) {
predictions[[i]] <- predict(madClimScaled, esm2$models[[i]], type = 'response')
}
# combine into a "stack"
predictions <- do.call(c, predictions)
names(predictions) <- esm2$tuning$model
plot(predictions)
# calculate (unweighted) mean
prediction <- mean(predictions)
plot(prediction)
plot(occs, pch = 1, add = TRUE)