Bambi model correlated intercept slopes

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

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 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!

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

At the time random effects (apologies to those who hate the term :smile:) 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.

1 Like