1 Why linear mixed models are needed
Code cells can be edited and re-run with “Run Code” (R is running inside your browser). The first run takes several tens of seconds to load the packages. Feel free to change numbers and see how the results change.
1.1 Today’s data
This is a fictional survey. In 20 schools, students were asked about their “daily study time (hours)”, and their “test score (score)” was recorded.
What we want to know is: “If study time increases by one hour, how many points does the score go up?”
1.2 First, look at everything in a scatter plot
There is an upward trend, but the scatter is quite large.
1.3 Split by school
Looking school by school, we see that
- every school slopes upward (more study, higher scores)
- but the overall level (baseline) differs from school to school
Because of differences in teaching policy, neighborhood, and so on, students in the same school tend to have similar scores.
1.4 What is the problem? The “independence” assumption breaks
Ordinary regression (least squares) assumes that each data point is independent of the others — like rolling a die many times, one student’s score is unrelated to another student’s score.
But this data has a structure in which
- students in the same school are similar → not independent
- it looks like “information from 300 students”, but in part it is closer to “information from 20 schools”
This is called a grouped structure or hierarchical structure.
- Students within schools, patients within hospitals, sales within stores
- Measuring the same person many times (repeated measures): measurements within a person
- Several plants within the same field, products within the same factory line
Such data are everywhere. Linear mixed models are a tool for correctly handling this “similarity within groups”.
1.5 Experience it: what happens if you ignore it (simulation)
Let’s run an experiment to see what happens when independence is ignored.
We create a variable indicating whether each school does or does not run a “special tutoring program”. However, the program actually has no effect (it is unrelated to the scores).
Since there is no effect, the probability of being “significant at the 5% level” (= wrongly judging that there is an effect) should be 5%.
With only 50 runs, the proportions fluctuate somewhat (2–3 out of 50 is 4–6%). If you have time, increase 1:50 to 1:300 or similar and watch the proportions stabilize.
- Ordinary regression (
lm): judges “significant” in a large share of runs, even though there is no real effect - Linear mixed model (
lmer): stays at about 5%
lm assumes that “there are 300 independent data points”, overestimates the amount of information, and underestimates the standard error. As a result, the p-values become too small.
- When data have a grouped structure, data within a group are similar (not independent)
- Ignoring this and using ordinary regression makes it easy to wrongly conclude “significant”
- Linear mixed models build the “offset of each group” into the model and handle this correctly
What (1 | school) in the code means is explained in detail in Chapter Chapter 4. Before that, let’s review ordinary regression in the next chapter.