# NOT RUN {
library(ggplot2)
library(dplyr)
if (
require("rstanarm", quietly = TRUE) &&
require("modelr", quietly = TRUE)
) {
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)
# draw 100 fit lines from the posterior and overplot them
mtcars %>%
group_by(cyl) %>%
data_grid(hp = seq_range(hp, n = 101)) %>%
add_fitted_draws(m_mpg) %>%
sample_draws(100) %>%
ggplot(aes(x = hp, y = mpg, color = ordered(cyl))) +
geom_line(aes(y = .value, group = paste(cyl, .draw)), alpha = 0.25) +
geom_point(data = mtcars)
}
# }
Run the code above in your browser using DataLab