glmer - predict with binomial data (cbind count data)

感情迁移 提交于 2019-12-03 15:03:23

Most of your problem is that you're using a pre-1.0 version of lme4, which doesn't have the predict method implemented. (Updating would be easiest, but I believe that if you can't for some reason, there's a recipe at http://glmm.wikidot.com/faq for doing the predictions by hand by extracting the fixed-effect design matrix and the coefficients ...)There's actually not a problem with the predictions, which predict the log-odds (by default) or the probability (if type="response"); if you wanted to predict numbers, you'd have to multiply by N appropriately.

You didn't give one, but here's a reproducible (albeit somewhat trivial) example using the built-in cbpp data set (I do get some warning messages -- no non-missing arguments to max; returning -Inf -- but I think this may be due to the fact that there's only one non-trivial fixed-effect parameter in the model?)

library(lme4)
packageVersion("lme4")  ## 1.1.4, but this should work as long as >1.0.0
library(MuMIn)

It's convenient for later use (with ggplot) to add a variable for the proportion:

cbpp <- transform(cbpp,prop=incidence/size)

Fit the model (you could also use glmer(prop~..., weights=size, ...))

gm0 <- glmer(cbind(incidence, size - incidence) ~ period+(1|herd),
           family = binomial, data = cbpp)
dredge.models<-dredge(gm0,trace=FALSE,rank="AICc")
my.dredge.models<-get.models(dredge.models)
silly<-model.avg(my.dredge.models,subset=delta<10)

Prediction does work:

predict(silly,type="response")

Creating a plot:

library(ggplot2)
theme_set(theme_bw())  ## cosmetic
g0 <- ggplot(cbpp,aes(period,prop))+
    geom_point(alpha=0.5,aes(size=size))

Set up a prediction frame:

predframe <- data.frame(period=levels(cbpp$period))

Predict at the population level (ReForm=NA -- this may have to be REForm=NA in lme4 `1.0.5):

predframe$prop <- predict(gm0,newdata=predframe,type="response",ReForm=NA)

Add it to the graph:

g0 + geom_point(data=predframe,colour="red")+
    geom_line(data=predframe,colour="red",aes(group=1))

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!