【发布时间】:2015-03-17 07:49:08
【问题描述】:
我使用 lme4 (type="response") 运行了混合效应逻辑回归。现在我使用了预测功能,还想确定置信区间。
我发现此代码 (http://glmm.wikidot.com/faq) 用于预测,它有效,但 CI 不适合二进制响应(我的预测在 0 和 1 之间,突然间 CI 在 -3 和 3 之间。有谁知道在哪里调整?
library(lme4)
library(ggplot2) # Plotting
fm1<- glmer(choice~rating + indi + rating*indi + (1|ID),data=z,family="binomial")
newdat<-data.frame(indi=factor(c(1,1,1,1,1,1,2,2,2,2,2,2)),
rating=factor(1:6), ID=factor(rep(c(1:30), each=12)), choice=0)
newdat$prob<-predict(fm1,newdata=newdat, re.form=NULL, type="response")
mm <- model.matrix(terms(fm1),newdat)
newdat$choice<- predict(fm1,newdat)
## or newdat$choice<- mm %*% fixef(fm1)
pvar1 <- diag(mm %*% tcrossprod(vcov(fm1),mm))
tvar1 <- pvar1+VarCorr(fm1)$ID[1] ## must be adapted
tvar1 <-
newdat <- data.frame(
newdat
, plo = newdat$choice-2*sqrt(pvar1)
, phi = newdat$choice+2*sqrt(pvar1)
, tlo = newdat$choice-2*sqrt(tvar1)
, thi = newdat$choice+2*sqrt(tvar1)
)
#plot confidence
g0 <- ggplot(newdat, aes(x=rating, y=choice, colour=indi))+geom_point()
g0 + geom_errorbar(aes(ymin = plo, ymax = phi))+
opts(title="CI based on fixed-effects uncertainty ONLY")
#plot prediction
g0 + geom_errorbar(aes(ymin = tlo, ymax = thi))+
opts(title="CI based on FE uncertainty + RE variance")
非常感谢!
【问题讨论】:
-
我在您的代码中看不到任何逻辑回归。
-
对不起,我会根据我的范式改代码
标签: r confidence-interval lme4