Exporting to a dataframe the summaries from a brmsMarginalEffects object

I would like to get in a data.frame format the means/medians and their credible intervals from brmsMarginalEffects objects like the one below:


fit <- brm(count ~ log_Age_c + log_Base4_c *  
Trt + (1 | patient), data = epilepsy, family = 

me <- marginal_effects(fit, "log_Base4_c:Trt")

I tried the classical code below but it does not work. I could not find an answer for this in the help document marginal_effects.brmsfit {brms}

Error in as.data.frame.default(me) : 
cannot coerce class ‘"brmsMarginalEffects"’ to 
a data.frame

Any help would be much appreciated. Please!

  • Operating System: Windows 10
  • brms Version:

The Value section at ?marginal_effects says:

An object of class brmsMarginalEffects, which is a named list with one data.frame per effect containing all information required to generate marginal effects plots.

which implies that your me object should be a list containing a data frame. If that’s the case then either:

df <- me[[1]]


df <- me[["log_Base4_c:Trt"]]

should work.

Thanks, @jonah. Either of them works just as I wanted.

In the context of binomial regression, I would appreciate any clarification as to whether the Y values from an object of class brmsMarginalEffects are percentages already.

If not, how could this n be set to 100 to make these Y values percentages in the ME plot or data frame?

Any help would be much appreciated.

marginal_effects tells you via a message what it does if you don’t specify trials explictely.

To set trials yourself, please use the conditions argument of marginal_effects.

Thanks a lot, @paul.buerkner.

For example, given the binomial model from here, to define that the marginal effects must be estimated for n=100 (that is, the Y values must be in percentages), would specifying conditions as:

conditions <- data.frame(n = 100)


Thank you very much for this help.

Yes, this is sufficient.

1 Like

@paul.buerkner, please accept my deepest gratitude for your tremendous help.