Skip to content
Branch: master
Go to file
Code

Latest commit

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
R
 
 
 
 
 
 
 
 
man
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Readme.md

ENMTools

R build status

This package implements various tests, visualizations, and metrics for use with environmental niche models (ENMs) and species distribution models (SDMs).

Citation

Warren, D.L., N. Matzke, M. Cardillo, J. Baumgartner, L. Beaumont, N. Huron, M. Simões, Teresa L. Iglesias, and R. Dinnage. 2019. ENMTools (Software Package). URL: https://github.com/danlwarren/ENMTools. doi:10.5281/zenodo.3268814


Installation

At present, ENMTools is downloadable from https://github.com/danlwarren/ENMTools. There are multiple ways to download it. The easiest is to use devtools and install from GitHub.

Installing from GitHub using devtools

Run the following code from your R console:

install.packages("devtools")
library(devtools)
install_github("danlwarren/ENMTools")
library(ENMTools)

Install from zip file

A zipped version of the package is available at https://github.com/danlwarren/ENMTools/archive/master.zip. To install from the zip file, download a copy of it to your system. Once it’s finished downloading, type the following (where PATH is the path to the zip file):

install.packages("devtools")
library(devtools)
install_local("PATH")
library(ENMTools)

Installing extras

ENMTools uses functionality from a LOT of other R packages, and it’s possible that you don’t want to install them all. For that reason many of the packages are not automatically installed with ENMTools, but instead “suggested”. If you want to install all of the suggested packages, we have a function for that. You should only need to use it after you first install ENMTools or update R. If you choose not to install the extra packages, you will get warnings when you try to use functions that require them.

install.extras()

Interacting with ENMTools

Creating enmtools.species objects

We’re going to load in some environmental data. You can do this from local rasters, like so:

env.files <- list.files(path = "./env_pca/", pattern = "pc", full.names = TRUE)
env <- stack(env.files)
names(env) <- c("pc1", "pc2", "pc3", "pc4")
env <- setMinMax(env)

Or you can load them from the internet using the raster package’s getData() function.

library(raster)
env <- raster::getData('worldclim', var='bio', res=10)
env <- crop(env, extent(-10, 17, 39, 48))
plot(env[[1]])

ENMTools is primarily designed to examine patterns of similarity and difference between ENMs for different species. In order to simplify interactions with the functions in ENMTools, you need to put your data for each of your species into an enmtools.species object. You can create and view an empty enmtools.species object just by typing:

monticola <- enmtools.species()
monticola
## 
## 
## Range raster not defined.
## 
## Presence points not defined.
## 
## Background points not defined.
## 
## Species name not defined.

You can add bits of it when the object is created:

monticola.path <- paste(system.file(package="ENMTools"), "/monticola.csv", sep='')
monticola <- enmtools.species(species.name = "monticola", 
                            presence.points = read.csv(monticola.path))
monticola$range <- background.raster.buffer(monticola$presence.points, 50000, mask = env)
monticola$background.points <- background.points.buffer(points = monticola$presence.points,
                                                   radius = 20000, n = 1000, mask = env[[1]])

Or you can add data to this object after it’s created:

names(monticola)
monticola$species.name <- "monticola"
monticola$presence.points <- read.csv(monticola.path)
monticola$range <- background.raster.buffer(monticola$presence.points, 50000, mask = env)
monticola$background.points <- background.points.buffer(points = monticola$presence.points,
                                                   radius = 20000, n = 1000, mask = env[[1]])

It’s always a very good idea to run the check.species() function on an enmtools.species object after you build or modify it.

monticola <- check.species(monticola)

And now we can take a look at our species! The following code is currently not working in the GitHub vignette due to compatibility issues with GitHub markdown, but if you try it locally it’s pretty cool:

interactive.plot.enmtools.species(monticola)

However, ENMTools also contains some sample data. It contains an enmtools.clade object called “iberolacerta.clade”, which holds several enmtools.species objects including an already-built version of Iberolacerta monticola. It also has some low-resolution Worldclim data that we can use to demonstrate functions. We’ll pull two of those species out now.

data(iberolacerta.clade)
data(euro.worldclim)
monticola <- iberolacerta.clade$species$monticola
cyreni <- iberolacerta.clade$species$cyreni
env <- euro.worldclim

Building an ENM

ENMTools contains functions to simplify the ENM construction process. Using enmtools.species objects and the correct modeling commands, we can build models very quickly. These commands are primarily wrappers to dismo model construction and projection functions, and at present are only available for GLM, Maxent, Domain, and Bioclim models. One of the nice bits about this setup is that it allows enmtools to automatically generate suitability maps, do model evaluation, and plot the marginal suitability of habitat for each variable separately.

Before we get started modeling, however, we might want to trim our predictor set so as to reduce collinearity. ENMTools contains a couple of handy functions for this.

raster.cor.matrix(env)
##              bio1         bio2        bio3        bio4       bio5        bio6
## bio1   1.00000000  0.208715969  0.36766171 -0.21357041  0.8853513  0.93295069
## bio2   0.20871597  1.000000000  0.58362743  0.30918934  0.5417192 -0.01723188
## bio3   0.36766171  0.583627431  1.00000000 -0.56601419  0.3052211  0.42521254
## bio4  -0.21357041  0.309189341 -0.56601419  1.00000000  0.1754180 -0.52131150
## bio5   0.88535133  0.541719174  0.30522110  0.17541802  1.0000000  0.68751772
## bio6   0.93295069 -0.017231880  0.42521254 -0.52131150  0.6875177  1.00000000
## bio7  -0.05329392  0.709020870 -0.14900115  0.87998888  0.4016817 -0.38884596
## bio8   0.24945265  0.068562982 -0.16262988  0.30400935  0.2862188  0.09285337
## bio9   0.73849291  0.183148800  0.42195497 -0.37866158  0.6400464  0.77343845
## bio10  0.96045067  0.311324869  0.21975162  0.06247355  0.9606340  0.80726704
## bio11  0.95921479  0.101719882  0.48107648 -0.47305206  0.7533941  0.98896057
## bio12 -0.60846674 -0.482731496 -0.31488066 -0.07961919 -0.7137513 -0.48704454
## bio13 -0.41433559 -0.510317568 -0.25626002 -0.19124801 -0.5722885 -0.28138429
## bio14 -0.72871981 -0.338229901 -0.37618609  0.16712793 -0.7297745 -0.67564389
## bio15  0.45421260 -0.007931916  0.11838593 -0.21759678  0.3821863  0.47445069
## bio16 -0.44597594 -0.506032477 -0.26995545 -0.16924912 -0.5920746 -0.31485467
## bio17 -0.70338945 -0.343806007 -0.33935275  0.10832583 -0.7243068 -0.63769389
## bio18 -0.81091065 -0.331213024 -0.47078459  0.28348226 -0.7776879 -0.80126262
## bio19 -0.12414066 -0.396445432  0.07348362 -0.48121938 -0.3430162  0.07715578
##              bio7        bio8       bio9       bio10       bio11       bio12
## bio1  -0.05329392  0.24945265  0.7384929  0.96045067  0.95921479 -0.60846674
## bio2   0.70902087  0.06856298  0.1831488  0.31132487  0.10171988 -0.48273150
## bio3  -0.14900115 -0.16262988  0.4219550  0.21975162  0.48107648 -0.31488066
## bio4   0.87998888  0.30400935 -0.3786616  0.06247355 -0.47305206 -0.07961919
## bio5   0.40168174  0.28621877  0.6400464  0.96063401  0.75339408 -0.71375135
## bio6  -0.38884596  0.09285337  0.7734384  0.80726704  0.98896057 -0.48704454
## bio7   1.00000000  0.24603268 -0.1633540  0.20071994 -0.29134540 -0.29133189
## bio8   0.24603268  1.00000000 -0.2200483  0.33135353  0.12985480 -0.30622476
## bio9  -0.16335400 -0.22004834  1.0000000  0.66175815  0.78861884 -0.46297347
## bio10  0.20071994  0.33135353  0.6617581  1.00000000  0.84907884 -0.65829840
## bio11 -0.29134540  0.12985480  0.7886188  0.84907884  1.00000000 -0.54163405
## bio12 -0.29133189 -0.30622476 -0.4629735 -0.65829840 -0.54163405  1.00000000
## bio13 -0.37121605 -0.19351156 -0.3074562 -0.48860809 -0.32972845  0.90648180
## bio14 -0.07381576 -0.25699492 -0.6265099 -0.71411350 -0.72423144  0.77619752
## bio15 -0.11344874  0.17648853  0.3970693  0.41693023  0.49288586 -0.24313910
## bio16 -0.35410914 -0.21747973 -0.3274923 -0.51511693 -0.36513899  0.91789736
## bio17 -0.11473790 -0.30488093 -0.5552129 -0.70368996 -0.68356379  0.80791339
## bio18  0.02381502  0.03291308 -0.8163353 -0.76592247 -0.82992875  0.76402046
## bio19 -0.53249335 -0.51356967  0.1072073 -0.26914814  0.01511498  0.74717071
##            bio13       bio14        bio15      bio16      bio17       bio18
## bio1  -0.4143356 -0.72871981  0.454212596 -0.4459759 -0.7033895 -0.81091065
## bio2  -0.5103176 -0.33822990 -0.007931916 -0.5060325 -0.3438060 -0.33121302
## bio3  -0.2562600 -0.37618609  0.118385927 -0.2699555 -0.3393528 -0.47078459
## bio4  -0.1912480  0.16712793 -0.217596785 -0.1692491  0.1083258  0.28348226
## bio5  -0.5722885 -0.72977455  0.382186338 -0.5920746 -0.7243068 -0.77768793
## bio6  -0.2813843 -0.67564389  0.474450688 -0.3148547 -0.6376939 -0.80126262
## bio7  -0.3712161 -0.07381576 -0.113448735 -0.3541091 -0.1147379  0.02381502
## bio8  -0.1935116 -0.25699492  0.176488534 -0.2174797 -0.3048809  0.03291308
## bio9  -0.3074562 -0.62650986  0.397069264 -0.3274923 -0.5552129 -0.81633533
## bio10 -0.4886081 -0.71411350  0.416930231 -0.5151169 -0.7036900 -0.76592247
## bio11 -0.3297284 -0.72423144  0.492885864 -0.3651390 -0.6835638 -0.82992875
## bio12  0.9064818  0.77619752 -0.243139096  0.9178974  0.8079134  0.76402046
## bio13  1.0000000  0.48404051  0.130409758  0.9902335  0.5245850  0.58835234
## bio14  0.4840405  1.00000000 -0.743674332  0.5012004  0.9859234  0.84673991
## bio15  0.1304098 -0.74367433  1.000000000  0.1244492 -0.7294802 -0.46226779
## bio16  0.9902335  0.50120043  0.124449153  1.0000000  0.5362200  0.61586400
## bio17  0.5245850  0.98592339 -0.729480228  0.5362200  1.0000000  0.81764699
## bio18  0.5883523  0.84673991 -0.462267795  0.6158640  0.8176470  1.00000000
## bio19  0.7906993  0.31690226  0.128299899  0.7920907  0.3839700  0.17654879
##             bio19
## bio1  -0.12414066
## bio2  -0.39644543
## bio3   0.07348362
## bio4  -0.48121938
## bio5  -0.34301616
## bio6   0.07715578
## bio7  -0.53249335
## bio8  -0.51356967
## bio9   0.10720732
## bio10 -0.26914814
## bio11  0.01511498
## bio12  0.74717071
## bio13  0.79069926
## bio14  0.31690226
## bio15  0.12829990
## bio16  0.79209065
## bio17  0.38396995
## bio18  0.17654879
## bio19  1.00000000

That’s great, but it’s a bit hard to pick variables this way. Let’s try it visually instead.

raster.cor.plot(env)
## $cor.mds.plot

## 
## $cor.heatmap

The raster.cor.plot function gives us two visualizations. One heatmap that colors pairs of predictors by their Pearson correlation coefficient, and one cluster plot that does mds scaling of the predictor variables and then plots them in a two dimensional space so that more correlated predictors are closer to each other. We’re going to make an arbitrary decision to just use three predictors, and to keep those predictors relatively uncorrelated we’ll select predictors that are far apart in this mds plot. Here we’ll choose bio1, bio12, and bio7.

env <- env[[c("bio1", "bio12", "bio7")]]
plot(env)

raster.cor.matrix(env)
##              bio1      bio12        bio7
## bio1   1.00000000 -0.6084667 -0.05329392
## bio12 -0.60846674  1.0000000 -0.29133189
## bio7  -0.05329392 -0.2913319  1.00000000

GLM

GLMs usually require the user to supply a formula, an enmtools.species object, and some environmental data. If your formula is a strictly additive function of all of the environmental layers in env, though, enmtools.glm will build a formula automatically.

monticola.glm <- enmtools.glm(species = monticola, env = env, f = pres ~ bio1 + bio12 + bio7, test.prop = 0.2)
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
monticola.glm
## 
## 
## Formula:  presence ~ bio1 + bio12 + bio7
## <environment: 0x7fea2984b818>
## 
## 
## Data table (top ten lines): 
## 
##       Longitude   Latitude   bio1   bio12   bio7   presence
## ---  ----------  ---------  -----  ------  -----  ---------
## 1     -5.171215   43.06957     78     917    249          1
## 2     -6.036635   43.02531     76    1012    246          1
## 3     -7.679727   40.38852    137    1143    247          1
## 4     -7.790437   40.30959    129    1231    242          1
## 5     -7.473340   43.78935    140     931    179          1
## 7     -5.132756   43.49572    133     822    190          1
## 8     -7.787378   40.39362    137    1143    247          1
## 9     -4.941888   43.35310    128     843    194          1
## 10    -7.621731   40.34170    101    1514    229          1
## 11    -7.645674   40.36543    101    1514    229          1
## 
## 
## Model:  
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1, 
##     2)], weights = weights)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.6527  -0.7811  -0.5097   0.8047   2.2145  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  8.2122653  2.2013429   3.731 0.000191 ***
## bio1        -0.0353661  0.0064408  -5.491    4e-08 ***
## bio12        0.0002369  0.0007165   0.331 0.740929    
## bio7        -0.0192589  0.0049933  -3.857 0.000115 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 576.70  on 719  degrees of freedom
## Residual deviance: 509.73  on 716  degrees of freedom
## AIC: 251.17
## 
## Number of Fisher Scoring iterations: 4
## 
## 
## 
## Model fit (training data):  class          : ModelEvaluation 
## n presences    : 208 
## n absences     : 512 
## AUC            : 0.7429575 
## cor            : 0.3173725 
## max TPR+TNR at : 0.03411238 
## 
## 
## Environment space model fit (training data):  class          : ModelEvaluation 
## n presences    : 208 
## n absences     : 10000 
## AUC            : 0.4229404 
## cor            : -0.01636811 
## max TPR+TNR at : 0.3778779 
## 
## 
## Proportion of data wittheld for model fitting:  0.2
## 
## Model fit (test data):  class          : ModelEvaluation 
## n presences    : 52 
## n absences     : 512 
## AUC            : 0.7113882 
## cor            : 0.1686447 
## max TPR+TNR at : -0.02975727 
## 
## 
## Environment space model fit (test data):  class          : ModelEvaluation 
## n presences    : 52 
## n absences     : 10000 
## AUC            : 0.4041673 
## cor            : -0.01417595 
## max TPR+TNR at : 0.360269 
## 
## 
## Suitability:  
## class      : RasterLayer 
## dimensions : 54, 162, 8748  (nrow, ncol, ncell)
## resolution : 0.1666667, 0.1666667  (x, y)
## extent     : -10, 17, 39, 48  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## source     : memory
## names      : layer 
## values     : 0.02620772, 0.9971873  (min, max)
## 
## 
## 
## Notes:

Notice this produces the same formula as:

monticola.glm <- enmtools.glm(species = monticola, env = env, test.prop = 0.2)
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
monticola.glm
## 
## 
## Formula:  presence ~ bio1 + bio12 + bio7
## <environment: 0x7fea28a69a10>
## 
## 
## Data table (top ten lines): 
## 
##       Longitude   Latitude   bio1   bio12   bio7   presence
## ---  ----------  ---------  -----  ------  -----  ---------
## 1     -5.171215   43.06957     78     917    249          1
## 2     -6.036635   43.02531     76    1012    246          1
## 4     -7.790437   40.30959    129    1231    242          1
## 5     -7.473340   43.78935    140     931    179          1
## 6     -6.575039   42.91070     84    1012    247          1
## 7     -5.132756   43.49572    133     822    190          1
## 8     -7.787378   40.39362    137    1143    247          1
## 9     -4.941888   43.35310    128     843    194          1
## 10    -7.621731   40.34170    101    1514    229          1
## 11    -7.645674   40.36543    101    1514    229          1
## 
## 
## Model:  
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1, 
##     2)], weights = weights)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.6464  -0.7919  -0.4889   0.8096   2.2365  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  9.114e+00  2.282e+00   3.994 6.50e-05 ***
## bio1        -3.606e-02  6.580e-03  -5.480 4.25e-08 ***
## bio12       -2.198e-05  7.503e-04  -0.029    0.977    
## bio7        -2.177e-02  5.163e-03  -4.216 2.48e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 576.70  on 719  degrees of freedom
## Residual deviance: 508.11  on 716  degrees of freedom
## AIC: 249.64
## 
## Number of Fisher Scoring iterations: 4
## 
## 
## 
## Model fit (training data):  class          : ModelEvaluation 
## n presences    : 208 
## n absences     : 512 
## AUC            : 0.7402625 
## cor            : 0.3195048 
## max TPR+TNR at : 0.00282174 
## 
## 
## Environment space model fit (training data):  class          : ModelEvaluation 
## n presences    : 208 
## n absences     : 10000 
## AUC            : 0.4236447 
## cor            : -0.0150564 
## max TPR+TNR at : 0.3845752 
## 
## 
## Proportion of data wittheld for model fitting:  0.2
## 
## Model fit (test data):  class          : ModelEvaluation 
## n presences    : 52 
## n absences     : 512 
## AUC            : 0.6979041 
## cor            : 0.1666565 
## max TPR+TNR at : 0.00282174 
## 
## 
## Environment space model fit (test data):  class          : ModelEvaluation 
## n presences    : 52 
## n absences     : 10000 
## AUC            : 0.404225 
## cor            : -0.01389759 
## max TPR+TNR at : 0.5006304 
## 
## 
## Suitability:  
## class      : RasterLayer 
## dimensions : 54, 162, 8748  (nrow, ncol, ncell)
## resolution : 0.1666667, 0.1666667  (x, y)
## extent     : -10, 17, 39, 48  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## source     : memory
## names      : layer 
## values     : 0.02322096, 0.9968632  (min, max)
## 
## 
## 
## Notes:

If you want a more complicated formula, though (e.g., with interactions or polynomial effects), you’ll need to supply that manually.

monticola.glm <- enmtools.glm(species = monticola, env = env, f = pres ~ poly(bio1, 2) + poly(bio7, 2) * poly(bio12, 2), test.prop = 0.2)
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
monticola.glm
## 
## 
## Formula:  presence ~ poly(bio1, 2) + poly(bio7, 2) + poly(bio12, 2) + poly(bio7, 
##     2):poly(bio12, 2)
## <environment: 0x7fea30bdf618>
## 
## 
## Data table (top ten lines): 
## 
##       Longitude   Latitude   bio1   bio12   bio7   presence
## ---  ----------  ---------  -----  ------  -----  ---------
## 1     -5.171215   43.06957     78     917    249          1
## 2     -6.036635   43.02531     76    1012    246          1
## 3     -7.679727   40.38852    137    1143    247          1
## 4     -7.790437   40.30959    129    1231    242          1
## 6     -6.575039   42.91070     84    1012    247          1
## 7     -5.132756   43.49572    133     822    190          1
## 8     -7.787378   40.39362    137    1143    247          1
## 9     -4.941888   43.35310    128     843    194          1
## 10    -7.621731   40.34170    101    1514    229          1
## 11    -7.645674   40.36543    101    1514    229          1
## 
## 
## Model:  
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1, 
##     2)], weights = weights)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.3180  -0.7152  -0.3802   0.6370   2.7671  
## 
## Coefficients:
##                                 Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                      -0.7662     0.3916  -1.957   0.0504 .  
## poly(bio1, 2)1                  -32.7436     5.9817  -5.474  4.4e-08 ***
## poly(bio1, 2)2                  -34.6319     6.7837  -5.105  3.3e-07 ***
## poly(bio7, 2)1                  -22.9120    12.2787  -1.866   0.0620 .  
## poly(bio7, 2)2                   13.9627    10.5439   1.324   0.1854    
## poly(bio12, 2)1                  28.3536    11.9317   2.376   0.0175 *  
## poly(bio12, 2)2                  -7.1273     9.4148  -0.757   0.4490    
## poly(bio7, 2)1:poly(bio12, 2)1   41.7648   416.1367   0.100   0.9201    
## poly(bio7, 2)2:poly(bio12, 2)1 -235.6388   285.0767  -0.827   0.4085    
## poly(bio7, 2)1:poly(bio12, 2)2  409.1193   272.5473   1.501   0.1333    
## poly(bio7, 2)2:poly(bio12, 2)2  165.9995   171.8864   0.966   0.3342    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 576.70  on 719  degrees of freedom
## Residual deviance: 449.53  on 709  degrees of freedom
## AIC: 240.4
## 
## Number of Fisher Scoring iterations: 5
## 
## 
## 
## Model fit (training data):  class          : ModelEvaluation 
## n presences    : 208 
## n absences     : 512 
## AUC            : 0.796706 
## cor            : 0.4137759 
## max TPR+TNR at : 0.04030547 
## 
## 
## Environment space model fit (training data):  class          : ModelEvaluation 
## n presences    : 208 
## n absences     : 10000 
## AUC            : 0.6928058 
## cor            : 0.09893607 
## max TPR+TNR at : 0.4410304 
## 
## 
## Proportion of data wittheld for model fitting:  0.2
## 
## Model fit (test data):  class          : ModelEvaluation 
## n presences    : 52 
## n absences     : 512 
## AUC            : 0.7625075 
## cor            : 0.2327113 
## max TPR+TNR at : 0.231778 
## 
## 
## Environment space model fit (test data):  class          : ModelEvaluation 
## n presences    : 52 
## n absences     : 10000 
## AUC            : 0.6702365 
## cor            : 0.04219867 
## max TPR+TNR at : 0.311183 
## 
## 
## Suitability:  
## class      : RasterLayer 
## dimensions : 54, 162, 8748  (nrow, ncol, ncell)
## resolution : 0.1666667, 0.1666667  (x, y)
## extent     : -10, 17, 39, 48  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## source     : memory
## names      : layer 
## values     : 2.220446e-16, 0.9865399  (min, max)
## 
## 
## 
## Notes:

To check out the marginal response functions, you only need to type

monticola.glm$response.plots
## $bio1

## 
## $bio7

## 
## $bio12

These plots present a smoothed estimate of the frequency of different levels of the environemntal variable in the presence data and the background points, along with the estimated relationship between that environmental predictor and the suitability of habitat from the model.

You can also visualize your models and data in a 2D environment space using any pair of layers from your environment stack. These plots hold all non-plotted variables (bio7 in this case) constant at their mean value across all presence points, then vary the plotted variables between the minimum and maximum values in env.

The suit.plot shows you suitability in environment space as a function of your two variables, with brighter colors representing variable combinations predicted to be more suitable. The points represent the occurrence points for your species in that environment space.

The colored raster of the background.plot shows you the density of background points in environment space, while the white points again represent your occurrence points in environment space.

visualize.enm(monticola.glm, env, layers = c("bio1", "bio12"), plot.test.data = TRUE)
## $background.plot

## Warning: Removed 396 rows containing missing values (geom_raster).

## 
## $suit.plot

GAM, Bioclim, Domain, and Maxent

The procedure for building Bioclim, Domain, and Maxent models is similar to the procedure for GLMs, with the exception that you do not need to pass a formula to the model function for Maxent, Domain, and Bioclim models. Note that running Maxent models requires a bit of extra setup; see dismo documentation for details.

monticola.gam <- enmtools.gam(monticola, env, f = pres ~ poly(bio1, 2) + poly(bio7, 2) * poly(bio12, 2), test.prop = 0.2)
monticola.dm <- enmtools.dm(monticola, env, test.prop = 0.2)
monticola.bc <- enmtools.bc(monticola, env, test.prop = 0.2)
monticola.mx <- enmtools.maxent(monticola, env, test.prop = 0.2)

Metrics: breadth, correlation, and overlap

ENMTools provides a number of metrics for ENMs and for similarities between ENMs. These include measures of niche breadth, based on Levins(1968). An important caveat when interpreting these metrics is that they are driven to some (variable) extent by the availability of different combinations of environmental predictors. As such they are more accurately interpreted as a measurment of the smoothness of the geographic distribution of suitability scores than as an estimate of the breadth of the fundamental niche; an organism with narrow fundamental niche breadth that nonetheless encompasses a set of environmental conditions that is quite common will have a high breadth when measured using ENMs, while having a low breadth in environment space.

raster.breadth(monticola.glm)
## $B1
## [1] 0.9448153
## 
## $B2
## [1] 0.5399201

ENMTools also provides metrics for measuring similarity between ENMs. These include Schoener’s D (Schoener 1968), I (Warren et al. 2008), and the Spearman rank correlation coefficient between two rasters. While D and I are commonly used in the ENM literature, they may tend to overestimate similarity between ENMs when many grid cells are of similar values (e.g., when two species prefer different habitat but the region contains a great deal of habitat that is unsuitable for both).

monticola.glm <- enmtools.glm(species = monticola, env = env, f = pres ~ poly(bio1, 2) + poly(bio7, 2) + poly(bio12, 2), test.prop = 0.2)
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
cyreni.glm <- enmtools.glm(species = cyreni, env = env, f = pres ~ poly(bio1, 2) + poly(bio7, 2) + poly(bio12, 2), test.prop = 0.2)
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
raster.overlap(monticola.glm, cyreni.glm)
## $D
## [1] 0.6886992
## 
## $I
## [1] 0.8962556
## 
## $rank.cor
## [1] 0.3095624

A new feature of the R version of ENMTools is that you can now use these same metrics in the n-dimensional space of all combinations of environmental variables, instead of restricting your measures of model similarity to those sets of conditions that appear in the training region. This is done by repeatedly drawing Latin hypercube samples from the space of all possible combinations of environmental variables given the min and max of each variable within the training region. ENMTools continues to draw samples until subsequent iterations differ by less than a specified tolerance value. Lower tolerance values result in more precise estimates of overlap, but can take much longer to calculate.

monticola.glm <- enmtools.glm(species = monticola, env = env, f = pres ~ poly(bio1, 2) + poly(bio7, 2) + poly(bio12, 2), test.prop = 0.2)
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
cyreni.glm <- enmtools.glm(species = monticola, env = env, f = pres ~ poly(bio1, 2) + poly(bio7, 2) + poly(bio12, 2), test.prop = 0.2)
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
env.overlap(monticola.glm, cyreni.glm, env, tolerance = .001)
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."

## $env.D
## [1] 0.9720725
## 
## $env.I
## [1] 0.9988971
## 
## $env.cor
## [1] 0.9988582
## 
## $env.D.plot

## 
## $env.I.plot

## 
## $env.cor.plot

The plots that come out of these environment space functions are used for diagnosing convergence of the overlap/breadth metric. Ideally what you want is a relationship between the metric and the number of samples that shows no clear directional trend.

Hypothesis testing

Niche identity or equivalency test

In this example, we will run a niche identity (also called equivalency) test, as in Warren et al. 2008. This test takes the presence points for a pair of species and randomly reassigns them to each species, then builds ENMs for these randomized occurrences. By doing this many times, we can estimate the probability distribution for ENM overlap between species under the null hypothesis that the two species’ occurrences in the environment are effectively a random draw from the same underlying distribution. Note that niche evolution is only one of many reasons why two species’ realized environmental distributions might cause departures from this null hypothesis. See Warren et al. 2014 for details.

To run an identity test, we need to decide what type of models we will build, how many replicates we will run, and (in the case of GLM and GAM) a model formula to use for empirical models and the Monte Carlo replicates. The resulting object contains the replicate models, p values, and plots of the results. Typically idenity tests are run with at least 99 replicates, but we are using a smaller number here for the sake of execution time.

NOTE: In order for models to be comparable, both empirical and pseudoreplicate models for the identity test are conducted with pseudoabsence points pooled for the two species being compared.

id.glm <- identity.test(species.1 = monticola, species.2 = cyreni, env = env, type = "glm", nreps = 4)
id.glm
## 
## 
##  
## 
## Identity test monticola vs. cyreni
## 
## objectentity test p-values:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.2      0.2      0.2      0.2      0.2      0.2 
## 
## 
## Replicates:
## 
## 
##                      D           I    rank.cor       env.D       env.I     env.cor
## ----------  ----------  ----------  ----------  ----------  ----------  ----------
## empirical    0.3244487   0.5929443   0.1049357   0.3682927   0.6161923   0.1360666
## rep 1        0.9765794   0.9994436   0.9940210   0.9715600   0.9990031   0.9940687
## rep 2        0.8827986   0.9873842   0.9758915   0.9144146   0.9853457   0.9779154
## rep 3        0.8840139   0.9870933   0.9232828   0.9217613   0.9888025   0.9291080
## rep 4        0.9857272   0.9997983   0.9993446   0.9882608   0.9996803   0.9994138

Background or similarity test

The background or similarity test compares the overlap seen between two species’ ENMs to the overlap expected by chance if one or both species was effectively choosing habitat at random from within their broad geographic range. The purpose of this test is to correct for the availability of habitat and ask whether the observed similarity between species or populations is significantly more (or less) than expected given the available set of environments in the regions in which they occur.

NOTE: In order for models to be comparable, both empirical and pseudoreplicate models for the background test are conducted with pseudoabsence points pooled for the two species being compared.

In Warren et al. 2008, we developed this test in the context of comparing one species’ actual occurrence to the random background occurrences of the other species. This is what we call an “asymmetric” test, and in our case we did the test in both directions with the idea that we might compare the results of A vs. B background to the results of B vs. A background. This may be informative in some cases, but many people have also found this asymmetry confusing (and indeed it is often difficult to interpret). For that reason, the background test here can be conducted against a null hypothesis that is generated from “asymmetric” (species.1 vs species.2 background) or “symmetric” (species.1 background vs. species.2 background) comparisons.

Here, for instance, is a Bioclim background test using the classical asymmetric approach:

bg.bc.asym <- background.test(species.1 = monticola, species.2 = cyreni, env = env, type = "bc", nreps = 4, test.type = "asymmetric" )
bg.bc.asym
## 
## 
##  
## 
## Asymmetric background test
##  monticola vs. cyreni background
## 
## background test p-values:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.4      0.4      0.4      0.4      0.4      0.4 
## 
## 
## Replicates:
## 
## 
##                      D           I     rank.cor       env.D       env.I     env.cor
## ----------  ----------  ----------  -----------  ----------  ----------  ----------
## empirical    0.0242365   0.1455829   -0.0450249   0.0140446   0.1112266   0.1604221
## rep 1        0.6133528   0.8642002    0.6526089   0.5203400   0.7861611   0.7647684
## rep 2        0.6279082   0.8708753    0.6924823   0.5105405   0.7654790   0.6538866
## rep 3        0.6357870   0.8785414    0.6904330   0.5461624   0.7899489   0.6814220
## rep 4        0.6193478   0.8676220    0.6700723   0.5088732   0.7699090   0.6837504

And here is a Domain background test using the symmetric approach:

bg.dm.sym <- background.test(species.1 = monticola, species.2 = cyreni, env = env, type = "dm", nreps = 4, test.type = "symmetric" )
bg.dm.sym
## 
## 
##  
## 
## Symmetric background test
##  monticola background vs. cyreni background
## 
## background test p-values:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.4      0.4      0.4      0.4      0.4      0.4 
## 
## 
## Replicates:
## 
## 
##                      D           I     rank.cor       env.D       env.I     env.cor
## ----------  ----------  ----------  -----------  ----------  ----------  ----------
## empirical    0.1974048   0.4272913   -0.1020590   0.0788000   0.2671629   0.2229175
## rep 1        0.9501028   0.9974058    0.7029178   0.8690309   0.9681439   0.9354426
## rep 2        0.9689530   0.9979032    0.8998751   0.8601197   0.9566349   0.9760561
## rep 3        0.9448187   0.9945018    0.7764072   0.7856842   0.9193155   0.9488629
## rep 4        0.9388375   0.9957586    0.5661593   0.8083104   0.9421562   0.8913642

Ecospat tests

Using enmtools.species objects also provides a simplified interface to the niche equivalency and similarity tests (or identity and background tests, respectively) that were developed by Broennimann et al. (2012). These tests do not rely on ENMs, instead using kernel density smoothing to estimate density of the species in environment space. Ecospat also uses the density of the available environment to correct for availability when measuring overlaps, so that overlaps are not strictly driven by availability of combinations of environmental variables.

These tests only work with two environmental axes, so they are often done with the top two PC axes of a set of environments. In our case we’ll just pick a couple of environmental layers, though (bio1 and bio2). Here’s an equivalency/identity test:

esp.id <- enmtools.ecospat.id(monticola, cyreni, env[[c("bio1", "bio12")]])
## 
## 
## No background points provided, drawing background from range raster.
## 
## 
## 
## No background points provided, drawing background from range raster.
esp.id
## 
## 
##  
## 
## Ecospat identity test monticola vs. cyreni
## 
## ecospat.id test empirical overlaps:
## $D
## [1] 0.0477392
## 
## $I
## [1] 0.2097866
## 
## 
## 
## ecospat.id test p-values:
## D I 
## 0 0

## NULL

And here’s a symmetric background test. The difference between symmetric and asymmetric for these tests is the same as for the background tests presented above.

esp.bg.sym <- enmtools.ecospat.bg(monticola, cyreni, env[[c("bio1", "bio12")]], test.type = "symmetric")
## 
## 
## No background points provided, drawing background from range raster.
## 
## 
## 
## No background points provided, drawing background from range raster.
esp.bg.sym
## 
## 
##  
## 
## Ecospat background test symmetric monticola vs. cyreni
## 
## ecospat.bg test empirical overlaps:
## $D
## [1] 0.0477392
## 
## $I
## [1] 0.2097866
## 
## 
## 
## ecospat.bg test p-values:
##    D    I 
## 0.48 0.42

## NULL

Note that if you provide more than two layers to the enmtools.ecospat function, it will performa a PCA analysis on the provided layers and measure overlaps on the first two axes of that PCA space.

esp.bg.sym <- enmtools.ecospat.bg(monticola, cyreni, env, test.type = "symmetric")
## 
## 
## No background points provided, drawing background from range raster.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## [1] "More than two layers in environment stack and no layers argument passed, performing PCA..."
esp.bg.sym
## 
## 
##  
## 
## Ecospat background test symmetric monticola vs. cyreni
## 
## ecospat.bg test empirical overlaps:
## $D
## [1] 0.01293428
## 
## $I
## [1] 0.109648
## 
## 
## 
## ecospat.bg test p-values:
##    D    I 
## 0.56 0.50

## NULL

Rangebreak tests

ENMTools also allows you to perform linear, blob, and ribbon rangebreak tests as developed in Glor and Warren 2011. The linear and blob tests are two versions of a test that permit one to ask whether the geographic regions occupied by two species are more environmentally different than expected by chance. The ribbon test, meanwhile, is designed to test whether the ranges of two species are divided by a region that is relatively unsuitable to one or both forms.

For the linear and blob tests, you call them very much like you would the identity and background tests. Here’s a linear one using GLM models:

rbl.glm <- rangebreak.linear(monticola, cyreni, env, type = "glm", nreps = 4)
## 
## 
## No background points provided, drawing background from range raster.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## 
## Building empirical models...
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Building replicate models...
## 
## Replicate 1 ...
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 2 ...
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 3 ...
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 4 ...
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
rbl.glm
## 
## 
##  
## 
## Linear rangebreak test monticola vs. cyreni
## 
## rangebreak test p-values:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.2      0.2      0.4      0.2      0.4      0.8 
## 
## 
## Replicates:
## 
## 
##                      D           I    rank.cor       env.D       env.I      env.cor
## ----------  ----------  ----------  ----------  ----------  ----------  -----------
## empirical    0.3244487   0.5929443   0.1049357   0.3681019   0.6158336    0.1340129
## rep 1        0.5971889   0.8541483   0.1981989   0.5649468   0.7668103   -0.2832184
## rep 2        0.4164302   0.6969905   0.2230349   0.4454763   0.6986642    0.3859231
## rep 3        0.7279909   0.9329203   0.3193344   0.6256573   0.8363120   -0.2135940
## rep 4        0.4917095   0.7111988   0.0023427   0.4141336   0.5950910   -0.3483040

And here’s a blob test using Bioclim:

rbb.bc <- rangebreak.blob(monticola, cyreni, env, type = "bc", nreps = 4)
## 
## 
## No background points provided, drawing background from range raster.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## 
## Building empirical models...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Building replicate models...
## 
## Replicate 1 ...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 2 ...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 3 ...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 4 ...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
rbb.bc
## 
## 
##  
## 
## blob rangebreak test monticola vs. cyreni
## 
## rangebreak test p-values:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.6      0.6      0.4      0.4      0.4      0.4 
## 
## 
## Replicates:
## 
## 
##                      D           I     rank.cor       env.D       env.I     env.cor
## ----------  ----------  ----------  -----------  ----------  ----------  ----------
## empirical    0.0242365   0.1455829   -0.0450249   0.0136553   0.1102247   0.1604765
## rep 1        0.0037397   0.0333060   -0.2084307   0.0024187   0.0271124   0.0312522
## rep 2        0.0172571   0.0983554   -0.0222236   0.0373351   0.1576568   0.2103904
## rep 3        0.3272086   0.5455610    0.4582136   0.2503312   0.4675411   0.3636188
## rep 4        0.6752625   0.8550095    0.6797352   0.4985722   0.7324030   0.7461211

If you want to access the individual replicates (for instance to see how your ranges are being split up), you can find them in the list named “replicate.models” inside your rangebreak test object.

rbl.glm$replicate.models$monticola.rep.1
## 
## 
## Formula:  presence ~ bio1 + bio12 + bio7
## <environment: 0x7fea31568448>
## 
## 
## Data table (top ten lines): 
## 
##  Longitude   Latitude   bio1   bio12   bio7   presence
## ----------  ---------  -----  ------  -----  ---------
##       0.13      42.64     61    1053    238          1
##      -0.02      42.59     62    1027    240          1
##      -2.45      42.46    126     512    265          1
##      -3.65      41.05     93     569    284          1
##      -3.65      41.05     93     569    284          1
##      -3.65      41.05     93     569    284          1
##      -3.77      40.78    114     491    287          1
##      -3.77      40.78    114     491    287          1
##      -3.77      40.78    114     491    287          1
##      -3.77      40.96     94     558    283          1
## 
## 
## Model:  
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1, 
##     2)], weights = weights)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.7194  -0.7133  -0.5149   0.7256   1.8994  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept) 11.339919   2.474542   4.583 4.59e-06 ***
## bio1        -0.052827   0.006398  -8.256  < 2e-16 ***
## bio12       -0.002136   0.000832  -2.567  0.01026 *  
## bio7        -0.015994   0.005546  -2.884  0.00393 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 720.87  on 901  degrees of freedom
## Residual deviance: 618.19  on 898  degrees of freedom
## AIC: 301.75
## 
## Number of Fisher Scoring iterations: 4
## 
## 
## 
## Model fit (training data):  class          : ModelEvaluation 
## n presences    : 260 
## n absences     : 642 
## AUC            : 0.7594566 
## cor            : 0.3596942 
## max TPR+TNR at : -0.09265008 
## 
## 
## Environment space model fit (training data):  class          : ModelEvaluation 
## n presences    : 260 
## n absences     : 10000 
## AUC            : 0.4533877 
## cor            : 0.0001488125 
## max TPR+TNR at : 0.3586391 
## 
## 
## Proportion of data wittheld for model fitting:  0
## 
## Model fit (test data):  [1] NA
## 
## 
## Environment space model fit (test data):  [1] NA
## 
## 
## Suitability:  
## class      : RasterLayer 
## dimensions : 54, 162, 8748  (nrow, ncol, ncell)
## resolution : 0.1666667, 0.1666667  (x, y)
## extent     : -10, 17, 39, 48  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## source     : memory
## names      : layer 
## values     : 0.02993972, 0.9987319  (min, max)
## 
## 
## 
## Notes:

rbl.glm$replicate.models$cyreni.rep.1
## 
## 
## Formula:  presence ~ bio1 + bio12 + bio7
## <environment: 0x7fea1c8c3498>
## 
## 
## Data table (top ten lines): 
## 
##  Longitude   Latitude   bio1   bio12   bio7   presence
## ----------  ---------  -----  ------  -----  ---------
##      -7.09      43.11    110     994    227          1
##      -7.09      43.11    110     994    227          1
##      -7.10      43.02    110     994    227          1
##      -7.09      43.20    111    1015    219          1
##      -7.09      43.20    111    1015    219          1
##      -7.09      43.29    111    1015    219          1
##      -7.09      43.29    111    1015    219          1
##      -7.08      43.47    128     927    204          1
##      -7.08      43.47    128     927    204          1
##      -7.09      43.38    128     927    204          1
## 
## 
## Model:  
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1, 
##     2)], weights = weights)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.89458  -0.29042  -0.05284  -0.03554   1.17010  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -0.481421   4.436111  -0.109 0.913581    
## bio1         0.009131   0.014623   0.624 0.532352    
## bio12        0.005892   0.001743   3.381 0.000723 ***
## bio7        -0.028004   0.010090  -2.775 0.005513 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 210.72  on 847  degrees of freedom
## Residual deviance: 107.55  on 844  degrees of freedom
## AIC: 49.111
## 
## Number of Fisher Scoring iterations: 6
## 
## 
## 
## Model fit (training data):  class          : ModelEvaluation 
## n presences    : 76 
## n absences     : 772 
## AUC            : 0.8972934 
## cor            : 0.4254139 
## max TPR+TNR at : 0.01714139 
## 
## 
## Environment space model fit (training data):  class          : ModelEvaluation 
## n presences    : 76 
## n absences     : 10000 
## AUC            : 0.7779447 
## cor            : 0.08950445 
## max TPR+TNR at : 0.5042102 
## 
## 
## Proportion of data wittheld for model fitting:  0
## 
## Model fit (test data):  [1] NA
## 
## 
## Environment space model fit (test data):  [1] NA
## 
## 
## Suitability:  
## class      : RasterLayer 
## dimensions : 54, 162, 8748  (nrow, ncol, ncell)
## resolution : 0.1666667, 0.1666667  (x, y)
## extent     : -10, 17, 39, 48  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## source     : memory
## names      : layer 
## values     : 0.002519097, 0.9979295  (min, max)
## 
## 
## 
## Notes:

For the ribbon rangebreak test, you will need one extra thing; a third enmtools.species object representing the occurrence points (for one or both species) that fall within the ribbon of putatively unsuitable habitat. In the case of these two lizards we don’t have such a ribbon, so we’ll just simulate one based on some random points.

plot(env[[1]])
points(cyreni$presence.points, col = "red")
points(monticola$presence.points, col = "blue")

ribbon <- enmtools.species(species.name = "ribbon")
ribbon$presence.points <- data.frame(Longitude = runif(n = 10, min = -9, max = 0),
                                      Latitude = runif(n = 10, min = 40.5, max = 42))
points(ribbon$presence.points, pch = 16)

ribbon$range <- background.raster.buffer(ribbon$presence.points, 20000, mask = env)
ribbon
## 
## 
## Range raster: 
## class      : RasterLayer 
## dimensions : 54, 162, 8748  (nrow, ncol, ncell)
## resolution : 0.1666667, 0.1666667  (x, y)
## extent     : -10, 17, 39, 48  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## source     : memory
## names      : bio1 
## values     : 1, 1  (min, max)
## 
## 
## 
## Presence points (first ten only): 
## 
##   Longitude   Latitude
## -----------  ---------
##  -1.8665925   41.58785
##  -0.0836034   41.32046
##  -3.2149914   40.50337
##  -0.4705731   41.13235
##  -1.5143153   41.64996
##  -1.5581139   41.13326
##  -7.8220046   40.83394
##  -8.6325728   41.72766
##  -8.9118011   41.89424
##  -5.8346455   41.50144
## 
## 
## Background points not defined.
## 
## Species name:  ribbon

Now we’ll run a ribbon rangebreak test using GLM models with quadratic effects. We also need to tell it the width of the ribbons to generate for the replicates. The units for the width argument are the same units that the presence points are in; e.g., if the points are in decimal degrees you should supply the width of the barrier in decimal degrees.

rbr.glm <- rangebreak.ribbon(monticola, cyreni, ribbon, env, type = "glm", f = pres ~ poly(bio1, 2) + poly(bio12, 2) + poly(bio7, 2), width = 0.5, nreps = 4)
## 
## 
## No background points provided, drawing background from range raster.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## 
## Building empirical models...
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species ribbon 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species outside 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Building replicate models...
## 
## Replicate 1 ...
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species ribbon 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species outside 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 2 ...
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species ribbon 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species outside 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 3 ...
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species ribbon 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species outside 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 4 ...
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species ribbon 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## Drawing background from species background points.
## 
## Adding environmental data to species outside 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
rbr.glm
## 
## 
##  
## 
## ribbon rangebreak test monticola vs. cyreni
## 
## rangebreak test p-values...
## 
## Species 1 vs. Species 2:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.4      0.4      1.2      0.4      0.4      0.4 
## 
## Species 1 vs. Ribbon:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.8      0.8      0.8      0.8      0.8      0.4 
## 
## Species 2 vs. Ribbon:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.8      0.8      0.8      0.4      0.4      0.4 
## 
## Outside vs. Ribbon:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.8      0.8      0.4      0.8      0.8      0.4 
## 
## 
## Replicates:
## 
## Species 1 vs. Species 2:
##                   D         I    rank.cor     env.D     env.I     env.cor
## empirical 0.1035467 0.2963885 -0.09445049 0.1788367 0.3101216  0.10834269
## rep 1     0.3327560 0.5523501 -0.32545779 0.2265204 0.3442903 -0.22696830
## rep 2     0.4365708 0.6975324  0.30479501 0.1898505 0.3815130  0.07852734
## rep 3     0.1954782 0.4040600 -0.36433991 0.2230447 0.3653639 -0.07347805
## rep 4     0.3282535 0.6026923 -0.07826871 0.2083030 0.3492703 -0.16756854
## 
## Species 1 vs. Ribbon:
##                   D         I     rank.cor     env.D     env.I      env.cor
## empirical 0.3016786 0.5909455  0.062315768 0.3082952 0.5309906 -0.005506514
## rep 1     0.3483526 0.6181835  0.118160322 0.2146811 0.3501626  0.009770182
## rep 2     0.5640303 0.7990713  0.537349936 0.2880312 0.5469291  0.687281018
## rep 3     0.2434021 0.4455656  0.220427938 0.3548411 0.4587706  0.249918436
## rep 4     0.4478438 0.7319591 -0.006386414 0.3023550 0.4789191  0.004916438
## 
## Species 2 vs. Ribbon:
##                   D         I   rank.cor     env.D     env.I    env.cor
## empirical 0.1931953 0.3574360 -0.1041608 0.1015229 0.1932719 -0.7245471
## rep 1     0.6591174 0.8574106  0.8279234 0.7080712 0.8750649  0.9415926
## rep 2     0.6412057 0.8351158  0.7786542 0.3666512 0.5664658  0.4248406
## rep 3     0.1535903 0.2383875 -0.1317132 0.1597406 0.2302568 -0.1468643
## rep 4     0.8581289 0.9747570  0.9856240 0.8678402 0.9502162  0.9627926
## 
## Outside vs. Ribbon:
##                   D         I    rank.cor     env.D     env.I     env.cor
## empirical 0.3096498 0.5933247 -0.12344160 0.3383000 0.5484379 -0.05243294
## rep 1     0.3939746 0.6684162  0.27686117 0.3046983 0.4702084  0.18906668
## rep 2     0.5763671 0.7948884  0.51647872 0.2392333 0.4813640  0.18605427
## rep 3     0.1560634 0.3737797 -0.06710502 0.2376977 0.3946395  0.02381567
## rep 4     0.5662873 0.8325162  0.34573934 0.5076678 0.7125416  0.47955824

Note that the output table here has slope, intercept, and intercept offset.

rbr.glm$lines.df
##       slope intercept    offset
## 1 -1.499359  34.87621 0.4505606
## 2 -4.508562  11.18973 1.1545326
## 3  1.394952  47.68929 0.4290899
## 4 -1.070391  34.89399 0.3662084

The intercept denotes the intercept corresponding to the CENTER of each ribbon. To get the lines denoting the edges of the ribbons (for example if you want to plot the ribbons on a map), you add and substract the offset. In other words, the top edge of the ribbon is given by y = (slope * x) + intercept + offset, while the bottom edge is given by y = (slope * x) + intercept - offset.

Building an enmtools.clade object

Some of the tests in ENMTools, including some really neat ones that are still in development, require you to build an enmtools.clade object. These objects are simply lists that contain a phylogeny and a set of enmtools.species objects. It’s important that the names of the species objects and their species.name attributes match the names in the phylogeny’s tip.labels. For demonstration, we’re going to build an object for a clade of five anoles from Hispaniola. We have the tree, so we’re just going to grab occurrence data from GBIF using the rgbif package.

library(rgbif)
library(ape)

tree.path <- paste(system.file(package="ENMTools"), "/StarBEAST_MCC.species.txt", sep='')

hisp.anoles <- read.nexus(file = tree.path)

keepers <- c("brevirostris", "marron", "caudalis", "websteri", "distichus")

hisp.anoles <- drop.tip(phy = hisp.anoles, tip = hisp.anoles$tip.label[!hisp.anoles$tip.label %in% keepers])
plot(hisp.anoles)

So there’s our tree. Now we’re going to grab some environmental data.

hisp.env <- raster::getData('worldclim', var='bio', res=10)
hisp.env <- raster::crop(hisp.env, extent(-75, -65, 16, 21))

hisp.env <- setMinMax(hisp.env)

And then we’ll create a function to build species from GBIF.

# Automate the process of downloading data and removing duds and dupes
species.from.gbif <- function(genus, species, name = NA, env){

  # Name it after the species epithet unless told otherwise
  if(is.na(name)){
    name <- species
  }

  # Get GBIF data
  this.sp <- enmtools.species(presence.points = gbif(genus = genus, species = species)[,c("lon", "lat")],
                              species.name = name)

  # Rename columns, get rid of duds
  colnames(this.sp$presence.points) <- c("Longitude", "Latitude")
  this.sp$presence.points <- this.sp$presence.points[complete.cases(extract(env, this.sp$presence.points)),]
  this.sp$presence.points <- this.sp$presence.points[!duplicated(this.sp$presence.points),]

  this.sp$range <- background.raster.buffer(this.sp$presence.points, 50000, mask = hisp.env)

  return(this.sp)
}

Now we’ll create five species and add them to a species.clade object that is called brev.clade.

brevirostris <- species.from.gbif(genus = "Anolis", species = "brevirostris", env = hisp.env)
marron <- species.from.gbif(genus = "Anolis", species = "marron", env = hisp.env)
caudalis <- species.from.gbif(genus = "Anolis", species = "caudalis", env = hisp.env)
websteri <- species.from.gbif(genus = "Anolis", species = "websteri", env = hisp.env)
distichus <- species.from.gbif(genus = "Anolis", species = "distichus", env = hisp.env)


brev.clade <- enmtools.clade(species = list(brevirostris, marron, caudalis, websteri, distichus), tree = hisp.anoles)
check.clade(brev.clade)
## 
## 
## An enmtools.clade object with 5 species
## 
## Species names: 
##   brevirostris    caudalis    distichus   marron      websteri
## 
## Tree: 
## 
## Phylogenetic tree with 5 tips and 4 internal nodes.
## 
## Tip labels:
## [1] "brevirostris" "caudalis"     "distichus"    "marron"       "websteri"    
## 
## Rooted; includes branch lengths.
## 
## 
## Data Summary: 
## 
## 
##                species.names   in.tree   presence   background   range   
## -------------  --------------  --------  ---------  -----------  --------
## brevirostris   brevirostris    TRUE      200        0            present 
## caudalis       caudalis        TRUE      26         0            present 
## distichus      distichus       TRUE      834        0            present 
## marron         marron          TRUE      14         0            present 
## websteri       websteri        TRUE      21         0            present

That’s one way to build a clade object by hand, but there’s already one built into ENMTools to experiment with so we’ll just use that.

data(iberolacerta.clade)

Age-overlap correlation tests (AOC)

The AOC tests allow you to examine patterns of range, point, and ENM overlap in the context of a phylogeny. This is effectively a generalized version of several analyses: age-range correlation (e.g., Fitzpatrick and Turelli 2006), ENM overlap in the context of a phylogeny (e.g., Knouft et al. 2006, Warren et al. 2008), and point overlaps (e.g., Cardillo and Warren 2016).

These tests require the creation of an enmtools.clade object, as above. AOC tests consist of two steps: first, the average overlap at each node in the phylogeny is calcualted using a method that takes tree topology into account (see Fitzpatrick and Turelli 2006), then we perform a linear regression to measure the relationship between node age and average overlap. Due to the fact that these overlaps violate many of the assumptions of a regular linear regression, however (e.g., errors are not iid), we can’t calculate significance in the typical way. Instead we performa Monte Carlo test, permuting the identity of the tips of the tree and repeating the node averaging and modeling steps. Finally we measure statistical significance by comparing the empirical slope and intercept to the distribution of slopes and intercepts from the Monte Carlo replicates.

First, let’s do one using geog.range.overlaps, as in Fitzpatrick and Turelli 2006. Note that this analysis requires that each of your species have a range raster stored in their species object (we did that as part of the function used above).

range.aoc <- enmtools.aoc(clade = iberolacerta.clade,  nreps = 50, overlap.source = "range")
summary(range.aoc)
## 
## 
## Age-Overlap Correlation test
## 
## 50 replicates 
## 
## p values:
##      (Intercept) empirical.df$age 
##       0.03921569       0.03921569

## NULL

Now we can do one using point overlaps just by changing the overlap.source argument:

point.aoc <- enmtools.aoc(clade = iberolacerta.clade,  nreps = 50, overlap.source = "points")
summary(point.aoc)
## 
## 
## Age-Overlap Correlation test
## 
## 50 replicates 
## 
## p values:
##      (Intercept) empirical.df$age 
##        0.2745098        0.3529412

## NULL

Or we can use similarity between ENMs built for each species. Here we’ll use GLM models:

glm.aoc <- enmtools.aoc(clade = iberolacerta.clade,  env = env, nreps = 50, overlap.source = "glm", f = pres ~ poly(bio1, 2) + poly(bio12, 2))
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species monticola 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species martinezricai 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species cyreni 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species horvathi 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species aurelioi 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species aranica 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
## 
## 
## 
## No background points provided, drawing background from range raster.
## 
## Adding environmental data to species bonnali 
##  Processing presence points...
##  Processing background points...
## 
## 
## Drawing background from species background points.
summary(glm.aoc)
## 
## 
## Age-Overlap Correlation test
## 
## 50 replicates 
## 
## p values:
##      (Intercept) empirical.df$age 
##        0.1960784        0.2352941

## NULL

Literature cited

Broennimann, O., Fitzpatrick, M. C., Pearman, P. B., Petitpierre, B., Pellissier, L., Yoccoz, N. G., Thuiller, W., Fortin, M.-J., Randin, C., Zimmermann, N. E., Graham, C. H. and Guisan, A. (2012), Measuring ecological niche overlap from occurrence and spatial environmental data. Global Ecology and Biogeography, 21: 481–497. doi:10.1111/j.1466-8238.2011.00698.x

Fitzpatrick, B. M., & Turelli, M. (2006). The geography of mammalian speciation: mixed signals from phylogenies and range maps. Evolution, 60(3), 601-615.

Knouft, J. H., Losos, J. B., Glor, R. E., & Kolbe, J. J. (2006). Phylogenetic analysis of the evolution of the niche in lizards of the Anolis sagrei group. Ecology, 87(sp7).

Levins, R. 1968. Evolution In Changing Environments. Monographs in Population Biology, volume 2. Princeton University Press, Princeton, New Jersey, USA.

Schoener, T. W. 1968. Anolis lizards of Bimini: resource partitioning in a complex fauna. Ecology 49:704- 726.

Warren, D.L., R.E. Glor, and M. Turelli. 2008. Environmental niche identity versus conservatism: quantitative approaches to niche evolution. Evolution 62:2868-2883. doi: 10.1111/j.1558-5646.2008.00482.x

Warren, D.L., M. Cardillo, D.F. Rosauer, and D.I. Bolnick. 2014. Mistaking geography for biology: inferring processes from species distributions. Trends in Ecology and Evolution 29 (10), 572-580. doi: 10.1016/j.tree.2014.08.003

Warren, D.L., L. Beaumont, R. Dinnage, and J. Baumgartner. 2019. New methods for measuring ENM breadth and overlap in environmental space. Ecography. Doi: 10.1111/ecog.03900

You can’t perform that action at this time.