Multilevel Modeling

Fixed Slopes

Spring 2026 | CLAS | PSYC 894
Jeffrey M. Girard | Lecture 06b

Roadmap

  1. Fixed Slopes for L2
    • Specifications and Examples
  2. Fixed Slopes for L1
    • Specifications and Examples

Fixed Slopes for L2

L2 Predictors

  • Random intercepts partition the \(y\) variance by level
    • By adding predictor variables to MLMs, we can
      try to explain/predict those variance partitions
  • L2 predictors \((z)\) will try to explain variance at L2
    • For now, we are focusing on explaining the intercept variance.
    • i.e., Which clusters have higher or lower intercepts?
    • e.g., Do clusters with higher \(z\) have higher average \(y\)?
  • Crucial Concept: Because \(z\) is measured at the cluster level, it has no variance within a cluster. Therefore, it has a single, fixed effect across the entire population.

Review: The Null Model

Before we add complexity, let’s recall our baseline null model.

Level One (Observation)

\[ y_{ij} = \beta_{0j} + \color{#4daf4a}{e_{ij}}, \qquad \color{#4daf4a}{e_{ij}} \sim \text{Normal}(0, \color{#4daf4a}{\sigma}) \]

Level Two (Cluster)

\[ \beta_{0j} = \color{#377eb8}{\gamma_{00}} + \color{#e41a1c}{u_{0j}}, \qquad \color{#e41a1c}{u_{0j}} \sim \text{Normal}(0, \color{#e41a1c}{\tau_{00}}) \]

Adding a Level 2 Predictor

Level One (Observation)

\[ y_{ij} = \beta_{0j} + \color{#4daf4a}{e_{ij}}, \qquad \color{#4daf4a}{e_{ij}} \sim \text{Normal}(0, \color{#4daf4a}{\sigma}) \]

Level Two (Cluster)

\[ \beta_{0j} = \color{#377eb8}{\gamma_{00}} + \color{#377eb8}{\gamma_{01}} z_j + \color{#e41a1c}{u_{0j}}, \qquad \color{#e41a1c}{u_{0j}} \sim \text{Normal}(0, \color{#e41a1c}{\tau_{00}}) \]

  • \(\color{#377eb8}{\gamma_{00}}\) is the Fixed Intercept (The expected cluster average when \(z_j = 0\))

  • \(\color{#377eb8}{\gamma_{01}}\) is the Fixed Slope (Main effect of L2 predictor \(z\))

  • Notice that \(z_j\) is in the L2 equation and predicts the cluster average \((\beta_{0j})\)

Decoding the Gammas

Why \(\gamma_{00}\) and \(\gamma_{01}\)? The subscripts follow a specific rule:

  1. The first number says which Level 1 parameter is being predicted.
    • 0 = The Intercept \((\beta_{0j})\)
  2. The second number says which Level 2 predictor is doing the predicting.
    • 0 = The base intercept predictor (essentially a column of 1s)
    • 1 = The first L2 variable you added \((z_j)\)

Putting it together:

  • \(\gamma_{00}\) is the base intercept for the Level 1 intercept.
  • \(\gamma_{01}\) is the effect of predictor 1 on the Level 1 intercept.
  • If we had more L2 predictors, we would add \(\gamma_{02}\), \(\gamma_{03}\), and so on…

Reading the Level 2 Equation

\[ \beta_{0j} = \color{#377eb8}{\gamma_{00}} + \color{#377eb8}{\gamma_{01}} z_j + \color{#e41a1c}{u_{0j}}, \qquad \color{#e41a1c}{u_{0j}} \sim \text{Normal}(0, \color{#e41a1c}{\tau_{00}}) \]

\(\beta_{0j}\) is the Conditional Cluster Average for cluster \(j\) and equals…
…the Intercept \((\color{#377eb8}{\gamma_{00}})\) (the expected average when \(z_j=0\)) plus the Effect of Predictor \(z\) \((\color{#377eb8}{\gamma_{01}} z_j)\) plus the Cluster Deviation \((\color{#e41a1c}{u_{0j}})\).
These deviations are normally distributed with 0 mean and \(\tau_{00}\) SD.

Note

Notice the Shift: In the null model, \(\gamma_{00}\) was the grand population average. Now that we have a predictor, \(\gamma_{00}\) is specifically the expected average for a cluster where \(z_j = 0\).

Students in Schools Example

\[ \begin{align} \text{MATH}_{ij} &= \beta_{0j} + \color{#4daf4a}{e_{ij}} \\ \beta_{0j} &= \color{#377eb8}{\gamma_{00}} + \color{#377eb8}{\gamma_{01}} \text{PUBLIC}_j + \color{#e41a1c}{u_{0j}} \end{align} \]

  • \(\gamma_{00}\) is the fixed intercept: what is the expected average math score for a private school?
  • \(\gamma_{01}\) is the fixed slope of PUBLIC: do public schools have higher average math scores than private schools? ## Path Diagram

Mixed (or Reduced) Equation

\[ \begin{alignat}{2} &\text{L1:} \quad & y_{ij} &= \beta_{0j} + \color{#4daf4a}{e_{ij}} \\ &\text{L2:} \quad & \beta_{0j} &= \color{#377eb8}{\gamma_{00}} + \color{#377eb8}{\gamma_{01}} z_j + \color{#e41a1c}{u_{0j}} \end{alignat} \]

Substitute the L2 equation directly into the L1 equation:

\[ y_{ij} = (\color{#377eb8}{\gamma_{00}} + \color{#377eb8}{\gamma_{01}} z_j + \color{#e41a1c}{u_{0j}}) + \color{#4daf4a}{e_{ij}} \]

Rearrange to group the fixed and random parts:

\[ y_{ij} = \underbrace{\color{#377eb8}{\gamma_{00}} + \color{#377eb8}{\gamma_{01}} z_j}_{\text{Fixed}} + \underbrace{\color{#e41a1c}{u_{0j}}}_{\text{Random}} + \color{#4daf4a}{e_{ij}} \]

This mixed equation is the basis of the R formula.

Formula

\[ y_{ij} = \underbrace{\color{#377eb8}{\gamma_{00}} + \color{#377eb8}{\gamma_{01}} z_j}_{\text{Fixed}} + \underbrace{\color{#e41a1c}{u_{0j}}}_{\text{(Random)}} + \color{#4daf4a}{e_{ij}} \]

Generic

y ~ 1 + z + (1 | cluster)

where z is a predictor you want a fixed slope
for and the rest is the same as last time

Example

math ~ 1 + public + (1 | school)

Setup

library(tidyverse)
library(easystats)
library(lme4)

dat <- 
  read_csv("heck2011.csv") |> 
  mutate(school = factor(school), public = factor(public))
glimpse(dat)
Rows: 6,871
Columns: 7
$ school  <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,…
$ student <dbl> 6701, 6702, 6703, 6704, 6705, 6706, 6707, 6708, 6709, 6710, 67…
$ female  <dbl> 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ ses     <dbl> 0.586, 0.304, -0.544, -0.848, 0.001, -0.106, -0.330, -0.891, 0…
$ math    <dbl> 47.1400, 63.6100, 57.7100, 53.9000, 58.0100, 59.8700, 62.5556,…
$ puniv   <dbl> 0.08333333, 0.08333333, 0.08333333, 0.08333333, 0.08333333, 0.…
$ public  <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…

Fitting the Model

fit <- lmer(
  formula = math ~ 1 + public + (1 | school),
  data = dat,
  REML = TRUE
)
model_parameters(fit)
# Fixed Effects

Parameter   | Coefficient |   SE |         95% CI | t(6867) |      p
--------------------------------------------------------------------
(Intercept) |       57.48 | 0.36 | [56.77, 58.18] |  159.54 | < .001
public [1]  |        0.27 | 0.42 | [-0.55,  1.09] |    0.64 | 0.521 

# Random Effects

Parameter              | Coefficient
------------------------------------
SD (Intercept: school) |        3.27
SD (Residual)          |        8.16

Interpreting the Fixed Effects

  • The fixed intercept \((\color{#377eb8}{\gamma_{00}})\) is the Conditional Base Rate
    • The expected average math score when our predictor equals zero.
    • Since public is dummy coded (0 = Private, 1 = Public), this is the average math score of a typical private school.
  • The fixed slope of public \((\color{#377eb8}{\gamma_{01}})\) is the Main Effect
    • The expected change in the school average for a 1-unit increase in the predictor. This is the estimated difference in average math scores between public and private schools.
    • Significance: Because \(p > .05\), this difference is not statistically significant. We lack evidence to say public and private schools differ in their population averages.

Interpreting the Random Effects

  • The random intercept SD \((\color{#e41a1c}{\tau_{00}})\) is the Conditional Between-Cluster Spread
    • The spread of average math scores between schools (specifically, the spread of the intercepts).
    • Crucial update: This is the spread after accounting for a school’s public or private status.
  • The L1 residual SD \((\color{#4daf4a}{\sigma})\) is the Within-Cluster Spread
    • The spread of students’ math scores within their respective schools.
    • This represents how much individual students deviate from their school’s conditional average.

Deriving More Information

We can extract the residuals and group-level estimates from our fitted model

e <- get_residuals(fit) # e_ij
u <- estimate_grouplevel(fit, type = "random") # u_0j
b <- estimate_grouplevel(fit, type = "total") # beta_0j
# Preview the random intercept deviations
head(u)
Group  | Level | Parameter   | Coefficient |   SE |          95% CI
-------------------------------------------------------------------
school | 1     | (Intercept) |        1.00 | 1.91 | [ -2.74,  4.74]
school | 2     | (Intercept) |        4.16 | 1.86 | [  0.52,  7.81]
school | 3     | (Intercept) |       -7.36 | 1.66 | [-10.61, -4.11]
school | 4     | (Intercept) |        5.99 | 1.69 | [  2.68,  9.31]
school | 5     | (Intercept) |       -6.85 | 1.69 | [-10.17, -3.54]
school | 6     | (Intercept) |        2.56 | 1.73 | [ -0.83,  5.95]

The Conditional ICC

  • The Unconditional ICC (from the null model) tells us the proportion of total variance situated between clusters. Usually this is what you want to report.
  • The Conditional ICC (from this model) tells us the proportion of remaining variance situated between clusters, after accounting for our predictors.
icc(fit, ci = 0.95)
# Intraclass Correlation Coefficient

    Adjusted ICC: 0.138 [0.118, 0.161]
  Unadjusted ICC: 0.138 [0.118, 0.161]

Why does this matter?

Adding predictors changes the variance components! If we add a strong L2 predictor, the L2 variance drops, and the conditional ICC shrinks. This can be misleading!

Which ICC Should You Report?

If you are only going to report one, report the Unconditional ICC.

  • The Unconditional ICC is the standard.
    • It tells the reader how much clustering exists in the raw data.
    • It justifies your decision to use a multilevel model in the first place.
    • If a paper simply says “The ICC was .15,” this is what they mean.
  • The Conditional ICC is a stepping stone.
    • It is rarely reported as a standalone final metric.
    • Instead, we use it to figure out how much variance our predictors explained (by comparing it back to the null model).
    • We will learn how to calculate this “variance explained” (Pseudo-\(R^2\))

Plotting the Marginal Effect

rel <- estimate_relation(fit, by = "public")
plot(rel)

Why not just a t-test?

Looking at that plot, you might wonder: Couldn’t we just run a simple t-test comparing math scores of public vs. private school students?

No! And here is why:

fit0 <- lm(math ~ public, data = dat)
compare_parameters(fit0, fit, select = "se")
Parameter    |         fit0 |          fit
------------------------------------------
(Intercept)  | 57.58 (0.20) | 57.48 (0.36)
public [1]   |  0.21 (0.24) |  0.27 (0.42)
------------------------------------------
Observations |         6871 |         6871
  • Ignores Clustering: The standard model (fit0) assumes all students are completely independent.
  • Shrinks Standard Errors: Notice the artificially tiny SE for public in fit0. This false confidence drastically increases our risk of false positives.

Fixed Slopes for L1

L1 Predictors

  • Just like L2 predictors explain variance between clusters, L1 predictors try to explain variance within clusters.
  • For now, we are focusing on adding a fixed slope at Level 1.
    • We assume the same effect of our L1 predictor across all clusters.
  • When is this assumption reasonable?
    • Theoretically: When we have no strong reason to believe the relationship changes based on the cluster context.
    • Pragmatically: As a baseline model-building step before testing random slopes, or when a random slopes model fails to converge.
    • e.g., Does a student’s sex predict their math score, assuming this relationship is identical at every school?

Adding a Level 1 Predictor

Let \(x_{ij}\) be the value of L1 predictor \(x\) for observation \(i\) in cluster \(j\).

Level One (Observation)

\[ y_{ij} = \beta_{0j} + \beta_{1j} x_{ij} + \color{#4daf4a}{e_{ij}}, \qquad \color{#4daf4a}{e_{ij}} \sim \text{Normal}(0, \color{#4daf4a}{\sigma}) \]

Level Two (Cluster)

\[ \begin{align} \beta_{0j} &= \color{#377eb8}{\gamma_{00}} + \color{#e41a1c}{u_{0j}}, \qquad \color{#e41a1c}{u_{0j}} \sim \text{Normal}(0, \color{#e41a1c}{\tau_{00}}) \\ \beta_{1j} &= \color{#377eb8}{\gamma_{10}} \end{align} \]

  • \(\color{#377eb8}{\gamma_{00}}\) is the Fixed Intercept (The expected cluster average when \(x_{ij} = 0\))
  • \(\beta_{1j}\) is the L1 Slope for predictor \(x\) in cluster \(j\)
  • \(\color{#377eb8}{\gamma_{10}}\) is the Fixed L1 Slope (The overall main effect of \(x\))

Decoding the Gammas

Let’s use our rule again for the new fixed effect:

  1. The first number says which Level 1 parameter is being predicted.
    • 0 = The Intercept \((\beta_{0j})\)
    • 1 = The L1 slope \((\beta_{1j})\)
  2. The second number says which Level 2 predictor is doing the predicting.
    • 0 = The base intercept (no L2 predictors are predicting this slope yet)

Putting it together:

  • \(\gamma_{10}\) is the baseline effect of our Level 1 predictor.

Reading the Equations

\[ y_{ij} = \beta_{0j} + \beta_{1j} x_{ij} + \color{#4daf4a}{e_{ij}}, \qquad \color{#4daf4a}{e_{ij}} \sim \text{Normal}(0, \color{#4daf4a}{\sigma}) \]

\(y_{ij}\) is the outcome for observation \(i\) in cluster \(j\) and equals…
…the Cluster Average \((\beta_{0j})\) (the expected average when \(x_{ij}=0\)) plus the Effect of Predictor \(x\) \((\beta_{1j} x_{ij})\) plus the Observation Deviation \((\color{#4daf4a}{e_{ij}})\).

\[ \begin{align} \beta_{0j} &= \color{#377eb8}{\gamma_{00}} + \color{#e41a1c}{u_{0j}}, \qquad \color{#e41a1c}{u_{0j}} \sim \text{Normal}(0, \color{#e41a1c}{\tau_{00}}) \\ \beta_{1j} &= \color{#377eb8}{\gamma_{10}} \end{align} \]

\(\beta_{1j}\) is the Cluster Slope for cluster \(j\) and equals…
…the Fixed Population Slope \((\color{#377eb8}{\gamma_{10}})\).
We are assuming the effect of \(x\) is identical across all clusters.

Students in Schools Example

\[ \begin{align} \text{MATH}_{ij} &= \beta_{0j} + \beta_{1j} \text{FEMALE}_{ij} + \color{#4daf4a}{e_{ij}} \\ \beta_{0j} &= \color{#377eb8}{\gamma_{00}} + \color{#e41a1c}{u_{0j}} \\ \beta_{1j} &= \color{#377eb8}{\gamma_{10}} \end{align} \]

  • \(\gamma_{00}\) is the fixed intercept: what is the expected math score for a male student (where female = 0)?
  • \(\gamma_{10}\) is the fixed slope of FEMALE: do female students have different math scores than male students, assuming this relationship is identical at every school? ## Path Diagram

Mixed (or Reduced) Equation

\[ \begin{alignat}{2} &\text{L1:} \quad & y_{ij} &= \beta_{0j} + \beta_{1j} x_{ij} + \color{#4daf4a}{e_{ij}} \\ &\text{L2:} \quad & \beta_{0j} &= \color{#377eb8}{\gamma_{00}} + \color{#e41a1c}{u_{0j}} \\ & & \beta_{1j} &= \color{#377eb8}{\gamma_{10}} \end{alignat} \]

Substitute the L2 equations directly into the L1 equation:

\[ y_{ij} = (\color{#377eb8}{\gamma_{00}} + \color{#e41a1c}{u_{0j}}) + (\color{#377eb8}{\gamma_{10}}) x_{ij} + \color{#4daf4a}{e_{ij}} \]

Rearrange to group the fixed and random parts:

\[ y_{ij} = \underbrace{\color{#377eb8}{\gamma_{00}} + \color{#377eb8}{\gamma_{10}} x_{ij}}_{\text{Fixed}} + \underbrace{\color{#e41a1c}{u_{0j}} + \color{#4daf4a}{e_{ij}}}_{\text{Random}} \]

This mixed equation is the basis of the R formula.

Formula

Generic

y ~ 1 + x + (1 | cluster)

where x is an L1 predictor you want a fixed slope
for and the rest is the same as the null model

Example

math ~ 1 + female + (1 | school)

Fitting the Model

fit <- lmer(
  formula = math ~ 1 + female + (1 | school),
  data = dat,
  REML = TRUE
)
model_parameters(fit)
# Fixed Effects

Parameter   | Coefficient |   SE |         95% CI | t(6867) |      p
--------------------------------------------------------------------
(Intercept) |       58.28 | 0.21 | [57.86, 58.70] |  272.97 | < .001
female      |       -1.21 | 0.20 | [-1.61, -0.81] |   -5.94 | < .001

# Random Effects

Parameter              | Coefficient
------------------------------------
SD (Intercept: school) |        3.24
SD (Residual)          |        8.14

Interpreting the Fixed Effects

  • The fixed intercept \((\color{#377eb8}{\gamma_{00}})\)
    • The expected math score for a male student (female = 0) in a typical school.
  • The fixed slope of female \((\color{#377eb8}{\gamma_{10}})\)
    • The expected difference in math scores for female students compared to male students.
    • Significance: Because \(p < .001\), this slope is statistically significant.

Interpreting the Random Effects

  • The random intercept SD \((\color{#e41a1c}{\tau_{00}})\) is the Conditional Between-Cluster Spread
    • The spread of average math scores between schools (specifically, the spread of the intercepts).
    • This is the spread after accounting for student gender.
  • The L1 residual SD \((\color{#4daf4a}{\sigma})\) is the Conditional Within-Cluster Spread
    • The spread of students’ math scores within their respective schools.
    • This represents how much individual students deviate from their expected score after accounting for their gender.

Deriving More Information

We can extract the individual residuals and cluster deviations as before.

e <- get_residuals(fit) # e_ij
u <- estimate_grouplevel(fit, type = "random") # u_0j
b <- estimate_grouplevel(fit, type = "total") # beta_0j
  • We can also calculate the Conditional ICC, which tells us the proportion of remaining variance between clusters after accounting for student sex.
icc(fit, ci = 0.95)
# Intraclass Correlation Coefficient

    Adjusted ICC: 0.137 [0.117, 0.155]
  Unadjusted ICC: 0.136 [0.116, 0.155]

Plotting the Marginal Effect

rel <- estimate_relation(fit, by = "female")
plot(rel)

Visualizing a Fixed Slope

Because we only estimated a random intercept and a fixed slope, our model assumes the effect of being female on math scores is identical across all schools. Notice how the lines (one per cluster/school) are parallel!

Putting It All Together

In practice, applied researchers usually include both L1 and L2 predictors in the same model. We can combine our previous equations into one full model. \[ y_{ij} = \underbrace{\color{#377eb8}{\gamma_{00}} + \color{#377eb8}{\gamma_{01}} \mathit{PUBLIC}_j + \color{#377eb8}{\gamma_{10}} \mathit{FEMALE}_{ij}}_{\text{Fixed}} + \underbrace{\color{#e41a1c}{u_{0j}} + \color{#4daf4a}{e_{ij}}}_{\text{Random}} \]

math ~ 1 + public + female + (1 | school)

Why do this?

  1. Simultaneous Control: We can estimate the effect of school type while controlling for student gender (and vice versa).
  2. Explaining Variance: + \(\mathit{PUBLIC}_j\) tries to explain variance between schools (reducing \(\tau_{00}\))
    • \(\mathit{FEMALE}_{ij}\) tries to explain variance within schools (reducing \(\sigma\))

Combined Path Diagram

Fitting the Combined Model

fit_combined <- lmer(
  formula = math ~ 1 + public + female + (1 | school),
  data = dat,
  REML = TRUE
)
model_parameters(fit_combined)
# Fixed Effects

Parameter   | Coefficient |   SE |         95% CI | t(6866) |      p
--------------------------------------------------------------------
(Intercept) |       58.12 | 0.37 | [57.38, 58.85] |  155.20 | < .001
public [1]  |        0.23 | 0.42 | [-0.59,  1.05] |    0.54 | 0.588 
female      |       -1.21 | 0.20 | [-1.60, -0.81] |   -5.93 | < .001

# Random Effects

Parameter              | Coefficient
------------------------------------
SD (Intercept: school) |        3.25
SD (Residual)          |        8.14

Interpreting the Combined Model

  • The Fixed Effects (Gammas)
    • The intercept is now the expected math score for a male student in a private school.
    • The slope for public is the difference between public and private schools, controlling for student gender.
    • The slope for female is the difference between female and male students, controlling for school type.
  • The Random Effects (Variance Components)
    • \(\color{#e41a1c}{\tau_{00}}\) has decreased compared to the null model because public explained some of the between-school variance.
    • \(\color{#4daf4a}{\sigma}\) has decreased because female explained some of the within-school variance.