Model formula for Multilevel Model where different sets of predictors are used for random intercept and random slope

Hi, my question will be a more general question regarding the model formula. I have been searching for many web sources just to try to get any information on this issue. I am working on a two-level multilevel model.

Problem Background

In level one, I have one response variable with one predictor variable. Therefore, I have one intercept and one slope coefficient at this level.
In level two, I have one model that predicts the level-one intercept and another model that predicts the level-one slope. In my study, I will be using different predictors in both level-two models. Nearly all tutorials in the internet and the books I have read assume the same level-two predictors are being used to predict the level-one intercept and level-one slope.

Level One Model

y_{ij} = \beta_{0j} + \beta_{1j}\cdot x_{ij} + \varepsilon_{ij}

Level Two Model

\beta_{0j} = \gamma_{00} + \gamma_{01}\cdot z_j + u_{0j}
\beta_{1j} = \gamma_{10} + \gamma_{11} \cdot k_j + u_{1j}


How should I specify my model in r? (Packages in use: lmerTest, blmer, and brms. They all use the same model formulation)

What I Know

Null Model: This is simple. I think I have done it correctly.

y ~ (1 | j)

Random Intercept Model: I am pretty sure this is correct too. x will be a fixed effect predictor and this only allows the incept to be varying across the different groups.

y ~ x + (1 | j)

What I Don’t Know

How do I make a formula for random intercept and random slope and beyond? I know that when you have the same level-two predictors for both the intercept and the slope, it is

y ~ x + (z | j)

But to my understanding, this assumes z to be the level-two predictor of both Level-one intercept and slope. How do I formulate my model when the level-two predictors for level-one intercept and slope are different? I hope my question makes sense to everybody.


Please let me know what I should do to make the question clearer to you. I have recently been experimenting with brms package for a thorny model estimation issue. Thank you.

For the full model, I believe you want this: y ~ 1 + x + z + x:k + (1 + x | j)


Dear Solomon, I was super busy lately… I will try asap and will let you know. Do you know if any thoroughly written reference in this topic? I haven’t used x:k , but I assume it is the same with x * k ? Thank yo so much!

The : and * are similar, yet distinct. Try them both out with a simplified version of your model with just the lm() function. You’ll see.

1 Like

I tried with lm() only so far. It appears that it is only providing the interaction between two. It functions the same as I(x*z) ? I will try it with the brms soon. Thanks for your help.

In R formulas, the “*” operator implies main effects + inxn.

So, A*BA + B + A:B

Where A:B is indeed the same as I(A*B), the interaction.

If you take your model, and plug things in, you’ll see:

y_{ij} = \gamma_{00} + \gamma_{01}z_j + u_{0j} + x_{ij}(\gamma_{10} + \gamma_{11}k_j + u_{1j}) + \epsilon_{ij} \\ y_{ij} = \gamma_{00} + \gamma_{01}z_j + x_{ij}\gamma_{10} + \gamma_{11}x_{ij}k_j + u_{0j} + x_{ij}u_{1j} + \epsilon_{ij}

Which is the same as:

y ~ 1 + z + x + x:k + (1 + x|group)
1 Like

Thank you very much. It is very clear now. Thank you both!

1 Like