Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graph GLM in ggplot2 where x variable is categorical

Tags:

graph

r

ggplot2

glm

I need to graph the predicted probabilities of a logit regression in ggplot2. Essentially, I am trying to graph a glm by each treatment condition within the same graph. However, I am getting quite confused about how to do this seeing that my treat variable (i.e. the x I am interested in) is categorical.This means that when I try to graph the treatment effects using ggplot I just get a bunch of points at 0, 1, and 2 but no lines.

My question is... How could I graph the logit prediction lines in this case? Thanks in advance!

set.seed(96)
df <- data.frame(
  vote = sample(0:1, 200, replace = T),
  treat = sample(0:3, 200, replace = T))

glm_output <- glm(vote ~ as.factor(treat), data = df, family = binomial(link = "logit"))

predicted_vote <- predict(glm_output, newdata = df, type = "link", interval = "confidence", se = TRUE)
df <- cbind(df, data.frame(predicted_vote))
like image 989
rowbust Avatar asked Oct 31 '25 11:10

rowbust


1 Answers

Since the explanatory variable treat is categorical, it will make more sense if you use boxplot instead like the following:

ggplot(df, aes(x = treat, y = predicted_prob)) +
  geom_boxplot(aes(fill = factor(treat)), alpha = .2)

enter image description here

If you want to see the predicted probabilities by glm across different values of some of other explanatory variables you may try this:

ggplot(df, aes(x = treat, y = predicted_prob)) +
  geom_boxplot(aes(fill = factor(treat)), alpha = .2) + facet_wrap(~gender)

enter image description here

# create age groups 
df$age_group <- cut(df$age, breaks=seq(0,100,20))

ggplot(df, aes(x = treat, y = predicted_prob)) +
  geom_boxplot(aes(fill = factor(treat)), alpha = .2) + facet_grid(age_group~gender)

enter image description here

like image 191
Sandipan Dey Avatar answered Nov 03 '25 02:11

Sandipan Dey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!