Announcement

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

  • Difference in Difference with Three Time Periods

    For my paper, I am looking to do a three period Difference in Difference Analysis of electoral outcomes on crime. My three periods are represented by the variable time_period which takes on the options of "pre_election", "election" and "post_election". My treated and control groups are represented by the variable treated which takes on the value 0 or 1. The complication arises from the fact that my treatement period is my election time period and my control periods are both pre and post election.

    I am confused about how exactly to go about this. I tried using didregress but I think that doesn't work with 3 time periods. Before running a regression however, I want to plot a graph with two lines for the treated and control groups and two vertical lines so three vertical divisions across my x axis for my graph.

    Any help is appreciated!
    This is my first time using statalist so I hope my coding re the data is fine.

    Code:
    input str18 District str44 PS float(Year sec188_ps_quarter_1000k treated) str13 time_period float quarter
    "CENTRAL"        "KAMLA MARKET"    2017 74.312485 0 "election"      2
    "OUTER NORTH"    "BHALSWA DAIRY"   2017  12.92194 1 "post_election" 3
    "NORTH"          "SARAI ROHILLA"   2017         0 1 "election"      2
    "SOUTH EAST"     "SARITA VIHAR"    2017         0 0 "post_election" 4
    "EAST"           "LAXMI NAGAR"     2017         0 0 "election"      2
    "EAST"           "PANDAV NAGAR"    2016  41.60357 0 "pre_election"  2
    "WEST"           "VIKASPURI"       2019  82.23737 1 ""              1
    "SOUTH WEST"     "INDER PURI"      2016  9.841681 0 "election"      4
    "CENTRAL"        "ANAND PARBAT"    2018         0 0 ""              3
    "SHAHDARA"       "FARASH BAZAR"    2018    80.444 0 ""              3
    "NORTH EAST"     "KARAWAL NAGAR"   2018 25.534346 0 "post_election" 1
    "CENTRAL"        "JAMA MASJID"     2018         0 0 ""              2
    "NORTH WEST"     "MUKHERJEE NAGAR" 2017  324.8565 0 "post_election" 3
    "EAST"           "ANAND VIHAR"     2016         0 0 "election"      4
    "SHAHDARA"       "G.T.B.ENCLAVE"   2019  42.43609 0 ""              2
    "SHAHDARA"       "GANDHI NAGAR"    2017         0 0 "election"      1
    "OUTER NORTH"    "BAWANA"          2017  45.44059 0 "election"      1
    "SOUTH"          "GREATER KAILASH" 2018 292.96277 0 ""              3
    "NORTH EAST"     "JYOTI NAGAR"     2017  77.86678 1 "election"      1
    "OUTER DISTRICT" "MUNDKA"          2019 15.842152 1 ""              2
    end



    .

  • #2
    For a DID regression, you need both pre-treatment and post-treatment periods for treated and untreated units. Perhaps what you’re showing is just a subset of your full dataset, but I don’t see any pre-treatment observations (year = 2016) for the treated units.

    Comment


    • #3
      Sreejita:
      in addition to Andrew's helpful reply, you may want to take a look at: https://www.princeton.edu/~otorres/DID101.pdf.
      Kind regards,
      Carlo
      (StataNow 18.5)

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        For a DID regression, you need both pre-treatment and post-treatment periods for treated and untreated units. Perhaps what you’re showing is just a subset of your full dataset, but I don’t see any pre-treatment observations (year = 2016) for the treated units.
        Hello, thank you for your response.

        Yes you're right what I'm showing is just a small subset and I didn't realise enough pre electio observations didn't get included, hopefully this is better.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str44 PS float Year str18 District float(quarter treated) str13 time_period float sec188_ps_quarter_1000k
        "ADARSH NAGAR"       2017 "NORTH WEST"     1 0 "election"              0
        "ADARSH NAGAR"       2017 "NORTH WEST"     3 0 "election"              0
        "ADARSH NAGAR"       2017 "NORTH WEST"     4 0 "post_election"         0
        "ADARSH NAGAR"       2018 "NORTH WEST"     1 0 "post_election" 13.261588
        "ADARSH NAGAR"       2018 "NORTH WEST"     2 0 "post_election"         0
        "ADARSH NAGAR"       2016 "NORTH WEST"     2 0 "pre_election"          0
        "ADARSH NAGAR"       2016 "NORTH WEST"     4 0 "pre_election"          0
        "ADARSH NAGAR"       2016 "NORTH WEST"     3 0 "pre_election"          0
        "ADARSH NAGAR"       2017 "NORTH WEST"     2 0 "election"              0
        "AMAN VIHAR"         2018 "OUTER DISTRICT" 2 1 "post_election"         0
        "AMAN VIHAR"         2017 "ROHINI"         1 1 "election"       63.74356
        "AMAN VIHAR"         2017 "ROHINI"         3 1 "election"       5.311963
        "AMAN VIHAR"         2018 "OUTER DISTRICT" 1 1 "post_election" 23.903835
        "AMAN VIHAR"         2017 "ROHINI"         4 1 "post_election" 2.6559815
        "AMAN VIHAR"         2017 "ROHINI"         2 1 "election"              0
        "AMAR COLONY"        2017 "SOUTH EAST"     2 0 "election"      17.247198
        "AMAR COLONY"        2017 "SOUTH EAST"     3 0 "election"              0
        "AMAR COLONY"        2018 "SOUTH-EAST"     2 0 "post_election" 17.247198
        "AMAR COLONY"        2018 "SOUTH-EAST"     1 0 "post_election" 103.48318
        "AMAR COLONY"        2016 "SOUTH-EAST"     3 0 "pre_election"   310.4496
        "AMAR COLONY"        2017 "SOUTH EAST"     1 0 "election"       155.2248
        "AMAR COLONY"        2017 "SOUTH EAST"     4 0 "post_election"         0
        "AMAR COLONY"        2016 "SOUTH-EAST"     2 0 "pre_election"  327.69675
        "AMAR COLONY"        2016 "SOUTH-EAST"     4 0 "pre_election"  34.494396
        "AMBEDKAR NAGAR"     2017 "SOUTH"          2 0 "election"              0
        "AMBEDKAR NAGAR"     2017 "SOUTH"          4 0 "post_election" 17.026814
        "AMBEDKAR NAGAR"     2016 "SOUTH-EAST"     2 0 "pre_election"  17.026814
        "AMBEDKAR NAGAR"     2016 "SOUTH-EAST"     4 0 "pre_election"          0
        "AMBEDKAR NAGAR"     2018 "SOUTH"          2 0 "post_election"  39.72923
        "AMBEDKAR NAGAR"     2017 "SOUTH"          1 0 "election"      102.16088
        "AMBEDKAR NAGAR"     2016 "SOUTH-EAST"     3 0 "pre_election"  17.026814
        "AMBEDKAR NAGAR"     2018 "SOUTH"          1 0 "post_election" 17.026814
        "AMBEDKAR NAGAR"     2017 "SOUTH"          3 0 "election"              0
        "ANAND PARBAT"       2018 "CENTRAL"        1 0 "post_election" 242.56982
        "ANAND PARBAT"       2018 "CENTRAL"        2 0 "post_election"         0
        "ANAND PARBAT"       2016 "CENTRAL"        3 0 "pre_election"  116.01166
        "ANAND PARBAT"       2017 "CENTRAL"        3 0 "election"       147.6512
        "ANAND PARBAT"       2016 "CENTRAL"        4 0 "pre_election"          0
        "ANAND PARBAT"       2016 "CENTRAL"        2 0 "pre_election"  116.01166
        "ANAND PARBAT"       2017 "CENTRAL"        4 0 "post_election"  84.37211
        "ANAND PARBAT"       2017 "CENTRAL"        1 0 "election"      242.56982
        "ANAND PARBAT"       2017 "CENTRAL"        2 0 "election"      21.093027
        "ANAND VIHAR"        2017 "SHAHDARA"       2 0 "election"              0
        "ANAND VIHAR"        2018 "SHAHDARA"       1 0 "post_election"  20.35064
        "ANAND VIHAR"        2017 "SHAHDARA"       4 0 "post_election"         0
        "ANAND VIHAR"        2017 "SHAHDARA"       1 0 "election"       101.7532
        "ANAND VIHAR"        2016 "EAST"           4 0 "pre_election"          0
        "ANAND VIHAR"        2018 "SHAHDARA"       2 0 "post_election"  20.35064
        "ANAND VIHAR"        2016 "EAST"           3 0 "pre_election"          0
        "ANAND VIHAR"        2016 "EAST"           2 0 "pre_election"          0
        "ANAND VIHAR"        2017 "SHAHDARA"       3 0 "election"       40.70127
        "ASHOK VIHAR"        2018 "NORTH WEST"     1 0 "post_election"         0
        "ASHOK VIHAR"        2016 "NORTH WEST"     2 0 "pre_election"   47.08516
        "ASHOK VIHAR"        2017 "NORTH WEST"     2 0 "election"              0
        "ASHOK VIHAR"        2017 "NORTH WEST"     3 0 "election"              0
        "ASHOK VIHAR"        2018 "NORTH WEST"     2 0 "post_election"         0
        "ASHOK VIHAR"        2017 "NORTH WEST"     4 0 "post_election"         0
        "ASHOK VIHAR"        2016 "NORTH WEST"     4 0 "pre_election"          0
        "ASHOK VIHAR"        2016 "NORTH WEST"     3 0 "pre_election"   47.08516
        "ASHOK VIHAR"        2017 "NORTH WEST"     1 0 "election"       23.54258
        "BABA HARIDAS NAGAR" 2018 "DWARKA"         1 1 "post_election"  54.71367
        "BABA HARIDAS NAGAR" 2017 "DWARKA"         1 1 "election"       43.77094
        "BABA HARIDAS NAGAR" 2016 "SOUTH WEST"     3 1 "pre_election"  10.942734
        "BABA HARIDAS NAGAR" 2017 "DWARKA"         4 1 "post_election"  21.88547
        "BABA HARIDAS NAGAR" 2016 "SOUTH WEST"     2 1 "pre_election"  10.942734
        "BABA HARIDAS NAGAR" 2018 "DWARKA"         2 1 "post_election"         0
        "BABA HARIDAS NAGAR" 2016 "SOUTH WEST"     4 1 "pre_election"          0
        "BABA HARIDAS NAGAR" 2017 "DWARKA"         2 1 "election"              0
        "BABA HARIDAS NAGAR" 2017 "DWARKA"         3 1 "election"              0
        "BADAR PUR"          2017 "SOUTH EAST"     4 0 "post_election"         0
        "BADAR PUR"          2016 "SOUTH-EAST"     2 0 "pre_election"   91.27331
        "BADAR PUR"          2016 "SOUTH-EAST"     4 0 "pre_election"          0
        "BADAR PUR"          2017 "SOUTH EAST"     1 0 "election"       39.11713
        "BADAR PUR"          2016 "SOUTH-EAST"     3 0 "pre_election"   91.27331
        "BADAR PUR"          2017 "SOUTH EAST"     2 0 "election"              0
        "BADAR PUR"          2017 "SOUTH EAST"     3 0 "election"              0
        "BARA HINDU RAO"     2017 "NORTH"          1 1 "election"              0
        "BARA HINDU RAO"     2016 "NORTH"          3 1 "pre_election"  533.36053
        "BARA HINDU RAO"     2017 "NORTH"          3 1 "election"      266.68027
        "BARA HINDU RAO"     2017 "NORTH"          4 1 "post_election" 33.335033
        "BARA HINDU RAO"     2018 "NORTH"          2 1 "post_election"         0
        "BARA HINDU RAO"     2016 "NORTH"          2 1 "pre_election"  533.36053
        "BARA HINDU RAO"     2016 "NORTH"          4 1 "pre_election"          0
        "BARA HINDU RAO"     2017 "NORTH"          2 1 "election"              0
        "BARA HINDU RAO"     2018 "NORTH"          1 1 "post_election" 233.34523
        "BAWANA"             2017 "OUTER NORTH"    2 0 "election"      11.601852
        "BAWANA"             2017 "OUTER NORTH"    4 0 "post_election"  4.834105
        "BAWANA"             2018 "ROHINI"         1 0 "post_election"  5.800926
        "BAWANA"             2017 "OUTER NORTH"    1 0 "election"       45.44059
        "BAWANA"             2018 "ROHINI"         2 0 "post_election"  1.933642
        "BAWANA"             2017 "OUTER NORTH"    3 0 "election"       44.47377
        "BHAJAN PURA"        2016 "NORTH EAST"     3 1 "pre_election"          0
        "BHAJAN PURA"        2017 "NORTH EAST"     4 1 "post_election"  46.22381
        "BHAJAN PURA"        2018 "NORTH EAST"     2 1 "post_election"         0
        "BHAJAN PURA"        2017 "NORTH EAST"     3 1 "election"      26.413605
        "BHAJAN PURA"        2017 "NORTH EAST"     2 1 "election"       6.603401
        "BHAJAN PURA"        2016 "NORTH EAST"     2 1 "pre_election"          0
        "BHAJAN PURA"        2016 "NORTH EAST"     4 1 "pre_election"          0
        "BHAJAN PURA"        2018 "NORTH EAST"     1 1 "post_election" 19.810204
        "BHAJAN PURA"        2017 "NORTH EAST"     1 1 "election"       66.03401
        end
        Unfortunately I cannot edit the original post but I hope this is better.
        Last edited by Sreejita Roy; 07 Apr 2025, 02:47.

        Comment


        • #5
          Originally posted by Carlo Lazzaro View Post
          Sreejita:
          in addition to Andrew's helpful reply, you may want to take a look at: https://www.princeton.edu/~otorres/DID101.pdf.
          Thank you, I will have a look!

          My biggest issue is that while there is literature and resources for DiD across multiple time periods, they mainly focus on scenarios where different groups receive treatment at different periods. Whereas in my situation, all groups receive treatment at the same time (election), I am just defining my entire panel into three groups.

          Comment


          • #6
            Originally posted by Sreejita Roy View Post
            Whereas in my situation, all groups receive treatment at the same time (election), I am just defining my entire panel into three groups.
            In a DID analysis, not all groups can be treated. Otherwise, there's no way to determine whether the treatment had an effect. Think of it in terms of a classical randomized controlled trial (RCT) in a medical study. If you give the drug to everyone, how can you tell whether it was effective if some patients show improvement? They might have recovered simply due to the passage of time—like with a bout of the flu. The only way to observe a treatment effect is by comparing a group that received the drug to one that did not (the control group).

            Comment


            • #7
              Originally posted by Andrew Musau View Post

              In a DID analysis, not all groups can be treated. Otherwise, there's no way to determine whether the treatment had an effect. Think of it in terms of a classical randomized controlled trial (RCT) in a medical study. If you give the drug to everyone, how can you tell whether it was effective if some patients show improvement? They might have recovered simply due to the passage of time—like with a bout of the flu. The only way to observe a treatment effect is by comparing a group that received the drug to one that did not (the control group).
              Thank you for your patience.

              I understand this - but I do have a control and treatment group. I am defining my treatment group as the group of PS jurisdictions that have a majority (>0.5) of its constituencies made up of close contests. My control group is the remaining that do not have thus majority (<0.5)

              My only difference from a conventional DiD is that I have three instead of two periods - pre election, election and post election. I should ideally see non zero results only for the election period.

              This is how I approached it :

              Code:
              xtset ps year_quarter
              . xtreg sec188_ps_quarter_1000k treated1 pre_election post_election election interaction
              > _pre1 interaction_election1 interaction_post_election1 muslim_proportion sc_proportion
              > literate_proportion mainwork_employment margwork_employment , re robust
              note: election omitted because of collinearity.
              note: interaction_post_election1 omitted because of collinearity.
              
              Random-effects GLS regression Number of obs = 1,238
              Group variable: ps Number of groups = 140
              
              R-squared: Obs per group:
              Within = 0.0124 min = 6
              Between = 0.1899 avg = 8.8
              Overall = 0.0781 max = 9
              
              Wald chi2(10) = 28.55
              corr(u_i, X) = 0 (assumed) Prob > chi2 = 0.0015
              
              (Std. err. adjusted for 140 clusters in ps)
              ---------------------------------------------------------------------------------------
              | Robust
              sec188_ps_quart~1000k | Coefficient std. err. z P>|z| [95% conf. interval]
              ----------------------+----------------------------------------------------------------
              treated | -2.048139 9.446461 -0.22 0.828 -20.56286 16.46659
              pre_election | 6.173922 6.823476 0.90 0.366 -7.199846 19.54769
              post_election | -8.398214 6.598486 -1.27 0.203 -21.33101 4.534581
              election | 0 (omitted)
              interaction_pre1 | 23.32229 17.47306 1.33 0.182 -10.92429 57.56886
              interaction_election1 | 19.41411 9.794151 1.98 0.047 .2179242 38.61029
              interaction_post_el~1 | 0 (omitted)
              control 1 | 211.8057 72.41369 2.92 0.003 69.87748 353.7339
              control 2 | 189.304 84.62732 2.24 0.025 23.43751 355.1705
              control 3 | -19.17801 160.1269 -0.12 0.905 -333.021 294.665
              control 4 | 415.3198 335.727 1.24 0.216 -242.6929 1073.333
              control 5 | -112.7235 672.8338 -0.17 0.867 -1431.454 1206.007
              _cons | -130.2321 86.87045 -1.50 0.134 -300.495 40.03089
              ----------------------+----------------------------------------------------------------
              sigma_u | 49.171832
              sigma_e | 87.853852
              rho | .23853876 (fraction of variance due to u_i)
              ---------------------------------------------------------------------------------------
              I cannot use a fixed effects model as my treated dummy does not vary within a PS since the treatment is on the PS itself so my treatment dummies and interaction get dropped. Nonethless I ran it so I could do a Hausman Test to see if a random effects model would be fine and got this :

              Code:
              . hausman re_model fe_model
              
              ---- Coefficients ----
              | (b) (B) (b-B) sqrt(diag(V_b-V_B))
              | re_model fe_model Difference Std. err.
              -------------+----------------------------------------------------------------
              pre_election | 6.173922 5.8698 .3041217 1.024753
              post_elect~n | -8.398214 -7.417827 -.9803865 1.093868
              interacti~e1 | 23.32229 24.50798 -1.185692 2.088922
              ~n_election1 | 19.41411 20.39449 -.9803865 2.200095
              ------------------------------------------------------------------------------
              b = Consistent under H0 and Ha; obtained from xtreg.
              B = Inconsistent under Ha, efficient under H0; obtained from xtreg.
              
              Test of H0: Difference in coefficients not systematic
              
              chi2(4) = (b-B)'[(V_b-V_B)^(-1)](b-B)
              = 1.68
              Prob > chi2 = 0.7942
              I think this means an re model is fine? Let me know what you think! Thanks again for your help!
              Last edited by Sreejita Roy; 07 Apr 2025, 09:03.

              Comment


              • #8
                The issue I'd say isn't the number of periods. Whether there are two or three periods, you would just define the period before the treatment as "pre" and the period after as "post". However, one important clarification: in the context of DiD, a “treatment” typically refers to an actual event, policy change, or intervention that occurs at a certain point in time and affects some units but not others.

                In your case, it sounds like you’re classifying units based on a characteristic (i.e., the proportion of close contests in constituencies), rather than identifying a specific policy or event that happens to the treatment group and not the control group. So, rather than a treatment/control distinction based on exposure to an external shock or intervention, it seems more like you're defining subsamples based on a continuous or threshold-based feature. That’s perfectly valid for analysis, but it’s a different setup from a classical DiD, where identification hinges on comparing how outcomes change before and after a treatment that only some units receive.

                So, I'd set it up as a regular panel regression model with unit and time effects. There's no need to add dummies for the periods, as these will be collinear with the time effects. You should also rename the groups to better convey what they represent (instead of 'treated' and 'control').

                Comment

                Working...
                X