chaisemartinPackages / did_multiplegt_dyn

|| Stata | R || Estimation of event-study Difference-in-Difference (DID) estimators in designs with multiple groups and periods, and with a potentially non-binary treatment that may increase or decrease multiple times.
38 stars 8 forks source link

Fails when providing weight and control due to missing weight variable #55

Closed beaslera closed 4 months ago

beaslera commented 4 months ago

When providing both the weight and control options, the collapse removes the weight variable, but weight is still used in later calls. As a result, the program fails due to a missing variable. I think the later uses should be replaced with weight_XX like here:

diff --git a/Stata/did_multiplegt_dyn.ado b/Stata/did_multiplegt_dyn.ado
index 8681a70..633af08 100644
--- a/Stata/did_multiplegt_dyn.ado
+++ b/Stata/did_multiplegt_dyn.ado
@@ -1394,7 +1394,7 @@ if L_u_XX!=.&L_u_XX!=0{
 * Perform the estimation of effects and placebos outside of the loop on
 * number of effects if trends_lin not specified
 if "`trends_lin'"==""{
-       did_multiplegt_dyn_core_new outcome_XX group_XX time_XX treatment_XX, effects(`=l_XX') placebo(`=l_placebo_XX') switchers_core(in) `only_never_switchers' controls(`controls') trends_nonparam(`trends_nonparam') `normalized' `same_switchers' `same_switchers_pl' `effects_equal' continuous(`continuous') `less_conservative_se' weight(`weight')
+       did_multiplegt_dyn_core_new outcome_XX group_XX time_XX treatment_XX, effects(`=l_XX') placebo(`=l_placebo_XX') switchers_core(in) `only_never_switchers' controls(`controls') trends_nonparam(`trends_nonparam') `normalized' `same_switchers' `same_switchers_pl' `effects_equal' continuous(`continuous') `less_conservative_se' weight(weight_XX)

        // Store the number of the event-study effect for switchers-in
                forv k = 1/`=l_XX' {
chaisemartinPackages commented 4 months ago

Dear Ryan, This is Diego from Clement de Chaisemartin's RA Team. Thanks for your interest in did_multiplegt_dyn! In the inner program, we use the weight local only to index the selection of a weighted estimator by the user. The actual weights are already generated in the outer program. Because of this, there should not be any issue in specifying both options at the same time. Here's a simulation showcasing that:

clear
set seed 0
local GG = 20
local TT = 10
set obs `=`GG' * `TT''

gen G = mod(_n-1, `GG') + 1
bys G: gen T = _n
sort G T

sum G
gen D = uniform() > 0.5
gen Y = uniform() * (1 + D)

gen weight_temp = floor(uniform()*10)
bys G T: egen weight = sum(weight_temp)
drop weight_temp
gen X = uniform()

did_multiplegt_dyn Y G T D, weight(weight) effects(3) placebo(1) controls(X)

If you have issues with this setting, please provide us with a minimal dataset where this error replicates. In this way, we will be able to debug the program. This is our team email: chaisemartin.packages@gmail.com Feel free to contact us! Best, Diego

beaslera commented 4 months ago

Howdy Diego, Thank you very much for your quick response, and I very much appreciate that you sent a barebones example I could start from. That example does not trigger the collapse, which is the source of the problem.

Below is an example that causes aggregated_data to be set to 0 in ///// Collapse and weight The collapse then happens, which removes the weight' variable. (I've named it "WWWW" here so Stata does not think I meant "weight_XX" when I type "weight".) Then when trying to rundid_multiplegt_dyn_core_new ... weight(weight')... the program fails with

variable WWWW not found (error in option weight())

clear

set seed 0 local GG = 20 local TT = 20 local CC = 2 set obs =GG' `TT' `CC''

gen G = mod(_n-1, GG') + 1 sort G gen T = mod(_n-1,TT') + 1 sort G T gen C = mod(_n-1, `CC') sort G T C

gen D = uniform() > 0.5 gen Y = uniform() * (1 + D)

gen weight_temp = floor(uniform()*10) bys G T C: egen WWWW = sum(weight_temp) drop weight_temp

did_multiplegt_dyn Y G T D, weight(WWWW) effects(2) placebo(1) controls(C)

Respectfully, ~Ryan

On Thu, Jun 20, 2024 at 2:32 AM Clément de Chaisemartin - ERC Repository < @.***> wrote:

Dear Ryan, This is Diego from Clement de Chaisemartin's RA Team. Thanks for your interest in did_multiplegt_dyn! In the inner program, we use the weight local only to index the selection of a weighted estimator by the user. The actual weights are already generated in the outer program. Because of this, there should not be any issue in specifying both options at the same time. Here's a simulation showcasing that:

clear set seed 0local GG = 20local TT = 10 set obs =GG' * TT'' gen G = mod(_n-1,GG') + 1bys G: gen T = _n sort G T

sum Ggen D = uniform() > 0.5gen Y = uniform() (1 + D) gen weight_temp = floor(uniform()10)bys G T: egen weight = sum(weight_temp) drop weight_tempgen X = uniform()

did_multiplegt_dyn Y G T D, weight(weight) effects(3) placebo(1) controls(X)

If you have issues with this setting, please provide us with a minimal dataset where this error replicates. In this way, we will be able to debug the program. This is our team email: @.*** Feel free to contact us! Best, Diego

— Reply to this email directly, view it on GitHub https://github.com/chaisemartinPackages/did_multiplegt_dyn/issues/55#issuecomment-2180008983, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAPRRX74RXTKVTHJPIUNDUTZIKAQPAVCNFSM6AAAAABJSQVBE2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBQGAYDQOJYGM . You are receiving this because you authored the thread.Message ID: @.***>

karinwutw commented 4 months ago

I encountered the same problem after updating to the most recent package two days ago. I used weights that are more disaggregated than the group-period level (e.g., individual-period) and was unable to get the weights to work using the code that initially functioned correctly.

fe-knau commented 4 months ago

Dear Ryan,

You were absolutely right, there was exactly this issue as you described it with the weight variable in scenarios when the data is collapsed (this was also the source of the error when using the bootstrap option + weights several other users have reported as drawing bootstrap samples with replacement will trigger the collapse inside the package), thanks a lot for spotting this and providing so many details about its origin! We have adjusted this and will fix it within the next update of the package.

Much appreciated and best regards, Felix