Tutorials › Regression Analysis (Undergraduate) › Worked Analysis of Students Clustered in Schools

Grouped Data and Dependence Awareness · Tutorial 890 of 991

Worked Analysis of Students Clustered in Schools

Work through a student-level regression with schools as clusters, comparing conventional and cluster-robust uncertainty and examining what school count and size contribute.

Advanced 10 min read

What You'll Learn

  • Set up and interpret a student-outcome mean regression with a school-level clustering structure.
  • Calculate conventional and uncorrected cluster-robust standard errors from a small hypothetical data set.
  • Explain why cluster-robust uncertainty can differ even when the fitted coefficient is unchanged.
  • Distinguish the number of students from the number of independent schools informing inference.
  • Calculate how unequal school sizes affect a student-weighted regression coefficient.
  • Identify why a cluster-robust standard error alone does not solve problems caused by very few schools.

Why Schools Matter in a Student Regression

Suppose a researcher models students’ test scores using their weekly tutoring hours. Students from the same school may share teachers, schedules, or other influences, so their regression errors need not be independent. Ignoring that grouping can make the reported uncertainty too small, even if the fitted regression line itself is unchanged. This worked analysis separates the mean model—the relationship being estimated—from the uncertainty calculation that accounts for school clustering.

Let \(i\) index students and \(g\) index schools. Write \(Y_{ig}\) for student \(i\)’s outcome and \(x_{ig}\) for a predictor. For the examples, \(Y_{ig}\) is a test score in points and \(x_{ig}\) is tutoring hours per week. The regression describes an average outcome conditional on the predictor; by itself, it does not establish that tutoring causes a change in scores.

Definition: A student-level linear mean model with school clusters is $$ Y_{ig}=\beta_0+\beta_1x_{ig}+u_{ig}, $$ where \(\beta_0+\beta_1x_{ig}\) is the conditional mean of the outcome under the model and \(u_{ig}\) is the error. The coefficient \(\beta_1\) is the change in mean test score, in points, associated with one additional weekly tutoring hour. Students within a school may have dependent errors; the working assumption for cluster-robust inference is that distinct schools are independent, with the errors having mean zero conditional on the predictors and finite variance. The mean model must also be an appropriate description of the conditional mean.

Ordinary least squares (OLS) estimates \(\beta_0\) and \(\beta_1\) by minimizing the sum of squared student-level residuals. Cluster-robust standard errors do not change that OLS fitting criterion or its coefficient estimates. Instead, they allow errors within a school to be related when estimating the coefficient uncertainty. The conventional OLS standard error additionally relies on independent errors with a common variance across students.

Worked Analysis: Fitting the Mean Model

Worked Example: Tutoring Hours and Test Scores Across Four Schools

Consider hypothetical scores for 12 students in four schools. Each school has three students, with \(x=0,1,2\) tutoring hours per week. The values are deliberately small so that both uncertainty calculations can be checked by hand.

SchoolScore at 0 hoursScore at 1 hourScore at 2 hours
A687276
B707274
C717273
D717273

Across all 12 students, the mean score at each tutoring-hour value is 70, 72, and 74. Thus the fitted line is \(\hat{Y}=70+2x\). The slope can also be checked using the OLS formula. Here \(\bar{x}=1\), \(\bar{Y}=72\), and

$$ \hat{\beta}_1 =\frac{\sum_{g,i}(x_{ig}-\bar{x})(Y_{ig}-\bar{Y})} {\sum_{g,i}(x_{ig}-\bar{x})^2} =\frac{16}{8}=2. $$

The intercept is \(\hat{\beta}_0=\bar{Y}-\hat{\beta}_1\bar{x}=72-2(1)=70\). The estimated association is therefore an increase of 2 test-score points per additional weekly tutoring hour. This is a description of the fitted mean relationship, not a causal conclusion.

The fitted values are 70, 72, and 74 for \(x=0,1,2\). The residuals in school A are \(-2,0,2\); in school B they are \(0,0,0\); in schools C and D they are each \(1,0,-1\). They sum to zero within every school. The residual sum of squares is

$$ SSE=2^2+0^2+2^2+0+1^2+0^2+(-1)^2+1^2+0^2+(-1)^2=12. $$

For the conventional OLS calculation, there are \(12-2=10\) residual degrees of freedom, and \(S_{xx}=\sum_{g,i}(x_{ig}-\bar{x})^2=8\). Under independent, equal-variance errors, the estimated variance of the residuals is \(s^2=SSE/10=1.2\). The conventional standard error of the slope is therefore

$$ SE_{\mathrm{conventional}}(\hat{\beta}_1) =\sqrt{\frac{s^2}{S_{xx}}} =\sqrt{\frac{1.2}{8}} =\sqrt{0.15} \approx 0.387. $$

This standard error uses the conventional independence assumption. The fact that the residuals happen to sum to zero within these schools does not demonstrate that student errors are independent; the school grouping must be considered in the uncertainty calculation.

Cluster-Robust Uncertainty for the Same Slope

For each school, the cluster’s contribution to the slope score is the sum of its students’ predictor deviations multiplied by their residuals. With an intercept in the model, the predictor deviation is \(x_{ig}-\bar{x}\). In the example, each school’s slope score is

$$ q_g=\sum_i(x_{ig}-\bar{x})\hat{u}_{ig}. $$

For school A, the residuals are \((-2,0,2)\), giving \(q_A=(-1)(-2)+(0)(0)+(1)(2)=4\). For school B, \(q_B=0\). For schools C and D, \(q_C=q_D=(-1)(1)+(0)(0)+(1)(-1)=-2\). The scores differ across schools: the pattern of residuals in A contributes differently to the slope than the patterns in C and D.

The uncorrected cluster-robust variance estimator, often called the CR0 sandwich estimator, sums the squared cluster scores and scales them by the squared inverse of \(S_{xx}\) for this slope calculation. It treats schools as independent units while allowing arbitrary dependence among students within a school. Small-sample corrections, which alter this estimator, are not applied in this example.

$$ \widehat{\mathrm{Var}}_{\mathrm{CR0}}(\hat{\beta}_1) =\frac{\sum_g q_g^2}{S_{xx}^2} =\frac{4^2+0^2+(-2)^2+(-2)^2}{8^2} =\frac{24}{64} =0.375, \qquad SE_{\mathrm{CR0}}(\hat{\beta}_1)=\sqrt{0.375}\approx 0.612. $$

The slope remains 2 points per weekly tutoring hour, but the two standard errors differ: about 0.387 conventionally and 0.612 using CR0. In this example, the cluster-robust value is about 1.58 times as large. These calculations illustrate how the uncertainty estimate can change without changing the fitted coefficient. With only four schools, however, this uncorrected cluster-robust standard error is not a reliable basis for treating a large-sample approximation as accurate. The computation does not remove the small-number-of-clusters problem.

Why the Sandwich Uses Schools

The calculation above is a special case of a general justification. Let \(X_g\) be the design matrix for the students in school \(g\), let \(u_g\) be their error vector, and let \(X\) collect the rows for all students. The OLS estimation error can be written

$$ \hat{\beta}-\beta=(X^\mathsf{T}X)^{-1}\sum_g X_g^\mathsf{T}u_g. $$

If different schools are independent and the errors have conditional mean zero, the variance of the sum of the school contributions is the sum of their variances. Writing \(\Omega_g=\mathrm{Var}(u_g\mid X_g)\), the resulting coefficient variance is

$$ \mathrm{Var}(\hat{\beta}\mid X) =(X^\mathsf{T}X)^{-1} \left\{\sum_g X_g^\mathsf{T}\Omega_gX_g\right\} (X^\mathsf{T}X)^{-1}. $$

The unknown within-school covariance matrix \(\Omega_g\) is not estimated by requiring errors within a school to be independent. Instead, the CR0 estimator uses the fitted residual vector \(\hat{u}_g\) for each school:

$$ \widehat{\mathrm{Var}}_{\mathrm{CR0}}(\hat{\beta}) =(X^\mathsf{T}X)^{-1} \left\{\sum_g X_g^\mathsf{T}\hat{u}_g\hat{u}_g^\mathsf{T}X_g\right\} (X^\mathsf{T}X)^{-1}. $$

The middle term is built from one summed score per school. This is why the number of schools, not just the total number of students, matters for the reliability of the cluster-robust approximation. The estimator is not a guarantee of accurate inference with few independent clusters.

Unequal School Sizes and the Meaning of the Slope

In a student-level OLS regression, each student contributes an observation to the fitting criterion. A school with more students can therefore have more influence on the fitted coefficient, particularly when the predictor varies between schools. Cluster-robust uncertainty changes how variability is estimated; it does not automatically reweight schools equally or change the OLS target.

Worked Example: Three Schools of Different Sizes

Suppose a hypothetical data set has 5 students in a school with a mean outcome of 60 points and predictor value 0, 20 students in a school with mean outcome 72 and predictor value 1, and 100 students in a school with mean outcome 80 and predictor value 2. Each student in a school has that school’s predictor value; the listed outcomes are the school means. The student-level regression is a weighted regression of the three school means, with weights equal to school sizes.

The weighted predictor and outcome means are

$$ \bar{x}_w=\frac{5(0)+20(1)+100(2)}{125}=1.76, \qquad \bar{Y}_w=\frac{5(60)+20(72)+100(80)}{125}=77.92. $$

The weighted cross-product sum is \(297.6\), and the weighted predictor sum of squares is \(32.8\). Hence the student-weighted slope is

$$ \hat{\beta}_1 =\frac{5(0-1.76)(60-77.92)+20(1-1.76)(72-77.92)+100(2-1.76)(80-77.92)} {5(0-1.76)^2+20(1-1.76)^2+100(2-1.76)^2} =\frac{297.6}{32.8} \approx 9.07. $$

The fitted student-level association is about 9.07 outcome points per one-unit increase in the predictor. If the three schools were instead given equal weight, the slope through their three means would be 10 points per unit. These are different weighting choices, not competing standard-error formulas. In this example, the largest school supplies 100 of the 125 student records, so its mean has substantial influence on the student-weighted fit. Cluster-robust standard errors do not turn this student-weighted slope into an equally weighted school-level slope.

School Count Is Not Student Count

Worked Example: More Students but the Same Number of Schools

Consider a simplified intercept-only regression in which two independent schools each have \(m\) students. Suppose every student in the first school has outcome \(\mu+d\) and every student in the second has outcome \(\mu-d\). The fitted intercept is their pooled mean, \(\mu\), and the residual for each student is \(d\) or \(-d\). This setup is only for illustrating how cluster size enters the calculation; two schools are far too few for dependable cluster-robust inference.

Each school’s intercept score is the sum of its residuals: \(md\) for the first school and \(-md\) for the second. The total number of students is \(2m\), so the inverse intercept bread is \(1/(2m)\). The CR0 variance is therefore

$$ \widehat{\mathrm{Var}}_{\mathrm{CR0}}(\hat{\beta}_0) =\left(\frac{1}{2m}\right)^2\left\{(md)^2+(-md)^2\right\} =\frac{d^2}{2}. $$

In this constructed example, increasing \(m\) while keeping the number of schools at two does not reduce that cluster-based variance. By contrast, a calculation that treats all \(2m\) student errors as independent would act as if the added students supplied independent information. The example shows why a large student count alone does not establish that cluster-robust inference is dependable: the number of independent schools remains crucial. It does not imply that larger schools never add information; their contribution depends on the predictor pattern and the dependence structure.

Practical Interpretation and a Common Mistake

A report of a student-level regression with school clustering should make clear what the coefficient describes, how many schools and students contributed, and which uncertainty estimate was used. Unequal school sizes matter for both the student-weighted fit and the cluster-score contributions to the variance. A cluster-robust standard error accounts for within-school dependence under its assumptions, but it does not repair a misspecified mean model, eliminate confounding, or create independent schools that were not observed.

  • Wrong reasoning: “There are thousands of students, so the cluster-robust standard error must be reliable.”
  • Correction: Cluster-robust inference relies on independent clusters. Many students concentrated in a small number of schools do not provide the same number of independent units as the same students spread across many schools.
  • Wrong reasoning: “Using cluster-robust standard errors gives every school equal weight in the regression.”
  • Correction: The cluster-robust calculation changes the estimated uncertainty, not the OLS fitting weights. With unequal school sizes, larger schools can have more influence on a student-level OLS coefficient.
  • Wrong reasoning: “The cluster-robust standard error is larger here, so tutoring has a smaller estimated association.”
  • Correction: In the four-school example the estimated slope remains 2 points per weekly hour; only the uncertainty calculation changes.
Key takeaway: Fit and interpret the student-level mean model first, then match the uncertainty calculation to the school-level dependence. Report both the number of students and the number and sizes of schools: the student count affects the fitted regression, while the independent school count is central to cluster-robust inference.

Check Your Understanding

Use the worked calculations and explanations to answer these questions.

  1. In the four-school tutoring example, why does replacing the conventional slope standard error with the CR0 value leave the fitted slope unchanged?
  2. What assumption about schools, rather than individual students within the same school, supports the cluster-robust variance calculation?
  3. In the unequal-size example, why does the school with 100 students have more influence on the student-level OLS slope than the school with 5 students?
  4. In the two-school illustration, what happens to the CR0 variance when \(m\) increases while the number of schools remains two? Why should the result not be treated as dependable inference?
  5. Does a cluster-robust standard error make the tutoring-hours coefficient causal? Explain briefly.