Skip to contents

preENM covers the steps around fitting an ecological niche or species distribution model, in any software, but never fits a model itself. This vignette runs a short workflow on simulated data, so that every step can be reproduced offline in a few seconds. The articles of the website explain each step in detail and on real data.

library(preENM)
sim <- simulate_enm_data(seed = 1, n_presence = 150)
names(sim)
#> [1] "models"   "presence" "absence"  "truth"    "env"

sim$presence and sim$absence are records with coordinates, sim$env two predictor layers, and sim$models the predictions of seven models fitted elsewhere: preENM takes such predictions from any software.

terra::plot(sim$models[[c("Maxent_LQ", "GLM", "BIOCLIM", "Random")]])

Before modelling

The accessible area. M is delimited from the records; here the linking distance L is set by hand for the small example (by default it is derived from the records).

m <- delimit_m(sim$presence, link_km = 120, thin_km = 5)
m
#> Accessible area (M): method delaunay
#> Linking distance L = 120 km (set by hand); L2 = 120 km; 1 cluster(s) at L
#> Records: 150 read, 0 outside the extent or excluded, 150 after thinning to 5 km
#> Closing 60 km; holes filled: TRUE; corridors: FALSE
#> 
#>             part area_km2 records_inside_pct
#>        automatic   390427                100
#>  added_by_expert        0                 NA
#>                M   390427                100
plot_m_area(m)

How much does M matter? Several delimitations, compared side by side: one of preENM’s sensitivity analyses.

sc <- m_scenarios(sim$presence, link_km = 120, thin_km = 5,
                  scenarios = list(delaunay = list(),
                                   hull = list(method = "convex_hull"),
                                   buffer_50 = list(method = "buffer",
                                                    buffer_km = 50)))
sc$comparison[c("scenario", "area_km2", "jaccard_with_reference")]
#>    scenario area_km2 jaccard_with_reference
#> 1  delaunay   390427                  1.000
#> 2      hull   409762                  0.953
#> 3 buffer_50   350657                  0.898
plot_m_area(sc)

Thinning. Records are thinned in environmental space, keeping the extremes of every predictor.

th <- thin_environment(sim$presence, sim$env, radius_km = 30,
                       verbose = FALSE)
th
#> Environmental thinning: kept 130 of 150 records (86.7%)
#> Space: PC1-PC2; bin widths: PC1 = 0.26, PC2 = 0.346
#> Geographic quota: 2 per 50 km block; blocks below quota: 0
#> 
#> Records' environmental uncertainty varies 1.2-fold (10th to 90th percentile,
#> 1 to 1.2 bin widths). Kept: 89% of the third in the most uniform
#> surroundings, 90% of the third in the most varied: varied places (e.g.
#> mountains) are thinned more gently.
#> 
#>     keep_reason Freq
#>     block_quota   53
#>         env_bin   63
#>  extreme_minmax    4
#>            hull   10
#>         removed   20
#> 
#> 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.2179  0.2254
#>   env2 0.008333 0.008333  0.8917   0.8917 0.2336  0.2333

Background. Background points uniform over the predictors’ grid (with method = "effort", "target_group" or "thickening" they correct for sampling bias).

bg <- sample_background(sim$env, n = 2000, seed = 1)
head(bg)
#>   set cell     lon   lat
#> 1   1 3925 14.5625 50.05
#> 2   1 2822 16.6875 51.45
#> 3   1 4512 17.9375 49.35
#> 4   1 4488 14.9375 49.35
#> 5   1  273 18.0625 54.65
#> 6   1 3276 23.4375 50.95

Is the niche truncated by the study area?

tr <- niche_truncation(sim$presence, sim$env)
tr
#> Niche truncation: 150 records, 4800 cells of M; margin = outer 5% of M's conditions
#>   Truncated: none
#>   Other margins (1%, 2.5%, 5%, 10%): the same diagnosis at every margin
#> 
#>  layer   m_min m_max rec_min rec_max gap_low gap_high edge_low edge_high
#>   env1 0.00625 0.994 0.09380   0.994  0.0886    0.000    0.000     0.267
#>   env2 0.00833 0.992 0.00833   0.892  0.0000    0.102    0.533     0.000
#>  truncated_low truncated_high
#>          FALSE          FALSE
#>          FALSE          FALSE
#> 
#> A preENM sensitivity analysis: how much the result depends on the margin
#> that defines the edge of M. The others are listed in ?sensitivity_analyses.
plot_niche_truncation(tr)

After modelling

The models were fitted elsewhere; preENM evaluates their predictions at the presence and absence points.

ev <- evaluate_models(sim$models, sim$presence, sim$absence,
                      data_type = "absence")
ev
#> ENM evaluation: 7 model(s), 150 presence and 300 absence/background points
#> Data: presences against recorded absences. The metrics have their usual
#>   meaning, within the sampling design of the survey that recorded the
#>   absences.
#> Criteria: AUC (higher is better), MAE (lower is better)
#> Pareto front (best trade-offs): Maxent_LQ, Maxent_H_rm4, GLM
#> 
#>  rank        model   AUC   MAE MAEbal   bias   TSS  Boyce pareto_front distance
#>     1    Maxent_LQ 0.983 0.153  0.143 -0.078 0.867  0.942            1    0.003
#>     2 Maxent_H_rm4 0.984 0.235  0.227 -0.101 0.867  0.980            1    0.143
#>     3          GLM 0.985 0.284  0.226 -0.249 0.897  0.934            1    0.228
#>     4          GBM 0.973 0.222  0.203 -0.125 0.843  0.945            2    0.121
#>     5 RandomForest 0.976 0.224  0.284  0.084 0.867  0.983            2    0.124
#>     6      BIOCLIM 0.799 0.559  0.430 -0.531 0.557  0.846            3    0.753
#>     7       Random 0.476 0.517  0.512 -0.186 0.030 -0.131            3    0.950

The models are selected in two ways at once (rule = "both"): they must reach required values (by default AUC of at least 0.8 and TSS of at least 0.6, which min_metrics changes), and they must lie on the Pareto front of the two criteria (by default AUC, higher is better, and MAE, lower is better; criteria changes them, e.g. to c(AUC = "max", MAEbal = "min")).

sel <- select_models(ev, rule = "both",
                     min_metrics = c(AUC = 0.8, TSS = 0.6),
                     criteria = c(AUC = "max", MAE = "min"))
sel
#> Model selection (rule: both)
#>   Required values: AUC >= 0.8, TSS >= 0.6 | pass: Maxent_LQ, Maxent_H_rm4, GLM, GBM, RandomForest
#>   (conventional limits, not validated for distribution models; change them with min_metrics / max_metrics)
#>   Pareto front (AUC:max, MAE:min; tol 0): Maxent_LQ, Maxent_H_rm4, GLM
#>   SELECTED: Maxent_LQ, Maxent_H_rm4, GLM
#> 
#>         model   AUC   MAE MAEbal   bias   TSS  Boyce n_presence n_absence
#>     Maxent_LQ 0.983 0.153  0.143 -0.078 0.867  0.942        150       300
#>  Maxent_H_rm4 0.984 0.235  0.227 -0.101 0.867  0.980        150       300
#>           GLM 0.985 0.284  0.226 -0.249 0.897  0.934        150       300
#>           GBM 0.973 0.222  0.203 -0.125 0.843  0.945        150       300
#>  RandomForest 0.976 0.224  0.284  0.084 0.867  0.983        150       300
#>       BIOCLIM 0.799 0.559  0.430 -0.531 0.557  0.846        150       300
#>        Random 0.476 0.517  0.512 -0.186 0.030 -0.131        150       300
#>  pareto_front distance rank  pass pareto rank_mean selected
#>             1    0.003    1  TRUE   TRUE       2.0     TRUE
#>             1    0.143    2  TRUE   TRUE       3.0     TRUE
#>             1    0.228    3  TRUE   TRUE       3.0     TRUE
#>             2    0.121    4  TRUE  FALSE       3.5    FALSE
#>             2    0.124    5  TRUE  FALSE       3.5    FALSE
#>             3    0.753    6 FALSE  FALSE       6.5    FALSE
#>             3    0.950    7 FALSE  FALSE       6.5    FALSE
plot_selection(sel)

Thresholds and range metrics for one model (GLM), under several threshold rules:

tab <- extract_predictions(sim$models, sim$presence, sim$absence)
thr <- thresholds(tab, rules = c("maxSSS", "p10", "maxMCC"))
rm <- range_metrics(sim$models[["GLM"]], thr, model = "GLM", threat_km = 50)
rm
#> Range metrics: 1 projection(s) x 3 threshold rule(s)
#> 
#> IN SHORT
#> - GLM: suitable area 261 495 km2 (p10) to 303 262 km2 (maxSSS); EOO 297 142
#>   km2 (p10) to 325 999 km2 (maxSSS); AOO 259 812 km2 (p10) to 298 840 km2
#>   (maxSSS); 134 locations (p10) to 145 locations (maxSSS); 4 to 11 patches.
#> - EOO and AOO are computed from the whole predicted suitable area, which is
#>   larger than the occupied area; occupied = TRUE keeps only patches with
#>   records, closer to IUCN practice.
#> 
#>    rule threshold area_km2  EOO_km2 AOO_km2 locations n_patches
#>  maxSSS       0.8 303262.4 325998.6  298840       145         4
#>     p10       0.9 261495.2 297141.5  259812       134        11
#>  maxMCC       0.9 267590.7 302316.0  265348       136         9
#>  largest_patch_index edge_density mean_nn_km
#>                 66.1          0.1       12.9
#>                 56.8          0.2        8.6
#>                 58.2          0.1        9.8
#> 
#> EOO = minimum convex polygon of the suitable cells; AOO = 2 x 2 km grid cells
#> with suitable area; locations = 50 km grid cells (the extent of one threat);
#> both grids are placed to give the fewest cells, in an equal-area projection.
#> largest_patch_index = largest patch, % of the landscape; edge_density = m of
#> edge per ha of landscape; mean_nn_km = mean distance to the nearest other
#> patch. IUCN (2012) criterion B: EOO < 100 / 5000 / 20000 km2 and AOO < 10 /
#> 500 / 2000 km2 (CR / EN / VU), locations 1 / <= 5 / <= 10; a threshold met is
#> not a listing, which also needs conditions (a)-(c) and evidence on threats.
#> 
#> A preENM sensitivity analysis: how much the result depends on the threshold
#> rule. The others are listed in ?sensitivity_analyses.
plot_range_metrics(rm)

Where next

  • The articles follow the workflow: records and the accessible area, predictors, sampling bias, design checks, evaluation, validation, maps and uncertainty, niche comparison and reporting.
  • ?sensitivity_analyses lists every step whose choices preENM can test.
  • assess_enm_data() gathers the diagnostics of the data in one table.