I am trying to calculate the logistic regression prediction for a set of data using the tidyverse and modelr packages. Clearly I am doing something wrong in the add_predictions
as I am not receiving the "response" of the logistic function as I would if I were using the 'predict' function in stats. This should be simple, but I can't figure it out and multiple searches yielded little.
library(tidyverse)
library(modelr)
options(na.action = na.warn)
library(ISLR)
d <- as_tibble(ISLR::Default)
model <- glm(default ~ balance, data = d, family = binomial)
grid <- d %>% data_grid(balance) %>% add_predictions(model)
ggplot(d, aes(x=balance)) +
geom_point(aes(y = default)) +
geom_line(data = grid, aes(y = pred))
predict.glm
'stype
parameter defaults to"link"
, whichadd_predictions
does not change by default, nor provide you with any way to change to the almost-certainly desired"response"
. (A GitHub issue exists; add your nice reprex on it if you like.) That said, it's not hard to just usepredict
directly within the tidyverse viadplyr::mutate
.Also note that ggplot is coercing
default
(a factor) to numeric in order to plot the line, which is fine, except that "No" and "Yes" are replaced by 1 and 2, while the probabilities returned bypredict
will be between 0 and 1. Explicitly coercing to numeric and subtracting one fixes the plot, though an extrascale_y_continuous
call is required to fix the labels.Also note that if all you want is a plot,
geom_smooth
can calculate predictions directly for you: