Help for diff_plot

Description

The diff_plot is designed to create a customized visual representation of the canonical difference-in-differences (DiD) estimation, which is commonly used in causal inference. This program is tailored for the simplest 2x2 case, where we have one treatment group, one control group, one period prior to intervention, and one period post-intervention. It generates a line graph that illustrates the changes in an outcome variable over the two time periods for both the treatment and control groups. Crucially, the graph also includes an optional parallel trend line, which simulates the counterfactual trend for the treatment group in the absence of any treatment, based on the parallel trend assumption. This tool is particularly useful for organizations such as NGOs and research institutions that have baseline and endline data and want to visualize changes in both the treatment and control groups as part of their project evaluations.

Syntax

diff_plot varlist [if] [in], time(varname) group(varname) [drop_trend] [title(string)] [decimals(integer)] [scale] [graphopts(string)] [l1_opts(string)] [l2_opts(string)] [l3_opts(string)] [m1_opts(string)] [m2_opts(string)] [m3_opts(string)]

Options

time(varname) Specifies the time variable, which must contain exactly two unique values representing the two time periods.

group(varname) Specifies the group variable, which must contain exactly two unique values representing the treatment and control groups.

drop_trend Drops the parallel trend line from the graph.

title(string) Specifies the title of the graph. If not specified, a default title will be generated.

decimals(integer) Specifies the number of decimal places to display for the values. Default is 2.

scale Specifies whether to scale the y-axis to have a range from 0 to 100. Useful for percentages, proportions, and rates.

graphopts(string) Specifies additional graph options to be passed to the graph command.

l1_opts(string) Specifies options for the line representing the control group.

l2_opts(string) Specifies options for the line representing the treatment group.

l3_opts(string) Specifies options for the parallel trend line.

m1_opts(string) Specifies options for the markers representing the control group.

m2_opts(string) Specifies options for the markers representing the treatment group.

m3_opts(string) Specifies options for the markers representing the parallel trend line.

Examples

Creating a difference-in-differences plot with default settings

. webuse bplong, clear

. diff_plot bp, group(sex) time(when)

Dropping the trend line

. diff_plot bp, group(sex) time(when) drop_trend

Fewer decimals

. diff_plot bp, group(sex) time(when) decimals(1)

Using the if clause

. use https://www.stata-press.com/data/r17/hospdd.dta, clear

. diff_plot satis if month == 4 | month == 7, group(procedure) time(month)

Using additional graphical options

. diff_plot satis if month == 4 | month == 7, group(procedure) time(month) graphopts(ylabel(3.3(1)4.3))

Using a different dataset

. use https://dss.princeton.edu/training/Panel101.dta, clear

. gen time = 0

. replace time = 1 if year >= 1994

. gen treated = 0

. replace treated = 1 if country > 4

. replace y = y / 1000000

Using graphical options to change colors of lines, markers, and values

. diff_plot y, group(treated) time(time) l1_opts(lcolor(red)) l2_opts(lcolor(black)) l3_opts(lcolor(black)) m1_opts(mcolor(red) mlabcolor(red)) m2_opts(mcolor(black) mlabcolor(black)) m3_opts(mcolor(black) mlabcolor(black))

Use a custom title and omitting the subtitle

. diff_plot y, group(treated) time(time) title("Difference-in-Differences") graphopts(subtitle(""))

Notes

This program requires the elabel package for full functionality. This must be installed from SSC with the command "ssc install elabel, replace".

Acknowledgments

Special thanks to Ketki Samel, Shritha Sampath, Zaeen de Souza, and Prabhmeet Kaur for their code reviews. Their insights and suggestions were invaluable in refining and improving this program.

Authors

Kabira Namit, World Bank

knamit@worldbank.org