kylebutts / did2s_stata

Two-Stage Difference-in-Differences following Gardner (2021)
30 stars 14 forks source link

Issues with unit option #15

Open dazzler2 opened 3 months ago

dazzler2 commented 3 months ago

Hi -

Thank you for this package! I wanted to flag a couple of issues I am experiencing. But please let me know if I am doing something incorrectly here.

First, I am finding that I get different results the second time I run the did2s command if I use factor variables to create a polynomial in the first stage. Here's the code I used:

use https://github.com/scunning1975/mixtape/raw/master/castle.dta, clear gen incomenew = income / 10000 did2s l_homicide [aweight=popwt], first_stage(i.year c.incomenew##c.incomenew) second_stage(i.post) treatment(post) cluster(sid) unit(sid) did2s l_homicide [aweight=popwt], first_stage(i.year c.incomenew##c.incomenew) second_stage(i.post) treatment(post) cluster(sid) unit(sid)

The second command produces different results. This seems to be an issue with using factor variables, as there is no issue when I manually create the polynomial:

use https://github.com/scunning1975/mixtape/raw/master/castle.dta, clear gen incomenew = income / 10000 gen incomenew2 = incomenew * incomenew did2s l_homicide [aweight=popwt], first_stage(i.year incomenew incomenew2 ) second_stage(i.post) treatment(post) cluster(sid) unit(sid) did2s l_homicide [aweight=popwt], first_stage(i.year incomenew incomenew2) second_stage(i.post) treatment(post) cluster(sid) unit(sid)

It is also only an issue when I include unit fixed effects using the unit option (as opposed to directly including them in the first stage). See: use https://github.com/scunning1975/mixtape/raw/master/castle.dta, clear gen incomenew = income / 10000 did2s l_homicide [aweight=popwt], first_stage(i.year i.sid c.incomenew##c.incomenew) second_stage(i.post) treatment(post) cluster(sid) did2s l_homicide [aweight=popwt], first_stage(i.year i.sid c.incomenew##c.incomenew) second_stage(i.post) treatment(post) cluster(sid)

A second issue is that when the unit option is used to include unit fixed effects, the package replaces the dependent variable with a de-meaned version. This doesn't affect the did2s estimate, but it would be preferable to leave the variable in its original form so it can be correctly used in later calculations. See the code below:

use https://github.com/scunning1975/mixtape/raw/master/castle.dta, clear sum l_homicide did2s l_homicide [aweight=popwt], first_stage(i.year) second_stage(i.post) treatment(post) cluster(sid) unit(sid) sum l_homicide

The summary stats for l_homicide change after did2s estimation with the unit option.

Thanks!

kylebutts commented 3 months ago

This is the problem:

A second issue is that when the unit option is used to include unit fixed effects, the package replaces the dependent variable with a de-meaned version.

I'm not entirely sure what's causing the bug (stata gets complicated quickly), but I should not be writing over variables in the user's dataset and need to change this code to use local variables: https://github.com/kylebutts/did2s_stata/blob/27893453e5a9983c3ee712eaf6754b7d277a64f7/ado/did2s.ado#L39-L49

Fixing that problem fixes the bug. Install the patch from github for now

dazzler2 commented 2 months ago

Thanks so much - this fixes it!

On Jul 3, 2024, at 6:14 PM, Kyle F Butts @.***> wrote:

This is the problem:

A second issue is that when the unit option is used to include unit fixed effects, the package replaces the dependent variable with a de-meaned version.

I'm not entirely sure what's causing the bug (stata gets complicated quickly), but I should not be writing over variables in the user's dataset and need to change this code to use local variables: https://github.com/kylebutts/did2s_stata/blob/27893453e5a9983c3ee712eaf6754b7d277a64f7/ado/did2s.ado#L39-L49

Fixing that problem fixes the bug. Install the patch from github for now

— Reply to this email directly, view it on GitHub https://github.com/kylebutts/did2s_stata/issues/15#issuecomment-2207396262, or unsubscribe https://github.com/notifications/unsubscribe-auth/APNML4NFQLCU3CYMDHKL2V3ZKRZUDAVCNFSM6AAAAABKKCQ4QGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBXGM4TMMRWGI. You are receiving this because you authored the thread.