# NOT RUN {
library(ggplot2)
library(dplyr)
if (
require("brms", quietly = TRUE) &&
require("modelr", quietly = TRUE)
) {
theme_set(theme_light())
m_mpg = brm(mpg ~ hp * cyl, data = mtcars,
# 1 chain / few iterations just so example runs quickly
# do not use in practice
chains = 1, iter = 500)
# plot posterior predictive intervals
mtcars %>%
group_by(cyl) %>%
data_grid(hp = seq_range(hp, n = 101)) %>%
# the line below is equivalent to add_fitted_draws(m_mpg), except that it does not
# standardize arguments across model types. `summary = FALSE` is not strictly necessary
# with posterior_linpred(), but because it is necessary on some functions (otherwise
# those functions return summaries instead of a matrix of draws) it is
# included in this example.
add_draws(posterior_linpred(m_mpg, newdata = ., summary = FALSE)) %>%
ggplot(aes(x = hp, y = mpg, color = ordered(cyl))) +
stat_lineribbon(aes(y = .value), alpha = 0.25) +
geom_point(data = mtcars) +
scale_fill_brewer(palette = "Greys")
}
# }
Run the code above in your browser using DataLab