Saturday, July 16, 2016

The new R version of ENMTools is in the works! Here's how to build an enmtools.species object.

But for real this time.  I've started over entirely from scratch, and I'm using the new R package as a foundation for some novel analyses that I'm developing as part of my current research.  You can download it and view a fairly lengthy manual of what's currently implemented here:

https://github.com/danlwarren/ENMTools


For reasons that will become clear with time (when some of the downstream stuff gets finished), the way you interface with ENMTools is going to be a bit different from how you work with dismo or Biomod.  First off, you start by defining enmtools.species objects for each species (or population) that you want to compare.


Here I'll create one called ahli (based on data from Anolis ahli).



ahli = enmtools.species()

Now that doesn't have any data associated with it, so if we get a summary of it, we basically just hear back from R that we don't have any data.



ahli
## 
## 
## Range raster not defined.
## 
## Presence points not defined.
## 
## Background points not defined.
## 
## Species name not defined.

So let's add some data:





ahli$species.name = "ahli"
ahli$presence.points = read.csv("test/testdata/ahli.csv")[,3:4]
ahli$background.points = background.points.buffer(ahli$presence.points, 20000, 1000, env[[1]])
ahli

And then look at it again:



## 
## 
## Range raster: 
## class       : RasterLayer 
## dimensions  : 418, 1535, 641630  (nrow, ncol, ncell)
## resolution  : 0.008333333, 0.008333333  (x, y)
## extent      : -86.90809, -74.11642, 19.80837, 23.2917  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
## data source : in memory
## names       : layer.1 
## values      : 1, 1  (min, max)
## 
## 
## 
## Presence points (first ten only): 
## 
## | Longitude| Latitude|
## |---------:|--------:|
## |  -80.0106|  21.8744|
## |  -79.9086|  21.8095|
## |  -79.8065|  21.7631|
## |  -79.8251|  21.8095|
## |  -79.8807|  21.8374|
## |  -79.9550|  21.8374|
## |  -80.3446|  22.0136|
## |  -80.2983|  21.9951|
## |  -80.1776|  21.9023|
## |  -80.1591|  21.9673|
## 
## 
## Background points (first ten only): 
## 
## | Longitude| Latitude|
## |---------:|--------:|
## | -79.78726| 21.72920|
## | -79.82892| 21.73754|
## | -79.83726| 21.69587|
## | -80.01226| 22.01254|
## | -79.63726| 21.76254|
## | -79.92892| 21.78754|
## | -79.99559| 22.12920|
## | -79.81226| 21.87087|
## | -80.30392| 22.07920|
## | -79.97892| 21.85420|
## 
## 
## Species name:  ahli

Neat, huh?  Next up I'll show you how to build an ENM.


12 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. What is the "env" that you used?

    ReplyDelete
    Replies
    1. The env in this case was a stack of four environmental PC axes.

      Delete
    2. This comment has been removed by the author.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I'm running into an issue which appears to be related to my use of the  background.points.buffer() function. I'm pretty sure I set up my species as suggested in the vignette provided here but I consistently get a error about self intersecting points:

    PcruWEST = enmtools.species()
    PcruWEST$species.name = "PcruWEST"
    PcruWEST$presence.points = read.csv("WEST_Occ.csv")
    mask<-raster("mn30_100.asc")
    PcruWEST$background.points = background.points.buffer(PcruWEST$presence.points, 20000, 1000, mask)

    Error in rgeos::gUnaryUnion(ci@polygons) :
     TopologyException: Input geom 0 is invalid: Self-intersection at or near point -118.5263168301187 -89.984997726323087 at -118.5263168301187 -89.984997726323087


    The error cite the same location each time. I can add the range in but still get an rgeos error. If I shrink my buffer to say 1000m I don't get an error but also I don't get the mask defined or any background points.


    Range raster:
    class : Raster
    Layer dimensions : 1516, 1338, 2028408 (nrow, ncol, ncell) resolution : 0.008333333, 0.008333333 (x, y)
    extent : -97.45014, -86.30014, 28.48319, 41.11653 (xmin, xmax, ymin, ymax)
    crs : NA
    source : /home/nick/PhD/Chapter3_niche/Identity_test/mn30_100.asc
    names : layer
    Presence points (first ten only):
     Latitude| Longitude|
    |--------:|---------:|
    | 36.12014| -94.88807|
    | 37.03224| -94.64201|
    | 35.96949| -94.80963|
    | 34.61530| -94.63110|
    | 35.69509| -94.82553|
    | 35.72999| -94.90363|
    | 35.73489| -94.53750|
    | 34.71500| -94.68023|
    | 34.68991| -94.30057|
    | 34.61858| -94.77128|

    Background points (first ten only):
    | Latitude| Longitude|
     |--------:|---------:|

    Species name: PcruWEST


    Any idea where this error has come from? Happy to share my inputs if that helps?

    Thank you for your help and providing this venue.
    Best,
    Nick

    ReplyDelete
  6. Is there a way to upload my inputs?

    ReplyDelete
  7. If you extract the environmental values at that point, do you get anything? It could just be a bad point.

    ReplyDelete
  8. I clipped the environmental layers and that point is well outside of the study area. I think the intersection was driven by the fact I was using one of my environmental layers as my mask which I've rectified (no longer getting that error)but I'm still not getting any background points but no error at all.

    env20<-raster("mn30_100.asc")
    env1<-raster("na_chelsa_bio10_1.tif100clip.asc")
    env2<-raster("na_chelsa_bio10_2.tif100clip.asc")
    env3<-raster("na_chelsa_bio10_3.tif100clip.asc")
    env4<-raster("na_chelsa_bio10_4.tif100clip.asc")
    env5<-raster("na_chelsa_bio10_5.tif100clip.asc")
    env6<-raster("na_chelsa_bio10_6.tif100clip.asc")
    env7<-raster("na_chelsa_bio10_7.tif100clip.asc")
    env8<-raster("na_chelsa_bio10_8.tif100clip.asc")
    env9<-raster("na_chelsa_bio10_9.tif100clip.asc")
    env10<-raster("na_chelsa_bio10_10.tif100clip.asc")
    env11<-raster("na_chelsa_bio10_11.tif100clip.asc")
    env12<-raster("na_chelsa_bio10_12.tif100clip.asc")
    env13<-raster("na_chelsa_bio10_13.tif100clip.asc")
    env14<-raster("na_chelsa_bio10_14.tif100clip.asc")
    env15<-raster("na_chelsa_bio10_15.tif100clip.asc")
    env16<-raster("na_chelsa_bio10_16.tif100clip.asc")
    env17<-raster("na_chelsa_bio10_17.tif100clip.asc")
    env18<-raster("na_chelsa_bio10_18.tif100clip.asc")
    env19<-raster("na_chelsa_bio10_19.tif100clip.asc")

    env= c(env20+env1+env2+env3+
    env4+env5+env6+env7+env8+
    env9+env10+env11+env12+env13+
    env14+env15+env16+env17+env18+env19)

    #Add taxa
    PcruWEST = enmtools.species()
    PcruWEST$species.name = "PcruWEST"
    PcruWEST$presence.points = read.csv("WEST_Occ.csv")
    PcruWEST$presence.points
    #background.points.buffer(points, radius(in m), n, mask(could add the 100 km buffer clip but the input layers have already been clipped))
    #If you need to load a raster
    mask<-raster("./WEST_ENM.asc")
    PcruWEST$range=mask
    PcruWEST$background.points = background.points.buffer(PcruWEST$presence.points, 1000, 1000, mask)
    PcruWEST$background.points
    PcruWEST

    > PcruWEST


    Range raster:
    class : RasterLayer
    dimensions : 1516, 1338, 2028408 (nrow, ncol, ncell)
    resolution : 0.008333333, 0.008333333 (x, y)
    extent : -97.45014, -86.30014, 28.48319, 41.11653 (xmin, xmax, ymin, ymax)
    crs : NA
    source : /home/nick/PhD/Chapter3_niche/Identity_test/WEST_ENM.asc
    names : WEST_ENM



    Presence points (first ten only):

    | Latitude| Longitude|
    |--------:|---------:|
    | 40.17635| -92.64996|
    | 39.00611| -90.56444|
    | 38.97013| -92.20568|
    | 38.75396| -92.20121|
    | 38.70809| -94.61945|
    | 38.62674| -90.30794|
    | 38.47334| -90.82132|
    | 38.41859| -94.79507|
    | 38.21999| -94.66727|
    | 38.14752| -91.03752|


    Background points (first ten only):

    | Latitude| Longitude|
    |--------:|---------:|


    Species name: PcruWEST

    > PcruWEST$background.points
    [1] Latitude Longitude
    <0 rows> (or 0-length row.names)

    ReplyDelete
  9. I think it is sorted. It looks like putting the Latitude values before the Longitude values caused my issues.

    ReplyDelete
    Replies
    1. That's weird, it should be able to handle that. I'll look into it.

      Delete