Announcement

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

  • Plotting parallel trends/DiD regression not working

    Hi, I am doing a DiD on affective polarisation between parties from 2022-2023 and that works fine. However, now I want to plot the pre-treatment trends and therefore merged data from 2011, 2015 and 2019 into my dataset. I am wanting to do a plot that one of my professors has a dofile for. apRBS1 is my outcome variable, TG =0 if control and =1 if treatment group, and year is my time variable, I have repeated cross-sectional data which is why I don't use cluster. The command looks like this:
    reg apRBS1 TG##year, r

    margins, at(TG=(0 1) year=(2011 (4) 2023)) post

    mat A = e(b)

    global M00 = A[1,1]

    global M01 = A[1,2]

    global M02 = A[1,3]

    global M03 = A[1,4]

    global M04 = A[1,5]

    global M10 = A[1,7]

    global M11 = A[1,8]

    global M12 = A[1,9]

    global M13 = A[1,10]

    global M14 = A[1,11]

    global M10c = A[1,7]

    global M11c = A[1,7]+(A[1,2]-A[1,1])

    global M12c = A[1,9]+(A[1,3]-A[1,2])

    global M13c = A[1,9]+(A[1,4]-A[1,2])

    global M14c = A[1,9]+(A[1,5]-A[1,2])

    tw (scatteri $M00 2011 $M01 2015 $M02 2019 $M03 2022 $M04 2023, connect(line) m(o) mc(gs10) lc(gs10)) (scatteri $M10c 2011 $M11c 2015 $M12c 2019 $M13c 2022 $M14c 2023, connect(line) m(o) mc(gs10) lc(gs10) mc(gs10) lp(dash)) (scatteri $M10 2011 $M11 2015 $M12 2019 $M13 2022 $M14 2023, connect(line) m(o)), xlabel(2011 (4) 2023) xscale(range(2010.9 2023.1)) ylabel(,nogrid) xtitle(År) ytit("Affektiv polarisering", size(medsmall)) legend(label (3 "Treatmentgruppens udvikling") label (2 "Kontrakfaktisk udvikling") label (1 "Kontrolgruppens udvikling") order(1 3 2) position(6))

    With control variables it works except
    margins, at(TG=(0 1) year=(2011 (4) 2023)) post "at level for factor year not present during estimation r(198);"

    And the plot doesn't look right since for my control group it says that it has the maximum level of my outcome variable in 2015, which is not true.

    I tried to run it without controls but running the regression it says:

    note: 0b.TG#2011b.year identifies no observations in the sample.

    note: 0b.TG#2015.year identifies no observations in the sample.

    note: 0b.TG#2019.year identifies no observations in the sample.

    note: 1.TG#2015.year omitted because of collinearity.

    note: 1.TG#2019.year omitted because of collinearity.

    note: 1.TG#2023.year omitted because of collinearity.

    however it does want to do the margins now. When I tab TG and year, there are observations for all values in all years, so I don't get why it says this^^.

    I am not that good with graphs in stata, so I have looked at lots of posts in here for alternative plots or explanations but most of the commands people give don't work for me and I can't seem to figure out what is wrong. (I have done a simple plot of means over time for control- and treatment group but that is just not as accurate as the DiD post one).

    Sorry for the long post, I hope someone can help me! Thank you!

  • #2
    Hi, I am doing a DiD on affective polarisation between parties from 2022-2023 and that works fine. However, now I want to plot the pre-treatment trends and therefore merged data from 2011, 2015 and 2019 into my dataset.
    In DID applications, the pre-treatment period is part of the data used in the estimation. That is exactly what margins is telling you with

    "at level for factor year not present during estimation r(198);"
    You do not show what model you estimate, but with no pre-treatment period, it is not DID. In any case, if you have Stata 17 or later versions, Stata can do all this for you automatically. See

    Code:
    help estat trendplots
    and

    Code:
    help estat ptrends

    Comment


    • #3
      Originally posted by Andrew Musau View Post

      In DID applications, the pre-treatment period is part of the data used in the estimation. That is exactly what margins is telling you with



      You do not show what model you estimate, but with no pre-treatment period, it is not DID. In any case, if you have Stata 17 or later versions, Stata can do all this for you automatically. See

      Code:
      help estat trendplots
      and

      Code:
      help estat ptrends
      Sorry, I meant prior trends before the pre-treatment group, i have included pre- and posttreatment groups as well as 3 prior years. I guess my problem is that the regression omits groups and says that there are no observations for certain points in time even though that's not true, and that way I can't do the post estimation plots. I have also tried to do the trendplot and it says "estat trendplot not valid r(321)", I don't know if you need to do more besides run the regression before that?

      Comment


      • #4
        Originally posted by Lea Rubin View Post

        Sorry, I meant prior trends before the pre-treatment group, i have included pre- and posttreatment groups as well as 3 prior years.
        The argument is the same. If you want to consider these years, you should include them in your estimation. There is no restriction on how far back you can extend the pre-treatment period, especially if you have data for those years.

        I have also tried to do the trendplot and it says "estat trendplot not valid r(321)", I don't know if you need to do more besides run the regression before that?
        What version of Stata do you have? You need Stata 17 or later and estimation of the model using didregress or xtdidregress.
        Last edited by Andrew Musau; 28 May 2023, 10:11.

        Comment

        Working...
        X