Hierarchical linear modeling, explained with an example

Hierarchical linear modeling for nested data: ICC, design effect, random intercepts and slopes, centering, lme4, SPSS and Stata, and APA reporting.

By the Twin Statistics teamUpdated 2026-11-158 min read

Hierarchical linear modeling (HLM), also called multilevel modeling or linear mixed modeling, is regression for nested data. You use it when observations are grouped, such as students in schools, patients in clinics or repeated measurements within people. It estimates effects at each level while correcting the standard errors for the fact that members of the same group tend to be alike.

What HLM is and when to use it

Ordinary regression assumes independent observations. Students in the same school share teachers and resources, so they are not independent. Ignore this, and standard errors are too small. HLM gives each group its own intercept, and if needed its own slope. Use it when:

  • Participants are clustered, such as employees in teams, nurses in units or students in classrooms.
  • You have repeated measures or longitudinal data, with time points nested in people.
  • Your research question involves variables at different levels, such as how school climate (level 2) relates to student achievement (level 1).

The terms "HLM", "multilevel model", "mixed-effects model" and "random-effects model" describe the same family of models. HLM is also the name of a specific software package. The standard textbooks are Raudenbush and Bryk (2002) and Snijders and Bosker (2012).

The model in two levels

For student i in school j, with a level-1 predictor X and a level-2 predictor W:

  • Level 1: Y_ij = β0j + β1j X_ij + r_ij
  • Level 2: β0j = γ00 + γ01 W_j + u0j
  • Level 2: β1j = γ10 + u1j

The γ terms are fixed effects: average intercepts and slopes across groups. The u terms are random effects: how much groups deviate from those averages. Their variances are written τ00 for intercepts and τ11 for slopes. The level-1 residual variance is σ².

Requirements and design checks

ICC and design effect

Start with a null (intercept-only) model. It splits the outcome variance into within-group (σ²) and between-group (τ00) parts. The intraclass correlation is:

ICC = τ00 / (τ00 + σ²)

The ICC is the share of variance that lies between groups. Even a small ICC matters when groups are large, because of the design effect (Kish, 1965):

Design effect = 1 + (m − 1) × ICC, where m is the average group size.

Divide your total N by the design effect to get the effective sample size if clustering were ignored.

Sample size at level 2

The number of groups matters more than the number of people per group for level-2 effects and variance components.

Number of groupsTypical consequence
Fewer than about 10Random effects poorly estimated; consider fixed effects for groups or cluster-robust methods
About 10–30Fixed effects can be fine with REML and small-sample corrections (McNeish & Stapleton, 2016); level-2 variance estimates are unstable
About 30–50Often adequate for fixed effects
50 or moreMaas and Hox (2005) found accurate estimates of level-2 standard errors from about 50 groups

These are guides, not rules. Cross-level interactions and random slopes need more groups than a simple random-intercept model.

Residuals at each level should also be roughly normal with constant variance.

Random intercepts vs random slopes

A random-intercept model lets each group have its own average outcome but forces the effect of X to be the same in every group.

A random-slope model also lets the effect of X vary across groups. Add a random slope when theory says the effect should differ by group, or when you test a cross-level interaction. A cross-level interaction asks whether a level-2 variable explains slope differences.

Random slopes add parameters, such as the slope variance and its covariance with the intercept. With few groups, they often fail to converge. Compare models with a likelihood-ratio test, and keep a slope when there is a substantive reason for it.

Centering: grand-mean vs group-mean

Centering changes what your coefficients mean, so decide on it deliberately (Enders & Tofighi, 2007).

ApproachHowWhat the level-1 slope means
Grand-mean centering (CGM)X − overall meanA blend of within- and between-group effects
Group-mean centering (CWC)X − group's own meanThe pure within-group effect
CWC plus group mean at level 2Add the group mean as a level-2 predictorSeparate within and between effects

Use group-mean centering when your question is about within-group relationships, such as whether students with higher SES than their classmates score higher. Add the group mean at level 2 to estimate the between-group effect. Use grand-mean centering when you mainly care about level-2 effects adjusted for level-1 covariates.

How to run HLM

R with lme4

The lme4 package (Bates, Mächler, Bolker & Walker, 2015) fits linear mixed models with lmer(). Load lmerTest as well to get p-values for fixed effects using Satterthwaite degrees of freedom.

library(lme4)
library(lmerTest)

# Group-mean centering
d$ses_mean <- ave(d$ses, d$school)
d$ses_cwc  <- d$ses - d$ses_mean

m0 <- lmer(math ~ 1 + (1 | school), data = d)                  # null model
m1 <- lmer(math ~ ses_cwc + ses_mean + (1 | school), data = d) # random intercept
m2 <- lmer(math ~ ses_cwc + ses_mean + (1 + ses_cwc | school), data = d) # random slope

summary(m2)
anova(m1, m2)   # likelihood-ratio test (refits with ML)

A "boundary (singular) fit" message usually means a variance or correlation was estimated at its limit. Often the random slope variance is near zero. Simplify the random part rather than ignoring the message.

SPSS MIXED

Use Analyze > Mixed Models > Linear. Put the grouping variable under Subjects, then set fixed and random effects in the following dialogs. The equivalent syntax:

MIXED math WITH ses_cwc ses_mean
  /FIXED=ses_cwc ses_mean
  /RANDOM=INTERCEPT ses_cwc | SUBJECT(school) COVTYPE(UN)
  /METHOD=REML
  /PRINT=SOLUTION TESTCOV.

Stata mixed

mixed math ses_cwc ses_mean || school: ses_cwc, covariance(unstructured) reml

After fitting a null model (mixed math || school:), estat icc gives the ICC.

How to interpret the output

  • Fixed effects read like regression coefficients. With group-mean centering, the ses_cwc coefficient is the within-school effect, and the ses_mean coefficient is the between-school effect. Their difference is the contextual effect.
  • Random effects are variances. A large intercept variance means schools differ in average outcome after the predictors. A slope variance means the effect of SES differs between schools.
  • Variance explained can be described as the proportional reduction in τ00 or σ² from the null model. Nakagawa and Schielzeth (2013) give marginal and conditional R² for mixed models.
  • Estimation: use REML for final variance estimates. Use ML when comparing models with different fixed effects.

Worked example

Example (invented data). A researcher studies math scores for 1,000 students in 50 schools (20 per school), with student SES as a predictor.

The null model gives τ00 = 4.2 and σ² = 25.8:

  • ICC = 4.2 / (4.2 + 25.8) = .14. Fourteen percent of the variance in math scores lies between schools.
  • Design effect = 1 + (20 − 1) × .14 = 3.66.
  • Effective sample size if clustering were ignored ≈ 1,000 / 3.66 ≈ 273.

Random-slope model with group-mean-centered SES and school mean SES:

EffectEstimateSEp
Fixed: intercept (γ00)50.120.34< .001
Fixed: SES, within school (γ10)2.150.21< .001
Fixed: school mean SES (γ01)4.800.95< .001
Random: intercept variance (τ00)2.60
Random: SES slope variance (τ11)0.35
Residual variance (σ²)24.10

Reading it: a student one SES unit above their school's average scores 2.15 points higher. A school one unit higher in mean SES scores 4.80 points higher on average. The contextual effect is 4.80 − 2.15 = 2.65. The intercept variance fell from 4.2 to 2.6, about 38% of the between-school variance.

How to report HLM in APA 7

Describe the model-building steps, the estimation method, the centering choices and the software. Then give a table of fixed and random effects.

A null model indicated that 14% of the variance in math scores lay between schools (ICC = .14). In the final model, estimated with REML in lme4, student SES (group-mean centered) predicted higher scores within schools, γ = 2.15, SE = 0.21, p < .001. School mean SES predicted higher school averages, γ = 4.80, SE = 0.95, p < .001.

Our APA results section guide covers table layout. For mediation or moderation inside multilevel data, see the mediation analysis and moderation analysis guides.

Common mistakes

  • Running ordinary regression on clustered data because the ICC "looked small".
  • Fitting random slopes with very few groups and ignoring convergence warnings.
  • Not stating or justifying the centering approach.
  • Comparing models with different fixed effects using REML likelihoods.
  • Reading a grand-mean-centered level-1 slope as a pure within-group effect.
  • Forgetting that a likelihood-ratio test of a variance is conservative, because the null value sits on the boundary.

Getting help

Multilevel models are a frequent topic in our statistical consulting sessions, from checking the design to interpreting lme4, SPSS or Stata output. You keep ownership of your model choices and write-up; we help you get them right and explain them. If you are not sure whether you need HLM at all, start with a free methods check.

FAQ

What is the difference between HLM and multilevel modeling?

They are the same family of models. "Hierarchical linear modeling" is common in education and the term used by Raudenbush and Bryk (2002). "Multilevel modeling" and "linear mixed models" are common in other fields and software. HLM is also the name of a software package.

When is the ICC large enough to need HLM?

There is no safe cutoff. The design effect depends on both the ICC and group size. An ICC of .05 with 30 people per group gives a design effect of 2.45. If your data are nested by design, a multilevel model is usually the defensible choice.

How many groups do I need for multilevel modeling?

More is better, and the number of groups matters more than group size. Maas and Hox (2005) found that about 50 groups gave accurate level-2 standard errors. Fewer can work for fixed effects with REML and small-sample corrections.

Should I use grand-mean or group-mean centering?

Use group-mean centering, plus the group mean at level 2, when you want to separate within-group and between-group effects. Use grand-mean centering when your focus is level-2 effects controlling for level-1 covariates. Enders and Tofighi (2007) give a clear account of the choice.

How do I calculate power for a multilevel study?

Standard tools such as G*Power assume independent observations. You can inflate a single-level sample size by the design effect as a rough guide, or use simulation for a proper estimate. Our G*Power sample size guide explains the single-level starting point.

Need help with this analysis?

A statistician can run, check and explain it with you. Fixed quote before we start.