Title: | Temporal Trends in Ecological Niche Models |
---|---|
Description: | Computes temporal trends in environmental suitability obtained from ecological niche models, based on a set of species presence point coordinates and predictor variables. |
Authors: | A. Marcia Barbosa [aut, cre], João Alírio [aut], Nuno Garcia [aut], João Campos [aut], Ana Cláudia Teodoro [aut], Lia Bárbara Duarte [aut], Isabel Pôças [aut], Salvador Arenas-Castro [aut], Neftalí Sillero [aut] |
Maintainer: | A. Marcia Barbosa <[email protected]> |
License: | GPL (>=3) |
Version: | 1.0 |
Built: | 2025-03-06 05:29:07 UTC |
Source: | https://github.com/ambarbosa/ecotrends |
This function computes the permutation importance of each variable in each model, by shuffling each variable in turn (a given number of times) and computing the root mean squared difference between the actual model predictions and those obtained with the shuffled variable. Values are then normalized to a percentage by dividing each by the sum of all values and multiplying by 100. Note that "importance" is a vague concept which can be measured in many other different ways.
getImportance( mods, nper = 10, verbosity = 2, plot = TRUE, palette = "Dark2", ... )
getImportance( mods, nper = 10, verbosity = 2, plot = TRUE, palette = "Dark2", ... )
mods |
output of |
nper |
integer value (default 10; increase for more accurate but computationally intensive results) indicating the number of permutations for shuffling each variable. |
verbosity |
integer value indicating the amount of messages to display. The default is 2, for the maximum number of messages available. |
plot |
logical value specifying whether to produce a line (spaghetti) plot of the mean importance of each variable along the periods. Note that this plot does not reflect the deviations around this mean, and that it may become hard to read if there are many variables or if their importances overlap. |
palette |
argument to pass to |
... |
additional arguments that can be passed to |
A data frame with the permutation importance (expressed as percentage) of each variable in each model replicate for each period, along with the cross-replicate mean and standard deviation. If plot=TRUE (the default), also a spaghetti plot of mean variable importance per period.
A. Marcia Barbosa
varImportance
in package predicts; bm_VariablesImportance
in package biomod2
# Several data prep steps required. # See https://github.com/AMBarbosa/ecotrends for a full worked example.
# Several data prep steps required. # See https://github.com/AMBarbosa/ecotrends for a full worked example.
This function computes maxnet::maxnet()
ecological niche models for a set of time steps or periods (e.g. years), given a set of presence point coordinates and periodly environmental layers.
getModels( occs, rasts, region = NULL, nbg = 10000, seed = NULL, bias = FALSE, collin = TRUE, maxcor = 0.75, maxvif = 5, classes = "default", regmult = 1, nreps = 10, test = 0.2, file = NULL, verbosity = 2 )
getModels( occs, rasts, region = NULL, nbg = 10000, seed = NULL, bias = FALSE, collin = TRUE, maxcor = 0.75, maxvif = 5, classes = "default", regmult = 1, nreps = 10, test = 0.2, file = NULL, verbosity = 2 )
occs |
species occurrence coordinates (2 columns in this order: x, y or LONGitude, LATitude) in an object coercible to a data.frame (e.g. a data.frame, matrix, tibble, sf object or SpatVector of points), and in the same coordinate reference system as 'rasts' |
rasts |
(multi-layer) SpatRaster with a time series of variables to use in the models. The layers should be ordered chronologically, and their names should be in the form "varname [underscore] time", e.g. "tmin_1985" or "tmin_1" (with no more underscores than this), as in the output of |
region |
optional SpatExtent or SpatVector polygon delimiting the region of 'rasts' within which to compute the models. The default is NULL, to use the entire extent of 'rasts' with pixel values. Note that 'region' should ideally include only reasonably surveyed areas that are accessible to the species, as pixels that don't overlap presence points are taken by Maxent as available and unoccupied. |
nbg |
integer value indicating the maximum number of background pixels to select randomly for use in the models. The default is 10,000, or the total number of non-NA pixels in 'rasts' if that's less. |
seed |
optional integer value to pass to |
bias |
argument to pass to |
collin |
logical value indicating whether multicollinearity among the variables should be reduced prior to computing each model. The default is TRUE, in which case the |
maxcor |
numeric value to pass to |
maxvif |
numeric value to pass to |
classes |
character value to pass to |
regmult |
numeric value to pass to |
nreps |
integer value indicating the number of train-test datasets for testing the models. The default is 10. With nreps = 0, there is no division of the dataset into train and test samples, so models are trained on the entire dataset for each period. If nreps > 0, presences are randomly assigned to the train and test sample in each replicate (in the proportion defined by the 'test' argument), while the background remains the same. |
test |
(if nreps > 0) numeric value indicating the proportion of presences to set aside for testing each model. The default is 0.2, i.e. 20%. |
file |
optional file name (including path, not including extension) if you want the output list of model objects to be saved on disk. If 'file' already exists in the working directory (meaning that models were already computed), models are imported from there. |
verbosity |
integer value indicating the amount of messages to display. The default is 2, for the maximum number of messages available. |
A list of three elements:
$models: a list of lists of model objects of class maxnet::maxnet. Each element of the list corresponds to a period (e.g. period), and each sub-element a replicate.
$data: a data frame with the presences, remaining background points and their environmental values used in the model(s).
A. Marcia Barbosa
Elith J., Phillips S.J., Hastie T., Dudik M., Chee Y.E., Yates, C.J. (2011) A Statistical Explanation of MaxEnt for Ecologists. Diversity and Distributions 17:43-57. http://dx.doi.org/10.1111/j.1472-4642.2010.00725.x
Merow C., Smith M.J., Silander J.A. (2013) A practical guide to MaxEnt for modeling species' distributions: what it does, and why inputs and settings matter. Ecography 36:1058-1069. https://doi.org/10.1111/j.1600-0587.2013.07872.x
# Several data prep steps required. # See https://github.com/AMBarbosa/ecotrends for a full worked example.
# Several data prep steps required. # See https://github.com/AMBarbosa/ecotrends for a full worked example.
Get model performance
getPerformance( rasts, mods, metrics = c("AUC", "TSS"), plot = FALSE, verbosity = 2 )
getPerformance( rasts, mods, metrics = c("AUC", "TSS"), plot = FALSE, verbosity = 2 )
rasts |
SpatRasterDataset output of |
mods |
output of |
metrics |
character vector with the metrics to compute. Can be any subset of c("AUC", "TSS"), the latter computed at its optimal threshold. The default is both. Performance metrics are computed with presence against all background (using 'modEvA' package functions with pbg=TRUE), so they evaluate the capacity of distinguishing presence from random, rather than presence from absence pixels (Phillips et al., 2006). |
plot |
logical value indicating whether plots should also be produced to illustrate the performance metrics for each model. The default is FALSE; TRUE can be slow for large datasets. |
verbosity |
integer value indicating the amount of messages to display. The default is 2, for the maximum number of messages available. |
This function returns a data frame of the performance evaluation results for each model.
Phillips, S.J., Anderson, R.P., Schapire, R.E. (2006) Maximum entropy modeling of species geographic distributions. Ecological Modelling, 190: 231-259. https://doi.org/10.1016/j.ecolmodel.2005.03.026
# Several data prep steps required. # See https://github.com/AMBarbosa/ecotrends for a full worked example.
# Several data prep steps required. # See https://github.com/AMBarbosa/ecotrends for a full worked example.
Get predictions
getPredictions( rasts, mods, region = NULL, type = "cloglog", clamp = TRUE, file = NULL, verbosity = 2 )
getPredictions( rasts, mods, region = NULL, type = "cloglog", clamp = TRUE, file = NULL, verbosity = 2 )
rasts |
(multi-layer) SpatRaster containing the variables (with the same names) used in the models. |
mods |
output of |
region |
optional SpatExtent or SpatVector polygon delimiting the region of 'rasts' within which to compute the predictions. The default is NULL, to use the entire extent of 'rasts'. Note that predictions may be unreliable outside the 'region' used for |
type |
character value to pass to |
clamp |
logical value to pass to |
file |
optional folder name (including path within the working directory) if you want the prediction rasters to be saved to disk. If 'file' already exists in the working directory (meaning that predictions were already computed), predictions are imported from there. |
verbosity |
integer value indicating the amount of messages to display. The default is 2, for the maximum number of messages available. |
This function returns a SpatRasterDataset with one sub-dataset per period, each of which is a (multilayer) SpatRaster with the predictions of each replicate (if there are replicates) for that period.
A. Marcia Barbosa
Phillips, S.J., Anderson, R.P., Dudik, M., Schapire, R.E., Blair, M.E., 2017. Opening the black box: an open-source release of Maxent. Ecography 40, 887-893. https://doi.org/10.1111/ecog.03049
# Several data prep steps required. # See https://github.com/AMBarbosa/ecotrends for a full worked example.
# Several data prep steps required. # See https://github.com/AMBarbosa/ecotrends for a full worked example.
This function uses terra::app()
to apply the trend::sens.slope()
function to each pixel of a multi-layer time series SpatRaster, testing for a monotonic (either increasing or decreasing) linear trend in the raster values, as well as the confidence interval of the slope.
getTrend( rasts, occs = NULL, alpha = 0.05, conf.level = 0.95, file = NULL, full = TRUE, verbosity = 2 )
getTrend( rasts, occs = NULL, alpha = 0.05, conf.level = 0.95, file = NULL, full = TRUE, verbosity = 2 )
rasts |
multi-layer SpatRaster with the output of |
occs |
SpatVector of species occurrence points, or their spatial coordinates (2 columns in this order: x, y or LONGitude, LATitude) in an object coercible to a data.frame (e.g. a data.frame, matrix, tibble, sf object), and in the same coordinate reference system as 'rasts'. If provided, output pixels that do not overlap these points will be NA |
alpha |
numeric value indicating the threshold significance level for Sen's slope. Default 0.05. Pixels with p-value above this will have NA value in the output. |
conf.level |
numeric value to pass to |
file |
optional file name (including path, not including extension) if you want the outputs raster(s) to be imported from or saved to disk. |
full |
logical value indicating whether to output a multi-layer raster with the full results of the Mann-Kendall test (namely the Tau value, p-value, S, and variance of S) and the results of Sen's slope calculation (slope estimate, p-value, upper and lower confidence limit). If set to FALSE, a single-layer raster will be returned with the (significant) Sen's slope values. |
verbosity |
integer value indicating the amount of messages to display. The default is 2, for the maximum number of messages available. |
If full=FALSE, a single-layer SpatRaster where each pixel (or each pixel with occurrence points, if 'occs' is not NULL) has the value of Sen's slope (positive if increasing, negative if decreasing), or NA if the trend is non-significant (i.e., if the p-value is larger than the specified 'alpha'). If full=TRUE (the default), additional layers are produced with associated statistics, including the lower and upper bounds of Sen's slope (given the input 'conf.level').
A. Marcia Barbosa
trend::sens.slope()
, trend::mk.test()
, Kendall::MannKendall(), spatialEco::raster.kendall()
# Several data prep steps required. # See https://github.com/AMBarbosa/ecotrends for a full worked example.
# Several data prep steps required. # See https://github.com/AMBarbosa/ecotrends for a full worked example.
This function downloads a specified time series of variables from a specified environmental data source, optionally (to save download time) within a specified region. Currently "TerraClimate" (Abatzoglou et al., 2018) is the only implemented data source.
getVariables( source = "TerraClimate", vars = varsAvailable(source)$vars, years = varsAvailable(source)$years, region = c(-180, 180, -90, 90), file = NULL, verbosity = 2 )
getVariables( source = "TerraClimate", vars = varsAvailable(source)$vars, years = varsAvailable(source)$years, region = c(-180, 180, -90, 90), file = NULL, verbosity = 2 )
source |
source to import the raster variables from, e.g. "TerraClimate" (currently the only one implemented) |
vars |
character vector of the names of the variables to be imported. Run varsAvailable() for options. The default is to download all available variables from the specified 'source'. Note that the download can take a long time, especially for many variables, long series of years, and/or large regions. |
years |
year range to get the variables from (e.g. 1979:2013). Note that the download can take a long time for long series of years. |
region |
optional length-four numeric vector (xmin, xmax, ymin, ymax geodetic coordinates in degrees), SpatExtent or SpatVector polygon delimiting the region of the world for which the variables should be downloaded. See ?getRegion for suggestions. The larger the region, the longer the download time. |
file |
optional file name (including the path, not including the filename extension) if you want the downloaded variable rasters to be saved on disk, in which case they are saved as a compressed multi-layer GeoTIFF. If 'file' already exists in the working directory, variables are imported from there. |
verbosity |
integer value indicating the amount of messages to display. The default is 2, for the maximum number of messages available. |
multi-layer SpatRaster
A. Marcia Barbosa
Abatzoglou, J.T., S.Z. Dobrowski, S.A. Parks, K.C. Hegewisch (2018) Terraclimate, a high-resolution global dataset of monthly climate and climatic water balance from 1958-2015. Scientific Data, 5, Article number: 170191. doi: 10.1038/sdata.2017.191(2018). Database URL: https://www.climatologylab.org/terraclimate.html
if (interactive()) { # note these downloads may take long! vars <- ecotrends::getVariables(vars = c("tmin", "tmax", "ppt", "pet", "ws"), years = 1981:1990, region = terra::ext(-11, -4, 37, 45), file = paste0(tempdir(), "/variables")) # tempdir() is here to comply with CRAN policy, but you should normally # use a directory that you can access again when reopening R # to avoid downloading the variables again every time names(vars) terra::plot(vars[[1:6]]) }
if (interactive()) { # note these downloads may take long! vars <- ecotrends::getVariables(vars = c("tmin", "tmax", "ppt", "pet", "ws"), years = 1981:1990, region = terra::ext(-11, -4, 37, 45), file = paste0(tempdir(), "/variables")) # tempdir() is here to comply with CRAN policy, but you should normally # use a directory that you can access again when reopening R # to avoid downloading the variables again every time names(vars) terra::plot(vars[[1:6]]) }
This function uses the terra::cellSize()
function to compute (and optionally map) the area covered by each (optionally non-NA) pixel in a raster map; and then it computes either the mean or the centroid pixel area in that map, for an idea of pixel size in that region. Pixel size can vary widely across latitudes, especially in unprojected longitude-latitude rasters, but also in rasters projected to other non-equal-area coordinate reference systems.
pixelArea( rast, type = "mean", unit = "m", mask = TRUE, map = TRUE, verbosity = 2 )
pixelArea( rast, type = "mean", unit = "m", mask = TRUE, map = TRUE, verbosity = 2 )
rast |
SpatRaster for which to compute the pixel area |
type |
character value indicating whether the output value should be the "mean" (default) or "centroid" pixel area |
unit |
numeric value indicating the units for the output value: either "m" (default) or "km" squared. |
mask |
logical value (default TRUE) indicating whether to consider only the areas of non-NA pixels |
map |
logical value (default TRUE) indicating whether to also plot a map |
verbosity |
integer value indicating the amount of messages to display. The default is 2, for the maximum number of messages available. |
numeric value
A. Marcia Barbosa, wrapping 'terra' functions by Robert H. Hijmans
terra::cellSize()
, which this function wraps
r <- terra::rast(system.file("ex/elev.tif", package = "terra")) pixelArea(r) pixelArea(r, unit = "km")
r <- terra::rast(system.file("ex/elev.tif", package = "terra")) pixelArea(r) pixelArea(r, unit = "km")
Variables available
varsAvailable(source = NULL)
varsAvailable(source = NULL)
source |
the source database where to check available variables. By default (i.e. if left NULL), all implemented sources are used. Currently "TerraClimate" (https://www.climatologylab.org/terraclimate.html) is the only implemented source. |
a named nested list with one element per data source, each of which containing two elements: a character vector of the names of the variables available from that source, and an integer vector of the years for which those variables are available.
A. Marcia Barbosa
Abatzoglou, J.T., S.Z. Dobrowski, S.A. Parks, K.C. Hegewisch (2018) Terraclimate, a high-resolution global dataset of monthly climate and climatic water balance from 1958-2015. Scientific Data, 5, Article number: 170191. doi: 10.1038/sdata.2017.191(2018). Database URL: https://www.climatologylab.org/terraclimate.html
varsAvailable()
varsAvailable()