if (FALSE) {
library(terra)
library(ENMeval)
occs <- read.csv(file.path(system.file(package="predicts"),
"/ex/bradypus.csv"))[,2:3]
envs <- rast(list.files(path=paste(system.file(package="predicts"),
"/ex", sep=""), pattern="tif$", full.names=TRUE))
occs.z <- cbind(occs, extract(envs, occs, ID = FALSE))
occs.z$biome <- factor(occs.z$biome)
bg <- as.data.frame(predicts::backgroundSample(envs, n = 10000))
names(bg) <- names(occs)
bg.z <- cbind(bg, extract(envs, bg, ID = FALSE))
bg.z$biome <- factor(bg.z$biome)
# set other.settings -- pred.type is only for Maxent models
os <- list(abs.auc.diff = FALSE, pred.type = "cloglog",
validation.bg = "partition")
# set partition.settings -- here's an example for the block method
# see Details for the required settings for other partition methods
ps <- list(orientation = "lat_lat")
# here's a run with maxnet -- note the tune.args for feature classes (fc)
# and regularization multipliers (rm), as well as the designation of the
# categorical variable we are using (this can be a vector if multiple
# categorical variables are used)
e.maxnet <- ENMevaluate(occs, envs, bg,
tune.args = list(fc = c("L","LQ","LQH","H"), rm = 1:5),
partitions = "block", other.settings = os, partition.settings = ps,
algorithm = "maxnet", categoricals = "biome", overlap = TRUE)
# print the tuning results
eval.results(e.maxnet)
# you can plot the marginal response curves of a maxnet object with plot(),
# and you can also extract the data for plotting to make your own custom plots
mods.maxnet <- eval.models(e.maxnet)
m <- mods.maxnet$fc.LQH_rm.2
plot(m, type = "cloglog")
rcurve_data <- maxnet::response.plot(m, "bio1", type = "cloglog", plot = FALSE)
# there is currently no native function to make raster model predictions for
# maxnet models, but ENMeval can be used to make them like this:
# here's an example where we make a prediction based on the L2 model
# (feature class: Linear, regularization multiplier: 2) for our envs data
pred.LQH2 <- maxnet.predictRaster(m, envs)
plot(pred.L2)
# here's a run with maxent.jar -- note that if the R package rJava cannot
# install or load, or if you have other issues with Java on your computer,
# maxent.jar will not function
e.maxent.jar <- ENMevaluate(occs, envs, bg,
tune.args = list(fc = c("L","LQ","LQH","H"), rm = 1:5),
partitions = "block", other.settings = os, partition.settings = ps,
algorithm = "maxent.jar", categoricals = "biome", overlap = TRUE)
# here's a run of maxent.jar with a path specified for saving the html and
# plot files -- you can also turn on jackknife variable importance or
# response curves, etc., to have these plots saved there
e.maxent.jar <- ENMevaluate(occs, envs, bg,
tune.args = list(fc = c("L","LQ","LQH","H"), rm = 1:5),
partitions = "block", partition.settings = ps,
algorithm = "maxent.jar", categoricals = "biome", overlap = TRUE,
other.settings = list(path = "analyses/mxnt_results",
other.args = c("jackknife=TRUE", "responsecurves=TRUE")))
# print the tuning results
eval.results(e.maxent.jar)
# raster predictions can be made for maxent.jar models with predicts or
# ENMeval
mods.maxent.jar <- eval.models(e.maxent.jar)
pred.L2 <- predict(mods.maxent.jar$fc.L_rm.2, envs,
args = "outputform=cloglog")
pred.L2 <- maxnet.predictRaster(mods.maxent.jar$fc.L_rm.2, envs, os)
plot(pred.L2)
# this will give you the percent contribution (not deterministic) and
# permutation importance (deterministic) values of variable importance for
# Maxent models, and it only works with maxent.jar
eval.variable.importance(e.maxent.jar)
# here's a run with BIOCLIM. Note that we need to remove the categorical
# variable here because this algorithm only takes continuous variables. We
# also should point out that the way BIOCLIM is tuned is by comparing
# performance for different ways to make predictions (as opposed to comparing
# performance for models fit in different ways like for maxnet or maxent.jar).
# Namely, BIOCLIM can ignore different tails of the distribution when making
# predictions, and this is what is tuned in ENMevaluate (see
# ?predicts::envelope).
# print the tuning results
eval.results(e.bioclim)
# make raster predictions with predicts or ENMeval
mods.bioclim <- eval.models(e.bioclim)
# note: the models for low, high, and both are actually all the same, and
# the only difference for tuning is how they are predicted during
# cross-validation
pred.both <- predict(mods.bioclim$tails.both, envs, tails = "both")
plot(pred.both)
# please see the vignette for more examples of model tuning,
# partitioning, plotting functions, and null models
# https://jamiemkass.github.io/ENMeval/articles/ENMeval-2.0-vignette.html
}
Run the code above in your browser using DataLab