glmer-使用二项式数据预测(结合计数数据) [英] glmer - predict with binomial data (cbind count data)
问题描述
我正在尝试针对基于二项式数据运行的glmer模型随时间(x轴上的天数)预测值. Total Alive和Total Dead是计数数据.这是我的模型,以及下面的相应步骤.
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")
您已经在代码(1:index)中看到了过度分散的原因.
We have accounted for overdispersion as you can see in the code (1:index).
然后我们使用dredge命令确定具有主要效果(CO2.处理,Lime.处理,日)及其对应交互的最佳拟合模型.
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)
但是,现在我想创建一个图形,Y轴上的总活动量",X轴上的天数",以及一条取决于模型输出的拟合线.我知道这很棘手,因为该模型将Total.Alive和Total.Dead串联在一起(请参见模型中的cbind(Total.Alive,Total.Dead)
.
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"
推荐答案
大多数问题是您使用的是lme4
的1.0之前版本,而该版本未实现predict
方法. (更新将是最简单的,但是我认为,如果由于某些原因您不能这样做,请在 http://glmm.wikidot.com/faq 可以通过提取固定效果设计矩阵和系数来手动进行预测...)预测实际上没有问题,可以预测对数奇数(默认情况下) )或概率(如果type="response"
);如果要预测数字,则必须适当地乘以N.
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.
您没有给出任何示例,但这是一个使用内置cbpp
数据集的可重现(虽然有些琐碎)的示例(我确实收到了一些警告消息-no non-missing arguments to max; returning -Inf
-但我认为这可能是是因为模型中只有一个非平凡的固定效应参数?)
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)
以后使用(与ggplot
一起)添加比例变量很方便:
It's convenient for later use (with ggplot
) to add a variable for the proportion:
cbpp <- transform(cbpp,prop=incidence/size)
适合模型(您也可以使用glmer(prop~..., weights=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)
预测有效:
predict(silly,type="response")
创建情节:
library(ggplot2)
theme_set(theme_bw()) ## cosmetic
g0 <- ggplot(cbpp,aes(period,prop))+
geom_point(alpha=0.5,aes(size=size))
设置预测框架:
predframe <- data.frame(period=levels(cbpp$period))
在总体水平上预测 (ReForm=NA
-在lme4`1.0.5中可能必须为REForm=NA
):
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)
将其添加到图形中
g0 + geom_point(data=predframe,colour="red")+
geom_line(data=predframe,colour="red",aes(group=1))
这篇关于glmer-使用二项式数据预测(结合计数数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!