# Marginal\_smooth in a binomial model

**URL:** <https://discourse.mc-stan.org/t/marginal-smooth-in-a-binomial-model/7600>\
**Category:** brms\
**Created:** [February 6, 2019, 11:41am UTC](https://discourse.mc-stan.org/t/marginal-smooth-in-a-binomial-model/7600 "2019-02-06T11:41:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![helene](https://avatars.discourse-cdn.com/v4/letter/h/7993a0/32.png) [@helene](https://discourse.mc-stan.org/u/helene)\
**Post date:** [February 6, 2019, 11:41am UTC](https://discourse.mc-stan.org/t/marginal-smooth-in-a-binomial-model/7600/1 "2019-02-06T11:41:13Z")

</div>

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

- Operating System: windows 8.1
- brms Version:2.7.0

Dear community, I am struggling with getting a good understanding of the model output and its calculation from brms.  
I built a model to understand butterfly mortality through time (week), region, and investigate differences among species. Here is my model (convergence was fine):

summary(mod21)  
Family: binomial  
Links: mu = logit  
Formula: ind\_dead | trials(total\_ind) ~ 0 + region\_butterfly +year + s(week, by = butterfly, k = 4)

I am now interested in the smooth part of the model and would like to plot s(week, by = butterfly, k = 4), all other predictors, year and region\_butterfly, conditionned on their mean (and not on the reference category, as they are categorical variables). I first extract the table of predicted values:

smooth\_week\<-marginal\_smooths(mod21,robust=FALSE,method=“predict”,conditions = data.frame(year = NA,reg\_butt=NA))[[1]]

the plot of this table is the following:

![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/0/008adcf489a7eedb4a3b44ac21023e2d53fe55b0.png)

But to get a better feeling of these variations, I would like the estimates to be backtransformed to my initial scale. As the model is binomial, the estimates and confidence intervals returned from the marginal\_smooth are on a logit scale, which I therefore transform (hoping that so far my understanding is correct):

smooth\_week$mortality=inv\_logit\_scaled(smooth\_week$estimate\_\_)  
smooth\_week$inv\_low=inv\_logit\_scaled(smooth\_week$lower\_\_)  
smooth\_week$inv\_upp=inv\_logit\_scaled(smooth\_week$upper\_\_)

I now get the following plot:  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/c/c547160a6acb496f3a4385c2cbf4cd88deccffdd.png)

here are my specific questions:

1. when extracting the marginal\_smooth, is the argument “conditions” necessary to get estimate on the average mean of the other predictors? I assumed this function works as the marginal\_effect function. In my specific case I also wonder if I should specify region\_butterfly=NA because I would like to get mortality per week and butterfly averaged on region (and not on region\_butterfly).

2. I am sceptical that the transformation (inv\_logit\_scaled) did what I initially intented to get, which is the average mortality. For example, I know from my data that the average mortality of A. levana is on average below 10%. While in the first plot my understanding is that what is plotted is the variations in mortality for each species relative to the mortality of that species at the average week (around 4-5) on a logit scale, I cannot make sence of what I plotted in the second figure.

3. is my only option is to extract the posterior from the 4 chains and average the values following the categories I intend to plot?

---

<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:** [February 6, 2019, 12:16pm UTC](https://discourse.mc-stan.org/t/marginal-smooth-in-a-binomial-model/7600/2 "2019-02-06T12:16:57Z")

</div>

1. and 2.: To see the smooth on the response scale, I would suggest to use `marginal_effects` and the `conditions` argument to condition on the desired values of the other variables. Just transforming the smooth part will likely yield strange results, for instance, because you did not include the intercept or because 0 is not a reasonable value for your other predictors.

2. What would be your preferred option?

---

<div class="post-metadata">

**Author:** ![helene](https://avatars.discourse-cdn.com/v4/letter/h/7993a0/32.png) [@helene](https://discourse.mc-stan.org/u/helene)\
**Post date:** [February 6, 2019, 1:38pm UTC](https://discourse.mc-stan.org/t/marginal-smooth-in-a-binomial-model/7600/3 "2019-02-06T13:38:19Z")

</div>

What I exactly would wish to do is to plot for each butterfly variation in the prediction on mortality according to week. I tried to extract the smooth term from the marginal effect:

marginal\_effects(mod21,“week:butterfly”,conditions = data.frame(year = NA,region\_butterfly=NA),robust=FALSE)

but it returned the following message:

Using the median number of trials by default if not specified otherwise. Error in tcrossprod(b, X) : non-conformable arguments  
Error: Something went wrong (see the error message above). Perhaps you transformed numeric variables to factors or vice versa within the model formula? If yes, please convert your variables beforehand. Or did you set a predictor variable to NA?

I explain this error by the fact that I set the region\_butterfly to NA while I also have “butterfly” in the term I would like to estimate. However, when building the model, I concatenated the 2 predictor variables region and butterfly because I have a missing contrast region\*butterfly.

---

<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:** [February 6, 2019, 1:55pm UTC](https://discourse.mc-stan.org/t/marginal-smooth-in-a-binomial-model/7600/4 "2019-02-06T13:55:46Z")

</div>

Yeah, the fact that you want predictions per butterfly and have butterfly in a separate variable as well (region\_butterfly) is unforunate for predictions as marginal\_effects can’t take “butterly” in “region\_butterfly” into account. The error message is indeed the result of setting your predictors to NA.

An easy option would be to have only main effects of region and butterfly, but I guess that’s not accurate with respect your subject matter knowledge, right?

---

<div class="post-metadata">

**Author:** ![helene](https://avatars.discourse-cdn.com/v4/letter/h/7993a0/32.png) [@helene](https://discourse.mc-stan.org/u/helene)\
**Post date:** [February 6, 2019, 2:14pm UTC](https://discourse.mc-stan.org/t/marginal-smooth-in-a-binomial-model/7600/5 "2019-02-06T14:14:41Z")

</div>

no, that is not what I would like indeed.

Therefore, would that be an option (point 3) to extract the posterior draws from the model and pool them together according to butterfly and week. I mean that I would calculate the mean of the posterior draw for each species and week, and this for the estimates, the lower and the upper limits and then backtransform these values with inv\_logit\_scaled. I am a bit behind the mathematics of how brms process the data… but that would sound correct or this is not the way to proceed?

---

<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:** [February 6, 2019, 2:22pm UTC](https://discourse.mc-stan.org/t/marginal-smooth-in-a-binomial-model/7600/6 "2019-02-06T14:22:53Z")

</div>

I don’t think this would work as you propose to be honest.

Alternatively, you could specify `region:butterfly` in your model and set priors on the coefficients so that they are identified even though the corresponding contrasts don’t actually appear in the data. You just had to make sure that illustrative predictions in `marginal_effects` are based on a region, which has no such missing contrasts so that these missing contrasts won’t affect the plot.

---

<div class="post-metadata">

**Author:** ![helene](https://avatars.discourse-cdn.com/v4/letter/h/7993a0/32.png) [@helene](https://discourse.mc-stan.org/u/helene)\
**Post date:** [February 6, 2019, 3:03pm UTC](https://discourse.mc-stan.org/t/marginal-smooth-in-a-binomial-model/7600/7 "2019-02-06T15:03:52Z")

</div>

ok I will think about it. Thanks for you time anyway!
