# \donttest{
library(raster)
library(dismo)
### make a maxent model
# path to maxent.jar file
path<- paste0(system.file(package="dismo"), "/java/maxent.jar")
if (file.exists(path) & require(rJava)) {
# get predictor variables
fnames <- list.files(path=paste(system.file(package="dismo"), '/ex', sep=''),
pattern='grd', full.names=TRUE )
predictors <- stack(fnames)
#plot(predictors)
# file with presence points
occurence <- paste(system.file(package="dismo"), '/ex/bradypus.csv', sep='')
occ <- read.table(occurence, header=TRUE, sep=',')[,-1]
colnames(occ) <- c("x","y")
occ <- ecospat.occ.desaggregation(occ,min.dist=1)
# fit a domain model, biome is a categorical variable
me <- maxent(predictors, occ, factors='biome')
# predict to entire dataset
pred <- predict(me, predictors)
plot(pred)
points(occ)
### make binary maps
# use MPA to convert suitability to binary map
mpa.cutoff <- ecospat.mpa(pred,occ)
# use Boyce index to convert suitability to binary map
boyce <- ecospat.boyce(pred, occ)
### use the boyce index to find a threshold
pred.bin.arbitrary <- ecospat.binary.model(pred,0.5)
pred.bin.mpa <- ecospat.binary.model(pred,mpa.cutoff)
names(pred.bin.mpa) <- "me.mpa"
pred.bin.arbitrary <- ecospat.binary.model(pred,0.5)
names(pred.bin.arbitrary) <- "me.arbitrary"
### rangesize calculations
if(require(rgeos,alphahull,quietly=TRUE)){
rangesize <- ecospat.rangesize(stack(pred.bin.mpa,pred.bin.arbitrary),
xy=occ,
resol=c(1,1),
eoo.around.modelocp =TRUE,
AOO.circles = TRUE,
d=200000,
lonlat =TRUE)
rangesize$RangeSize
names(rangesize$RangeObjects)
par(mfrow=c(1,3))
plot(ecospat.binary.model(pred,0),legend=FALSE, main="IUCN criteria")
### IUCN criteria & derivates
# plot AOO
plot(rangesize$RangeObjects$AOO,add=TRUE, col="red",legend=FALSE)
# plot EOO
plot(rangesize$RangeObjects$EOO@polygons,add=TRUE, border="red", lwd=2)
# plot circles around occurrences
plot(rangesize$RangeObjects$AOO.circle@polygons,add=TRUE,border="blue")
for(i in 1:2){
## plot the occupied patches of the model
plot(rangesize$RangeObjects$models.ocp[[i]],col=c("grey","blue","darkgreen"),
main=names(rangesize$RangeObjects$models.ocp[[i]]),legend=FALSE)
points(occ,col="red",cex=0.5,pch=19)
## plot EOO around model
plot(rangesize$RangeObjects$eoo.around.model[[i]]@polygons,add=TRUE,border="blue",lwd=2)
## plot EOO around occupied patches
plot(rangesize$RangeObjects$eoo.around.mo.ocp[[i]]@polygons,add=TRUE,border="darkgreen",
lwd=2)
## plot the modeled area within EOO
#plot(rangesize$RangeObjects$model.within.eoo[[i]],col=c("grey","blue","darkgreen"))
#points(occ,col="red",cex=0.5,pch=19)
#plot(rangesize$RangeObjects$EOO@polygons,add=TRUE, border="red", lwd=2)
}
par(mfrow=c(1,1))
### Alpha-hulls are not included in the function yet because of Licence limitations.
### However, alpha-hulls can easily be included manually (see also the help file of
### the alpha hull package):
alpha = 2 # alpha value of 2 recommended by IUCN
del<-alphahull::delvor(occ)
dv<-del$mesh
mn <- mean(sqrt(abs(del$mesh[,3]-del$mesh[,5])^2+abs(del$mesh[,4]-del$mesh[,6])^2))*alpha
alpha.hull<-alphahull::ahull(del,alpha=mn)
#Size of alpha-hulls
#areaahull(alpha.hull) #works but uses a deprecated function in alphahull 2.1
#plot alphahulls
plot(rangesize$RangeObjects$models.ocp[[i]],col=c("grey","blue","darkgreen"),
main=names(rangesize$RangeObjects$models.ocp[[i]]),legend=FALSE)
plot(alpha.hull,add=TRUE,lwd=1)
}
}
# }
Run the code above in your browser using DataLab