【问题标题】:predict lme4 for type "response" (binary choices) -- Confidence intervals预测“响应”类型的 lme4(二元选择)——置信区间
【发布时间】: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


【解决方案1】:

在这种情况下,您需要使用反向链接函数plogis()

 newdat <- transform(newdat,
        plo = plogis(plo),
        phi = plogis(phi),
        tlo = plogis(tlo),
        thi = plogis(thi))

更一般地说,如果您安装了模型gm1,则反向链接函数将存储在gm1@resp$family$linkinv 中(尽管不能保证像这样处理对象的内部结构并不能保证与未来版本保持兼容)。

【讨论】:

    猜你喜欢
    • 2013-04-21
    • 2015-11-18
    • 1970-01-01
    • 2021-01-22
    • 2021-01-22
    • 2013-07-07
    • 1970-01-01
    • 2018-07-29
    • 2018-12-21
    相关资源
    最近更新 更多