如何使用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 package的示例:

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,"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 package(v0.3.0)于2020-09-14创建

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...