Emmeans套餐的GLMM估计平均值

发布于 2025-02-04 20:19:35 字数 1740 浏览 4 评论 0原文

我正在尝试使用负二项式随机效应模型及其各自的置信区间来绘制估计的均值,其中数据和拟合的定义如下:

d.AD <- data.frame(treatment = gl(12,12),
                   treatment2 = gl(4,1,36),
                   counts = rpois(144, 4.03),
                   treatment3 = gl(3,4,36),
                   ID = gl(12,12))
d.AD$treatment2 = as.integer(d.AD$treatment2)
d.AD$treatment3 = as.factor(d.AD$treatment3)
d.AD$ID = as.factor(d.AD$ID)

library(ggplot2)
library(lme4)
library(emmeans)

mode1A <- glmer.nb(counts~treatment3+poly(treatment2, 2) + (1|ID), 
                   data=d.AD, verbose=F)

使使用置信区间的平均值的函数是emmeans emmeans软件包。但是,请参见图形的形状倒置,也就是说,我希望x轴为我的y轴,反之亦然。

emm1 = emmeans(mode1A,~"treatment3", type="response")
emm1df = as.data.frame(emm1)
plot(emm1) +
  geom_point(size=2.2)+
  labs(x = 'Response', y = 'Treatment') +  
  scale_shape_manual(NULL, values = 18) +
  theme(legend.title = element_text(size = 25,color = "black"),
        legend.text = element_text(size = 25,color = "black"),
        axis.title = element_text(size = 25,color = "black"),
        axis.text.x = element_text(color = "black", hjust=1),
        axis.text.y = element_text(color = "black", hjust=1),
        axis.text = element_text(size = 25,color = "black"),
        plot.title = element_blank(),
        strip.text.x = element_text(size = 15,color = "black"))

我想要这个示例附近的东西。

I am trying to plot the estimated means using a negative binomial random effects model with their respective confidence intervals, in which the data and the fit were defined as follows:

d.AD <- data.frame(treatment = gl(12,12),
                   treatment2 = gl(4,1,36),
                   counts = rpois(144, 4.03),
                   treatment3 = gl(3,4,36),
                   ID = gl(12,12))
d.AD$treatment2 = as.integer(d.AD$treatment2)
d.AD$treatment3 = as.factor(d.AD$treatment3)
d.AD$ID = as.factor(d.AD$ID)

library(ggplot2)
library(lme4)
library(emmeans)

mode1A <- glmer.nb(counts~treatment3+poly(treatment2, 2) + (1|ID), 
                   data=d.AD, verbose=F)

The function to make the averages with the confidence intervals used was the emmeans of the emmeans package. However, see that the shape of the graph is inverted, that is, I would like the x axis to be my y axis and vice versa.

emm1 = emmeans(mode1A,~"treatment3", type="response")
emm1df = as.data.frame(emm1)
plot(emm1) +
  geom_point(size=2.2)+
  labs(x = 'Response', y = 'Treatment') +  
  scale_shape_manual(NULL, values = 18) +
  theme(legend.title = element_text(size = 25,color = "black"),
        legend.text = element_text(size = 25,color = "black"),
        axis.title = element_text(size = 25,color = "black"),
        axis.text.x = element_text(color = "black", hjust=1),
        axis.text.y = element_text(color = "black", hjust=1),
        axis.text = element_text(size = 25,color = "black"),
        plot.title = element_blank(),
        strip.text.x = element_text(size = 15,color = "black"))

enter image description here

I would like something close to this example.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩虹直至黑白 2025-02-11 20:19:36

您可以做类似:

emm1df |>
  ggplot(aes(x     = treatment3,
             y     = response,
             label = round(response,2)
             )
         ) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = asymp.LCL,
                    ymax = asymp.UCL),
                width = 0
                ) +
  geom_label(nudge_y = (emm1df$asymp.UCL-emm1df$response)*1.2) +
  ylim(c(3,5)) +
  labs(x = "Treatment",
       y = "Response")

“示例”

You could do something like:

emm1df |>
  ggplot(aes(x     = treatment3,
             y     = response,
             label = round(response,2)
             )
         ) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = asymp.LCL,
                    ymax = asymp.UCL),
                width = 0
                ) +
  geom_label(nudge_y = (emm1df$asymp.UCL-emm1df$response)*1.2) +
  ylim(c(3,5)) +
  labs(x = "Treatment",
       y = "Response")

Example

奈何桥上唱咆哮 2025-02-11 20:19:36

尝试

plot(emm1, horizontal = FALSE)

看看`? plot.emmgrid

Try

plot(emm1, horizontal = FALSE)

See `? plot.emmGrid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文