MockDataLab

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.

The relational generator loads as it scrolls into view…

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_effect

    Random 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.

R
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