Panels as mixed models: Breusch–Pagan LM, Hausman and Mundlak
A panel is clustered data with a clock. Random effects are a random-intercept model, fixed effects remove the units, and three tests tell you whether unit effects exist and whether they're tangled with your predictor.
What runs in CodeSplash
- STAT182Hausman specification test Runs in CodeSplash
- STAT224Breusch–Pagan LM test for cross-sectional dependence Runs in CodeSplash
- Panel data models (fixed effects, random effects, first differences) Verified in Python and R
You will be able to
- Connect panel random effects to a random-intercept model
- Test for unit effects with Breusch–Pagan LM, Honda and the F test
- Read theta as the amount of partial demeaning
- Use Hausman and Mundlak tests to judge random against fixed effects
Read first
Clustered data with a clock
ReadA panel follows the same units over time: firms by year, regions by quarter. It's the data you've met in this track, but the question usually changes. You want to know what happens when a unit's own predictor moves.
You now have two tools you already know, under new names:
- Random effectsRandom effectsA panel model that treats unit differences as noise unrelated to the regressors, which gives more precise estimates when that holds. give each unit an intercept drawn from a distribution. That's a random-intercept model.
- Fixed effectsFixed effectsA panel model that compares each unit with itself, removing everything constant within a unit, measured or not. give each unit its own free intercept. That's no pooling.
The Causal track's panel lesson asks what these designs can claim. This one is about the tests.
Where is the variation?
PredictPredict first
Grunfeld's data follow 10 large firms over 20 years. How much of the variation in a firm's market value do you expect to be within firms, over time?
Reveal
6.7%. Big firms stay big, so nearly all the variation is between firms. Fixed effects use only the within part, so here they throw away most of the variation in the predictor. The within-between chart on the card shows this split.
Do unit effects exist?
ReadFirst ask whether units differ at all. If they don't, pooled least squares is fine.
The Breusch–Pagan LM testBreusch–Pagan LM test for panel effectsAsks whether pooled OLS residuals of one unit move together, which would mean unit effects exist. Honda and King–Wu are its one-sided forms. uses pooled residuals, and asks whether residuals within a unit are correlated: : . A variance can't be negative, so Honda's version tests the same thing one-sided.
| Test | Grunfeld |
|---|---|
| Breusch–Pagan LM | = 798.2, p < .001 |
| Honda | = 28.3, p < .001 |
| F test of unit effects | = 49.2, p < .001 |
Firms differ, so pooled least squares is out.
Don't confuse this test with the Breusch–Pagan LM test for cross-sectional dependence, which squares the correlations between units' residuals: , with degrees of freedom. On Grunfeld it gives p < .001 on 45 df: errors move together across firms, which is a question for the standard errors, not for pooling.
Random effects are partial pooling
ReadRandom effects subtract only part of each unit's mean:
At you get pooled least squares, and at you get fixed effects. So is a pooling dial. The data set it from the variance between units and the variance within them.
For Grunfeld, = 0.861, closer to fixed effects. The estimates agree to three decimals: 0.110 for random effects, and 0.110 for fixed.
Feel the dial
ExploreDExplorable · Partial pooling
How can small groups borrow strength from the rest of the data?
Which estimate gets closest to the truth?
Thicker lines are larger groups: they move little. Small groups shrink the most toward the overall mean.
Commit your prediction above to see the result
Make your prediction to see the result.
This is the same blend as . Raise True spread between groups, or reduce Noise within groups, and each group keeps more of its own information. That's moving toward 1.
When random effects go wrong
PredictThe price of random effects is one assumption: unit effects are unrelated to the predictors. The Hausman testHausman testCompares fixed and random effects estimates; a large gap suggests unit differences track the regressors, so random effects are biased. checks it by comparing the two sets of estimates.
Predict first
In a simulated panel, regions with higher doses have higher unit effects by design. In Grunfeld, the two estimates nearly match. On which do you expect Hausman to reject?
Reveal
On the simulated panel: (1) = 96.4, p < .001. For Grunfeld: (2) = 2.33, p = .312.
The same question with a mixed model
ReadThe Mundlak testMundlak testAdds each regressor's unit mean to a pooled regression and tests them; nonzero means unit differences are related to the regressors. adds each unit's mean predictor to a random-effects model:
If , unit effects aren't tied to the predictor. If it isn't, is the within-unit effect, and is how far the between-unit slope differs from it. In mixed-model papers, this is often called the within-between model.
It also works with clustered errors, where classic Hausman doesn't. Grunfeld: = 1.72, p = .233, a less comfortable answer than Hausman's. Simulated: p < .001.
What would you lose?
ReflectFixed effects can't estimate anything that never changes within a unit, such as a firm's founding country. Mundlak's model can keep those predictors, while still using within-unit change for the rest.
What's your main predictor: does it change within units, or differ between them? Would a mean-of-predictor term answer your question better than either model alone?
Check your understanding
CheckpointCheck yourself
Breusch–Pagan LM gives p < .001. What does it tell you?
Show the answer
Answer: a
It tests whether the variance of unit effects is zero. It says nothing about which of fixed or random effects to prefer, and it isn't the Breusch–Pagan heteroskedasticity test, despite the name.
Check yourself
A random-effects fit reports close to 0. What's going on?
Show the answer
Answer: b
near 0 subtracts almost none of each unit's mean. near 1 subtracts nearly all of it, which is fixed effects.
Check yourself
Hausman rejects. Which model does that speak against?
Show the answer
Answer: b
Fixed effects don't need that assumption. Rejecting says the random-effects estimates are probably biased, under this test's assumptions.
Check yourself
Why might you prefer Mundlak's form to Hausman's test?
Show the answer
Answer: a
It estimates the same contrast as a regression term. It handles only unit-level effects tied to the predictors, not confounders that change over time.
Fit a panel
In the appSet Outcome, Treatment, Unit and Time, then choose Model. Every run reports all of these tests, whichever model you choose. The Causal studio's panel design uses the same method.
import pandas as pd
from linearmodels.panel import PanelOLS, RandomEffects, compare
df = pd.read_csv("panel_grunfeld.csv").set_index(["firm", "year"])
fe = PanelOLS.from_formula("inv ~ value + capital + EntityEffects", df).fit()
re = RandomEffects.from_formula("inv ~ 1 + value + capital", df).fit()
print(compare({"FE": fe, "RE": re}))
print("theta:", re.theta.mean().iloc[0])
print(fe.f_pooled) # F test of unit effectslibrary(plm)
df <- read.csv("panel_grunfeld.csv")
pdf <- pdata.frame(df, index = c("firm", "year"))
fe <- plm(inv ~ value + capital, data = pdf, model = "within")
re <- plm(inv ~ value + capital, data = pdf, model = "random")
pooled <- plm(inv ~ value + capital, data = pdf, model = "pooling")
plmtest(pooled, type = "bp") # Breusch-Pagan LM
plmtest(pooled, type = "honda") # Honda
pFtest(fe, pooled) # F test of unit effects
phtest(fe, re) # HausmanRecap
Recap- Random effects are a random-intercept model, and fixed effects are no pooling.
- Breusch–Pagan LM and Honda ask whether unit effects exist at all.
- is a pooling dial between pooled least squares and fixed effects.
- Hausman and Mundlak ask whether unit effects are tied to the predictors. A large p-value doesn't settle it.
Concepts in this lesson