Also consider which scales are most appropriate for your use case. Say you're doing visual inspection for the purposes of modeling in logistic regression and want to visualize a continuous predictor to determine if you need to add a spline or polynomial term to your model. In this case, you may want a scale in log-odds rather than probability/proportion.
The function at the gist below uses some limited heuristics to split the continuous predictor into bins, calculate the mean proportion, convert to log-odds, then plot geom_smooth
over these aggregate points.
Example of what this chart looks like if a covariate has a quadratic relationship (+ noise) with the log-odds of a 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))
# 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)
For comparison, here is what that quadratic relationship would look like if you just plotted the 1's/0's and added a geom_smooth
:
simulated_df %>%
ggplot(aes(x, y))+
geom_smooth()+
geom_jitter(height = 0.01, width = 0)+
coord_cartesian(ylim = c(0, 1), xlim = c(-3.76, 3.59))
# set xlim to be generally consistent with prior chart
#> `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
Created on 2019-02-25 by the reprex package (v0.2.1)
Relationship to logit is less clear and using geom_smooth
has some problems.