# NOT RUN {
library(ggplot2)
library(dplyr)
library(rstanarm)
library(modelr)
theme_set(theme_light())
m_mpg = stan_glm(mpg ~ hp * cyl, data = mtcars,
# 1 chain / few iterations just so example runs quickly
# do not use in practice
chains = 1, iter = 500)
step = 1
mtcars %>%
group_by(cyl) %>%
data_grid(hp = seq_range(hp, by = step)) %>%
add_predicted_samples(m_mpg) %>%
do(density_bins(.$pred)) %>%
ggplot() +
geom_rect(aes(
xmin = hp - step/2, ymin = lower, ymax = upper, xmax = hp + step/2,
fill = ordered(cyl), alpha = density
)) +
geom_point(aes(x = hp, y = mpg, color = ordered(cyl)), data = mtcars) +
scale_alpha_continuous(range = c(0, 0.75))
# }
Run the code above in your browser using DataLab