by @meleantonio
Panel data analysis with Python using linearmodels and pandas.
This skill helps economists run panel data models in Python using pandas, statsmodels, and linearmodels, with correct fixed effects, clustering, and diagnostics.
Follow these steps to complete the task:
Before generating any code, ask the user:
Based on the context, generate Python code that:
pandaslinearmodels.PanelOLS or RandomEffectsAfter generating output:
# ============================================
# Panel Data Analysis in Python
# ============================================
import pandas as pd
from linearmodels.panel import PanelOLS
# Load data
df = pd.read_csv("panel_data.csv")
# Set panel index
df = df.set_index(["firm_id", "year"])
# Create treatment indicator
df["treat_post"] = df["treated"] * df["post"]
# Two-way fixed effects model
model = PanelOLS.from_formula(
"outcome ~ 1 + treat_post + EntityEffects + TimeEffects",
data=df
)
results = model.fit(cov_type="clustered", cluster...