dwplot()for lsmeans in R

8iwquhpp  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(127)

我想绘制多个回归的输出。
MWE:

df <- data.frame(
  y1 = runif(100),
  y2 = runif(100),
  x = c(rep("A",times=50), rep("B",times=50))
)

如果我想绘制截距和系数,我可以使用dwplot():

m0 <- lm(y1 ~ factor(x), data = df)
m1 <- lm(y2 ~ factor(x), data = df)
dwplot(list(m0, m1), show_intercept=T) + theme_bw()

然而,我对ls均值感兴趣(用library(lsmeans)包计算)。对于这种情况,dwplot()给了我一个错误。

lsm0 <- lsmeans(m0,~ factor(x), data = df)
lsm1 <- lsmeans(m1,~ factor(x), data = df)
dwplot(list(lsm0, lsm1), show_intercept=T) + theme_bw()

有哪些可能的解决方案?

e4eetjau

e4eetjau1#

你可以使用这两个对象的结果,并创建一个dataframe来用geom_pointrange制作你自己的ggplot,如下所示:

library(dotwhisker)
library(lsmeans)

m0 <- lm(y1 ~ factor(x), data = df)
m1 <- lm(y2 ~ factor(x), data = df)

lsm0 <- lsmeans(m0, ~ factor(x), data = df)
lsm1 <- lsmeans(m1, ~ factor(x), data = df)

library(tidyverse)
as.data.frame(lsm0) %>%
  mutate(model = "model 1") %>%
  bind_rows(., as.data.frame(lsm1) %>%
              mutate(model = "model 2")) %>%
  ggplot(aes(x = lsmean, y = x, color = model, xmin = lower.CL, xmax = upper.CL)) +
  geom_pointrange(position = position_dodge(width = 0.2)) +
  theme_bw()

创建于2023-04-07带有reprex v2.0.2

相关问题