Plot slope effects with quantile band

Here is my model fit through brms:

fm <- brm(Value | se(SE) ~ time:class + (time|subj) + (1|subj:class), data=dd)

I’d like to plot the slope effect of ‘time’ with the 90% quantile band for each level of factor ‘class’ in a panel layout. Below is my starting code:

dd %>%
  group_by(class) %>%
  data_grid(time = seq_range(time n = 101)) %>%
  add_predicted_draws(fm) %>%
  ggplot(aes(x = time, y = Value, color = ordered(class), fill = ordered(class))) +
  stat_lineribbon(aes(y = .prediction), .width = c(.95, .5, .05), alpha = 1/4) +
  geom_point(data = dd)

It failed with the following error:

Error in get(sgroup[1], data) : object 'subj' not found

How to fix this?

Hi @Gang,

Could you please provide a reproducible example to make it easier to answer your question?

It looks like the variable subj doesn’t exist in your data (dd I suppose).

Have you tried using brms::conditional_effects()?

1 Like

Thanks @matti for the suggestion!

The following

plot(conditional_effects(fm, effects = "time:class"))

does show all the slopes within one panel. However, how can I separate those slope effects into a matrix of panels with something like facet_grid() in ggplot2? I hope this is simple so that I don’t have to create a reproducible example.

With the conditions argument as per documentation.

1 Like

Thanks, Paul! Do you mean something like the following?

plot(marginal_effects(fm, effects = "time:class", conditions=make_conditions(fm, "class")))

It still plots everything in one panel.

Use a variable either in conditions or effects. Not both.

1 Like

Thanks a lot, Paul, for providing so many nice and powerful tools!