I'm trying to predict a negative binomial model to a stack of rasters using the predict
function in the raster
package. I need to include an offset term to normalize my count variable. I have tried unsuccessfully to get this to work using a method where the offset term is included in the model like this:
condor.glm <- glm.nb(y_count ~ logsafefood + logpigharvest +
logintdist + houseden + pubforest + pubrange +
privforest + privrange + offset(log(offset)),
data=merge.Hex, control=glm.control(maxit=1000))
predict(rasStack2, condor.glm, filename="cencal_predictlow_model15.img",
overwrite=TRUE, type="response", progress="text",na.rm=TRUE)
R throws this error:
Error in log(offset) : non-numeric argument to mathematical function
If I pass offset
as an argument instead:
condor.glm <- glm.nb(y_count ~ logsafefood + logpigharvest +
logintdist + houseden + pubforest + pubrange +
privforest + privrange,
offset=log(offset),
data=merge.Hex, control=glm.control(maxit=1000))
predict(rasStack2, condor.glm, filename="cencal_predictlow_model15.img",
overwrite=TRUE, type="response", progress="text",na.rm=TRUE)
R creates the prediction surfaces but the values are all equal.
Any suggestions for how to do this correctly?