predict.glm
type
parameter defaults to "link"
, which add_predictions
does not change by default, and does not provide you with any way to change the almost desired "response"
. (There is a GitHub problem , add some good rerefs to it if you want.) However, itβs not difficult to just use predict
directly within tidyverse via dplyr::mutate
.
Also note that ggplot forces default
(coefficient) to a number to build a line, which is good, except that "No" and "Yes" are replaced by 1 and 2, and the probabilities returned by predict
will be in the range of 0 up to 1. Explicitly forced numerical value and subtraction of one fixes the graph, although an additional call to scale_y_continuous
is required to correct the labels.
library(tidyverse) library(modelr) d <- as_tibble(ISLR::Default) model <- glm(default ~ balance, data = d, family = binomial) grid <- d %>% data_grid(balance) %>% mutate(pred = predict(model, newdata = ., type = 'response')) ggplot(d, aes(x = balance)) + geom_point(aes(y = as.numeric(default) - 1)) + geom_line(data = grid, aes(y = pred)) + scale_y_continuous('default', breaks = 0:1, labels = levels(d$default))
![](https://fooobar.com/https://i.imgur.com/vP7nugC.png)
Also note that if all you need is a chart, geom_smooth
can calculate forecasts directly for you:
ggplot(d, aes(balance, as.numeric(default) - 1)) + geom_point() + geom_smooth(method = 'glm', method.args = list(family = 'binomial')) + scale_y_continuous('default', breaks = 0:1, labels = levels(d$default))
![](https://fooobar.com/undefined)
source share