Hierarchical linear modeling (HLM) is a form of regression built for nested data, where each observation sits inside a larger group, like students inside classrooms inside schools. You'll also see it called multilevel modeling or mixed-effects modeling. Same method, different names, depending on which field trained you. This guide covers why ordinary regression breaks on grouped data, the level 1 and level 2 equations (every term in plain English), fixed versus random effects, the intraclass correlation, and a worked example you can run yourself.
What is hierarchical linear modeling (HLM)?
Hierarchical linear modeling is a regression technique for data that comes in layers. Instead of one flat table of independent rows, the rows are grouped, and the grouping carries information you don't want to throw away.
The classic example is schools. Students (the bottom layer, or level 1) are nested inside schools (level 2). Two kids in the same school share a principal, a budget, a neighborhood, and a hundred other things, so their test scores tend to look more alike than two kids picked at random from different schools. Berkeley's D-Lab uses exactly this setup in its introduction to the method.
Nesting shows up far beyond classrooms:
- Patients in hospitals: recovery times cluster by the hospital's staffing and protocols.
- Employees in departments: satisfaction scores cluster by manager and team culture.
- Repeated measurements over time: several readings from the same person are nested within that person, which is how HLM handles longitudinal and growth data.
If your data has a grouping like this, HLM is one of the standard tools for it, and it belongs in the same family as other statistical analysis methods you may already use. The names shift by discipline (education researchers say hierarchical linear models, statisticians say mixed-effects models, Wikipedia files it under multilevel models), but they describe the same math.
Why not just use ordinary regression?
Ordinary least squares regression makes one big assumption: every observation is independent of every other one. Nested data quietly breaks that assumption, and the damage is easy to miss.
When students share a school, their scores are correlated. Your 500 students don't really give you 500 independent pieces of information, because kids inside a school partly echo each other. The data acts like a smaller sample than the row count suggests.
💡 Note: Correlated observations shrink your effective sample size. OLS still divides by the raw count, so it thinks it has more evidence than it does.
That mismatch has a specific consequence. OLS underestimates the standard errors, which makes t-values look bigger than they should and turns ordinary effects into "statistically significant" ones. You end up confident about relationships that the data doesn't actually support. Berkeley's D-Lab makes the same point: ordinary regression fails to account for clustering, so it needs a model that does.
HLM also handles something OLS can't: predictors that live at different levels. A study of math scores might include a student-level variable (hours studied) and a school-level variable (student-teacher ratio) in the same model. That mix of levels is awkward in flat regression and natural in HLM. The same logic extends to repeated measures, which is why GLM repeated measures problems often get reframed as multilevel models.
How HLM works: the level 1 and level 2 equations
HLM writes one equation per level and links them together. Read the subscripts as "student i in school j," and the rest falls into place.
The level 1 (within-group) equation describes individuals:
Yij = β0j + β1j·Xij + eij
- Yij: the outcome for student i in school j (say, a math score).
- Xij: a student-level predictor, like hours studied.
- β0j: school j's own baseline (its intercept). Notice the j, every school gets its own.
- β1j: how much the predictor moves the outcome inside school j.
- eij: the individual residual, the part of this student's score that the model doesn't explain.
The level 2 (between-group) equation then explains where each school's baseline comes from:
β0j = γ00 + u0j
- γ00: the grand mean, the average intercept across all schools.
- u0j: how far school j sits above or below that average. This is the piece that makes the model "multilevel."
When you have a school-level predictor like the student-teacher ratio (call it Wj), it goes into the level 2 equation: β0j = γ00 + γ01·Wj + u0j. GeeksforGeeks lays out the same two-equation structure.
The key move is that the intercept (and, if you want, the slope) is allowed to vary from group to group, and HLM estimates the whole stack at once rather than running a separate regression per school.
Fixed effects vs random effects
Two terms do a lot of work in HLM, and most of the confusion around the method lives here. The split is simpler than it sounds.
Fixed effects are the parts held constant across every group. They're the γ terms, the overall averages, like the average effect of an extra study hour across all schools. You get one estimate, and it applies everywhere.
Random effects are the parts allowed to vary by group. They're the u terms, and they come in two common flavors:
- Random intercepts: each group starts at its own baseline. Some schools are higher-scoring than others before you account for anything else.
- Random slopes: a predictor's effect changes by group. An extra study hour might help more in one school than another.
Most analyses start with random intercepts, then add random slopes only when there's a reason to think the effect genuinely differs across groups. Wikipedia's rundown of random-intercept, random-slope, and combined models follows the same ladder.
The intraclass correlation (ICC): do you even need HLM?
Before you reach for HLM, it helps to know whether the grouping matters at all. The intraclass correlation coefficient (ICC) answers that in a single number.
The ICC is the share of total variance that sits between groups:
ICC = between-group variance / (between-group variance + within-group variance)
It runs from 0 to 1. Near 0 means groups barely differ, and ordinary regression is probably fine. Closer to 1 means a lot of the action is between groups, and you want a model that accounts for it.
In Berkeley's PISA example, the ICC comes out to about 0.19, so roughly 19% of the variation in math scores lives between schools rather than within them. That's more than enough clustering to justify HLM.
💡 Tip: There's no universal cutoff, but an ICC that rounds to zero is a signal you can skip the extra machinery. Anything in the double digits of percent usually earns the multilevel model.
A worked example: fitting a multilevel model
Say you have math scores for thousands of students across a few hundred schools, plus hours studied per student. You want the effect of study time without letting school-to-school differences fool you.
In R, the lme4 package does it in one line. The (1 | school) part is what adds the random intercept per school:
library(lme4)
model <- lmer(math ~ 1 + studytime + (1 | school), data = students)
summary(model)Python gets you the same model through statsmodels, which is a handy pattern to keep near your other R and Python statistical workflows:
import statsmodels.formula.api as smf
model = smf.mixedlm("math ~ studytime", data, groups=data["school"]).fit()Read the output in three passes. The fixed effect for study time is the average score bump per study hour across all schools. The random-intercept variance tells you how much baseline math differs school to school. Divide that between-school variance by the total, and you're back to the ICC from the last section.
You don't need an R environment to try this. In Julius, you can upload the nested dataset, ask for "a random-intercept model of math score on study time, grouped by school," and read back the fixed effects and the by-school variance in plain language. It runs the same mixed model under the hood, so the output lines up with the code above.
The assumptions behind HLM
HLM relaxes one OLS assumption, but it doesn't run assumption-free. The published lists (see this NIH-indexed overview and the GeeksforGeeks diagnostics section) come down to four checks.
- Linearity: the modeled relationships are linear in their parameters.
- Normality: the residuals at each level are roughly normal. The level 1 errors (eij) and the level 2 random effects (u0j) each sit around a mean of zero.
- Equal variance (homoscedasticity): residual variance stays about constant across groups.
- Independence of random effects: the group-level effects (u0j) are independent across clusters, and predictors are independent of the error at their own level.
This is where the old framing trips people up. HLM still relies on independence; it just applies it at a higher level. Individual observations inside a cluster are allowed to correlate, because the model captures the cluster structure directly. Independence then applies to the random effects across clusters, which is where the assumption actually lives. For the familiar single-level version of these checks, the assumptions of multiple linear regression are a useful companion read.
💡 Note: Check normality with a Q-Q plot or a residual histogram at each level. A plot per level takes a minute and catches most problems.
HLM vs hierarchical regression: not the same thing
These two get mixed up constantly, and the only thing they share is the word "hierarchical."
Hierarchical regression (also called sequential regression) is a single-level technique. You add predictors in blocks and watch how much the R² climbs at each step, to see whether a new set of variables earns its place. The data is flat. Nothing is nested.
Hierarchical linear modeling is about nested data and random effects, as we've covered throughout this guide. Its "hierarchy" lives in the data structure, students sitting inside schools, while hierarchical regression's "hierarchy" is just the order you feed in variables.
So if someone says they ran a "hierarchical" model, it's worth asking which one they mean. One is a variable-selection habit in plain regression; the other is a different model class for grouped data. If you're also weighing latent-variable approaches, our summary of structural equation modeling covers a third branch people sometimes confuse with both.
Where researchers and teams use HLM
HLM earns its keep anywhere data comes in groups. A few of the common homes for it:
- Education: how student traits like study time or socioeconomic status affect test scores, once you account for the school.
- Longitudinal and growth studies: how individuals change over time, with repeated measurements nested inside each person. A longitudinal study in athletic training used HLM this way, as an alternative to repeated-measures ANOVA
- Political science: how demographics and geography shape voting patterns across regions.
- Business analytics: repeated KPI readings nested within accounts, or customers nested within regions, where a marketer or analyst wants estimates that respect the grouping instead of averaging it away.
That last one is where a lot of business teams meet HLM without naming it. Any time you've got repeated measures per customer or per store, you're in multilevel territory.
Run your first multilevel model without the setup
Hierarchical linear modeling is worth the effort, but the effort is real: installing R or Python, wrangling the data into shape, remembering the lmer syntax, and reading a dense summary table. If the modeling is a means to an end, that setup is pure overhead.
Julius is an AI-powered data analysis tool, the pocket data scientist that runs the stats for you. We built it so you can ask for an analysis in plain English and get the model, the coefficients, and the charts back, without writing the code yourself.
Here's how Julius helps:
- Natural-language modeling: Ask for a random-intercept model grouped by school, and Julius sets it up, runs it, and explains the fixed and random effects in words.
- Real Python and R under the hood: Julius runs actual statistical code in a sandbox and can retry and fix its own errors, so you're not stuck with a single-shot guess.
- Direct connections: Pull your nested data straight from PostgreSQL, Snowflake, or BigQuery, or just upload a CSV.
- Built-in visualization: See the by-group effects as charts on the spot, so the variance between clusters shows up as something you can actually look at.
Ready to skip the setup and go straight to the model? Try Julius for free today.
Frequently asked questions
What is HLM in simple terms?
HLM is regression for grouped data, like students inside schools or repeated measurements inside one person. It estimates the usual relationships while accounting for the fact that observations in the same group are more alike than observations picked at random.
Is hierarchical linear modeling the same as multilevel or mixed-effects modeling?
Yes. Hierarchical linear modeling, multilevel modeling, and mixed-effects modeling are three names for the same method. The label you see usually depends on the field, education researchers, statisticians, and data scientists each picked a different one.
How is HLM different from hierarchical (sequential) regression?
Hierarchical regression is a single-level technique where you add predictor blocks in steps to see how much R² improves. HLM models nested data with random effects. They share the word "hierarchical" and nothing else.
When do I actually need HLM instead of ordinary regression?
Check the intraclass correlation. If the ICC is near zero, the grouping barely matters and ordinary regression is fine. If a meaningful share of the variance (Berkeley's example hit about 19%) sits between groups, use HLM.
What software runs HLM?
R (the lme4 package) and Python (statsmodels) are the common choices, and specialized tools like HLM software exist too. You can also run a multilevel model conversationally in Julius by uploading your data and describing the model you want.