# Does coef() work for nested varying effects?

**URL:** <https://discourse.mc-stan.org/t/does-coef-work-for-nested-varying-effects/19585>\
**Category:** brms\
**Created:** [December 1, 2020, 8:57pm UTC](https://discourse.mc-stan.org/t/does-coef-work-for-nested-varying-effects/19585 "2020-12-01T20:57:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jd\_c](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jd_c/32/16759_2.png) [@jd\_c](https://discourse.mc-stan.org/u/jd_c)\
**Post date:** [December 1, 2020, 8:57pm UTC](https://discourse.mc-stan.org/t/does-coef-work-for-nested-varying-effects/19585/1 "2020-12-01T20:57:35Z")

</div>

I recently discovered coef() in brms. Now, I think I may have discovered that I have been misinterpreting random effects for nested models.

Let’s say I have this model:  
outcome ~ 1 + treat + (1 + treat|Region/District/School)

If I wanted the slope of the effect of treatment on a specific School (say School A, in District B, in Region C), then I thought I would extract the posterior samples and do:  
(population level effect of treat) + (random effect of treat for Region C) + (random effect of treat for District B) + (random effect of treat for School A)

In other words, I thought all of these random effects were offsets from both the population-level effect and the Group level within which they were nested. Is that not correct??

When I call coef() it appears to simply add the population-level effect for treat to the random effect of treat for School A, without including the effects of Region or District that the School is in.

@paul.buerkner have I completely misunderstood nested random effects or coef() ?

Thanks

---

<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:** [December 1, 2020, 11:02pm UTC](https://discourse.mc-stan.org/t/does-coef-work-for-nested-varying-effects/19585/2 "2020-12-01T23:02:37Z")

</div>

Coef() will indeed nor consider nesting of random effects. Instead you have to use ranef() and add the random effects manually as you indicated.

---

<div class="post-metadata">

**Author:** ![jd\_c](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jd_c/32/16759_2.png) [@jd\_c](https://discourse.mc-stan.org/u/jd_c)\
**Post date:** [December 1, 2020, 11:53pm UTC](https://discourse.mc-stan.org/t/does-coef-work-for-nested-varying-effects/19585/3 "2020-12-01T23:53:04Z")

</div>

Awesome. Thanks for the clarification!  
I’ll just use ranef(model, summary=FALSE) and add them myself.  
It’s relieving that I wasn’t mistaken about random effects in nested models.

Thanks for the quick response, clarification, and super awesome brms package!
