Nimbios Setup

25 Nov 2018

Overview

This tutorial will guide you to process spatial data and implement Maxent modeling in R. Therefore, you need to make sure necessary software/libraries are ready in your computer.

Step 1: install the list of software and libraries (adapted from data carpentry)

Name Type Need Install Description and notes
R software Yes Go to CRAN’s cloud download page and select the version for your operating system. Download the base subdirectory and install it on your computer. Software environment for statistical and scientific computing
RStudio software Yes Go to RStudio download page and select RStudio Desktop for your operating system. Download and install it on your computer. Graphic User Interface (GUI) for R
Java JDK software Yes Go to JAVA download page and select Java SE Development Kit 8u191 for your operating system. Download and install version x86 if your operating system is 32-bit, or use version x64 if your operating system is 64-bit. Read more about 32-bit vs. 64-bit. A programing environmental to run Maxent modeling algorithm. This is different than Java (which you might already have installed on your computer)
raster R package Yes Option 1: install packages from Rstudio interface.

Option 2: use install.packages() function in R terminal.

Option 3: run the following script in R terminal to install:
packages_needed <- c("raster","rgdal","sp","dismo","ENMeval", "jsonlite" , "rJava")
pk_to_install <- packages_needed [!( packages_needed %in% rownames(installed.packages()) )]
if( length(pk_to_install) > 0 ) {
install.packages(pk_to_install,repos="http://cran.r-project.org")
}
for raster analysis
sp R package Yes see above for spatial analysis
rgdal R package Yes see above for spatial analysis
dismo R package Yes see above a collection of ENM/SDM tools, including a function to run Maxent.jar in R
rJava R package Yes Note: in macOS, if you see error like this when loading rJava library:
error: unable to load shared object ‘/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so’: Please run the following code in your terminal: sudo R CMD javareconf
An interface to Java
jsonlite R package Yes see above necessary for download data from GBIF
ENMeval R package Yes see above a collection of ENM/SDM tools, including a function to separate occurrences
Maxent software Yes Install dismo package first, then
Option 1: manually download from this link and move Maxent.jar to the path where dismo package is installed, which can be obtained from this functionsystem.file("java", package="dismo").

Option 2: run the following script in R terminal to download.
if( !file.exists(paste0(system.file("java", package="dismo"),"/maxent.jar")) ) {
utils::download.file(url="https://raw.githubusercontent.com/mrmaxent/Maxent/master/ArchivedReleases/3.3.3k/maxent.jar",
destfile=paste0(system.file("java", package="dismo"),"/maxent.jar"),
mode="wb")
}
Maxent modeling algorithm
GDAL software optional Windows: do the installations through OSGeo4W.
-Download either the [32 bit or 64 bit OSGeo4W installer](https://trac.osgeo.org/osgeo4w/). -Run the OSGeo4W setup program.
-Select “Advanced Install” and press Next.
-Select “Install from Internet” and press Next.
-Select a installation directory. The default suggestion is fine in most cases. Press Next.
-Select “Local packacke directory”. The suggestions is fine in most cases. Press Next.
-Select “Direct connection” and press Next.
-Choose the download.osgeo.org and press Next.
-Find “gdal” or “proj” under “Commandline_Utilities” and click the package in the “New” column until the version you want to install appears.
-Press next to install PROJ.


macOS: run the following code.
$ brew tap osgeo/osgeo4mac && brew tap --repair
$ brew install proj
$ brew install geos
$ brew install gdal2 --with-armadillo --with-complete --with-libkml --with-unsupported
$ brew link --force gdal2


Find more help via this link.
Geospatial model for reading and writing a variety of formats; this is necessary if you want to install rgdal package from source code
PROJ.4 software optional see above Coordinate reference system transformations; this is necessary if you want to install rgdal package from source code

Step 2: test if you have successfully install all necessary software/packages:

2.1 Open RStudio

2.2 Load libraries from R terminal:

library("raster")
library("dismo")
library("rgdal")
library("sp")
library("ENMeval")
library("rJava")

2.3 Run a simple Maxent model from R terminal:

# get predictor variables
fnames <- list.files(path=paste(system.file(package="dismo"), '/ex', sep=''), 
              pattern='grd', full.names=TRUE )
predictors <- stack(fnames)

# file with presence points
occurence <- paste(system.file(package="dismo"), '/ex/bradypus.csv', sep='')
occ <- read.table(occurence, header=TRUE, sep=',')[,-1]

# witholding a 20% sample for testing 
fold <- kfold(occ, k=5)
occtest <- occ[fold == 1, ]
occtrain <- occ[fold != 1, ]

# fit model, biome is a categorical variable
me <- maxent(predictors, occtrain, factors='biome')

# see the maxent results in a browser:
me