Menu

1 / 2

Fixed vs random effects in Python, and what a Hausman test result actually tells you

DEV Community: datascience·Abhimanyu Sunil·1 day ago
#hgToFTYa
#devtodatascience#science#devto#dev#effects
Reading 0:00
15s threshold

Most panel data tutorials stop at "run the Hausman test, and if p < 0.05 use fixed effects." That rule is fine as a starting point, but it hides a few things that matter once you look at real output. So here is a small worked example you can run yourself, with the numbers I got.

The setup

I used the wage_panel dataset that ships with linearmodels: 545 individuals observed over 8 years, with log wages as the outcome. The regressors are experience squared, marital status and union membership.

bash

pip install linearmodels

python

import numpy as np

from scipy import stats

from linearmodels.panel import PanelOLS, RandomEffects

from linearmodels.datasets import wage_panel

df = wage_panel.load().set_index(["nr", "year"])

y = df["lwage"]

X = df[["expersq", "married", "union"]]

the RE model needs an intercept, the FE (within) model does not

X_re = X.copy()

X_re.insert(0, "const", 1.0)

fe = PanelOLS(y, X, entity_effects=True).fit()

re = RandomEffects(y, X_re).fit()

The two sets of…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.