Loading...
Loading...
Panel data analysis with Python using linearmodels and pandas.
npx skill4agent add meleantonio/awesome-econ-ai-stuff python-panel-datapandasstatsmodelslinearmodelspandaslinearmodels.PanelOLSRandomEffects# ============================================
# 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_entity=True)
print(results.summary)pandaslinearmodelsstatsmodelspip install pandas linearmodels statsmodels