Using ENMeval package with prepared dataframes and not directly with raster stack

233 views
Skip to first unread message

gafna jeff

unread,
Aug 9, 2022, 2:39:55 AM8/9/22
to Maxent
                   

Hello everyone,

Am using ENMeval package in large study area(Africa) with high resolution bioclim data. Since my raster stack for Africa is too big and of 0.5m (very high) resolution, I cannot use the raster stack directly in the ENMevaluate function, otherwise I get a memory error. My idea is to extract the values of the occurences and prepare the relevant dataframes instead of using the raster stack directly. Am able to extract the values, prepare a dataframe and run my model in Maxent in R. (see my reproducable code below). Does someone have an idea of how to do this in ENMeval package (I mean using the prepared dataframes and not the raster stack) as this will avoid loading the heavy raster stacks. I would really appreciate any idea


# installing many packages at a time
#install.packages(c("rgbif","spdep", "nfc"))
# load the libraries
library(raster)
library(maptools)
library(dismo)
library(rgdal)
library(rJava)
library(corrplot)

file <- paste(system.file(package = "dismo"), "/ex/bradypus.csv", sep="")
bradypus <- read.table(file, header=T, sep=",")
dim(bradypus)
head(bradypus)

#we only need the 2 and 3 rows
bradypus <- bradypus[,2:3]
head(bradypus)

data("wrld_simpl")
plot(wrld_simpl, col="red", axes=T)

#environmental data or predictors
# Bioclim data provided by the package dismo
files <- list.files(path=paste(system.file(package="dismo"),"/ex", sep=""), pattern="grd", full.names=TRUE )
predictors <- stack(files)
plot(predictors)

plot(predictors, 1)

plot(wrld_simpl, add=T)
points(bradypus, col="blue", cex=0.5, pch=15)

# create background points
set.seed(8)
backgrd <- randomPoints(predictors, 500)

plot(wrld_simpl, col="red", axes=T)
points(bradypus, col="blue", pch=16, cex=0.3)
points(backgrd, col="yellow", pch=16, cex=0.5)

# Extracting values from the raster
envalspre <- extract(predictors, bradypus)
envalabse <- extract(predictors, backgrd)
head(envalspre)
head(envalabse)

#create predictor dataset
pb <- c(rep(1, nrow(envalspre)), rep(0, nrow(envalabse)))
sdmdata <- data.frame(cbind(pb, rbind(envalspre, envalabse)))
head(sdmdata)
names(sdmdata)
sdmdata$biome
sdmdata[,"biome"] <- as.factor(sdmdata[,"biome"])
head(sdmdata)
dim(sdmdata)

#create vector with equal length to the rows of sdmdata and containing contain 0 (background) and 1 (presence) values
my_p <- sdmdata[,1]


#correlation among predictors
pairs(sdmdata[,2:8], cex=0.1, fig=T)
corrplot(cor(sdmdata[,1:8]), type="lower", diag=F)

#Model fitting using model types
#Algorithms require different data structure, i.e. sdmdata, predictors, bradypus.

##Now plot with the prepared data frame
my_maxent <- maxent(sdmdata[,4:10], my_p)
class(my_maxent)


Regards,

Jeff

Jamie M. Kass

unread,
Sep 23, 2022, 6:43:22 PM9/23/22
to Maxent
Jeff,

Sorry for the late reply, but you can do this easily in ENMeval 2 -- please see the new vignette for the SWD (species with data) example:

Jamie

gafna jeff

unread,
Sep 24, 2022, 12:14:12 AM9/24/22
to max...@googlegroups.com
Dear Jamie, 
Thanks alot for your reply. I already figured this out from the same site three weeks ago and got a perfect solution. I really appreciate your time for us. 
Regards 
Jeff

--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maxent+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/maxent/c2418f47-e017-4e1a-82f1-fc2d76d274c6n%40googlegroups.com.

Patrick Freeman

unread,
Sep 27, 2022, 6:37:29 PM9/27/22
to Maxent
Hi Jamie, 

A (hopefully) quick follow-up question on this. If one proceeds with the dataframe only workflow, I am a bit mystified on how you could then take the model results and then apply them to say a final raster stack post-hoc to get a prediction surface using the betas from a selected model. You might be asking "well, why not just include the rasters to begin with and avoid this issue"? 

I have a situation where I have different sets of occurrence/background points at different time points that I'm temporally matching to some covariates (e.g. average shrub cover between 1990 and 1994 matched to location data from the same interval, average shrub cover between 1995 and 1999 matched to location data from the same interval, etc.). What I ultimately want to do is produce a prediction surface based on the most recent set of covariates (e.g. predict habitat suitability based on average shrub cover between 2015 and 2019). I'm actually able to do this using the maxnet/glmnet package but the model evaluation capabilities of that package are not as easy to understand and implement as those in ENMeval. Is there a means of supplying a model object from ENMeval to a prediction function with a new raster stack? I haven't been able to parse whether that's possible or not. Any clarity you can provide is helpful and thanks for your work on this package! 

Regards
Patrick 

Jamie M. Kass

unread,
Sep 30, 2022, 7:22:21 PM9/30/22
to Maxent
Patrick,

Cool, sounds like a dynamic SDM. Yes, you can input your data frame with the temporally matched occurrence records into ENMevaluate() in the way I described above, then once you select your model settings you can pull that model out of the ENMevaluation object with eval.models(), which returns a list of all models built. Just select the model you want to use for prediction from that list, then use either the native maxnet predict() function, which makes a data frame, or use enm.maxnet@predict(), an ENMeval function, to make a prediction raster.

Jamie
Reply all
Reply to author
Forward
0 new messages