# Zero inflated beta binomial model under brms

**URL:** https://discourse.mc-stan.org/t/zero-inflated-beta-binomial-model-under-brms/13170
**Category:** brms
**Created:** [February 19, 2020, 5:11pm UTC](https://discourse.mc-stan.org/t/zero-inflated-beta-binomial-model-under-brms/13170 "2020-02-19T17:11:14Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Yeting\_Zhang](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yeting_zhang/32/7191_2.png) [@Yeting\_Zhang](https://discourse.mc-stan.org/u/Yeting_Zhang)
#### Post date: [February 19, 2020, 5:11pm UTC](https://discourse.mc-stan.org/t/zero-inflated-beta-binomial-model-under-brms/13170/1 "2020-02-19T17:11:14Z")

</div>

Hi,

I would like to model my data using zero inflated beta binomial distribution. I can do beta binomial using the code available on the link below.  
[https://bookdown.org/connect/#/apps/1850/access](https://bookdown.org/connect/#/apps/1850/access)

I wonder if brms has a zero inflated beta binomial distribution available? If not, is there a way that I can build one in brms? I am new to stan and brms. Previously the work was done under a frequentist framework.

Thanks a lot.

---

<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: [February 19, 2020, 9:46pm UTC](https://discourse.mc-stan.org/t/zero-inflated-beta-binomial-model-under-brms/13170/2 "2020-02-19T21:46:46Z")

</div>

To implement this in brms you will have to use a “custom response distribution”.  
[The brms vignette for this](https://cran.r-project.org/web/packages/brms/vignettes/brms_customfamilies.html) uses the beta binomial model.

This is a starting point, but to make this work you also need to code the zero inflation part. (i.e. you have so specify the design matrix, additional regression parameters, and implement the mixture-model to combine the zero inflation part with the beta binomial part.)

A good starting point for this is to specify a model with the zero inflated negative binomial family in brms ([see here](https://rdrr.io/cran/brms/man/brmsfamily.html)) and to use `make_stancode` to produce the output, which you can then to inspect to see how zero inflation is implemented.

The harder part will then be to (a) [generate a brms model with the stan code you generated](https://discourse.mc-stan.org/t/creating-a-brmsfit-object-with-a-modified-brms-generated-stan-model/6840) and (b) write the `posterior_predict` and `log_lik` functions that are needed for post processing (but looking at the [similar other brms functions](https://github.com/paul-buerkner/brms/blob/master/R/posterior_predict.R)) should also help with this.
