Posterior predictive check with two grouping variables

I am trying to visualize the posterior predictive distributions across two grouping variables. I am using bayesplot::ppc_violin_grouped() which is working like a charm for the first grouping variable, but then I’d like to facet by the second grouping variable.

I thought I might be able to just add this to an existing plot using facet_wrap(). I looked at the data component of the bayesplot object and first tried to emulate that. However, I got an error that the length was wrong. If I then change the length of the facet variable to what the error asks for, it then wants a different length. Minimally reproducible example:

mod <- brms::brm(mpg ~ cyl + gear, data = mtcars, iter = 200)
ppc <- brms::posterior_predict(mod)
base.plot <- bayesplot::ppc_violin_grouped(y = mtcars$mpg, yrep = ppc, group = mtcars$gear) 

# try to replicate structure of base.plot$data
base.plot +
  facet_wrap(~c(rep(mtcars$cyl, each = 400), mtcars$cyl))

# change based on resulting error
base.plot +
  facet_wrap(~rep(mtcars$cyl, each = 400))

# change based on resulting error
base.plot +
  facet_wrap(~mtcars$cyl)

I am guessing the function is plotting two separate data frames or something? Is there straightforward way around this that I am missing?

1 Like

I just responded over on the github issue with one possible option:

My solution there is pretty hacky and it’s possible someone else (e.g. @tjmahr) can come up with an easier way to do it. Maybe we can even support this natively in bayesplot at some point.