在 R 中使用 glmmTMB 预测人口级别的栅格堆栈 [英] Predicting to raster stack at population level with glmmTMB in R

查看:73
本文介绍了在 R 中使用 glmmTMB 预测人口级别的栅格堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用适合 glmmTMB 包的 GLMM 在总体级别(即将随机效应设置为 0)预测 R 中的栅格堆栈.我在 中遵循了 Ben Bolker 的解决方案这个线程适用于符合 lme4 的模型,但即使函数参数 re.form=~0 似乎适用于 predict.glmmTMB 除了 predict.merMod 之外,当我使用 glmmTMB 模型进行预测时,它对我不起作用.以下是使用 Robert Hijmans 在上述线程中提供的相同示例代码的示例:

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)

当我运行上面的 predict 代码(生成对象 x)时,出现错误:Error in eval(predvars, data, env) : object 'Year' not found.有人能告诉我我可能做错了什么或者我可以如何解决这个问题吗?

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?

推荐答案

自年"起不是您需要以不同方式提供的预测器栅格.你可以这样做

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)

这篇关于在 R 中使用 glmmTMB 预测人口级别的栅格堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆