# Exporting to a dataframe the summaries from a brmsMarginalEffects object

**URL:** <https://discourse.mc-stan.org/t/exporting-to-a-dataframe-the-summaries-from-a-brmsmarginaleffects-object/4257>\
**Category:** brms\
**Tags:** windows\
**Created:** [May 19, 2018, 6:36pm UTC](https://discourse.mc-stan.org/t/exporting-to-a-dataframe-the-summaries-from-a-brmsmarginaleffects-object/4257 "2018-05-19T18:36:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![realkrantz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/realkrantz/32/3346_2.png) [@realkrantz](https://discourse.mc-stan.org/u/realkrantz)\
**Post date:** [May 19, 2018, 6:36pm UTC](https://discourse.mc-stan.org/t/exporting-to-a-dataframe-the-summaries-from-a-brmsmarginaleffects-object/4257/1 "2018-05-19T18:36:19Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 19, 2018, 6:59pm UTC](https://discourse.mc-stan.org/t/exporting-to-a-dataframe-the-summaries-from-a-brmsmarginaleffects-object/4257/2 "2018-05-19T18:59:59Z")

</div>

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:

```r
df <- me[[1]]

```

or

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

```

should work.

---

<div class="post-metadata">

**Author:** ![realkrantz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/realkrantz/32/3346_2.png) [@realkrantz](https://discourse.mc-stan.org/u/realkrantz)\
**Post date:** [May 19, 2018, 7:06pm UTC](https://discourse.mc-stan.org/t/exporting-to-a-dataframe-the-summaries-from-a-brmsmarginaleffects-object/4257/3 "2018-05-19T19:06:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![realkrantz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/realkrantz/32/3346_2.png) [@realkrantz](https://discourse.mc-stan.org/u/realkrantz)\
**Post date:** [October 23, 2018, 8:58am UTC](https://discourse.mc-stan.org/t/exporting-to-a-dataframe-the-summaries-from-a-brmsmarginaleffects-object/4257/4 "2018-10-23T08:58:33Z")

</div>

In the context of [binomial regression](https://discourse.mc-stan.org/t/error-operator-is-invalid-for-atomic-vectors/6156/9), 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.

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [October 23, 2018, 9:04am UTC](https://discourse.mc-stan.org/t/exporting-to-a-dataframe-the-summaries-from-a-brmsmarginaleffects-object/4257/5 "2018-10-23T09:04:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![realkrantz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/realkrantz/32/3346_2.png) [@realkrantz](https://discourse.mc-stan.org/u/realkrantz)\
**Post date:** [October 23, 2018, 9:19am UTC](https://discourse.mc-stan.org/t/exporting-to-a-dataframe-the-summaries-from-a-brmsmarginaleffects-object/4257/6 "2018-10-23T09:19:44Z")

</div>

Thanks a lot, @paul.buerkner.

For example, given the binomial model from [here](https://discourse.mc-stan.org/t/error-operator-is-invalid-for-atomic-vectors/6156), 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.

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [October 23, 2018, 9:30am UTC](https://discourse.mc-stan.org/t/exporting-to-a-dataframe-the-summaries-from-a-brmsmarginaleffects-object/4257/7 "2018-10-23T09:30:26Z")

</div>

Yes, this is sufficient.

---

<div class="post-metadata">

**Author:** ![realkrantz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/realkrantz/32/3346_2.png) [@realkrantz](https://discourse.mc-stan.org/u/realkrantz)\
**Post date:** [October 23, 2018, 9:36am UTC](https://discourse.mc-stan.org/t/exporting-to-a-dataframe-the-summaries-from-a-brmsmarginaleffects-object/4257/8 "2018-10-23T09:36:46Z")

</div>

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