Как я могу добавить слой, показывающий распределение условной переменной на графике вероятности в R studio? - PullRequest
2 голосов
/ 10 июля 2020

Я использую следующую регрессию: 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")>

пред сюжет

Но я не могу понять, как добавить слой, показывающий распределение по переменной кондиционирования, как я могу легко сделать, установив "hist = TRUE" с помощью 'interplot':

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 .

Я открыт для любых предложений!

1 Ответ

2 голосов
/ 10 июля 2020

Мне, очевидно, пришлось попытаться воссоздать ваши данные, чтобы это работало, поэтому они не будут соответствовать вашему оригиналу, но если мы предположим, что ваш график выглядит примерно так:

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

p

enter image description here

Then we can add a histogram of the predictor variable like this:

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

enter image description here

And if you wanted to do the whole thing in ggplot, you need to remember to tell geom_smooth that your glm is a probit model, otherwise it will just fit a normal linear regression. I've copied the color palette over too for this example, though note the smoothing lines for the groups start at their lowest x value rather than extrapolating back to 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...