Announcement

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

  • Producing a difference in difference graph

    Hi,

    I intend to produce a difference in difference graph of my data such as the graph give below;

    Click image for larger version

Name:	Capture.JPG
Views:	1
Size:	43.3 KB
ID:	1557853


    My data is here;

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year byte(time treatment) float percentage
    2018 0 0 .28
    2020 1 0 .44
    2018 0 1 .27
    2020 1 1 .51
    end
    I have produced the following graph using the code below, but it is not the one that I want to present, and it does not have the dotted line.

    Code:

    Code:
    reshape wide percentage, i(time) j(treatment)
    graph twoway line percentage0 percentage1 year, ytitle(%) xtitle(year end) xline(2018.3) sort

    Click image for larger version

Name:	Capture.JPG
Views:	1
Size:	26.0 KB
ID:	1557854



    Could you guys help me out producing the graph with the dotted line above.
    Thanks

  • #2
    Here is how you would generate the dashed line:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year byte(time treatment) float percentage
    2018 0 0 .28
    2020 1 0 .44
    2018 0 1 .27
    2020 1 1 .51
    end
    
    gen id = ceil(_n / 2)
    separate percentage, by(id)
    
    // counterfactual = starting value of treated in year 2018, + change of untreated in 2020)
    gen counterfactual = percentage if id == 2 & year == 2018
    replace counterfactual = .27 + (.44 - .28) if id == 2 & year == 2020
    
    line percentage? counterfactual year, xlabel(2018 2020) lpattern(solid solid dash)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	36.4 KB
ID:	1557866



    It doesn't look so great yet, it is a bit unfortunate that the lines cross each other, but it is what it is. I didn't do any formatting so you can still do that to make it look better.

    I don't see a need for a vertical line as you have only two periods.

    Comment


    • #3
      Thank you. This is a good start.

      Comment

      Working...
      X