# Varying intercepts with single level group effect

**URL:** <https://discourse.mc-stan.org/t/varying-intercepts-with-single-level-group-effect/37589>\
**Category:** Modeling\
**Tags:** brms, fitting-issues, specification\
**Created:** [December 11, 2024, 2:26pm UTC](https://discourse.mc-stan.org/t/varying-intercepts-with-single-level-group-effect/37589 "2024-12-11T14:26:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amynang](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/amynang/32/19114_2.png) [@amynang](https://discourse.mc-stan.org/u/amynang)\
**Post date:** [December 11, 2024, 2:26pm UTC](https://discourse.mc-stan.org/t/varying-intercepts-with-single-level-group-effect/37589/1 "2024-12-11T14:26:31Z")

</div>

Hello,

(I would also interested in how to solve this in Stan, particularly if there is a good example available but ideally I want to do this with `brms`)

I am working on something where a continuous response is related to a continuous predictor across different locations. I do not want to pool information across locations but I do want to let the intercept vary across blocks (sub-groups) within locations. However one location does not have sub-groups. If I was modelling this for each location separately, I would do something like `y ~ 1 + x + (1|b)` and drop the varying intercept term for the location without subgroups. But when modelling them together, location B gets varying intercepts across a single level of `b`, which inflates uncertainty.

(I was also surprised to see that `y ~ 1 + x + (1|b)` and `y ~ 1 + x` yield different estimates, at least in terms of uncertainty, when modelling that location alone but that’s probably just me not fully understanding what varying intercepts does under the hood; I was thinking of it as averaging over the levels of the group predictor, with partial pooling, which in this case would be like averaging over one)

Anyway, is there a way to “switch off” varying intercepts for those levels of a higher level predictor that do not have sub-groups?

```r
library(tidyverse)

d = data.frame(x = rnorm(500),
               z = rep(letters[1:5], each = 100),
               b = rep(c("1","2"), 50)) %>% 
  mutate(b = str_c(z, b))

d$y = 5 + 3*d$x + rnorm(500)

library(brms)
m = brm(
  bf(
    y ~ 1 + x*z + (1|gr(b, by = z))
  ),
  backend = "cmdstanr",
  chains = 4,
  iter = 4000,
  warmup = 2000,
  cores = 4,
  seed = 42,
  data = d %>% filter(!(z == "b" & b == "b2"))
)
summary(m)

conditional_effects(m, effects ="x:z") %>% 
  plot() %>% 
  .[[1]] +
  facet_wrap(~z)

```

 ![rand](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/e/6/e6b98b10671132a63eceaafe4d626b5f37327296.png)

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [December 11, 2024, 9:43pm UTC](https://discourse.mc-stan.org/t/varying-intercepts-with-single-level-group-effect/37589/2 "2024-12-11T21:43:00Z")

</div>

> [@amynang](#):
>
> not fully understanding what varying intercepts does under the hood

It just means that each element of `b` gets its own intercept. For instance, if I do `1 | age` and there are five age groups, then each age gets its own intercept. I’m not sure if brms uses a sum-to-zero or a pin age[1] == 0 to identify.

What’s going to happen here with the global intercept is that the `1 | b` terms will pick up differences among the groups indicated by `b` in intercept.

---

<div class="post-metadata">

**Author:** ![amynang](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/amynang/32/19114_2.png) [@amynang](https://discourse.mc-stan.org/u/amynang)\
**Post date:** [December 12, 2024, 12:53pm UTC](https://discourse.mc-stan.org/t/varying-intercepts-with-single-level-group-effect/37589/3 "2024-12-12T12:53:51Z")

</div>

My confusion is about what happens if you do `1 | age` when you only have only one age group. I would expect in that case the model to be equivalent to one without the `1 | age` but it is not. In my example, we have four locations with two groups each and one (panel b in the figure) with only one group.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [December 13, 2024, 10:38pm UTC](https://discourse.mc-stan.org/t/varying-intercepts-with-single-level-group-effect/37589/4 "2024-12-13T22:38:37Z")

</div>

> [@amynang](#):
>
> My confusion is about what happens if you do `1 | age` when you only have only one age group.

I’m not sure. It should just drop that out, but I can imagine it getting confused and producing two intercepts that are collinear and running into all kinds of problems. I suggest just not doing `1 | age` when there is only one age :-).
