Announcement

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

  • Staggered Diff-in-Diff: Visualization of Treatment Effects Over Time

    Dear Statalists,

    I try to visually evaluate pre-treatment trends using a staggered diff-in-diff model. Basically, I want to plot the coefficient estimates for treatment effects for each time period both before and after treatment period 0, as well as the confidence intervals of the estimates. My data looks like this:
    DV Company Quarter Post Treat Relative_Treat Relative_Treat_noNegative
    3 A 190 0 1 -1 1
    4 A 191 0 1 0 2
    2 A 192 1 1 1 3
    4 A 193 1 1 2 4
    5 A 194 1 1 3 5
    2 B 190 0 0 nan nan
    4 B 191 0 0 nan nan
    1 B 192 0 0 nan nan
    3 B 193 0 0 nan nan
    4 C 190 0 1 -2 0
    5 C 191 0 1 -1 1
    6 C 192 0 1 0 2
    1 C 193 1 1 1 3
    3 C 194 1 1 2 4
    For the DiD regression I used the following code:
    Code:
    xtset Company Quarter
    xtreg DV i.Post#i.Treat i.Quarter, fe
    Is there a difference between calculating the interaction term beforehand and then simply including the newly calculated variable (Post * Treat) into the model?


    Now I would like to assess the pretreatment trends. So far, I used the following code:
    Code:
    xtset Company Quarter
    xtreg DV i.Quarter##i.Relative_Treat_noNegative i.Quarter, fe
    margins Quarter#Relative_Treat
    marginsplot
    This does so far not result in a usable plot and takes quite some time to calculate. Is this the correct approach?
    Last edited by Chris Hellig; 24 Jun 2019, 11:26. Reason: difference-in-differences

  • #2
    Your data tableau does not reflect your actual Stata data, as there is no "nan" in Stata. I suppose those are supposed to represent missing values? Also, you could not have -xtset Company Quarter- when Company appears to be a string variable. I imagine the actual data have numeric values there with value labels attached. Please read the Forum FAQ for excellent advice on how to get the most out of your Statalist experience. In FAQ #12, in particular, you will learn about using the -dataex- command to show actual example data from your Stata data set that those who want to help you can work with.

    Is there a difference between calculating the interaction term beforehand and then simply including the newly calculated variable (Post * Treat) into the model?
    You will get the same results either way. But if you calculate Post*Treat and include it in the model you will be unable to use -margins- afterwards. So there aren't many circumstances where it makes sense to do that.

    Concerning your pre-treatment trends, I see that you have created a non-negative integer version of your Relative_Treat variable. You did that, I presume, because you wanted to use it in your interaction term, and you can't have negative numbers in variables prefixed with i.. But your transformation of it actually makes no sense as a meaningful variable because, for example, the same numbers in Relative_treat_nonegative mean different things in different companies. The good news is that there is no reason to use the i.prefix with Relative_Treat: it is conceptually a continuous variable, and you should model it as such. I think the following will get you what you want.
    Code:
    xtreg DV Relative_Treat, fe
    margins,  at(Relative_Treat = (-5(1)5))
    marginsplot
    Note: I just picked -5(1)5 because I guessed from your example that you have something like 5 years of pre-treatment and 5 years of post-treatment data. But replace that numlist by whatever is actually appropriate to your data.



    Comment


    • #3
      Clyde Schechter

      This is something I’ve been meaning to ask. I hope you don’t mind if I ask here since it’s related this thread’s topic and fresh on my mind. Is there a general rule to follow for visualizing the effect of a treatment over time? I tried to adapt your code to my example, but couldn’t follow the data example in this case. Would hope you could help me out with some example data below. Here are quarterly data for rents by market, with a particular market receiving a treatment. How can I visualize the treatment across quarters after the intervention?

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int quarter double rent float(id treat period)
      204 1020 1 1 0
      205 1015 1 1 0
      206 1029 1 1 0
      207 1025 1 1 0
      208 1016 1 1 0
      209 1034 1 1 0
      210 1029 1 1 0
      211 1030 1 1 0
      212 1031 1 1 0
      213 1031 1 1 0
      214 1035 1 1 0
      215 1089 1 1 0
      216 1073 1 1 0
      217 1076 1 1 0
      218 1087 1 1 0
      219 1091 1 1 0
      220 1106 1 1 0
      221 1123 1 1 0
      222 1148 1 1 0
      223 1179 1 1 0
      224 1207 1 1 1
      225 1243 1 1 1
      226 1262 1 1 1
      227 1300 1 1 1
      228 1308 1 1 1
      229 1341 1 1 1
      230 1379 1 1 1
      231 1420 1 1 1
      232 1467 1 1 1
      233 1527 1 1 1
      234 1584 1 1 1
      235 1597 1 1 1
      236 1610 1 1 1
      204 1127 2 0 0
      205 1129 2 0 0
      206 1139 2 0 0
      207 1153 2 0 0
      208 1155 2 0 0
      209 1161 2 0 0
      210 1169 2 0 0
      211 1175 2 0 0
      212 1174 2 0 0
      213 1177 2 0 0
      214 1185 2 0 0
      215 1192 2 0 0
      216 1191 2 0 0
      217 1195 2 0 0
      218 1197 2 0 0
      219 1200 2 0 0
      220 1201 2 0 0
      221 1210 2 0 0
      222 1249 2 0 0
      223 1260 2 0 0
      224 1258 2 0 1
      225 1267 2 0 1
      226 1292 2 0 1
      227 1291 2 0 1
      228 1335 2 0 1
      229 1334 2 0 1
      230 1353 2 0 1
      231 1425 2 0 1
      232 1450 2 0 1
      233 1515 2 0 1
      234 1525 2 0 1
      235 1574 2 0 1
      236 1578 2 0 1
      204  793 3 0 0
      205  794 3 0 0
      206  795 3 0 0
      207  793 3 0 0
      208  795 3 0 0
      209  798 3 0 0
      210  801 3 0 0
      211  800 3 0 0
      212  806 3 0 0
      213  815 3 0 0
      214  817 3 0 0
      215  822 3 0 0
      216  828 3 0 0
      217  833 3 0 0
      218  838 3 0 0
      219  852 3 0 0
      220  863 3 0 0
      221  899 3 0 0
      222  918 3 0 0
      223  942 3 0 0
      224  956 3 0 1
      225  976 3 0 1
      226 1016 3 0 1
      227 1051 3 0 1
      228 1039 3 0 1
      229 1046 3 0 1
      230 1041 3 0 1
      231 1068 3 0 1
      232 1070 3 0 1
      233 1079 3 0 1
      234 1117 3 0 1
      235 1116 3 0 1
      236 1128 3 0 1
      end
      format %tq!Qq-YY quarter
      
      xtset id quarter
      xtreg rent i.treat##i.period, fe

      Comment


      • #4
        Your situation is different from the one in this thread. You have a classical DID model, not a staggered one. Your situation is much simpler. For your data I would do this as:

        Code:
        xtreg rent i.treat##i.quarter, fe
        margins treat#quarter, noestimcheck
        marginsplot, xdimension(quarter)

        Comment


        • #5
          Got it. Thanks, Clyde.

          Comment


          • #6
            First of all, thank you for your time Clyde! Below you will find an data example:
            DV Company Quarter Post Treat Relative_Treat
            3,988984 1 223 0 0 0
            3,772761 1 225 0 0 0
            4,553877 1 226 0 0 0
            3,044522 1 227 0 0 0
            3,569532 1 230 0 0 0
            3,688879 1 232 0 0 0
            0,693147 1 234 0 0 0
            3,784191 2 195 0 1 -32
            5,204007 2 204 0 1 -23
            5,393628 2 207 0 1 -20
            5,774551 2 212 0 1 -15
            2,197226 2 213 0 1 -14
            2,772589 2 214 0 1 -13
            3,198673 2 215 0 1 -12
            4,007333 2 218 0 1 -9
            4,836282 2 222 0 1 -5
            4,234105 2 223 0 1 -4
            3,258965 2 224 0 1 -3
            5,198497 2 225 0 1 -2
            3,737227 2 226 0 1 -1
            3,557575 2 227 0 1 0
            3,871201 2 228 1 1 1
            2,197226 2 229 1 1 2
            4,102646 2 230 1 1 3
            4,973279 2 231 1 1 4
            4,143146 2 232 1 1 5
            5,433285 2 233 1 1 6
            3,921973 2 234 1 1 7
            1,609438 3 222 0 0 0
            3,433987 3 226 0 0 0
            4,859812 3 229 0 0 0
            5,010635 3 230 0 0 0
            4,867535 3 231 0 0 0
            I further tested your code below, thank you!

            Code:
            xtreg DV Relative_Treat, fe
            margins,  at(Relative_Treat = (-5(1)5))
            marginsplot
            Nonetheless, what I am really looking for is a plot that looks like this. Just ploting the margins for "xtreg DV Relative_Treat, fe" will result in a linear line since I'm basically ploting the regression results. Maybe I just can't follow their approach here. I'm thankful for any help!
            Click image for larger version

Name:	pretreat.PNG
Views:	1
Size:	22.5 KB
ID:	1505638

            Click image for larger version

Name:	pretreat2.PNG
Views:	1
Size:	30.4 KB
ID:	1505639

            Comment

            Working...
            X