I am trying to create a raster with predictions for a model, using glmmTMB.
This is based on a model, and a rasterstack.
I converted the rasterstack to a data frame, as I think this is a requirement for the function predict.glmmTMB
to run.
The model
model6 <- glmmTMB(Used~scale(Road_density) scale(nonforprop) scale(devprop)
scale(forprop) scale(nonfordist_cap3000) scale(fordist_cap3000)
scale(agridist_cap3000) scale(devdist_cap3000) (1|animal_ID),
data=rasterpoints3,na.action=na.omit,family=binomial(link="logit"))
The data frame containing the rasterstack values to predict for
predstack <- as.data.frame(stack2)
The error
glmmTMB:::predict.glmmTMB(model6,predstack,re.form=NA)
Error in eval(predvars, data, env) : object 'animal_ID' not found
I was hoping someone more experienced could help me resolve this. animal_ID is the random intercept in my glmmTMB
object model 6. I am using this package, and not e.g. raster::predict
, exactly because it should be able to deal with random effects. To my understanding, re.form=NA
should take care of this?
CodePudding user response:
There's an open issue about this, but the workaround should be easy: define
predstack$animal_ID <- NA
The random effect variable has to exist in the data, but it's not used. (Due to the internal structure of glmmTMB
it's not completely trivial to fix this at the package level.)
CodePudding user response:
Given Ben's answer, this ought to work as well with either the raster
or terra
package:
p <- predict(stack2, model6, const=data.frame(animal_ID=NA), re.form=NA)
(But in the absence of an example I cannot check it)