# Bambi model correlated intercept slopes

**URL:** <https://discourse.pymc.io/t/bambi-model-correlated-intercept-slopes/16054>\
**Category:** General\
**Created:** [November 2, 2024, 3:54pm UTC](https://discourse.pymc.io/t/bambi-model-correlated-intercept-slopes/16054 "2024-11-02T15:54:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HPCurtis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hpcurtis/32/6924_2.png) [@HPCurtis](https://discourse.pymc.io/u/HPCurtis)\
**Post date:** [November 2, 2024, 3:54pm UTC](https://discourse.pymc.io/t/bambi-model-correlated-intercept-slopes/16054/1 "2024-11-02T15:54:53Z")

</div>

Hey just sanity check here.

model = bmb.Model(“y ~ x + (1|subject) + (x|ROI)”, data=df)

This Bambi model results in correlated random intercepts and slopes correct in a non-centered form correct? In addition i have tried to hand code the model in PyMC directly.

with pm.Model() as model:

```
# Intercept (alpha) and beta (fixed) distribution
alpha = pm.Normal("alpha", mu=0.17, sigma=4.66)
beta = pm.Normal("beta", mu=0, sigma=0.27)
sigma = pm.HalfStudentT("sigma",nu=4, sigma=2.3)

# Subjct random intercept.
tau_u = pm.HalfNormal("tau_u", sigma=4.66)
z_u = pm.Normal('z_u', mu=0, sigma=1, shape=N_subj)
u = pm.Deterministic("u", z_u * tau_u)

# Randomintecept and slope correlated.
z_u2 = pm.Normal('z_u2', 0., 1., shape=(J, N_ROI))
  
sd_dist = pm.HalfNormal.dist(sigma=[4.66,0.27], shape=J)
L_u, rho, tau_u2 = pm.LKJCholeskyCov('L_u',
                                    eta=1, n=J,
                                    sd_dist=sd_dist)
u2 = pm.Deterministic("u2", pt.dot(L_u, z_u2).T)

# Likelihood
mu = alpha + beta * Xc + u[subj] + u2[ROI, 0] + X * u2[ROI, 1]
y = pm.Normal('y', mu = mu, sigma = sigma, observed=y)

```

However, my implementation is slower so just curious on any advice on my implementation.

Both Bambi and my implementation tested on nutpie sampler. and i have attmepted to take the priors from bambi to my implementation

---

<div class="post-metadata">

**Author:** ![joshualeond](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/joshualeond/32/8031_2.png) [@joshualeond](https://discourse.pymc.io/u/joshualeond)\
**Post date:** [April 9, 2025, 9:39pm UTC](https://discourse.pymc.io/t/bambi-model-correlated-intercept-slopes/16054/2 "2025-04-09T21:39:33Z")

</div>

Actually came here to see if anyone had discussed this thoroughly. I’m coming from the `brms` R package world where it defaults to using the correlated intercept/slopes. However, I’m not seeing this in the posterior when using `bambi` so that’s making me think it doesn’t do this by default.

I saw this post from the `bambi` main developer @tcapretto [Mixed models and pm.LKJCholeskyCov](https://discourse.pymc.io/t/mixed-models-and-pm-lkjcholeskycov/7613) about incorporating the LKJ prior but I’m not sure if that made it all the way to implementation in `bambi`.

Would be interested to learn if I’m mistaken and this is what’s used by default before I move over to a PyMC implementation. Thanks to anyone ahead of time if you’re able to share!

---

<div class="post-metadata">

**Author:** ![tcapretto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/tcapretto/32/4902_2.png) [@tcapretto](https://discourse.pymc.io/u/tcapretto)\
**Post date:** [April 10, 2025, 9:38pm UTC](https://discourse.pymc.io/t/bambi-model-correlated-intercept-slopes/16054/3 "2025-04-10T21:38:06Z")

</div>

Just realized I never saw the original post here, sorry!

At the time random effects (apologies to those who hate the term 😄) were implemented in Bambi, it was not that straightforward to put a non-diagonal correlation matrix in PyMC. Or at least it was not that simple to me. So the default is to not use correlated intercept and slopes, and that explains why you can’t see them in the posterior.

I would like to revisit the topic so I can change this behavior (allowing users to turn it on and off), but I can’t promise any timelines.
