I'm attempting to predict elk elevational selection using elk location points (Elk
) relative to an NED elevational raster (Elev
) using maxent
in R. Both are projected in NAD83 using lat/long, and I've cropped the raster to get around memory issues in R.
I've divided the Elk dataset into 5 groups, 4 of which are now the training group (ElkTrain
) and 1 I've reserved as the test group (ElkTest
). I also created my own background data (NonElk
) with its own training and test data (NonElkTrain
, NonElkTest
). I'm running into this error (with and without using my background data) and I can't find anyone discussing this relative to maxent or when using only one dataframe:
> Max <- maxent(x=Elev, p=ElkTrain, a=NonElkTrain)
or
> Max <- maxent(x=Elev, p=ElkTrain, a=NULL, nbg=5000, factors=NULL, removeDuplicates=TRUE)
Error in match.names(clabs, names(xi)) : names do not match previous names
In addition: Warning message:
In .local(x, p, ...) : 1 (0.02%) of the presence points have NA predictor values
Since I'm only using one dataframe (ElkTrain
), what names aren't matching?
That is a bug that occurs when you make a maxent model with only one predictor. You can see it with the data from the example in ?maxent
library(dismo)
# example data
predictors <- stack(list.files(path=paste(system.file(package="dismo"), '/ex', sep=''), pattern='grd', full.names=TRUE ))
occ <- read.table(paste(system.file(package="dismo"), '/ex/bradypus.csv', sep=''), header=TRUE, sep=',')[,-1]
bg <- randomPoints(predictors, 1000)
# this works
me <- maxent(x=predictors[[1:2]], p =occ)
# fails
me <- maxent(x=predictors[[1]], p =occ)
#Error in match.names(clabs, names(xi)) :
# names do not match previous names
This is because with a single layer, the matrix is dropped (the cause of many R bugs...), illustrated here:
extract(predictors[[1:2]], occtrain[1:2,])
# bio1 bio12
#[1,] 263 1639
#[2,] 263 1639
extract(predictors[[1]], occtrain[1:2,])
#[1] 263 263
I will fix that. But here is are two work-arounds.
= Either make a single layer RasterStack (as suggested by you); the simplest approach:
prd <- stack(predictors[[1]])
me <- maxent(x=prd, p =occ)
= Or make a data.frame with extracted raster values for presence and background points:
abs <- cbind(pa=0, bio1=extract(predictors[[1]], bg))
prs <- cbind(pa=1, bio1=extract(predictors[[1]], occ))
and use these data to build the maxent model
x <- data.frame(rbind(prs, abs))
m <- maxent(x[,2,drop=F], p=x[,1,drop=F] )
p <- predict(predictors, m)
plot(p)