# How to model a bimodal distribution with brms

**URL:** <https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270>\
**Category:** Modeling\
**Created:** [September 29, 2020, 3:36pm UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270 "2020-09-29T15:36:57Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![BiancaS](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/biancas/32/16101_2.png) [@BiancaS](https://discourse.mc-stan.org/u/BiancaS)\
**Post date:** [September 29, 2020, 3:36pm UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/1 "2020-09-29T15:36:58Z")

</div>

Hi there,

I am trying to fit a mixed model with brms to a bimodal distribution, and initially kept the defaults for distribution family (= gaussian). This is my model:

```
prior1 <- c(set_prior("normal(0,10)", class = "b"), # sets all coefficient priors
            set_prior("normal(0,10)", class = "Intercept"),
            set_prior("cauchy(0,5)", class = "sd")) 

bayes_conf$m1 <- brm(formula = ALLAccuracies ~ lg10JerksZ*allConditions*lg10OwnStimDiff_jerkZ +
                       (1 + lg10JerksZ*lg10OwnStimDiff_jerkZ*allConditions || allVidIDs) +
                       (1 + lg10JerksZ*lg10OwnStimDiff_jerkZ*allConditions || allSubs), 
                     data = db, warmup = 1000, iter = 5000, 
                     cores = options(mc.cores = parallel::detectCores()),
                     chains = 4, control = list(adapt_delta = .99), 
                     prior = prior1, sample_prior = TRUE,
                     save_all_pars = TRUE)

```

And this is how the posterior predictive distribution fits onto the observed values:  
 ![model1-post_pred](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/2/2dc6a8dd7b3667fbf70ad2af54d06b86928bfcc9.jpeg)

I assume by specifying a different distribution family I could increase the fit of my model. However, I am new to bayesian modelling and R, and I have no idea which type of distribution family would be suitable to represent my clearly bimodal outcome variable. Any suggestions would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Ara\_Winter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ara_winter/32/4284_2.png) [@Ara\_Winter](https://discourse.mc-stan.org/u/Ara_Winter)\
**Post date:** [September 29, 2020, 3:56pm UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/2 "2020-09-29T15:56:19Z")

</div>

Hi and welcome!

Can you share a snippet of the raw data or simulated data to give an idea of what the data look like?

There a tutorial here [https://tem11010.github.io/regression\_brms/](https://tem11010.github.io/regression_brms/) that uses a bimodel data set with the Gaussian family in brms,

---

<div class="post-metadata">

**Author:** ![BiancaS](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/biancas/32/16101_2.png) [@BiancaS](https://discourse.mc-stan.org/u/BiancaS)\
**Post date:** [September 29, 2020, 4:12pm UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/3 "2020-09-29T16:12:44Z")

</div>

Hi Ara,

here is the data of my outcome variable & predictors:  
[stanForums\_dat.csv](https://discourse.mc-stan.org/uploads/short-url/jdzZUvne1oK4SzJT8rwc9hU3zdy.csv) (24.7 KB)

I have come across the website you linked, and they suggest that changing the distribution family could lead to improvement, but give no insight into what family that would be…

---

<div class="post-metadata">

**Author:** ![andymilne](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andymilne/32/21046_2.png) [@andymilne](https://discourse.mc-stan.org/u/andymilne)\
**Post date:** [September 29, 2020, 10:59pm UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/4 "2020-09-29T22:59:58Z")

</div>

Does your dependent variable ALLAccuracies have a minimum possible value of 0? Does it have a maximum possible value (e.g. of 10) or can it be arbitrarily high? I ask because the range of your response data can help determine the best family to use.

---

<div class="post-metadata">

**Author:** ![Guido\_Biele](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/guido_biele/32/3414_2.png) [@Guido\_Biele](https://discourse.mc-stan.org/u/Guido_Biele)\
**Post date:** [September 30, 2020, 6:37am UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/5 "2020-09-30T06:37:52Z")

</div>

You could try a mixture model: [https://rdrr.io/cran/brms/man/mixture.html](https://rdrr.io/cran/brms/man/mixture.html)

---

<div class="post-metadata">

**Author:** ![BiancaS](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/biancas/32/16101_2.png) [@BiancaS](https://discourse.mc-stan.org/u/BiancaS)\
**Post date:** [September 30, 2020, 9:55am UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/6 "2020-09-30T09:55:59Z")

</div>

Thanks everyone for your replies so far.

> [@andymilne](#):
>
> Does your dependent variable ALLAccuracies have a minimum possible value of 0? Does it have a maximum possible value (e.g. of 10) or can it be arbitrarily high?

My dependent variable ranges from -5.6 to 9. So contains negative values and the maximum possible value is 9.

> [@Guido\_Biele](#):
>
> You could try a mixture model: [mixture: Finite Mixture Families in 'brms' in brms: Bayesian Regression Models using 'Stan'](https://rdrr.io/cran/brms/man/mixture.html)

Thanks for your suggestion. I have come across this post before, but I guess I was hoping so far there would be an easier fix to my problem. Maybe there isn’t…

---

<div class="post-metadata">

**Author:** ![andymilne](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andymilne/32/21046_2.png) [@andymilne](https://discourse.mc-stan.org/u/andymilne)\
**Post date:** [September 30, 2020, 12:00pm UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/7 "2020-09-30T12:00:46Z")

</div>

Is there a theoretical minimum? If not, it may be beneficial to convert your “accuracy” score to an “inaccuracy” score by subtracting it from 9. Then use a family which supports non-negative reals such as hurdle\_gamma or hurdle\_lognormal. Choosing gaussian when another family is more appropriate can certainly lead to a model not capturing bimodality in the data.

---

<div class="post-metadata">

**Author:** ![BiancaS](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/biancas/32/16101_2.png) [@BiancaS](https://discourse.mc-stan.org/u/BiancaS)\
**Post date:** [October 3, 2020, 10:06am UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/8 "2020-10-03T10:06:18Z")

</div>

Hi Andy,

That sounds like a reasonable approach. Although I still don’t understand how a hurdle\_gamma / hurdle\_lognormal family would capture bimodality?

Anyway, I’ve tried changing the family specification and now Stan seems to throw a problem.

I’ve opened another topic for this new issue here: [https://discourse.mc-stan.org/t/brms-model-doesnt-run-after-custom-family-hurdle-gamma-specified/18365](https://discourse.mc-stan.org/t/brms-model-doesnt-run-after-custom-family-hurdle-gamma-specified/18365)

---

<div class="post-metadata">

**Author:** ![andymilne](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andymilne/32/21046_2.png) [@andymilne](https://discourse.mc-stan.org/u/andymilne)\
**Post date:** [October 3, 2020, 10:21am UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/9 "2020-10-03T10:21:19Z")

</div>

My hope is that the “bimodality” arises from your predictors – some sets of predictor values produce low accuracy, some high. If that is not the case, it suggests either you are missing an important predictor or that the distribution is genuinely bimodal (by “genuinely bimodal” I mean accuracy has two modes when all experimental conditions are otherwise the same).

---

<div class="post-metadata">

**Author:** ![BiancaS](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/biancas/32/16101_2.png) [@BiancaS](https://discourse.mc-stan.org/u/BiancaS)\
**Post date:** [October 3, 2020, 10:37am UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/10 "2020-10-03T10:37:54Z")

</div>

I see, that makes sense. And should definitely be true for me, as my categorical predictor should describe the bimodality almost perfectly. Let’s see, hopefully once I figure out how to run the model with one of your suggested families, it will work.

Thanks so far!

---

<div class="post-metadata">

**Author:** ![andymilne](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andymilne/32/21046_2.png) [@andymilne](https://discourse.mc-stan.org/u/andymilne)\
**Post date:** [October 3, 2020, 10:44am UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/11 "2020-10-03T10:44:16Z")

</div>

Just for starters, while the hurdle distribution doesn’t seem to be functioning, it may be worth using something like `9.01 - accuracy` as your dependent variable (9.01 to ensure all values are slightly larger than 0), and then modelling with a Gamma or lognormal family.

---

<div class="post-metadata">

**Author:** ![BiancaS](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/biancas/32/16101_2.png) [@BiancaS](https://discourse.mc-stan.org/u/BiancaS)\
**Post date:** [October 3, 2020, 12:28pm UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/12 "2020-10-03T12:28:11Z")

</div>

Thanks! Was going to try that, but not actually none of my brm models will run anymore, and I have no idea why. Any input in this topic: [Brms model doesn't run after custom family (hurdle\_gamma()) specified](https://discourse.mc-stan.org/t/brms-model-doesnt-run-after-custom-family-hurdle-gamma-specified/18365)  
would be hugely appreciated

---

<div class="post-metadata">

**Author:** ![JAQuent](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jaquent/32/5763_2.png) [@JAQuent](https://discourse.mc-stan.org/u/JAQuent)\
**Post date:** [October 12, 2023, 3:27am UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/13 "2023-10-12T03:27:31Z")

</div>

I have a similar issue and I am wondering what would you do if the distribution is genuinely bimodal?

---

<div class="post-metadata">

**Author:** ![andymilne](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andymilne/32/21046_2.png) [@andymilne](https://discourse.mc-stan.org/u/andymilne)\
**Post date:** [October 12, 2023, 3:37am UTC](https://discourse.mc-stan.org/t/how-to-model-a-bimodal-distribution-with-brms/18270/14 "2023-10-12T03:37:43Z")

</div>

If you don’t have an additional factor that would explain the bimodality and could be included in the model, you could use a mixture model - e.g., [Finite Mixture Families in brms — mixture • brms](https://paul-buerkner.github.io/brms/reference/mixture.html)
