Search code examples
rggplot2logistic-regression

Multiple logistic regression ggplot with groups


This is just kind of annoying me, so I'm hoping someone has an idea. I'm running a multiple logistic regression where there is one numeric predictor and one categorical predictor. I'd like to make a nice looking ggplot of the logistic regression of the model (without an interaction term, so the curves should just be translations of each other). For example:

data("mtcars")

library(ggplot2)

glm(data = mtcars, vs ~ mpg + as.factor(gear))

Creates a model. One idea would be

ggplot(data = mtcars, aes(x = mpg, y = vs, color = as.factor(gear))) +
  geom_point() +
  geom_smooth(
    method = "glm",
    method.args = list(family = "binomial"),
    se = F
  )

but this creates a separate logistic model for each group, which is a different model. The best I've come up with is to use predict() with a response type and then add a geom_line() with y = prediction_value. This looks right-ish, but it's not as smooth as using geom_smooth. I know I could also use predict() on more points to smooth it out, but this seems like there must be a nicer way to do this.


Solution

  • Usually I find if you're trying to get ggplot to do something that is non-standard (i.e. an uncommon or unusual transformation), it works out easier and quicker if you just calculate what you want to plot, then plot it using straightforward ggplot syntax.

    library(ggplot2)
    
    fit <- glm(vs ~ mpg + factor(gear), data = mtcars, family = binomial)
    
    new_data <- data.frame(gear = factor(rep(3:5, each = 100)),
                           mpg  = rep(seq(10, 35, length.out = 100), 3))
    
    new_data$vs <- predict(fit, newdata = new_data, type = "response")
    
    
    ggplot(data = mtcars, 
           aes(x = mpg, y = vs, color = as.factor(gear))) +
      geom_point() +
      geom_line(data = new_data, size = 1)
    

    Created on 2020-11-30 by the reprex package (v0.3.0)