如何在R中的ggplot2中绘制混合效果模型估计? [英] How to plot mixed-effects model estimates in ggplot2 in R?

查看:528
本文介绍了如何在R中的ggplot2中绘制混合效果模型估计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2x2x2阶乘设计,具有一种随机效果.数据(日期)如下:

I have a 2x2x2 factorial design with one random effect. The data (dat) is as follows:

  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)

我通常使用以下代码进行绘图:

I usually use the following code to do my plots:

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中绘制最终模型的估计值的任何想法?如果有人也可以建议一种简单的方法来打印模型估计值,那将是有帮助的

I'm unsure on how to replace the terms with coefficients from the model estimates. Any ideas on how can I plot the estimates of the final model in gpplot2? It would be helpful if anyone can suggest a easy way to print the model estimates too

此外,无论如何,我是否可以让ggplot2在图形上方显示条形图,以显示重要的交互作用?

In addition, is there anyway that I can get ggplot2 to display bars on top of the graphs showing interactions that are significant?

推荐答案

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

Here's one approach to plotting predictions from a linear mixed effects model for a factorial design. You can access the fixed effects coefficient estimates with fixef(...) or coef(summary(...)). You can access the random effects estimates with 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")

这些是对您在帖子中显示的数据的固定效果预测.颜色的点是重叠的,但这将取决于模型中包含的数据.您选择通过轴,构面或形状表示的因素的哪种组合可能会改变图形的视觉重点.

These are fixed effects predictions for the data you presented in your post. Points for the colors are overlapping, but that will depend on the data included in the model. Which combination of factors you choose to represent via the axes, facets, or shapes may shift the visual emphasis of the graph.

这篇关于如何在R中的ggplot2中绘制混合效果模型估计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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