r - 如何使用 glmmTMB 从模型输出中计算特定固定效应的预测均值
问题描述
我正在使用 glmmTMB 运行 glmm 并使用 predict() 来计算预测均值。我想知道是否可以根据模型的特定固定效应计算预测均值。
运行模型
model_1 <- glmmTMB(step.rate ~ Treatment*Week + Logger.ID + (1|Animal.ID),
data = data.df, family = nbinom1)
我目前正在使用以下代码来计算所有固定效应的预测均值:
新数据
new.dat <- data.frame(Treatment = data.df$Treatment, Week = data.df$Week, Logger.ID =
data.df$Logger.ID, Animal.ID = data.df$Animal.ID)
预测均值
new.dat$predicted <- predict(model_1, new.data = new.dat, type = "response", re.form = NA)
我不希望在计算预测均值时包含 Logger.ID,并且想知道是否可以忽略 Logger.ID 以及如何执行此操作。
解决方案
您不能从预测均值中“删除”预测变量(当您使用 时predict()
),因为这只会返回NA
值。因此,我会推荐与 Allen 相同的方法,并使用一个合理/有意义的值,让您保持Logger.ID
不变。这是ggeffects包中的一个示例:
library(ggeffects)
library(glmmTMB)
data("Salamanders")
m <- glmmTMB(count ~ spp * mined + sample + (1 | site), family = nbinom1, data = Salamanders)
# hold "sample" constant at its mean value
ggpredict(m, c("spp", "mined"))
#>
#> # Predicted counts of count
#> # x = spp
#>
#> # mined = yes
#>
#> x | Predicted | SE | 95% CI
#> --------------------------------------
#> GP | 0.04 | 1.01 | [0.01, 0.27]
#> PR | 0.11 | 0.60 | [0.03, 0.36]
#> DM | 0.38 | 0.36 | [0.19, 0.78]
#> EC-A | 0.11 | 0.60 | [0.03, 0.36]
#> EC-L | 0.32 | 0.38 | [0.15, 0.68]
#> DF | 0.56 | 0.32 | [0.30, 1.04]
#>
#> # mined = no
#>
#> x | Predicted | SE | 95% CI
#> --------------------------------------
#> GP | 2.27 | 0.20 | [1.53, 3.37]
#> PR | 0.46 | 0.33 | [0.24, 0.88]
#> DM | 2.42 | 0.20 | [1.64, 3.58]
#> EC-A | 0.89 | 0.27 | [0.53, 1.50]
#> EC-L | 3.20 | 0.19 | [2.21, 4.65]
#> DF | 1.85 | 0.21 | [1.22, 2.81]
#>
#> Adjusted for:
#> * sample = 2.50
#> * site = NA (population-level)
#> Standard errors are on the link-scale (untransformed).
# predicted means when sample is set to "0"
ggpredict(m, c("spp", "mined"), condition = list(sample = 0))
#>
#> # Predicted counts of count
#> # x = spp
#>
#> # mined = yes
#>
#> x | Predicted | SE | 95% CI
#> --------------------------------------
#> GP | 0.04 | 1.02 | [0.00, 0.27]
#> PR | 0.11 | 0.62 | [0.03, 0.36]
#> DM | 0.38 | 0.38 | [0.18, 0.80]
#> EC-A | 0.11 | 0.61 | [0.03, 0.36]
#> EC-L | 0.32 | 0.40 | [0.15, 0.69]
#> DF | 0.54 | 0.34 | [0.28, 1.06]
#>
#> # mined = no
#>
#> x | Predicted | SE | 95% CI
#> --------------------------------------
#> GP | 2.22 | 0.24 | [1.40, 3.52]
#> PR | 0.45 | 0.36 | [0.22, 0.90]
#> DM | 2.37 | 0.24 | [1.49, 3.78]
#> EC-A | 0.87 | 0.30 | [0.49, 1.58]
#> EC-L | 3.14 | 0.22 | [2.04, 4.81]
#> DF | 1.81 | 0.25 | [1.11, 2.95]
#>
#> Adjusted for:
#> * site = NA (population-level)
#> Standard errors are on the link-scale (untransformed).
由reprex 包(v0.3.0)于 2020 年 9 月 14 日创建
推荐阅读
- javascript - 使用JSoup获取被onclick按钮javascript隐藏的表的内容
- ionic-framework - 如何使用 ionic 检查 NFC 是否适用于 iOS?
- javascript - 如何与 d3.js v5 中的元素以及缩放事件进行交互
- android - 将已发布的应用程序链接到 Firebase 控制台
- scala - Apache Spark 在一次运行中读取多个文本文件
- r - 通过 R 中的光栅循环保存/存储
- javascript - 角度路由 - 当它是应用程序的默认路径时,尝试刷新相同的路由不起作用
- ios - 如果标题文本为空或未分配,如何隐藏 uibutton?
- javascript - 技术解释为什么反引号不适用于导入语句
- python - 想要将 dataset.info() 结果存储到 Python 中的数据框中