繁体   English   中英

使用 ggplot 绘制二项式 GLM

[英]Plotting a Binomial GLM using ggplot

我已经创建了以下二项式 GLM model 并希望使用 ggplot 将其转换为 plot。 我遇到的问题是我如何将 plot 多个变量作为 x 和 y 轴。

notUsing, using 是使用 age + education + wantsMore 来预测的。 由于有多个变量,我如何将这些作为 arguments 添加到 ggplot 的 aes() 中?

这在下面应该更清楚。

model 创建;

my_model = glm(cbind(notUsing, using) ~ age + education + wantsMore,
           data = contraceptive2,
           family = binomial(link = "logit"))

绘制 model; (希望这将使我更容易看到我失败的地方。我需要为我的数据重播 x 和 y,但由于它们包含多个变量,我不知道如何执行此操作)

#make predictions
my_model_preds = predict(my_model, contraceptive2,  se.fit = TRUE, type = 'response')

#ggplot of the model
ggplot(contraceptive2, aes(x, y)) +
  geom_point() +
  geom_line(aes(x, my_model_preds), col = 'blue')

部分数据(如果需要);

head(contraceptive2)
    age education wantsMore notUsing using
1   <25       low       yes       53     6
2   <25       low        no       10     4
3   <25      high       yes      200    52
4   <25      high        no       50    10
5 25-29       low       yes       60    14
6 25-29       low        no       19    10
7  25-29      high       yes      155    54
8  25-29      high        no       65    27
9  30-39       low       yes      112    33
10 30-39       low        no       77    80
11 30-39      high       yes      118    46
12 30-39      high        no       68    78
13 40-49       low       yes       35     6
14 40-49       low        no       46    48
15 40-49      high       yes        8     8
16 40-49      high        no       12    31

通过提供的数据,我们可以看到,因为您的两个自变量是二进制的,所以可以使用带刻面的误差条和 colors 来计算 plot 整个 model:

df <- with(contraceptive2,
     expand.grid(age = unique(age), education = unique(education),
                 wantsMore = unique(wantsMore)))

fits <- predict(my_model, newdata = df, se.fit = TRUE)

# Get odds from modrl
df$prediction <- exp(fits$fit) 
df$upper <- exp(fits$fit + 1.96 * fits$se.fit)
df$lower <- exp(fits$fit - 1.96 * fits$se.fit)

# Convert odds to probabilities
df$prediction <- df$prediction / (1 + df$prediction)
df$upper <- df$upper / (1 + df$upper)
df$lower <- df$lower / (1 + df$lower)

# Plot probabilities
ggplot(df, aes(age, prediction)) +
  geom_errorbar(aes(ymin = lower, ymax = upper, colour = wantsMore), 
                width = 0.25, size = 1, position = position_dodge(width = 0.4)) +
  geom_point(aes(fill = wantsMore), shape = 21, size = 3, 
             position = position_dodge(width = 0.4)) +
  facet_grid(~education) +
  theme_light(base_size = 16) +
  scale_y_continuous(name = "Probability of not using", limits = c(0, 1),
                     labels = scales::percent)

在此处输入图像描述

暂无
暂无

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

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