如何在 ggplot 中绘制具有 95% CI 误差条的二项式广义混合效应模型?

问题描述 投票:0回答:1

我刚刚完成模型选择,并且有一个二项式广义混合效应模型,我想在 ggplot 中绘制它。对于上下文,我进行了模型选择以了解哪些因素可以预测出勤率。我查看的因素是日(从 -30 到 -2,相对于第 0 天)、性别(男或女)和性别:日互动。我还包括了几个随机效应,包括 AgeClass(年轻、中年或老年)、Year(日历年,2014-2018)和 Plastic(个人 ID,用于纠正重复样本)。

这里是我的相关资料和代码:

数据(csv文件):https://ufile.io/o0ns03ho

代码:

library(lme4)

data <- read.csv("test2.csv")

m1 <- glmer(
  Attendance ~ Day + Sex + Sex:Day + (1|AgeClass) + (1|Year) + (1|Plastic), 
  data = data, 
  family = binomial(link = "logit")
)

summary(m1)

根据贝塔的估计值,我希望图表看起来像这样:

我希望男性比女性更频繁地参加,并且男性的斜率与女性不同。我还希望能够绘制男性和女性的 95% CI,如图中每种性别实线上方和下方的虚线所示。

我只有 ggplot 的基础知识,所以任何和所有的建议都是有帮助的。我不需要精确的代码,但是在与模型和 ggplot 相关的功能方面向正确的方向推动将不胜感激。

谢谢!

r ggplot2 glm mixed-models
1个回答
3
投票

sjPlot::plot_model()
是一个开箱即用的函数,用于绘制模型结果:

library(lme4)
library(sjPlot)

# example model
m1 <- glmer(
  r2 ~ Anger * Gender + btype + situ + (1|id) + (1|item), 
  family = binomial,
  data = VerbAgg
)

plot_model(m1, type = "pred", terms = c("Anger", "Gender"))

结果是一个 ggplot,因此您可以使用 ggplot2 函数对其进行调整。例如,

library(ggplot2)
library(scales)

plot_model(m1, type = "pred", terms = c("Anger", "Gender")) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(labels = percent, limits = c(0, 1)) +
  labs(x = "Trait Anger", y = "Predicted Probability of Response", title = NULL) +
  theme_light()

© www.soinside.com 2019 - 2024. All rights reserved.