Education & Social Science
Education (HLM): schools → classes → students
Twenty schools with two to four classes each and Poisson-distributed class sizes. Test scores depend on prior score, student SES, teacher experience and school sector, plus school- and class-level random intercepts. Class and school means are aggregated back up from the student table.
schools (20) → classes (2–4) → students (Poisson ≈ 22, 10–32)
Data structure
Students in classes in schools is the original hierarchical linear model example (Raudenbush & Bryk) and still the most taught multilevel structure.
- schoolsone row = one school
- Sector, mean SES, a school random intercept u0, and the mean of its class means.
- classesone row = one class
- Teacher experience in years, a class random intercept, class size and the class mean score aggregated from students.
- studentsone row = one student
- Sex, SES (school mean plus individual deviation), prior score and the end-of-year test score clipped to 0–100.
Generate the schools / classes / students tables
The preset is a starting point: change any formula, noise step or row count and the preview, ER diagram and relation check update.
Key relationships
prior_score ~ Normal(mean = 50 + 4*ses, sd = 9)Distribution parameters are expressions too, so draws can depend on other columns.
test_score = 20 + 0.6*prior_score + 3*ses + 0.2*parent.teacher_exp_years + if(schools.sector == "private", 2, 0) + schools.u0 + parent.class_effectRandom intercepts at two levels: variance splits into school, class and residual parts.
class_mean = mean(students.test_score); school_mean = mean(classes.class_mean)Aggregates run after their inputs, so roll-ups can chain across levels.
Common pitfalls
- Single-level OLS on students: standard errors for school-level predictors such as sector are badly understated.
- Ecological fallacy: reading the school-mean SES slope as an individual-level effect.
- Not centering SES within school, which mixes within- and between-school effects.
- Taking an unweighted mean of class means when class sizes differ.
Typical analysis
Three-level random-intercept model; add a cross-level interaction (sector × SES) if it is of interest. Report the intraclass correlations for school and class.
library(lme4)
sc <- read.csv("schools.csv", comment.char = "#"); cl <- read.csv("classes.csv", comment.char = "#"); st <- read.csv("students.csv", comment.char = "#")
d <- merge(merge(st, cl, by = "class_id"), sc, by = "school_id")
fit <- lmer(test_score ~ prior_score + ses + teacher_exp_years + sector +
(1 | school_id/class_id), data = d)
summary(fit)Further reading
- Raudenbush SW, Bryk AS (2002). Hierarchical Linear Models, 2nd ed. Sage.
- Snijders TAB, Bosker RJ (2012). Multilevel Analysis, 2nd ed. Sage.
Other relational presets
- A/B test analytics : experiments (4) ⟷ exposures ⟷ users (1 500); users → sessions (Poisson 3) → events (Poisson, duration-dependent)
- Chemical kinetics : runs (12) → timepoints (15)
- Split-plot field trial : blocks (4) → main_plots (3 irrigation levels) → sub_plots (4 varieties) → quadrats (3)
- All presets and the formula reference