繁体   English   中英

如何在 R 工作室中以概率 plot 添加一个显示条件变量分布的图层?

[英]How can I add a layer showing the distribution on a conditional variable in a probability plot in R studio?

我正在拟合以下回归: model <- glm(DV ~ conditions + predictor + conditions*predictor, family = binomial(link = "probit"), data = d)

我使用'sjPlot'(和'ggplot2')制作以下plot:

library("ggplot2")
library("sjPlot")
plot_model(model, type = "pred", terms = c("predictor", "conditions")) +
  xlab("Xlab") +
  ylab("Ylab") +
  theme_minimal() +
  ggtitle("Title")>

预测图

但是我不知道如何添加一个显示条件变量分布的图层,就像我可以通过使用“interplot”设置“hist = TRUE”轻松做到的那样:

library("interplot")
interplot(model, var1 = "conditions", var2 = "predictor", hist = TRUE) +
      xlab("Xlab") +
      ylab("Ylab") +
      theme_minimal() +
      ggtitle("Title") 

在预测变量上绘制分布图

我也只使用 ggplot 尝试了一堆图层,但没有成功

ggplot(d, aes(x=predictor, y=DV, color=conditions))+
  geom_smooth(method = "glm") +
  xlab("Xlab") +
  ylab("Ylab") +
  theme_minimal() +
  ggtitle("Title")

ggplot .

我愿意接受任何建议!

我显然不得不尝试重新创建您的数据以使其正常工作,因此它不会忠实于您的原始数据,但如果我们假设您的 plot 是这样的:

p <- plot_model(model, type = "pred", terms = c("predictor [all]", "conditions")) +
  xlab("Xlab") +
  ylab("Ylab") +
  theme_minimal() +
  ggtitle("Title")

p

在此处输入图像描述

然后我们可以像这样添加预测变量的直方图:

p + geom_histogram(data = d, inherit.aes = FALSE, 
                   aes(x = predictor, y = ..count../1000),
                   fill = "gray85", colour = "gray50", alpha = 0.3)

在此处输入图像描述

如果你想在 ggplot 中做所有事情,你需要记住告诉geom_smooth你的 glm 是一个概率 model,否则它只会适合正常的线性回归。 对于这个示例,我也复制了调色板,但请注意组的平滑线从它们的最低 x 值开始,而不是外推回 0。

ggplot(d, aes(x = predictor, y = DV, color = conditions))+
  geom_smooth(method = "glm", aes(fill = conditions),
              method.args = list(family = binomial(link = "probit")),
              alpha = 0.15, size = 0.5) +
  xlab("Xlab") +
  scale_fill_manual(values = c("#e41a1c", "#377eb8")) +
  scale_colour_manual(values = c("#e41a1c", "#377eb8")) +
  ylab("Ylab") +
  theme_minimal() +
  ggtitle("Title") + 
  geom_histogram(aes(y = ..count../1000),
                 fill = "gray85", colour = "gray50", alpha = 0.3)

在此处输入图像描述


数据

set.seed(69)

n_each     <- 500
predictor  <- rgamma(2 * n_each, 2.5, 3)
predictor  <- 1 - predictor/max(predictor)
log_odds   <- c((1 - predictor[1:n_each]) * 5 - 3.605, 
              predictor[n_each + 1:n_each] * 0 + 0.57)
DV         <- rbinom(2 * n_each, 1, exp(log_odds)/(1 + exp(log_odds)))
conditions <- factor(rep(c("  ", " "), each = n_each))
d          <- data.frame(DV, predictor, conditions)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM