# Chapter 24: Panel Data and Fixed Effects
# Fundamentals of Python for Financial Econometrics - Panel Data and Fixed Effects
# Dataset: Ceteris Lab teaching sample

# Chapter 24: Panel Data and Fixed Effects
# **Economic question:** What can repeated observations teach us that a single cross-section cannot?
#
# Fixed effects use within-entity variation to remove time-invariant unobserved heterogeneity.

# %% Cell 2
import numpy as np, pandas as pd, statsmodels.formula.api as smf
rng=np.random.default_rng(24); n,t=30,8
id=np.repeat(np.arange(n),t); year=np.tile(np.arange(t),n); a=rng.normal(0,2,n)
x=rng.normal(size=n*t); y=a[id]+.7*x+.2*year+rng.normal(size=n*t)
df=pd.DataFrame({'y':y,'x':x,'id':id,'year':year})
fit=smf.ols('y ~ x + C(id) + C(year)',data=df).fit(cov_type='cluster',cov_kwds={'groups':df['id']})
print(fit.params['x'], fit.bse['x'])

# Interpretation checklist
# - State the unit of observation and units of every variable.
# - Separate association, prediction, and causation.
# - Report magnitude and uncertainty.
# - Identify the most important threat to validity.
