简体   繁体   English

使用截断计数从 glmmTMB 进行预测

[英]Predicting from glmmTMB with truncated counts

I'm running a glmmTMB model with various truncated count distributions ( truncated_poisson , truncated_compois , truncated_nbinom1 , truncated_nbinom2 ).我正在运行具有各种截断计数分布( truncated_poissontruncated_compoistruncated_nbinom1truncated_nbinom2 )的 glmmTMB model。 When I predict from the model, the values seem to be lower than expected, as if the prediction is not accounting for the truncation.当我根据 model 进行预测时,这些值似乎低于预期,好像预测没有考虑截断。 Where am I going wrong?我哪里错了? A toy example is provided, showing that predicted values are lower than observed means.提供了一个玩具示例,显示预测值低于观察到的平均值。

Any advice would be appreciated.任何意见,将不胜感激。 Extra points if the advice can extend to the other truncated count distributions (see above) and if it shows how to correctly get the 95% confidence band around the estimated values in these cases.如果建议可以扩展到其他截断计数分布(见上文),并且它显示了如何在这些情况下正确地获得估计值周围的 95% 置信带,则加分。

library(dplyr)
library( extraDistr)
library(glmmTMB)

set.seed(1)
df <- data.frame(Group = rep(c("a", "b"), each = 20), N = rtpois(40, 1, a = 0), ran = "a") %>%
        mutate(N = ifelse(N == 0, 1, N))
m <- glmmTMB(N ~ Group + (1|ran), data = df, family = "truncated_poisson")

df %>% group_by(Group) %>% summarize(mean(N))
predict(m, newdata = data.frame(Group = c("a", "b"), ran = NA), type = "response")

I think the main issue is probably that you're using a slightly older version of glmmTMB (< 1.1.5, where a bug was fixed, see egeg https://github.com/glmmTMB/glmmTMB/issues/860 ).我认为主要问题可能是您使用的是稍旧版本的glmmTMB (< 1.1.5,修复了错误,请参见egeg https://github.com/glmmTMB/glmmTMB/issues/860 )。

sample data样本数据

streamlined slightly (we don't need to include a random effect for this example), and adding a truncated nbinom2.略微简化(我们不需要为此示例包含随机效果),并添加截断的 nbinom2。

library(dplyr)
library(extraDistr)
library(glmmTMB)

set.seed(1)
df <- data.frame(Group = rep(c("a", "b"), each = 20),
                 Np = rtpois(40, 1, a = 0))

## clunky trunc nbinom generator
tnb <- rep(0, 40)
z <- (tnb==0)
while(any(z)) {
    tnb[z] <- rnbinom(sum(z), mu = 1, size = 1)
    z <- (tnb==0)
}
df$Nnb <- tnb
## summarize
df %>% group_by(Group) %>% summarize(across(starts_with("N"), mean))
##   Group    Np   Nnb
## 1 a      1.75  1.8 
## 2 b      1.45  2.35

fit models适合模特

m1 <- glmmTMB(Np ~ Group, data = df, family = "truncated_poisson")
m2 <- update(m1, Nnb ~ ., family = truncated_nbinom2)

Predicting with se.fit = TRUE will give you standard errors for the predictions, from which you can compute confidence intervals (assuming Normality/Wald intervals/blah blah blah...)...使用se.fit = TRUE进行预测将为您提供预测的标准误差,您可以从中计算置信区间(假设正态性/Wald 间隔/等等......)...

pfun <- function(m, level = 0.95) {
    pp <- predict(m, newdata = data.frame(Group = c("a", "b")),
            type = "response",
            se.fit = TRUE)
    list(est = unname(pp$fit), 
         lwr = unname(pp$fit + qnorm((1-level)/2)*pp$se.fit),
         upr = unname(pp$fit + qnorm((1+level)/2)*pp$se.fit))

}
pfun(m1)
pfun(m2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM