0

I'm utilizing the following code to plot each regressor in X_train against the response variable won using a logistic model from glm()

require(reshape2)
require(ggplot2)
regressors = melt(X_train[2:16], id.vars='won')
ggplot(regressors) +
  geom_jitter(aes(value,won, colour=variable),) + 
  geom_smooth(aes(value,won, colour=variable), method="glm", method.args = list(family = "binomial")) +
  facet_wrap(~variable, scales="free_x") 

Output

I thought the geom_smooth() command would add the fitted logistic regression line to each plot, but I can't seem to get it to work. I also tried passing "data=X_train" to the command as well. Any advice is appreciated, thank you.

Scoops
  • 61
  • 4

1 Answers1

1

I think your TRUE/FALSE needs to be converted to 1/0.

Here's an example with mtcars data:

library(dplyr); library(ggplot2)

# doesn't work
regressors <- pivot_longer(mtcars, -mpg)

# still doesn't work
regressors <- pivot_longer(mtcars, -mpg) %>% 
                mutate(mpg = (mpg > median(mpg)))
# Warning messages:
# 1: Computation failed in `stat_smooth()`:
# y values must be 0 <= y <= 1 
# ...


# works
regressors <- pivot_longer(mtcars, -mpg) %>% 
                mutate(mpg = (mpg > median(mpg)) %>% as.numeric())

ggplot(regressors) +
  geom_jitter(aes(value,mpg, colour=name), height = 0.05) + 
  geom_smooth(aes(value,mpg, colour=name), method="glm", method.args = list(family = "binomial")) +
  facet_wrap(~name, scales="free_x") 

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53