Convenience function for plotting random/group effects?

Please also provide the following information in addition to your question:

  • Operating System: Mac OS 10.14.6
  • brms Version: 2.11.6

Hello all,
This is probably a daft question, as I’m relatively new to brms. Does anyone know of a convenience function for plotting random/group effects across participants or items? Something that looks like the plot produced in merTools for lme4 models is what I’m looking to produce but based on a brms model. For an example, see the first plot as you scroll down this link.
Any ideas would be appreciated.
Rich

1 Like

I use this

# Full group level estimates
ranef(k_fit_brms, groups="site", probs = 0.5)

for looking at my numbers group level effects in brms. Let me check how I have my plots setup.

1 Like

I use something like this:

k_fit_brms %>%
  spread_draws(b_cw_Intercept, r_site__cw[site,]) %>%
  # add the grand mean to the group-specific deviations
  mutate(mu = b_cw_Intercept + r_site__cw) %>%
  ungroup() %>%
  mutate(site = str_replace_all(site, "[.]", " ")) %>% 
  
  # plot
  ggplot(aes(x = mu, y = reorder(site, mu))) +
  geom_vline(xintercept = fixef(k_fit_brms)[1, 1], color = "#839496", size = 1) +
  geom_vline(xintercept = fixef(k_fit_brms)[1, 3:4], color = "#839496", linetype = 2) +
  geom_halfeyeh(.width = .5, size = 2/3, fill = "#859900") +
  labs(x = expression("Cottonwood litterfall (g/m^2)"),
       y = "BEMP sites ordered by mean predicted litterfall") +
  theme(panel.grid   = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y  = element_text(hjust = 0),
        text = element_text(family = "Ubuntu")) 

which isn’t really convenient but it works.

6 Likes

Coo, can you upload also a figure showing how it looks?

Oh yeah that would be helpful. :) I’ll get that as soon as I am in at work. I don’t remember where I swiped the original code from. I will dig around and see if I can find that reference.

1 Like

So it makes this plot which I am not sure if that is what @Rich wants.

I’ve attached my fully annotated R script as well.

community_comp_structural_eq_model.R (13.1 KB)

10 Likes

Thank you very much. That’s helpful.

Possibly useful: brsmtools by Matti Vuorre.

1 Like

You are welcome. That’s site (a category) running down the left hand side of the plot.

It is in my opinion the best one, however Vuorre is not actively working on it anymore. Which is quite unfortunate, since it solves the problem with one line of code on our side and removes hours of work on potential debugging.

1 Like

Did anything come up in the mean time to plot random effects?

E.g. I would love to visualise my random slopes by plotting them via conditional_effects() or something where in addition to the fixed effect I can show the slopes for each subject.

Is there a better way than extracting the coefficents via coef()? I am working with truncation (see Trunc() and conditional_effects() - #5 by Solomon) and it seems a pain to manually create the slopes for each subject using the extracted coefficients.

2 Likes