Why grouped data breaks ordinary regression
Today let's understand why an ordinary regression's confidence interval can be far too narrow, the moment your rows come from groups instead of individuals.
Picture a clinical trial spread across thirty clinics, ten patients in each, three hundred patients in total. Fifteen clinics try a new care protocol, and fifteen stick with standard care. You want to know whether the new protocol genuinely does better.
If you treated those three hundred patients as three hundred strangers, you would compute one standard error for the treatment effect. But patients at the same clinic share the same staff, the same equipment and the same routines. They behave more like thirty pieces of evidence than three hundred.
Here is a simulation of exactly this design. Drag the dial and watch four different ways of computing the standard error for the same treatment effect pull apart as the clinics' patients start to resemble each other more.
The naive row above is the one almost everyone reaches for first, and it is already the one furthest from the truth. Everything in this lesson is about why, and what to do instead.
Thirty clinics, ten patients, and one pooled regression
Thirty clinics take part in a trial of a new care protocol for high blood pressure. Fifteen clinics run the new protocol, and fifteen keep their standard care. Each clinic enrolls ten patients, so the trial has three hundred patients in total. The outcome measured on every patient is the drop in systolic blood pressure after eight weeks of their assigned care.
Build that data in R, with a true treatment effect of 4 points and patients at the same clinic correlated at 0.35. Then fit the most direct regression you could reach for: each patient's blood pressure drop against which arm they were in, treating all three hundred rows as separate pieces of evidence.
Build the data and fit that pooled regression now.
The treatment coefficient is 3.95, close to the real effect of 4 points built into the simulation. Its 95% confidence interval runs from 3.72 to 4.18, barely half a point wide. Call this the pooled analysis, because it pools every one of the three hundred rows into one regression, as if each patient were an independent, standalone observation.
That interval looks precise. It looks like you know the treatment effect almost to the decimal point. The next two steps ask whether you are entitled to feel that confident.
Why patients in the same clinic are not independent
Patients at the same clinic share the same nurses, the same equipment and the same day to day routine at that clinic. So two patients from the same clinic tend to have more similar outcomes than two patients picked from two different clinics, even before any treatment is applied.
Statisticians give that similarity a number: the intraclass correlation, written rho. It runs from 0 to 1. A rho of 0 means patients at the same clinic are no more alike than patients at different clinics, which is the independence the pooled regression assumed. A rho of 1 means every patient at a clinic has exactly the same outcome, so measuring ten of them tells you nothing more than measuring one.
The consequence for your standard error has a name too: the design effect. For m patients per clinic and an intraclass correlation of rho, it is 1 + (m - 1) * rho. It is how many times larger your true variance really is than the variance a regression reports when it assumes every row is independent.
Our clinics have m = 10 patients each, and the simulation used rho = 0.35. So the design effect is 1 + (10 - 1) x 0.35, which is 1 + 3.15, or 4.15. That one number says the three hundred rows carry the statistical weight of about 300 divided by 4.15, roughly seventy two independent patients, not three hundred.
Dragging the intraclass correlation yourself
You do not have to take that number on faith. Here is the same thirty clinics of ten design, this time with the dial starting at rho equal to 0, where there is nothing to correct for yet.
Raise the dial and watch the ordinary OLS line barely move, because its formula assumes every row is independent and never adjusts for clustering. The cluster robust and cluster mean lines widen instead, tracking the simulated truth closely, and their coverage holds near 95% the whole way up. Read off the design effect and the naive interval's coverage at two or three settings. Every point past rho equal to 0 is a clinic study whose published interval would have been too narrow to trust.
Quick check: which standard error would you trust
Take a new study: twenty patients per clinic this time, and an intraclass correlation of 0.5, even stronger clustering than our trial. One treatment effect, four candidate standard errors. Which one would you actually report?
Analysis 1: ignoring the grouping
Go back to that pooled regression. Call it what it actually is: Analysis 1, ignoring the grouping entirely.
Read its coefficients again, now that you know what to look for.
The estimate, 3.95 points, is unbiased. The simulation really did build in a 4 point effect, and pooling all three hundred rows lands close to it no matter how the clinics are grouped.
The standard error is the problem. It was computed as though three hundred independent patients could each contribute independent evidence of the treatment effect. But the design effect means these three hundred rows carry the information of only about seventy two independent patients, so the pooled interval, 3.72 to 4.18, is too narrow. It would reject reasonable alternative effects that a wider, honest interval would still allow.
Analysis 2: averaging within each clinic
Analysis 2 throws away the patient-level detail on purpose. Collapse every clinic's ten patients down to one number, that clinic's average outcome, and compare the two arms on those thirty averages instead of on three hundred rows.
Average within each clinic and run the comparison.
Fifteen numbers belong to the treated clinics and fifteen to the control clinics now, instead of three hundred patient rows. Compare those thirty clinic means with a two-sample t-test, and the estimate comes back at 3.95, the same gap the pooled regression found, because the design is balanced.
The interval is different though: 3.47 to 4.43, almost twice as wide as the pooled regression's 3.72 to 4.18. This wider interval is the trustworthy one. For a balanced design like this, where every clinic has the same number of patients and the clinics split evenly between arms, comparing clinic means gives you exactly the standard error a mixed model would report, without even fitting one.
The cost is real too. Every one of the individual patient measurements beyond each clinic's single average disappeared into those thirty numbers. You traded patient-level detail for a standard error you can actually trust.
Analysis 3: adding clinic as a fixed factor
Analysis 3 looks like the careful, rigorous choice at first. Control for which clinic a patient came from, by adding clinic as a regular predictor, then see what is left over for the treatment effect to explain.
Fit that regression and look at the treatment row.
R cannot give the treatment coefficient a value at all. It comes back as NA, not a small number, not a wide interval: nothing.
The reason is collinearity, and it is exact here rather than approximate. factor(clinic) adds one dummy variable for every clinic beyond the first, enough columns to represent each clinic's own average exactly. But treatment never changes within a clinic, so every patient at a treated clinic has treatment equal to 1 and every patient at a control clinic has treatment equal to 0. The entire treatment column is already fully determined by which clinic dummy is switched on, so there is nothing left for treatment to explain.
This is not a stricter version of Analysis 1 or Analysis 2. It is a different question with no answer in this data: holding the clinic fixed, what does treatment do, when treatment never varies while the clinic is held fixed. There is no within-clinic contrast to estimate, because no clinic ever ran both arms.
Previewing the mixed model: borrowing strength across clinics
Analysis 2 got the standard error right by averaging within clinic. But it also treated every clinic's average as equally trustworthy, when a clinic with two patients and a clinic with forty patients clearly are not.
A mixed model fixes that by borrowing strength across clinics: a clinic with few patients gets pulled toward the overall average more than a clinic with many. Here is that idea on its own small example, eight clinics ranging from 2 to 40 patients, standing in for our thirty clinics of ten so the pulling effect is easy to see.
Drag the pooling slider toward no pooling, and every clinic is judged purely on its own raw average, so the clinics with only two or three patients swing far from the grand mean on almost no evidence. Drag it toward complete pooling, and every clinic gets assigned the exact same number, throwing away any real difference between clinics.
Somewhere in between is partial pooling: the small, noisy clinics get pulled hardest toward the grand mean, and the large clinics barely move, because they already carry enough evidence on their own. That is what a mixed model with a random intercept per clinic does automatically, fit here with lme4::lmer() in the widget's own R code.
A mixed model is this same idea done properly on our own thirty clinics of ten. It keeps every one of the three hundred patient rows, the way Analysis 1 did, and it gets the standard error right, the way Analysis 2 did, by modelling the clinic structure directly instead of averaging it away.
Quick check: which analysis answers the right question
Treatment was assigned to whole clinics, not to individual patients, in every one of the three analyses above. Which one gives both an unbiased estimate and a standard error you can actually trust?
Your turn: average within clinic and compare arms
clin still holds all three hundred patients from this lesson, with columns clinic, treatment and y. Average y within each clinic to get thirty clinic means, then compare the treated clinics against the control clinics with a t-test, the same move Analysis 2 made.
Two lines get you there. Press Check when you have them.
Show answer
# Average within each clinic, then compare the two treatment arms
clinic_means <- tapply(clin$y, clin$clinic, mean)
clinic_arm <- tapply(clin$treatment, clin$clinic, unique)
t.test(clinic_means[clinic_arm == 1], clinic_means[clinic_arm == 0])
#>
#> Welch Two Sample t-test
#>
#> data: clinic_means[clinic_arm == 1] and clinic_means[clinic_arm == 0]
#> t = 16.935, df = 27.895, p-value = 3.286e-16
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#> 3.472114 4.427853
#> sample estimates:
#> mean of x mean of y
#> 3.6951132 -0.2548706 The estimate lands at 3.95, the same gap the pooled regression found, but this time the interval, 3.47 to 4.43, is wide enough to be honest about what thirty clinics can really tell you.
References
- Data Analysis Using Regression and Multilevel/Hierarchical Models - Gelman, A. and Hill, J. (2007), Cambridge University Press.
- Kish, L. (1965). Survey Sampling. Wiley. The book that introduced the design effect.
- Fitting Linear Mixed-Effects Models Using lme4 - Bates, D., Machler, M., Bolker, B. and Walker, S. (2015), Journal of Statistical Software, 67(1).
- Multilevel Analysis: An Introduction to Basic and Advanced Multilevel Modeling - Snijders, T. and Bosker, R. (2012), 2nd edition, Sage.
- Donner, A. and Klar, N. (2000). Design and Analysis of Cluster Randomization Trials in Health Research. Arnold.
What grouped data demands of a regression
Every result in this lesson traces back to one number: the design effect, 1 + (m - 1) * rho. With thirty clinics of ten patients and an intraclass correlation of 0.35, that number is 4.15, and it is the reason three hundred patient rows carried the statistical weight of only about seventy two independent ones.
Three analyses tried to deal with that:
- Ignoring the grouping gave an unbiased estimate with a standard error that was too narrow, because it assumed three hundred independent rows.
- Averaging within each clinic gave the same estimate with a standard error you can trust, by making the clinic, not the patient, the unit being compared.
- Adding clinic as a fixed factor asked a different question entirely, and could not answer it, because treatment never varied inside a clinic.
Averaging within clinic is the one that gets both the estimate and the standard error right, but it costs you every patient-level number along the way. A mixed model is the fix that keeps those patient-level rows and still respects the clinic structure, because it models the clinic to clinic variation directly instead of collapsing it into thirty averages.
The next lesson fits that mixed model directly, with random intercepts for the clinics and, where the treatment effect itself varies by clinic, random slopes too, on this same thirty clinics of ten design.