Help with MaxEnt modeling in R using Maxnet package

806 views
Skip to first unread message

Alex Barth

unread,
Jul 29, 2017, 10:37:55 PM7/29/17
to Maxent
Hello,
I'm pretty new to Maxent and fairly new at R, I'm attempting to write a program in R which I can use to make SDMs for a variety of species that have presence only data (where I plan on making pseudo-absence points). I've really hit a speedbump and I could use some help. I have been able to pull out the data and make training and test groups, as well as size the model (currently just using WorldClim data but I plan on adding more). Once I go to create the training environment, I am creating a numeric array of all NA values?

Below is this section of my code-------------------

#size model and crop data
model.extent<-extent(-114,-89,25,38)
modelEnv=crop(currentEnv,model.extent)

#build test and train groups
specModl=cbind.data.frame(spec$Longitude,spec$Latitude)
fold <- kfold(specModl,k=5)
modelTest <- specModl[fold==2, ]
modelTrain <- specModl[fold!=2, ]

#build train environment
trainEnv <- extract(modelEnv, modelTrain) ## This is where I'm having trouble

#build absence vals
set.seed(0)
backGround <- randomPoints(modelEnv,1000)
absVals <- extract(modelEnv, backGround)

#make pres/abs columns
presabs <- c(rep(1, nrow(trainEnv)), rep(0, nrow(absVals)))
----------------------------------------------------------------------------------------------------------------
Does any know how I could be approaching this better? Or any resources I should follow? Also I plan on having the model fit 100 times randomly - although I'm uncertain how to include this into the code as well.

Any help is appreciated!

Thanks,
Alex Barth

Jamie M. Kass

unread,
Jul 29, 2017, 11:00:50 PM7/29/17
to Maxent
I restructured your code a bit, showed you an easier way to subset a df using dplyr, extracted the occurrences and background points together after an rbind, and added the call to maxnet. Didn't test this, so let me know if there are any issues. But it should work.

library(dplyr)


#size model and crop data
model.extent <- extent(-114,-89,25,38)
modelEnv <- crop(currentEnv, model.extent)


#build test and train groups
specModl <- spec %>% select(Longitude, Latitude)
fold <- kfold(specModl, k=5)

modelTest <- specModl[fold==2, ]
modelTrain <- specModl[fold!=2, ]

#build absence vals
set.seed(0)
backGround <- randomPoints(modelEnv, 10000)
colnames(backGround) <- c('Longitude', 'Latitude')
trainPts <- rbind(modelTrain, backGround)

#build train environment
trainVals <- as.data.frame(extract(modelEnv, trainPts))

#make pres/abs columns
presabs <- c(rep(1, nrow(modelTrain)), rep(0, nrow(backGround)))

#run maxnet
m <- maxnet(p=presabs, data=trainVals, f=maxnet.formula(p=presabs, data=trainVals, classes='lqh'))
Message has been deleted

Alex Barth

unread,
Aug 1, 2017, 3:47:00 PM8/1/17
to Maxent
Thanks for the help Jamie,

I feel like this is getting me onto the right track, although I still am returning NA values for all the presence points in trainVals.
I've played around trying to convert in Spatial Points dataframes and I've been unsucessful

Any idea where this problem could be coming from?

Thanks,
Alex Barth


ndimhypervol

unread,
Aug 1, 2017, 4:06:20 PM8/1/17
to max...@googlegroups.com
Have you plotted the points over the raster you want to extract from? Like this:

plot(raster1)
points(pts1)

If they fall where you expect, then I guess the problem becomes a little more mysterious. The code I wrote above assumes the points are in the same geographic projection as the raster and are within its extent.

Jamie Kass
PhD Candidate, CCNY
--
You received this message because you are subscribed to a topic in the Google Groups "Maxent" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/maxent/ddDVEf2DiaI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to maxent+un...@googlegroups.com.
To post to this group, send email to max...@googlegroups.com.
Visit this group at https://groups.google.com/group/maxent.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages