如何在 R 中的 ggplot2 中绘制混合效应模型估计值?

How to plot mixed-effects model estimates in ggplot2 in R?

我有一个具有一个随机效应的 2x2x2 析因设计。数据(dat)如下:

  colour  size  level   marbles set
  Blue    Large Low     80      1
  Blue    Large High    9       2
  Blue    Small Low     91      1
  Blue    Small High    2       1 
  White   Large Low     80      2
  White   Large High    9       1
  White   Small Low     91      2
  White   Small High    2       1

我想绘制两个模型:

mod1 <- lmer(marbles ~ colour + size + level + colour:size + colour:level + size:level + (1|set), data = dat)

mod2 <- lmer(marbles ~ colour + size + level +(1|set), data = dat)

我通常使用以下代码来做我的情节:

pd <- position_dodge(0.82)
  ggplot(dat, aes(x=colour, y=marbles, fill = level)) + theme_bw() + 
  stat_summary(geom="bar", fun.y=mean, position = "dodge") +  
  stat_summary(geom="errorbar", fun.data=mean_cl_boot, position = pd)+
  + facet_grid(~size)

我不确定如何用模型估计的系数替换项。关于如何在 gpplot2 中绘制最终模型的估计值的任何想法?如果有人能提出一种简单的方法来打印模型估计值,那将会很有帮助

此外,我是否可以让 ggplot2 在显示重要交互的图表顶部显示条形图?

这是一种绘制因子设计的线性混合效应模型预测的方法。您可以使用 fixef(...)coef(summary(...)) 访问固定效应系数估计值。您可以使用 ranef(...).

访问随机效应估计值
library(lme4)
mod1 <- lmer(marbles ~ colour + size + level + colour:size + colour:level + size:level + (1|set), data = dat)
mod2 <- lmer(marbles ~ colour + size + level +(1|set), data = dat)

dat$preds1 <- predict(mod1,type="response")
dat$preds2 <- predict(mod2,type="response")

dat<-melt(dat,1:5)

pred.plot <- ggplot() +
  geom_point(data = dat, aes(x = size, y = value, 
                            group = interaction(factor(level),factor(colour)),
                            color=factor(colour),shape=variable)) +
  facet_wrap(~level) +
  labs(x="Size",y="Marbles")

这些是对您在 post 中提供的数据的固定效应预测。颜色点重叠,但这取决于模型中包含的数据。您选择通过轴、面或形状表示的因素组合可能会改变图表的视觉重点。