glmer - predict with binomial data (cbind count da

2019-03-29 23:38发布

I am trying to predict values over time (Days in x axis) for a glmer model that was run on my binomial data. Total Alive and Total Dead are count data. This is my model, and the corresponding steps below.

full.model.dredge<-glmer(cbind(Total.Alive,Total.Dead)~(CO2.Treatment+Lime.Treatment+Day)^3+(Day|Container)+(1|index),
                         data=Survival.data,family="binomial")

We have accounted for overdispersion as you can see in the code (1:index).

We then use the dredge command to determine the best fitted models with the main effects (CO2.Treatment, Lime.Treatment, Day) and their corresponding interactions.

dredge.models<-dredge(full.model.dredge,trace=FALSE,rank="AICc")

Then made a workspace variable for them

my.dredge.models<-get.models(dredge.models)

We then conducted a model average to average the coefficients for the best fit models

silly<-model.avg(my.dredge.models,subset=delta<10)

But now I want to create a graph, with the Total Alive on the Y axis, and Days on the X axis, and a fitted line depending on the output of the model. I understand this is tricky because the model concatenated the Total.Alive and Total.Dead (see cbind(Total.Alive,Total.Dead) in the model.

When I try to run a predict command I get the error

# 9: In UseMethod("predict") :
#   no applicable method for 'predict' applied to an object of class "mer"

标签: r glm predict lme4
1条回答
放荡不羁爱自由
2楼-- · 2019-03-30 00: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))

enter image description here

查看更多
登录 后发表回答