Exporting to a dataframe the summaries from a brmsMarginalEffects object

windows

#1

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

library(rstan)
library(brms)

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

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}

df<-as.data.frame(me)
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:

#2

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]]

or

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

should work.


Error: $ operator is invalid for atomic vectors
#3

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


#4

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.


#5

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.


#6

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)

suffice?

Thank you very much for this help.


#7

Yes, this is sufficient.


#8

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