I have created the following Binomial GLM model and wish to plot it using ggplot. The issue i am having is how I plot multiple variables as the x and y axis.
notUsing, using are being predicted using age education wantsMore here. How do i add these as arguments into the aes() of ggplot as there are multiple variables?
This should be clearer below.
model created;
my_model = glm(cbind(notUsing, using) ~ age education wantsMore,
data = contraceptive2,
family = binomial(link = "logit"))
plotting the model; (Hopefully this will make it easier to see where i am failing. I need to replay the x and y for my data, but as they contain multiple variables i dont know how to do this)
#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')
part of data if needed;
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
CodePudding user response:
With the supplied data, we can see that, because two of your independent variables are binary, it is possible to plot the whole model using error bars with facets and colors:
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)