Fixed vs Random Effects in Stata: How to Run and Interpret the Hausman Test
Panel data in your thesis? Here is how to set it up in Stata, estimate fixed and random effects models, run the Hausman test and explain your choice to examiners.
If your thesis follows the same units, such as households, firms, counties or countries, over several time periods, you have panel data. The first modelling decision examiners will ask about is whether you used fixed effects (FE) or random effects (RE), and why. This guide shows you how to make and defend that choice in Stata.
Why panel data needs special models
Each unit has characteristics you can't observe that stay roughly constant over time: management quality, culture, geography. If these unobserved traits are correlated with your explanatory variables, ordinary pooled OLS gives biased estimates. Panel models deal with them in two different ways.
- Fixed effects uses only the variation within each unit over time, so it wipes out all time-invariant traits, observed or not. The cost is that you can't estimate the effect of variables that never change for a unit (for example, gender or region).
- Random effects assumes the unobserved unit traits are uncorrelated with your explanatory variables. If that holds, RE is more efficient and can estimate time-invariant variables. If it doesn't, RE is biased.
The Hausman test checks whether that key RE assumption holds.
Step 1: Declare your panel
* id = unit identifier, year = time variable
xtset id year
xtdescribe // check how balanced the panel is
xtsum yield credit rainfall // within vs between variation
If xtset complains about repeated time values within a panel, you have duplicate unit-year rows. Find them with duplicates report id year before going further.
Step 2: Estimate both models and store them
xtreg yield credit rainfall fert_use, fe
estimates store fe
xtreg yield credit rainfall fert_use, re
estimates store re
Step 3: Run the Hausman test
hausman fe re
The order matters: put the consistent model (FE) first and the efficient model (RE) second.
Step 4: Interpret the result
The null hypothesis is that the differences between FE and RE coefficients are not systematic, which means RE is consistent and preferred.
- Prob > chi2 < 0.05: reject the null. The unit effects are correlated with your regressors, so RE is inconsistent. Use fixed effects.
- Prob > chi2 ≥ 0.05: you fail to reject the null, so random effects is consistent and more efficient. Use RE.
You might report: "The Hausman test rejected the null hypothesis that the random effects estimator is consistent (χ²(3) = 18.42, p < 0.001); the fixed effects model is therefore preferred."
Common problems and how to handle them
"V_b-V_B is not positive definite"
This warning is common in finite samples. Try hausman fe re, sigmamore, which bases both covariance matrices on the same error variance estimate and usually resolves it.
Robust or clustered standard errors
The classic Hausman test is not valid with robust or clustered standard errors. Most panel data needs those because of heteroskedasticity and serial correlation. The usual alternatives are:
- The Mundlak approach: add the unit means of your time-varying regressors to the RE model and test whether they're jointly zero with clustered standard errors.
- The user-written
xtoveridcommand afterxtreg ..., re vce(cluster id), which gives a robust test of overidentifying restrictions with the same interpretation.
Whichever model you choose, report the final estimates with clustered standard errors: xtreg yield credit rainfall fert_use, fe vce(cluster id).
Beyond FE and RE
- Time effects: add year dummies (
i.year) to control for shocks common to all units. - Dynamic panels: if the lagged outcome is a regressor, FE is biased with short panels. Consider system GMM (
xtabond2). - Diagnostics: test for serial correlation (
xtserial) and cross-sectional dependence, especially in macro panels.
Running panel models for your thesis? We help Masters and PhD students set up panel data, choose and test the right estimator, and interpret the output, with commented do-files you can re-run and defend.
Want support with your analysis?
Thesis data analysis and defense prep in R, Stata, SPSS, EViews, Excel and Power BI, with code and explanations you can defend.
Get a free quote →