如何获得lmer对象的置信区间? [英] How to get confidence intervals for lmer object?

查看:115
本文介绍了如何获得lmer对象的置信区间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为混合模型的预测获取置信区间.预测函数不输出任何置信区间.很少有人建议使用merTools软件包中的predictInterval函数来获取间隔的StackOverflow答案,但是这两个函数的预测估计之间存在差异,我尝试在下图中进行比较.有人可以让我知道我在这里做错了吗?另外,我尝试构建的实际模型与下面的代码段中显示的模型相似,在该代码段中,除截距外,我没有固定的效果分量.

I am trying to get confidence intervals for predictions on the mixed model. The predict function does not output any confidence intervals. Few StackOverflow answers suggested using predictInterval function from the merTools package to obtain the intervals but there is a discrepancy between the prediction estimates from these two function which I am trying to compare in the plot below. Can someone kindly let me know what I am doing wrong here? Also, the actual model I am trying to build is similar to the one showed in the snippet below where I only have no fixed effect components except for the intercept.

library(merTools)
library(lme4)

dat <- iris
mod <- lmer(Sepal.Length ~ 1 + (1 + Sepal.Width + Petal.Length + 
                                  Petal.Width|Species), data=dat)

c1 <- predict(mod, dat)
c2 <- predictInterval(mod, dat)

plot_data <- cbind(c1, c2)
plot_data$order <- c(1:nrow(plot_data))

library(ggplot2)
ggplot(plot_data) + geom_line(aes(x=order, y=c1), color='red') + 
  geom_ribbon(aes(x=order, ymin=lwr, ymax=upr), color='blue', alpha=0.2) +  
  geom_line(aes(x=order, y=fit), color='blue')

红线表示预测'c1'蓝色线表示预测为"c2"

The red line indicates the predictions 'c1' and the blue line indicates the predictions 'c2'

推荐答案

我还不能完全隔离导致问题的 predictInterval 部分,但是针对您的特定问题的解决方案是请注意,如果您想要的是不同组的截距和斜率,则可以拟合以下等效模型

I haven't quite been able to isolate the part of predictInterval that's causing the problem, but a solution to your specific problem is to note that if what you want is group-varying intercepts and slopes, then you can fit the following equivalent model

mod2 <- lmer(Sepal.Length ~ 1 + Sepal.Width + Petal.Length + Petal.Width + 
                           (1 + Sepal.Width + Petal.Length + Petal.Width|Species), 
             data = dat)

现在如果我们将 predictInterval 应用到这个拟合模型

Now if we apply predictInterval to this fitted model

c2 <- predictInterval(mod2, dat)

在其余示例中,我们得到以下图表:

keeping the rest of your example, we get the following plot:

这就是我们想要的.(要强调的是,红线表示来自原始模型规格的预测,即仅固定"组件中的截距.)

which is what we want. (To emphasize, the red line represents predictions from your original model specification i.e. with only the intercept in the "fixed" component.)

这篇关于如何获得lmer对象的置信区间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆