- Panel data follow the same units over time. Both fixed effects (FE) and random effects (RE) deal with an unobserved, time-constant unit effect \( a_i \).
- Fixed effects removes \( a_i \) completely, so it is consistent even if \( a_i \) is correlated with the regressors. The price: it ignores between-unit variation and cannot estimate time-invariant variables.
- Random effects assumes \( a_i \) is uncorrelated with every regressor. If that is true it is more efficient; if not, it is biased.
- The Hausman test compares the two. A significant difference means the RE assumption fails, so use FE.
- Worked example (60 countries, 20 years): \( H = 10.66 \), 4 degrees of freedom, \( p = 0.031 \), so fixed effects is preferred.
Fixed effects vs random effects is the first big decision in any panel-data regression, and the Hausman test is the standard way to make it. This guide explains what each estimator does, when each one is right, how to run and read the Hausman test in Python, R and Stata, and what to do when the test misbehaves. Every number comes from a real worked example, so you can see the two estimators disagree and watch the test pick the right one.
The problem: an effect you cannot see
Suppose you want the effect of investment on economic growth, using data on 60 countries over 20 years. Countries differ in many ways that hardly change over two decades: institutions, geography, legal origin, culture. Call all of that \( a_i \). The model is
The trouble is that \( a_i \) is usually related to the regressors. Countries with good institutions both invest more and grow faster for other reasons. If you simply pool all 1,200 country-years and run OLS, \( a_i \) sits in the error term and causes omitted variable bias. Fixed and random effects are two different answers to this problem, and they rest on different assumptions about \( a_i \).
Panel data, first differences, fixed and random effects and clustered standard errors are covered step by step in Chapter 15 of the Econometrics Made Simple textbook, which supplies the dataset used here.
The worked example
The file countries_panel.csv from Econometrics Made Simple, Chapter 15, has \( N = 60 \) countries and \( T = 20 \) years (2000–2019), 1,200 rows in all. The outcome is GDP growth (%) and the regressors are investment (% of GDP), years of schooling, trade openness and inflation. The data are simulated, so we know the truth: the effect of investment is 0.11, and the country effects were built to be positively correlated with investment and schooling. That is exactly the situation in which the choice between FE and RE matters.
Pooled OLS, which ignores the country effects, gives an investment coefficient of 0.140, with a 95% confidence interval of [0.118, 0.162] that misses the true 0.11. The bias is upward, as you would expect when the omitted country effect is positively related to investment.
Fixed effects: compare each unit with itself
The fixed effects (FE) estimator subtracts each country’s own average from every variable. Because \( a_i \) is the same in every year, it equals its own average and disappears:
OLS on these demeaned data is the within estimator. You get exactly the same slopes by adding a dummy variable for every country (least squares dummy variables, LSDV). In our data the FE investment coefficient is 0.0875 (clustered s.e. 0.025); adding year effects as well (two-way fixed effects) gives 0.0781 (0.022). Both are close to the true 0.11.
Read the FE coefficient as a within-country effect: in years when a country invests one percentage point more than its own average, its growth is about 0.09 points higher than its own average, holding the other regressors fixed.
The cost is real. FE throws away all variation between countries, which makes estimates noisier, and it cannot estimate the effect of anything that does not change over time, such as region, a person’s gender or a country’s colonial history: these are wiped out by the demeaning along with \( a_i \).
Random effects: use the between variation too
Random effects (RE) makes a stronger assumption in exchange for more precision.
If that holds, pooled OLS is already unbiased, but inefficient: every year of a country shares the same \( a_i \), so the errors are correlated within each country. RE is the generalised least squares fix. Instead of subtracting the whole country average, it subtracts a fraction \( \theta \) of it:
When \( \theta = 0 \), RE is pooled OLS; as \( \theta \to 1 \), it becomes fixed effects. So RE is a weighted compromise between the two. Because it does not remove the whole mean, it can estimate time-invariant variables.
The RE model of the country panel, with year dummies, estimates \( \hat{\sigma}_u^2 = 3.083 \) and \( \hat{\sigma}_a^2 = 0.078 \), with \( T = 20 \). Compute \( \theta \) and say what it means.
RE subtracts less than a fifth of each country’s mean, so it stays close to pooled OLS. Its investment coefficient is 0.131, much nearer the biased pooled 0.140 than the FE 0.078.
Why is \( \hat{\sigma}_a^2 \) so small when the simulation contains sizeable country effects? RE estimates it from residuals of a model in which investment and schooling have already absorbed much of the between-country variation through their biased coefficients. A small estimated unit-effect variance is not evidence that the unit effects are unimportant. It can be a symptom of exactly the correlation that RE rules out.
Fixed effects vs random effects: the key differences
| Fixed effects | Random effects | |
|---|---|---|
| Assumption about \( a_i \) | May be correlated with \( x \) | Uncorrelated with every \( x \) |
| Variation used | Within units only | Within and between (weighted by \( \theta \)) |
| Time-invariant regressors | Cannot be estimated | Can be estimated |
| Efficiency | Less precise | More precise, if its assumption holds |
| If \( a_i \) is correlated with \( x \) | Still consistent | Biased and inconsistent |
| Typical use | Causal questions with countries, firms, workers | Random samples of units, or when time-invariant effects are the focus |
Figure 2 shows the trade-off in our data. Pooled OLS and RE look very precise but are centred on the wrong value. The fixed effects estimators have intervals two to three times wider, but they contain the truth. Fixed effects buys consistency with precision.
The Hausman test: how to choose
The logic of the test, proposed by Hausman (1978), is simple.[1] If the RE assumption holds, FE and RE are both consistent, so their estimates should be close; RE is then more efficient. If it fails, FE is still consistent but RE is not, so the estimates drift apart. A large, statistically significant gap is evidence against random effects.
- \( H_0 \): the unit effects are uncorrelated with the regressors (RE consistent and efficient).
- \( H_1 \): they are correlated (only FE consistent).
For a single coefficient the statistic is
With \( k \) coefficients compared at once, the same idea uses the vector of differences and the difference of the two covariance matrices:
Reject \( H_0 \), and prefer fixed effects, when \( H \) exceeds the critical value (or \( p < 0.05 \)).
With conventional standard errors, the two-way FE investment coefficient is 0.0781 (s.e. 0.0214) and the RE coefficient is 0.1312 (s.e. 0.0123). Run the Hausman test for this coefficient at the 5% level.
The gap between the two estimates is far too large to be sampling error. Random effects is inconsistent here; use fixed effects.
Running the test in software
The usual version compares all the time-varying coefficients together. Here it compares four.
import numpy as np, pandas as pd
from scipy import stats
from linearmodels.panel import PanelOLS, RandomEffects
p = pd.read_csv("countries_panel.csv").set_index(["country", "year"])
xs = ["investment", "schooling", "openness", "inflation"]
yr = pd.get_dummies(p.index.get_level_values("year"), drop_first=True,
dtype=float).set_index(p.index)
fe = PanelOLS(p.gdp_growth, p[xs], entity_effects=True, time_effects=True).fit()
re = RandomEffects(p.gdp_growth, pd.concat([p[xs].assign(const=1.0), yr], axis=1)).fit()
d = fe.params[xs] - re.params[xs]
V = fe.cov.loc[xs, xs] - re.cov.loc[xs, xs]
H = float(d @ np.linalg.inv(V) @ d)
print(H, stats.chi2.sf(H, len(xs))) # 10.66, 0.031
library(plm)
panel <- read.csv("countries_panel.csv")
f <- gdp_growth ~ investment + schooling + openness + inflation
fe2 <- plm(f, data = panel, index = c("country", "year"),
model = "within", effect = "twoways")
re2 <- plm(update(f, . ~ . + factor(year)), data = panel,
index = c("country", "year"), model = "random")
phtest(fe2, re2) # chisq = 10.662, df = 4, p-value = 0.03064
encode country, generate(id)
xtset id year
xtreg gdp_growth investment schooling openness inflation i.year, fe
estimates store fe
xtreg gdp_growth investment schooling openness inflation i.year, re
estimates store re
hausman fe re // consistent model first, efficient model second
Result. The coefficients being compared are:
| Two-way FE | RE | Difference | |
|---|---|---|---|
| investment | 0.0781 | 0.1312 | −0.0531 |
| schooling | 0.4633 | 0.4405 | 0.0228 |
| openness | 0.0069 | 0.0068 | 0.0001 |
| inflation | −0.0691 | −0.0633 | −0.0058 |
\( H = 10.66 \) with 4 degrees of freedom. The 5% critical value of \( \chi^2_4 \) is 9.49 and the \( p \)-value is 0.031, so we reject \( H_0 \) at the 5% level: the country effects are correlated with the regressors and fixed effects is preferred. That is the right answer, because we built the data that way. Python, R and Stata all give the same statistic when the two models are specified identically (R output from Econometrics Made Simple, Chapter 15).[9]
When the Hausman test goes wrong
1. Failing to reject is not proof that RE is right
A large \( p \)-value can simply mean the FE estimates are too imprecise to show a difference. In our data, running the same test without year effects gives \( H = 8.09 \) and \( p = 0.088 \): the unmodelled 2009 shock adds noise that hides the difference, and at 5% the test would wrongly let random effects through. Model the time effects, and when in doubt prefer FE for causal questions. Most applied economists working with country or firm panels use FE by default.
2. A negative statistic or “not positive definite” warning
The formula needs \( \hat{V}_{FE}-\hat{V}_{RE} \) to be positive definite, which holds in theory under \( H_0 \) but can fail in a finite sample. Stata then warns that the matrix is not positive definite, and the statistic can even come out negative. Common causes are comparing models with different regressors or year effects, or using robust standard errors. Make sure both models contain exactly the same time-varying regressors, use conventional standard errors for both (Stata’s sigmamore option bases both on the same error variance), or switch to the Mundlak version below.
3. You need clustered or robust standard errors
The classic Hausman test assumes RE is fully efficient, which fails if the errors are heteroskedastic or serially correlated, as they usually are in panels. Then do not feed clustered standard errors into the classic formula. Use a regression-based test instead.
The Mundlak (correlated random effects) test
Mundlak (1978) suggested adding each unit’s averages of the time-varying regressors, \( \bar{x}_i \), to the random-effects (or pooled) model.[2] If \( a_i \) is uncorrelated with the regressors, the coefficients on the averages should be zero. Test them jointly with cluster-robust standard errors, and you have a Hausman-type test that is valid under heteroskedasticity and serial correlation.[3]
import pandas as pd, statsmodels.formula.api as smf
d = pd.read_csv("countries_panel.csv")
xs = ["investment", "schooling", "openness", "inflation"]
for x in xs:
d[x + "_bar"] = d.groupby("country")[x].transform("mean")
f = "gdp_growth ~ " + "+".join(xs) + "+" + "+".join(x + "_bar" for x in xs) + "+ C(year)"
m = smf.ols(f, data=d).fit(cov_type="cluster", cov_kwds={"groups": d["country"]})
print(m.wald_test(", ".join(x + "_bar = 0" for x in xs), use_f=False, scalar=True))
The robust Wald statistic is 11.02 with 4 degrees of freedom (\( p = 0.026 \)), the same conclusion as the classic test. A neat bonus: the coefficients on the time-varying regressors in this model are exactly the two-way FE estimates (investment 0.0781), while the model can still include time-invariant variables. This “correlated random effects” approach gives you FE’s robustness and RE’s flexibility.
Time-invariant variables: the real reason people want RE
Often the variable you care about does not change over time: a region, an ethnic group, a firm’s founding sector. FE cannot estimate it, because it is perfectly collinear with the unit dummies. Its influence is absorbed into \( \hat{a}_i \).
RE can estimate it, but only if the random-effects assumption holds, and the Hausman test in our data says it does not. In that case the RE coefficient on a time-invariant variable is no more trustworthy than its other coefficients. The correlated random effects model is the honest compromise: include \( \bar{x}_i \) to absorb the correlation, then add the time-invariant variables. Their coefficients are still only causal if the time-invariant variables themselves are unrelated to the remaining part of \( a_i \).
A practical decision guide
- Ask the economic question first. Is it plausible that unmeasured, fixed characteristics of the units are related to your regressors? For countries, firms and workers the answer is almost always yes. Start with FE.
- Include time effects (year dummies) in both models, so that common shocks do not distort the comparison.
- Run the Hausman test with identical specifications and conventional standard errors, or the Mundlak test with clustered standard errors.
- If \( p < 0.05 \): use FE. If not: RE is defensible, but check that the FE standard errors are not simply too large to detect anything.
- Whatever you choose, cluster the standard errors by unit for inference. The errors of the same country are correlated over time.[6]
- Report both and explain any difference. Readers trust a result more when they can see it does not hinge on the choice.
If you have only two periods and a policy change, the same fixed-effects logic becomes difference-in-differences. For testing more generally, see hypothesis testing, t-tests and p-values; to interpret the coefficients in the output, see how to read regression output.
Practice questions
Use a 5% significance level unless told otherwise. Critical values: \( \chi^2_1 = 3.84 \), \( \chi^2_2 = 5.99 \), \( \chi^2_3 = 7.81 \), \( \chi^2_4 = 9.49 \).
EasyQ1. A random-effects model has \( T = 5 \), \( \hat{\sigma}_u^2 = 2 \) and \( \hat{\sigma}_a^2 = 1 \). Compute \( \theta \). Is RE closer to pooled OLS or to fixed effects? Show worked answer
RE removes about 47% of each unit’s mean, so it sits roughly halfway between pooled OLS (\( \theta = 0 \)) and FE (\( \theta = 1 \)), slightly closer to pooled OLS.
EasyQ2. A Hausman test comparing three coefficients gives \( H = 6.2 \). State the null hypothesis, the conclusion, and which estimator you would report. Show worked answer
\( 6.2 < 7.81 \) (\( p = 0.102 \)), so do not reject \( H_0 \).
RE is not rejected and can be reported, but a careful researcher would also show FE, since failure to reject may reflect imprecise FE estimates.
MediumQ3. In a panel of firms, the effect of R&D on productivity is \( \hat{\beta}^{FE} = 0.52 \) (s.e. 0.06) and \( \hat{\beta}^{RE} = 0.61 \) (s.e. 0.04). Carry out the Hausman test for this coefficient. Show worked answer
\( 4.05 > 3.84 \) (\( p = 0.044 \)), so reject \( H_0 \) at 5%, though only just. Report FE; RE overstates the effect, probably because more productive firms also do more R&D.
MediumQ4. A researcher studying wages wants the effect of gender, using a panel of workers observed for eight years. Explain why fixed effects cannot deliver it, and suggest an approach that can. Show worked answer
Random effects can estimate it, but only if the worker effect (ability, motivation) is uncorrelated with all regressors. A better approach is correlated random effects: add each worker’s averages of the time-varying regressors, then include gender. Its coefficient is still only causal if gender is unrelated to the rest of the worker effect.
ExamQ5. For school test scores, \( \hat{\beta}^{FE} = -0.42 \) (s.e. 0.05) and \( \hat{\beta}^{RE} = -0.35 \) (s.e. 0.03) on class size. (a) Compute the Hausman statistic. [2] (b) State your conclusion at 5% and at 10%. [2] (c) The FE regression did not include year dummies but RE did. Why is this a problem? [2] (d) What would you report, and why? [2] Show worked answer
(b) At 5%, \( 3.06 < 3.84 \): do not reject [1]. At 10% the critical value is 2.71, so reject (\( p = 0.080 \)) [1].
(c) The test compares the same coefficients under two assumptions about \( a_i \). With different regressors the estimates can differ for reasons unrelated to that assumption, and the variance difference may not be positive definite [2].
(d) Re-run both with identical specifications. Given borderline evidence and the likely link between school quality and class size, report FE with clustered standard errors and show RE as a check [2].
Frequently asked questions
When should I use fixed effects instead of random effects?
Use fixed effects when unmeasured, time-constant characteristics of the units are likely to be related to your regressors, which is the usual case with countries, firms and people, or when the Hausman test rejects. FE is the safer choice for causal questions.
When is random effects appropriate?
When the unit effects can reasonably be treated as unrelated to the regressors, for example when units are randomly sampled and the regressors are randomly assigned, or when you need time-invariant variables and the Hausman (or Mundlak) test does not reject.
What does a significant Hausman test mean?
A \( p \)-value below 0.05 means the FE and RE estimates differ by more than sampling error can explain. The random-effects assumption is rejected, RE is inconsistent, and you should use fixed effects.
What if the Hausman test is not significant?
Random effects is not rejected and is the more efficient choice. But check that the FE estimates are reasonably precise; a test with low power proves little.
Why is my Hausman test statistic negative?
The estimated variance difference is not positive definite in your sample. Make sure both models have the same regressors and year effects and use conventional standard errors, try Stata’s sigmamore option, or use the Mundlak test.
Can I use the Hausman test with robust or clustered standard errors?
Not the classic version, which assumes RE is efficient. Use the Mundlak (correlated random effects) test: add unit means of the time-varying regressors and test them jointly with cluster-robust standard errors.
What is the difference between fixed effects and pooled OLS?
Pooled OLS ignores the panel structure and leaves \( a_i \) in the error, so it is biased if \( a_i \) is correlated with the regressors. Fixed effects removes \( a_i \). In our data pooled OLS gives 0.140 for investment and FE 0.088, against a truth of 0.11.
Studying more than one subject? The Complete Book Bundle has every Economics, Statistics, Mathematics and Econometrics book on the site in one download.
References
- Hausman, J. A. (1978). Specification Tests in Econometrics. Econometrica, 46(6), 1251–1271.
- Mundlak, Y. (1978). On the Pooling of Time Series and Cross Section Data. Econometrica, 46(1), 69–85.
- Wooldridge, J. M. (2010). Econometric Analysis of Cross Section and Panel Data, 2nd edn, Chapter 10. Cambridge, MA: MIT Press.
- Wooldridge, J. M. (2020). Introductory Econometrics: A Modern Approach, 7th edn, Chapter 14 “Advanced Panel Data Methods”. Boston: Cengage Learning.
- Baltagi, B. H. (2021). Econometric Analysis of Panel Data, 6th edn. Cham: Springer.
- Cameron, A. C. and Miller, D. L. (2015). A Practitioner’s Guide to Cluster-Robust Inference. Journal of Human Resources, 50(2), 317–372.
- Croissant, Y. and Millo, G. (2008). Panel Data Econometrics in R: The plm Package. Journal of Statistical Software, 27(2), 1–43.
- Islam, N. (1995). Growth Empirics: A Panel Data Approach. Quarterly Journal of Economics, 110(4), 1127–1170.
- The EconTutorials team (2026). Econometrics Made Simple, Chapter 15 “Panel Data”, dataset
countries_panel.csv. econtutorials.com.

