py-econometrics / pyfixest

Fast High-Dimensional Fixed Effects Regression in Python following fixest-syntax
https://py-econometrics.github.io/pyfixest/pyfixest.html
MIT License
118 stars 27 forks source link

DID: Prettier and Unified Coefficient Names for DiD Methods #407

Open s3alfisc opened 2 months ago

s3alfisc commented 2 months ago

Context

Currently, the coefficient names of the DiD methods follow formulaic standard naming, which is very verbose.

Here is an example:

import pyfixest as pf
fit = pf.feols("Y ~ i(f1, X1, ref = 1)", data = pf.get_data())
fit.coef().head()
# C(f1, contr.treatment(base=1))[T.0.0]:X1   -1.069505
# C(f1, contr.treatment(base=1))[T.2.0]:X1   -1.803860
# C(f1, contr.treatment(base=1))[T.3.0]:X1   -1.529129
# C(f1, contr.treatment(base=1))[T.4.0]:X1   -2.039220

The lpdid function does better:

import pandas as pd
from pyfixest.did.estimation import lpdid, did2s

url = "https://raw.githubusercontent.com/s3alfisc/pyfixest/master/pyfixest/did/data/df_het.csv"
df_het = pd.read_csv(url)

lpdid_df = lpdid(
    df_het,
    yname="dep_var",
    idname="unit",
    tname="year",
    gname="g",
    vcov={"CRV1": "state"},
    pre_window=-20,
    post_window=20,
    att=False
)

lpdid_df.tidy().head()

image

while did2s produces

fit_did2s = did2s(
    df_het,
    yname="dep_var",
    first_stage="~ 0 | unit + year",
    second_stage="~i(rel_year, ref=-1.0)",
    treatment="treat",
    cluster="state",
)

fit_did2s.tidy().head()

image

For ATTs, we get

image

To Do

asteves commented 1 week ago

I can take this.

s3alfisc commented 1 week ago

Very cool! I've assigned this to you =) Let me know if I can help you in any way!