library(tidyverse)
library(broom)
# Basic logit predicting if a car is an SUV or not
cars_binary <- mpg %>%
# Make new column marking if it's an SUV or not
mutate(suv = class == "suv")
model <- glm(suv ~ cty + displ,
data = cars_binary,
family = binomial(link = "logit"))
# These are log odds
tidy(model, conf.int = TRUE)
#> # A tibble: 3 x 7
#> term estimate std.error statistic p.value conf.low conf.high
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 4.42 2.12 2.09 0.0367 0.371 8.74
#> 2 cty -0.384 0.0931 -4.13 0.0000369 -0.577 -0.209
#> 3 displ 0.132 0.219 0.603 0.546 -0.306 0.561
# These are exponentiated odds ratios
tidy(model, exponentiate = TRUE, conf.int = TRUE)
#> # A tibble: 3 x 7
#> term estimate std.error statistic p.value conf.low conf.high
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 83.4 2.12 2.09 0.0367 1.45 6235.
#> 2 cty 0.681 0.0931 -4.13 0.0000369 0.562 0.811
#> 3 displ 1.14 0.219 0.603 0.546 0.737 1.75
# Coefficient plot of log odds
coefs_to_plot_log_odds <- tidy(model, conf.int = TRUE) %>%
filter(term != "(Intercept)")
ggplot(coefs_to_plot_log_odds, aes(x = estimate, y = term)) +
geom_vline(xintercept = 0, color = "red") +
geom_pointrange(aes(xmin = conf.low, xmax = conf.high)) +
labs(x = "Coefficient (log odds)")
# Coefficient plot of odds ratios
coefs_to_plot_or <- tidy(model, exponentiate = TRUE, conf.int = TRUE) %>%
filter(term != "(Intercept)")
ggplot(coefs_to_plot_or, aes(x = estimate, y = term)) +
geom_vline(xintercept = 1, color = "red") +
geom_pointrange(aes(xmin = conf.low, xmax = conf.high)) +
labs(x = "Coefficient (odds ratio)")
Created on 2020-05-19 by the reprex package (v0.3.0)