为什么ggplot2 95%CI和手动计算的预测95%CI不同?

时间:2019-02-01 19:02:37

标签: r ggplot2 lme4 mixed-models confidence-interval

我想知道为什么当从线性混合效果模型计算95%置信带时,ggplot2会比手动计算时产生更窄的带,例如遵循confidence intervals on predictions中Ben Bolker的方法。也就是说,ggplot2是否给出了不正确的模型表示形式?

这是使用sleepstudy数据集(经过修改后的结构与我正在研究的df相似的示例):

data("sleepstudy") # load dataset 
height <- seq(165, 185, length.out = 18) # create vector called height
Treatment <- rep(c("Control", "Drug"), 9) # create vector called treatment
Subject <- levels(sleepstudy$Subject) # get vector of Subject
ht.subject <- data.frame(height, Subject, Treatment) 
sleepstudy <- dplyr::left_join(sleepstudy, ht.subject, by="Subject") # Append df so that each subject has its own height and treatment
sleepstudy$Treatment <- as.factor(sleepstudy$Treatment)

生成模型,将预测添加到原始df,然后绘制

m.sleep <- lmer(Reaction ~ Treatment*height + (1 + Days|Subject), data=sleepstudy)
sleepstudy$pred <- predict(m.sleep)
ggplot(sleepstudy, aes(height, pred, col=Treatment)) + geom_smooth(method="lm")[2] 

按照Bolker方法计算置信区间

newdf <- expand.grid(height=seq(165, 185, 1),
                   Treatment=c("Control","Drug"))
newdf$Reaction <- predict(m.sleep, newdf, re.form=NA) 
modmat <- model.matrix(terms(m.sleep), newdf)
pvar1 <- diag(modmat %*% tcrossprod(vcov(m.sleep), modmat))
tvar1 <- pvar1+VarCorr(m.sleep)$Subject[1]
cmult <- 1.96

newdf <- data.frame(newdf
,plo = newdf$Reaction-cmult*sqrt(pvar1)
,phi = newdf$Reaction+cmult*sqrt(pvar1)
,tlo = newdf$Reaction-cmult*sqrt(tvar1)
,thi = newdf$Reaction+cmult*sqrt(tvar1))

# plot confidence intervals
ggplot(newdf, aes(x=height, y=Reaction, colour=Treatment)) + 
geom_point() +
geom_ribbon(aes(ymin=plo, ymax=phi, fill=Treatment), alpha=0.4)[2]

1 个答案:

答案 0 :(得分:2)

通过一些调整,这似乎是一致的。置信区间确实更大,但是却没有很大。请记住,ggplot适合非常的不同模型;通过处理拟合单独的线性(非线性混合)模型,而忽略了(1)重复测量和(2)日间影响。

用随机斜率但没有总体水平斜率的模型进行拟合似乎很奇怪(例如,参见here),因此我添加了Days的固定效果:

m.sleep <- lmer(Reaction ~ Treatment*height + Days +
                (1 + Days|Subject),
                data=sleepstudy)

我重新整理了绘图代码:

theme_set(theme_bw())
gg0 <- ggplot(sleepstudy, aes(height, colour=Treatment)) +
    geom_point(aes(y=Reaction))+
    geom_smooth(aes(y=pred), method="lm")
  • 如果您要计算置信区间(可以与lm() / ggplot2进行比较),则可能添加VarCorr(m.sleep)$Subject[1]FAQ example中的tvar1变量用于创建预测间隔,而不是置信区间...)
  • 因为我在上面的模型中有Days,所以我在预测数据帧中添加了mean(sleepstudy$Days)
newdf <- expand.grid(height=seq(165, 185, 1),
                     Treatment=c("Control","Drug"),
                     Days=mean(sleepstudy$Days))
newdf$Reaction <- newdf$pred <- predict(m.sleep, newdf, re.form=NA) 
modmat <- model.matrix(terms(m.sleep), newdf)
pvar1 <- diag(modmat %*% tcrossprod(vcov(m.sleep), modmat))
tvar1 <- pvar1
cmult <- 1.96

newdf <- data.frame(newdf
,plo = newdf$Reaction-cmult*sqrt(pvar1)
,phi = newdf$Reaction+cmult*sqrt(pvar1)
,tlo = newdf$Reaction-cmult*sqrt(tvar1)
,thi = newdf$Reaction+cmult*sqrt(tvar1))

gg0 + 
    geom_point(data=newdf,aes(y=Reaction)) +
    geom_ribbon(data=newdf,
                aes(ymin=plo, ymax=phi, fill=Treatment), alpha=0.4,
                colour=NA)

enter image description here

与估计的斜率和标准误差比较:

m0 <- lm(Reaction~height*Treatment,sleepstudy)
ff <- function(m) {
    print(coef(summary(m))[-1,c("Estimate","Std. Error")],digits=2)
}

> ff(m0)
##                      Estimate Std. Error
## height                   -0.3       0.94
## TreatmentDrug          -602.2     234.01
## height:TreatmentDrug      3.5       1.34

ff(m.sleep)
##                      Estimate Std. Error
## TreatmentDrug          -55.03      425.3
## height                   0.41        1.7
## Days                    10.47        1.5
## TreatmentDrug:height     0.33        2.4

这看起来是一致的/大约正确的:混合模型对于坡度在高度和高度:处理相互作用方面给出了更大的标准误差。 (TreatmentDrug的主要效果看起来很疯狂,因为它们是height==0的预期治疗效果……)


作为交叉检查,我可以从sjPlot::plot_model()中获得类似的答案...

library(sjPlot)
plot_model(m.sleep, type="pred", terms=c("height","Treatment"))

enter image description here