使用扫帚从glmmTMB零膨胀模型中获取系数时出现错误消息

时间:2018-03-05 17:36:34

标签: r coefficients broom

使用Owls数据和glmmTMB包,我想直观地比较来自不同零膨胀模型的回归系数,这些模型在所用系列(ZIPOISS,ZINB1,ZINB2)和带偏移(out)之外不同( logBroodSize)。

然而,我的第一个问题是得到系数。包tidy中的broom函数应该为您提供系数,以便稍后使用ggplot绘制它们,但是当我尝试获取它们时出现以下错误:

modList= list(zipoiss, zinb1, zinb2, zinb1_bs, zinb2_bs)  
coefs= ldply(modList,tidy,effect="fixed",conf.int=TRUE,
                  .id="model") %>%
    mutate(term=abbfun(term)) %>%
    select(model,term,estimate,conf.low,conf.high) %>%
    filter(!term %in% c("Intercept","Intercept.1","NCalls","zi_NCalls"))

Error in as.data.frame.default(x) :
cannot coerce class ""glmmTMB"" to a data.frame
Also: Warning message:
In tidy.default(X[[i]], ...) :
No method for tidying an S3 object of class glmmTMB , using as.data.frame

任何可能出错的想法?我已经被告知没有正确版本的broom可能是原因,但是我已经安装了正确的版本...接下来提供了可重现示例的代码:

    # Packages and dataset
    library(glmmTMB)
    library(broom) # devtools::install_github("bbolker/broom")
    library(plyr)
    library(dplyr)
    data(Owls,package="glmmTMB")
    Owls = plyr::rename(Owls, c(SiblingNegotiation="NCalls"))
    Owls = transform(Owls,
             ArrivalTime=scale(ArrivalTime, center=TRUE, scale=FALSE),
             obs=factor(seq(nrow(Owls))))

    # Models
    zipoiss<-glmmTMB(NCalls~(FoodTreatment+ArrivalTime)*SexParent+
    offset(logBroodSize)+(1|Nest),
    data=Owls,
    ziformula = ~ 1,
    family="poisson")

    zinb2<- glmmTMB(NCalls~(FoodTreatment+ArrivalTime)*SexParent+
    offset(logBroodSize)+(1|Nest),
    data=Owls,
    ziformula = ~ 1,
    family="nbinom2")

    zinb1 <- glmmTMB(NCalls~(FoodTreatment+ArrivalTime)*SexParent+
    offset(logBroodSize)+(1|Nest),
    data=Owls,
    ziformula = ~ 1,
    family="nbinom1")

    zinb1_bs<- glmmTMB(NCalls~(FoodTreatment+ArrivalTime)*SexParent+ 
    BroodSize+(1|Nest),
    data=Owls,
    ziformula = ~ 1,
    family="nbinom1")

    zinb2_bs<- glmmTMB(NCalls~(FoodTreatment+ArrivalTime)*SexParent+
    BroodSize+(1|Nest),
    data=Owls,
    ziformula = ~ 1,family="nbinom2")

    # Get coefficients ("coefs" does not work yet...)
    modList = list(zipoiss, zinb1, zinb2, zinb1_bs, zinb2_bs)
    coefs = ldply(modList,tidy,effect="fixed",conf.int=TRUE,
          .id="model") %>%
    mutate(term=abbfun(term)) %>%
    select(model,term,estimate,conf.low,conf.high) %>%
    filter(!term %in% c("Intercept","Intercept.1","NCalls","zi_NCalls")) 

1 个答案:

答案 0 :(得分:0)

现在可以使用新的/开发不足的broom.mixed软件包(截至今天),例如

devtools::install_github("bbolker/broom.mixed")

希望很快就会出现在CRAN上,但它只是中等优先级 对我而言,我不想将它发布到CRAN,直到它至少有90%被烘焙。拉请求欢迎!

最后一步改变了一点(首先,我似乎没有abbfun):

modList = lme4:::namedList(zipoiss, zinb1, zinb2, zinb1_bs, zinb2_bs)
coefs = ldply(modList,tidy,effect="fixed",conf.int=TRUE,
              .id="model") %>%
  select(model,term,component,estimate,conf.low,conf.high) %>%
  filter(!term %in% c("(Intercept)","NCalls"))

警告glmmTMB模型的这些工具的开发是相当新的/实验性的;您应该(1)仔细检查您的结果,并且(2)report an issue如果某些事情没有按预期工作。