Predicting to raster stack at population level with glmmTMB in R

风流意气都作罢 提交于 2021-01-28 06:23:29

问题


I'm trying to predict to a raster stack in R, using a GLMM I fit with the glmmTMB package, at the population level (i.e. setting random effects to 0). I followed Ben Bolker's solution in this thread which works for models fit with lme4, but even though the function argument re.form=~0 appears to be applicable to predict.glmmTMB in addition to predict.merMod, it's not working for me when I predict using a glmmTMB model. Here is an example using the same example code provided by Robert Hijmans in the aforementioned thread:

# example data. See ?raster::predict
logo <- brick(system.file("external/rlogo.grd", package="raster"))
p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85, 
   66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31, 
   22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)
a <- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,
   99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,
   37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)
xy <- rbind(cbind(1, p), cbind(0, a))
v <- data.frame(cbind(pa=xy[,1], extract(logo, xy[,2:3])))
v$Year <- sample(2000:2001, nrow(v), replace=TRUE) 

#fit model using glmmTMB
library(glmmTMB)
m <- glmmTMB(pa ~ red + blue + (1 | Year), data=v)

# use argument "re.form=~0" to make population-level prediction
x <- predict(logo, m, re.form=~0)

When I run the above predict code (to make object x), I get the error: Error in eval(predvars, data, env) : object 'Year' not found. Can someone tell me what I might be doing wrong or how I can work around this?


回答1:


Since "Year" is not a predictor raster you need to provide it in a different way. You can do

x2000 <- predict(logo, m, re.form=~0, const=data.frame(Year=2000))
x2001 <- predict(logo, m, re.form=~0, const=data.frame(Year=2001))

This is equivalent (but less efficient)

logo$Year <- 2000
x2000 <- predict(logo, m, re.form=~0)

If there are many years, perhaps like

 years <- c(2000, 2001)
 s <- list()
 for (i in 1:length(years)) {
    s[[i]] <- predict(logo, m, re.form=~0, const=data.frame(Year=years[i]))
 }
 s <- stack(s)
 sm <- mean(s)



回答2:


That error says, kind of opaquely, that your test data, "logo", has no "Year" variable.

I only added relevant code here, didn't repeat all of the other code that you supplied above.

logo <- brick(system.file("external/rlogo.grd", package="raster"))
x <- predict(logo, m, re.form=~0)
Error in eval(predvars, data, env) : object 'Year' not found

logo$Year <- sample(2000:2001, ncell(logo), replace=TRUE) 
x <- predict(logo, m, re.form=~0)
> x
class      : RasterLayer 
dimensions : 77, 101, 7777  (nrow, ncol, ncell)
resolution : 1, 1  (x, y)
extent     : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
source     : memory
names      : layer 
values     : 0.1260796, 1.690493  (min, max)


来源:https://stackoverflow.com/questions/65329734/predicting-to-raster-stack-at-population-level-with-glmmtmb-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!