
Combine model predictions into an ensemble, with their disagreement
Source:R/ensemble.R
ensemble_models.RdTakes the prediction rasters of several models (from any software) and an evaluation or selection, and returns the ensemble prediction together with how much the models disagree in every cell. The disagreement is as much the result as the ensemble: where the models disagree, the ensemble is an average of different answers, not a confident prediction.
Usage
ensemble_models(
models,
evaluation = NULL,
method = c("pcam", "median", "mean", "weighted"),
use = c("selected", "all"),
weights = NULL,
filename = NULL,
overwrite = FALSE,
n_sample = 1e+05,
levels = c(0.1, 0.2),
pattern = "\\.tif{1,2}$",
recursive = FALSE,
seed = 1,
big_cells = 1e+07,
rescale = TRUE
)Arguments
- models
Prediction rasters: a terra::SpatRaster with one layer per model (layer names = model names), or raster files and/or folders (model names = file names without extension; with
patternandrecursiveas incrop_rasters()). All must share the same grid.- evaluation
Optional result of
select_models()orevaluate_models()(orevaluate_cv()passed throughselect_models()); itsmodelnames must match the rasters.- method
"pcam"(default),"median","mean"or"weighted".- use
"selected"(default; the Pareto front for anevaluate_models()result) or"all"models of the evaluation.- weights
Optional named weights for
method = "weighted"(names = models); default: the compromise score of the evaluation.- filename
Optional GeoTIFF for the result (layers
ensemble,sd,cv).- overwrite
Overwrite
filename.- n_sample
Number of cells sampled for the PCA of
"pcam"and for the summaries.- levels
Levels of
sdfor the area summary (for predictions on the 0-1 scale; preENM defaults, not from the literature).- pattern, recursive
File search in folders, as in
crop_rasters().- seed
Seed for the cell sample.
- big_cells
Rasters with more cells are combined with every intermediate layer in temporary files (terra option
todisk), so memory use stays bounded.- rescale
Bring every model to the 0-1 scale before combining: algorithms write 0-1, 0-100 or 0-1000, recognised from the largest value of each layer; layers on 0-100 or 0-1000 are divided by 100 or 1000, with a message.
FALSEkeeps them and warns.
Value
A list of class enm_ensemble: raster (a
terra::SpatRaster with layers ensemble, sd and cv), models
(the models used), left_out, method, weights (for "weighted"),
loadings (for "pcam": loading of each model on the consensus axis
and whether it was kept), pc_share (for "pcam": share of variance
of each principal component), summary (quantiles of sd and cv),
by_level (mean sd per class of the ensemble prediction), area
(area with sd above each level, km^2 and share), departure (mean
absolute difference of each model from the ensemble), scales (the
recognised scale of each model and what it was divided by) and
scale01 (whether the predictions are on the 0-1 scale).
Details
Which models. By default only the models chosen by the evaluation:
the selected models of select_models(), or the Pareto front
(pareto_front == 1) of evaluate_models(). Dominated or unselected
models are left out (use = "all" keeps every model of the
evaluation). Without an evaluation every supplied model is used.
How they are combined (method):
"pcam"(default): the median of the half of the models closest to their common trend: a principal component analysis of the model predictions (on a sample of cells) is computed and the models with the highest loadings on the first component, the consensus axis, are kept (Thuiller 2004; Araujo et al. 2005). Zhu et al. (2021) found it the most reliable consensus in transfers to new areas. Needs at least three models; with fewer, the default falls back to the median. In detail: the predictions of thenmodels at the sampled cells are centred and scaled, so the PCA is that of their correlation matrix and models with different spreads of values count alike; the first axis is oriented so that its loadings sum to a positive value; theceiling(n / 2)models with the highest loadings are kept, and the ensemble is their median in every cell.pc_sharegives the share of variance of each axis: a first axis that explains little means there is hardly a common trend to follow (the printout warns below 50%, a preENM default, not from the literature);plot_ensemble_pca()shows the loadings. Compare the methods withensemble_scenarios()."median": the median prediction per cell; robust to a single model with extreme values."mean": the unweighted mean."weighted": the mean weighted by the compromise score of the evaluation,1 - distance, wheredistanceis the normalised distance of the model to the ideal point on the evaluation criteria (as inevaluate_models()andpareto_rank(); all models of the evaluation define the scale), or by your ownweights.
Why weighting is not the default. Weights based on how well models
predict the data they were evaluated on reward interpolation, which is
not the same as predicting new areas or periods: in transfers across
continents, consensus models weighted by AUC on held-out data were worse
than the median of the models closest to their common trend (PCAm), and
the mean was pulled by models with extreme predictions (Zhu et al. 2021).
With spatially separated folds (see fold_report()) the evaluation is
closer to a transfer, but the evaluation criteria still do not measure
transferability.
Disagreement. For the models used, per cell: the standard deviation
of their predictions (sd) and the coefficient of variation (cv =
sd / mean; NA where the mean is 0). The printout summarises where the
models disagree: overall, by level of the ensemble prediction, the area
above levels of disagreement, and which model departs most from the
ensemble.
Large rasters. The rasters are read from disk and combined block by
block by terra, so stacks larger than memory work (above big_cells
cells every intermediate layer goes to temporary files); give filename to
write the result to a GeoTIFF (otherwise terra keeps it in memory when
it fits and in temporary files when it does not). The PCA of "pcam"
and the summaries use a random sample of n_sample cells.
Disagreement, not total uncertainty. sd and the other spread layers
measure model disagreement among the predictions given. They leave out the
uncertainty of the records, of the predictors, of each model's parameters
and of the future climate; see partition_uncertainty() and
extrapolation_risk().
References
Zhu, G., Fan, J., Peterson, A.T. (2021) Cautions in weighting individual ecological niche models in ensemble forecasting. Ecological Modelling 448, 109502. doi:10.1016/j.ecolmodel.2021.109502
Thuiller, W. (2004) Patterns and uncertainties of species' range shifts under climate change. Global Change Biology 10, 2020-2027.
Araujo, M.B., Whittaker, R.J., Ladle, R.J., Erhard, M. (2005) Reducing uncertainty in projections of extinction risk from climate change. Global Ecology and Biogeography 14, 529-538.
Examples
sim <- simulate_enm_data(seed = 1)
ev <- evaluate_models(sim$models, sim$presence, sim$absence)
sel <- select_models(ev, plot = FALSE)
ens <- ensemble_models(sim$models, sel)
ens
#> Ensemble of 5 models: median of the models closest to the consensus axis (PCAm)
#> Used: Maxent_LQ, Maxent_H_rm4, GLM, RandomForest, GBM
#> Left out (not selected or dominated): BIOCLIM, Random
#> Median of: Maxent_LQ, Maxent_H_rm4, GLM
#> Consensus axis (PC1): 92% of the variance of the standardised predictions
#>
#> IN SHORT
#> - The models disagree over much of the area: the median standard deviation
#> between models is 0.17 (90% of cells below 0.22).
#> - The standard deviation exceeds 0.1 on 92% of the area (423 790 km2) and
#> 0.2 on 22% of the area (99 390 km2).
#> - Disagreement is highest where the ensemble predicts 0.4-0.6 (mean sd
#> 0.19; 11% of the area), intermediate values.
#> - RandomForest departs most from the ensemble (mean absolute difference
#> 0.29; next GBM 0.11).
#>
#> Disagreement by level of the ensemble prediction:
#> ensemble share_of_area mean_sd
#> 0-0.2 12% 0.098
#> 0.2-0.4 12% 0.167
#> 0.4-0.6 11% 0.191
#> 0.6-0.8 13% 0.186
#> 0.8-1 53% 0.173
#>
#> sd = standard deviation of the predictions of the models used, per cell; cv =
#> sd / mean. Shares and levels come from a sample of 4800 cells; areas from all
#> cells. The grades (median sd below 0.05 "agree closely", below 0.1 "mostly
#> agree") and the levels of sd are preENM defaults for predictions on the 0-1
#> scale, not from the literature.
plot_ensemble(ens)