Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • code for time-varying difference-in-difference with continuous treatment effect

    Hi all,

    I'm trying to run a generalized difference in difference model in which: 1) my treatment is an exposure measure which is continuous 2) all treatment happens at the same time 3) I want coefficients for 6 periods before and 6 periods after (multiple period) the treatment.

    While I managed to figure out how to to multiperiod DID with dummy treatment, also pre-post DID with continuous treatment, I do not know how to do multiperiod +continuous treatment. I notice CSDID is very similar, but it requires groupvar, which indicates the staggered timing of treatment, in my case, there is no staggered treatment, every firms receive treatment at the same time.

    Could you tell me how to write the code?

    My data looks like the following, my panel var is company, time var is yq which is the combination of year and quarter, my time of treatment is 2020 Q1 (yq = 9)

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long company float(year quarter yq Y X)
     20 2018 1  1     .3345259           0
     20 2018 2  2     .3867121           0
     20 2018 3  3    .38757765  .001242236
     20 2018 4  4     .4699386  .003680982
     20 2019 1  5     .5084837   .00134662
     20 2019 2  6     .4780842  .001161103
     20 2019 3  7     .4791728  .000590842
     20 2019 4  8      .524586  .002759659
     20 2020 1  9     .4418367 .0030612245
     20 2020 2 10     .6855295 .0008084074
     20 2020 3 11     .7842389 .0031776296
     20 2020 4 12     .7052954  .035303146
     20 2021 1 13     .5136103  .007879656
     20 2021 2 14     .5307393  .004669261
     20 2021 3 15     .6822985  .005284016
     20 2021 4 16    .56962025  .006329114
     20 2022 1 17     .5385101  .012626262
     20 2022 2 18      .599182  .016359918
     20 2022 3 19     .4473455  .014795475
     20 2022 4 20     .3908046           0
     20 2023 1 21     .1804636  .008278145
     20 2023 2 22    .25898203   .00748503
     20 2023 3 23    .38026315  .005263158
     20 2023 4 24        .4875       .0125
     20 2024 1 25     .4089834  .009456265
     42 2018 1  1    .14471544           0
     42 2018 2  2    .12573965           0
     42 2018 3  3    .13366337 .0024752475
     42 2018 4  4    .19058554 .0022962112
     42 2019 1  5     .1645343 .0081883315
     42 2019 2  6    .11969323  .005204054
     42 2019 3  7    .09982639 .0046296297
     42 2019 4  8    .12998447 .0010357328
     42 2020 1  9    .14482127  .002749771
     42 2020 2 10    .04451039 .0014836795
     42 2020 3 11    .08847185   .00536193
     42 2020 4 12    .07915994  .001615509
     42 2021 1 13         .125  .003640777
     42 2021 2 14    .13510393   .01039261
     42 2021 3 15    .11148649 .0022522523
     42 2021 4 16    .13131313           0
     42 2022 1 17     .1117502  .011503697
     42 2022 2 18    .09550118  .014996054
     42 2022 3 19     .0784463  .009139376
     42 2022 4 20    .12711865  .033898305
     42 2023 1 21    .11158799   .03755365
     42 2023 2 22    .17479675   .07723577
     42 2023 3 23    .17052375   .06699147
     42 2023 4 24     .1612903  .021505376
     42 2024 1 25      .237785   .07709012
     99 2018 1  1    .07964053   .03470716
     99 2018 2  2   .071911804  .027060887
     99 2018 3  3    .06167501   .02162039
     99 2018 4  4    .08274097  .029570656
     99 2019 1  5    .09370058  .034001056
     99 2019 2  6    .08564342   .02815874
     99 2019 3  7    .10279945   .02863699
     99 2019 4  8    .07494701  .018736754
     99 2020 1  9    .09755269  .022093814
     99 2020 2 10    .11160714      .03125
     99 2020 3 11    .11837862   .04836481
     99 2020 4 12    .08381503    .0342692
     99 2021 1 13    .10544554    .0490099
     99 2021 2 14     .1392261   .08877087
     99 2021 3 15     .1514671   .08643933
     99 2021 4 16     .1818182   .09818182
     99 2022 1 17     .1572008   .09634888
     99 2022 2 18     .1533052   .10759494
     99 2022 3 19     .1610487   .11985019
     99 2022 4 20    .22916667   .15833333
     99 2023 1 21    .22823147   .13737155
     99 2023 2 22    .15698925   .07612903
     99 2023 3 23     .2140745   .11295091
     99 2023 4 24    .22137405     .129771
     99 2024 1 25    .13088235   .04632353
    340 2018 3  3  .0003022975           0
    340 2019 3  7 .00005888241           0
    340 2019 4  8 .00005590027           0
    340 2021 3 15  .0002423655           0
    386 2018 1  1    .07692308           0
    386 2018 4  4    .06666667           0
    386 2019 1  5   .030612245           0
    386 2019 2  6   .018018018           0
    386 2019 3  7   .029126214           0
    386 2019 4  8   .009615385           0
    386 2020 3 11       .03125           0
    386 2022 2 18    .02173913           0
    454 2018 3  3  .0003763643           0
    454 2019 1  5 .00008494012           0
    454 2019 3  7 .00017667844           0
    454 2019 4  8 .00016466326           0
    454 2020 2 10   .000691085           0
    454 2020 3 11  .0018281536           0
    454 2020 4 12  .0005546312           0
    454 2023 1 21  .0004198153           0
    454 2023 3 23   .001174398           0
    549 2018 1  1   .006622517           0
    549 2018 2  2    .02094241           0
    549 2018 3  3   .012048192           0
    549 2018 4  4    .01388889           0
    end

    I've tried the following code following examples of multiperiod coefficient but dummy treatment code, and pre-post but continuous treatment code:

    Code:
    gen time_to_treat = yq-9
    tab time_to_treat
    //i will choose -6-6
    
    * interact pre-period with treat
    forvalues i = 6(-1)1{
        gen bef_`i'=(time_to_treat == -`i')
    }
    
    forvalues i = 6(-1)1{
        gen bef_`i'_dd=bef_`i'*X
    }
    * interact the event period with treat
    gen current = (time_to_treat == 0)
    gen curr_dd_ = current*X
    
    * interact post-period with treat
    forvalues i = 1(1)6{
        gen aft_`i'=(time_to_treat == `i')
    }
    forvalues i = 1(1)6{
        gen aft_`i'_dd=aft_`i'*X
    }
    
    // normalize t = -1
    drop bef_1 bef_1_dd_1
    xtreg Y bef_6 bef_5 bef_4 bef_3 bef_2 aft_1 aft_2 aft_3 aft_4 aft_5 aft_6 bef_*_dd aft_*_dd X, fe vce(robust)

  • #2
    Just to make a correction:
    My intended regression before forget t=0 interaction, it should be
    Code:
     
     xtreg Y bef_6 bef_5 bef_4 bef_3 bef_2 current aft_1 aft_2 aft_3 aft_4 aft_5 aft_6 bef_*_dd curr_dd aft_*_dd X, fe vce(robust)
    In terms of mathematics formula what I did correspond to the following, where i is firm, s is state, t is time:
    Code:
    $$y=\alpha +\sum_{j = -6}^{6}\lambda_t X \times \text{Time to  treat} +\sum_{j = -6}^{6}\gamma_t X+\sum_{j = -6}^{6}\beta_t \text{Time to  treat}+\epsilon$$
    
    I'm not sure how to add formula on the forum

    Comment


    • #3
      Unless FernandoRios may correct me CSDID should be able to work with staggered adoption or block assignment (or when everyone's treated at the same time). It does not work, though, with continuous treatments. The approach you truly seek is did_multiplegt_dyn.

      Comment


      • #4
        Originally posted by Jared Greathouse View Post
        Unless FernandoRios may correct me CSDID should be able to work with staggered adoption or block assignment (or when everyone's treated at the same time). It does not work, though, with continuous treatments. The approach you truly seek is did_multiplegt_dyn.
        Hi Jared,

        Thanks for point them out. I checked the document of did_multiplegt_dyn of its usage and example. Is it correct what I wrote:
        Code:
        did_multiplegt_dyn l_mkt_conc_yq company yq wfh_expo_1_yq, effects(7) effects_equal placebo(5) cluster(naics4d)
        One thing that confuses me is the graph normalize t = 0 coefficient to 0, where the title of the graph is "DID, from last period before treatment changes (t=0) to t", but usually we normalize t=-1, one period before the event time. My doubt is: t=0 in did_multiplegt_dyn case is the event period, or one period before event time?

        Go back to the code I wrote, I have 13 periods, enumerate them 1,2,3,4,5,6,7,8,9,10,11,12,13, where my treatment happens at 7, should i write
        Code:
         effect(6) placebo(6)
        , or
        Code:
        effects(7) placebo(5)
        if I want to normalize the coefficient of one period before the treatment?

        Comment


        • #5
          I haven't worked with Clement's estimators since my masters thesis days, so I'm not sure.

          Comment

          Working...
          X