Skip to main content
1 of 7

If you're using this for modeling in logistic regression, you may want the line on a log-odds scale rather than probability/proportion. E.g. say you want to visualize a continuous predictor to determine if you need to add a spline or polynomial term to your model... the relevant scale then would be log-odds rather than probability. I wrote a gist that uses some limited heuristics to split the continuous predictor into bins and then plots a smoothed line over these. See gist below and example of visualizing a predictor that has a quadratic relationship with the log-odds of the binary target:

devtools::source_gist("https://gist.github.com/brshallo/3ccb8e12a3519b05ec41ca93500aa4b3")

# simulated dataset with quadratic relationship between x and y
set.seed(12)
samp_size <- 1000
simulated_df <- tibble(x = rlogis(samp_size), 
                       y_odds = 0.2*x^2,
                       y_probs = exp(y_odds)/(1 + exp(y_odds))) %>% 
  mutate(y = rbinom(samp_size, 1, prob = y_probs)) 

# not necessary maybe, but looking at on balanced dataset
simulated_df_balanced <- simulated_df %>% 
  group_by(y) %>% 
  sample_n(table(simulated_df$y) %>% min())


ggplot_continuous_binary(df = simulated_df,
                         covariate = x, 
                         response = y,
                         snip_scales = TRUE)
#> [1] "bin size: 18"
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Created on 2019-02-06 by the reprex package (v0.2.1)

As noted in the gist, there are some limitations in the heuristic I use for binning...