# Correlated random effects in categorical model?

**URL:** <https://discourse.mc-stan.org/t/correlated-random-effects-in-categorical-model/7433>\
**Category:** brms\
**Created:** [January 25, 2019, 11:16am UTC](https://discourse.mc-stan.org/t/correlated-random-effects-in-categorical-model/7433 "2019-01-25T11:16:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@Tobias](https://discourse.mc-stan.org/u/Tobias)\
**Post date:** [January 25, 2019, 11:16am UTC](https://discourse.mc-stan.org/t/correlated-random-effects-in-categorical-model/7433/1 "2019-01-25T11:16:34Z")

</div>

I recently encountered an [interesting paper](https://link.springer.com/article/10.1007/s00265-017-2363-8) by Koster & McElreath (2017) in which they propose a multinomial regression model with random effects that are correlated over each category equation, as shown in the paragraph " The multilevel multinomial behavior model". I am quite new to working with brms but as far as I understand the `family = categorical()`-argument it by default assumes independence between the random effects in each category. Is there a way around this assumption to fit a model similar to that proposed by Koster & McElreath?

Thanks for your 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:** [January 25, 2019, 12:03pm UTC](https://discourse.mc-stan.org/t/correlated-random-effects-in-categorical-model/7433/2 "2019-01-25T12:03:02Z")

</div>

See `vignette("brms_multilevel")` for the syntax to make random effects to be correlated across formulas / categories.

---

<div class="post-metadata">

**Author:** ![Tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@Tobias](https://discourse.mc-stan.org/u/Tobias)\
**Post date:** [January 25, 2019, 5:03pm UTC](https://discourse.mc-stan.org/t/correlated-random-effects-in-categorical-model/7433/3 "2019-01-25T17:03:41Z")

</div>

Thanks for the reference, Paul. Just to clarify using the terminology from McElreath et al.: Do you propose something like

```
brm(cbind(pi_1, pi_2, pi_3) ~ (1 | ID | v), 
family = bernoulli(link = "logit"), 
prior(lkj(eta), class = cor)

```

where for each observation either pi\_1, pi\_2 or pi\_3 = 1?

---

<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:** [January 25, 2019, 5:05pm UTC](https://discourse.mc-stan.org/t/correlated-random-effects-in-categorical-model/7433/4 "2019-01-25T17:05:57Z")

</div>

No. Go for something like

```
brm(y ~ (1 | ID | v), family = categorical(), ...)

```

The term `ID` is arbitrary and just ensures that the random effects will be modeled as correlated.

---

<div class="post-metadata">

**Author:** ![Tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@Tobias](https://discourse.mc-stan.org/u/Tobias)\
**Post date:** [January 25, 2019, 5:24pm UTC](https://discourse.mc-stan.org/t/correlated-random-effects-in-categorical-model/7433/5 "2019-01-25T17:24:16Z")

</div>

It is hard to believe that it is actually that simple. We should erect a monument to honor this package.

---

<div class="post-metadata">

**Author:** ![Tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@Tobias](https://discourse.mc-stan.org/u/Tobias)\
**Post date:** [January 31, 2019, 5:40pm UTC](https://discourse.mc-stan.org/t/correlated-random-effects-in-categorical-model/7433/6 "2019-01-31T17:40:58Z")

</div>

One more thing that came to my mind: Is it possible to induce correlations for a nesting of random effects? Something like:

```
brm(y ~ (1 | ID | state/county/zip), family = categorical(), ...)

```

---

<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:** [January 31, 2019, 8:46pm UTC](https://discourse.mc-stan.org/t/correlated-random-effects-in-categorical-model/7433/7 "2019-01-31T20:46:25Z")

</div>

Split the nested term in its parts and set separate ids for that, e.g.

```
(1 | ID1 | state) + (1 | ID2 | state:county) + (1 | ID3 | state:county:zip)

```

For more details, please consult `vignette("brms_multilevel")`
