如何在 R studio 的概率图中添加一个显示条件变量分布的图层?

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')来绘制以下图:

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")

.

我愿意接受任何建议!

我显然必须尝试重新创建您的数据才能使它正常工作,因此它不会忠实于您的原始数据,但如果我们假设您的情节是这样的:

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 是一个 probit 模型,否则它只会拟合一个正常的线性回归。对于这个例子,我也复制了调色板,但请注意,组的平滑线从它们的最低 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)