a9a25d4f7672da34df5c57b519f9a9b87ae81c2c 2023-05-11 00:47:25 -0500 Pedro H. C. Sant'Anna Merge pull request #6 from mcaceresb/main
2e573ca1f68d16cf99dfb5ddc06f826f502af774 2023-05-11 01:13:44 -0400 Mauricio Caceres Merge branch 'main' of github.com:mcaceresb/CS_RR
1c3e0fd71969fdb04560bbf0df20e51859e44eb7 2023-05-11 01:12:27 -0400 Mauricio Caceres Cleaned and moved honest_did function to own file
47b36c669ec745b1a0aba1b12c720375c49c3a0c 2023-05-08 14:44:25 -0500 Pedro H. C. Sant'Anna update
b6135510359c72ce42f9c712dad9717b42dd2686 2023-05-08 13:53:20 -0500 Pedro H. C. Sant'Anna Merge pull request #5 from mcaceresb/main
e1bc13fd100c27c094e063abfb2403e41818a262 2023-05-08 12:04:59 -0400 Mauricio Caceres Restored user paths in .Rproj
b30870a76cf34d2fd6ecf7b853735ddcbba41bb2 2023-05-08 12:03:32 -0400 Mauricio Caceres Avoid possible integer overflow in README example
795dbea03d2ac5e20387673d995c48e7ba92e3db 2023-05-07 19:23:13 -0500 Pedro H. C. Sant'Anna Merge pull request #4 from mcaceresb/main
bfd5e353b56b9af1d0b293514bcd341cce513480 2023-05-07 19:15:56 -0400 Mauricio Caceres Replaced honest_did with versions in HonestDiD README; sync READMEs
fb36b152dee7ac786d18e52479c8d5f1ad925e8f 2023-01-30 18:52:08 -0600 Pedro H. C. Sant'Anna Update Callaway_SantAnna_DiD_with_Rambachan_Roth_honest_DID.R
99a7b0fc1269eec28e8c11c2ddf6c9b1d6d7e6e4 2023-01-30 18:51:15 -0600 Pedro H. C. Sant'Anna Update README.md
ee1fa579aee9c46e3d4ee052c6d1c5de9e00d4ff 2022-12-22 22:23:39 -0600 Pedro H. C. Sant'Anna updates
32ca40d989dd8f100e4204c1877bf25b4c969c96 2022-12-22 21:37:34 -0600 Pedro H. C. Sant'Anna update plots
edffdda81890b9eb44d65142f44e2ddfb4606047 2022-12-22 21:29:37 -0600 Pedro H. C. Sant'Anna Update Callaway_SantAnna_DiD_with_Rambachan_Roth_honest_DID.R
cd19b5b8ce2b93a3c0d708c56e209a5453d066ab 2022-12-22 21:26:06 -0600 Pedro H. C. Sant'Anna Update README.md
e59912ea5b170639203f17a4a8f75d12e69e7adf 2022-10-10 00:24:49 -0500 Pedro H. C. Sant'Anna update references and remove -1 from event time to avoid glitches
3443fed62f659f347058b54f5fe7ea14f4254318 2022-10-09 23:49:05 -0500 Pedro H. C. Sant'Anna Update Callaway_SantAnna_DiD_with_Rambachan_Roth_honest_DID.R
627c8e05cc90e78df0db2e476839817ad58f59b4 2022-01-02 23:04:32 -0600 Pedro H. C. Sant'Anna update
81931aa997f8a94c7d6612406f9dabbeb1d25bce 2022-01-02 23:02:46 -0600 Pedro H. C. Sant'Anna update
b54ab7e3895e51614438b9830de74afd697c366a 2022-01-02 22:56:38 -0600 Pedro H. C. Sant'Anna update
ba4497c4b41fdc447e89c4421bb35a5ab579246cd92064adc4f21f69fd6f61a7  data/min_wage_CS.rds
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r setup2, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  echo = TRUE,
  eval = FALSE,
  comment = "#>",
  out.width = "100%"
)
```


## Sensitivity Analysis for Heterogeneity-Robust DiD Procedures:  \n  Combining Callaway and Sant'Anna (2021) with Rambachan and Roth (2022)

The main goal of this repository is to illustrate how one can combine the heterogeneity-robust Difference-in-Differences procedures proposed by Callaway and Sant'Anna (2021) with the sensitivity analysis tools for potential violations of parallel trends proposed by Rambachan and Roth (2022). These are currently implemented in the `did` and `HonestDiD` `R` packages.


Towards that end, we will use the empirical application considered in Callaway and Sant'Anna (2021) about the effect of minimum wage on teen employment. 


Before we jump into details, let's install and load some the necessary R packages to run the example.

```{r packages,  message = FALSE, warning = FALSE, cache = TRUE, cache.lazy = TRUE, fig.width=8,fig.height=5, fig.align='center', out.width="90%", dpi = 200}
# Install some packages
library(devtools)
install_github("bcallaway11/BMisc", dependencies = TRUE)
install_github("bcallaway11/did", dependencies = TRUE)
install_github("asheshrambachan/HonestDiD", dependencies = TRUE)
#--------------------------------------------------------------------------
# Load packages
#--------------------------------------------------------------------------
# Libraries
# Load libraries
library(ggplot2)
library(here)
library(foreign)
library(tidyverse)
library(dplyr)
library(did)
library(HonestDiD)
```

## Auxiliary functions

Since this integration is still a work-in-progress and has not yet been merged into neither the `did` nor the `HonestDiD` R packages, let's define some auxiliary functions that will help implementing it.

```{r, code = readLines("codes/honest_did.R")}
```

## Effect of Minimum Wage on Teen Employment: \n Replicating Callaway and Sant'Anna (2021)

Now, let's load the data used in Callaway and Sant'Anna (2021)
```{r load_data,  message = FALSE, warning = FALSE, cache = TRUE, cache.lazy = TRUE, fig.width=8,fig.height=5, fig.align='center', out.width="90%", dpi = 200}
# Load data used in Callaway and Sant'Anna (2021) application
min_wage <- readRDS((here("data",'min_wage_CS.rds')))
```

Now, let's compute the event-study plot using the Doubly-Robust DiD procedure of Callaway and Sant'Anna (2021) based on the ``never-treated'' comparison group. Here, we will fix the reference time period to be fixed (which differs from the original implementation of Callaway and Sant'Anna, 2021)

```{r event_study_cs,  message = FALSE, warning = FALSE, cache = TRUE, cache.lazy = TRUE, fig.width=8,fig.height=5, fig.align='center', out.width="90%", dpi = 200}
# Formula for covariates 
xformla <- ~ region + (medinc + pop ) + I(pop^2) + I(medinc^2)  + white + hs  + pov
#---------------------------------------------------------------------------
# Using covariates and DR DiD with never-treated as comparison group
# Fix the reference time periods
CS_never_cond <- did::att_gt(yname="lemp",
                             tname="year",
                             idname="countyreal",
                             gname="first.treat",
                             #xformla=~1,
                             xformla = xformla,
                             control_group="nevertreated",
                             data = min_wage,
                             panel = TRUE,
                             base_period="universal",
                             bstrap = TRUE,
                             cband = TRUE)
# Now, compute event study
CS_es_never_cond <- aggte(CS_never_cond, type = "dynamic",
                          min_e = -5, max_e = 5)
#summary(CS_es_never_cond)
# Plot event study
fig_CS <- ggdid(CS_es_never_cond,
      title = "Event-study aggregation \n DiD based on conditional PTA and using never-treated as comparison group ")

fig_CS

```
![](plots/CS_es_nv_X_fixed_baseline.png){ width=100% }
From a closing inspection of this event-study plot, one can see that a straight line with negative slope could be drawn such that it pass within the uniform confidence bands for all event-study coefficients. As so, one may be skeptical about the plausibility of the conditional paralell trends in this particular application. 


How can we tackle this concern?

## Sentitivity Analysis using Rambachan and Roth (2022)


Now, let's do the sensitivity analysis for violation of (conditional) parallel trends
```{r RR_sensitivity, message = FALSE, warning = FALSE, cache = TRUE, cache.lazy = TRUE, fig.width=8,fig.height=5, fig.align='center', out.width="90%", dpi = 200}

# code for running honest_did
hd_cs_smooth_never <- honest_did(es = CS_es_never_cond,
                           e = 0,
                           type="smoothness")
hd_cs_smooth_never


hd_cs_rm_never <- honest_did(es = CS_es_never_cond, 
                          e = 0,
                          type="relative_magnitude")
hd_cs_rm_never
# Drop 0 as that is not really allowed.
hd_cs_rm_never$robust_ci <- hd_cs_rm_never$robust_ci[-1,]

## -----------------------------------------------------------------------------
# make sensitivity analysis plots
cs_HDiD_smooth <- createSensitivityPlot(hd_cs_smooth_never$robust_ci,
                      hd_cs_smooth_never$orig_ci)
cs_HDiD_smooth

cs_HDiD_relmag <- createSensitivityPlot_relativeMagnitudes(hd_cs_rm_never$robust_ci,
                                         hd_cs_rm_never$orig_ci)

cs_HDiD_relmag

```
![](plots/cs_HDiD_smooth.png){ width=100% }
![](plots/cs_HDiD_relmag.png){ width=100% }


## Conclusion

In this repo, we have shown that it is straightforward to combine the Callaway and Sant'Anna (2021) DiD procedures implemented in the `did` package, with the sentivity analysis proposed by Rambachan and Roth (2022) implemented in the `HonestDiD` package. 

## References


* [Callaway, Brantly, and Pedro H. C. Sant'Anna. "Difference-in-differences with multiple time periods." Journal of Econometrics (2021)](https://www.sciencedirect.com/science/article/pii/S0304407620303948).


* [Rambachan, Ashesh, and Jonathan Roth. "A More Credible Approach to Parallel Trends." Forthcoming at the Review of Economic Studies (2022).](https://jonathandroth.github.io/assets/files/HonestParallelTrends_Main.pdf)

#---------------------------------------------------
#      Effect of Minimum Wage on Teen Employments
#      Codes based on Callaway and Sant'Anna (2021)
#      Main goal: show how we can do sensitivy analysis
#      for potential violations of parallel trends.
#      Combine Callaway and Sant'Anna (2021) with 
#      Rambachan and Roth (2022)
#---------------------------------------------------
# Install some packages
library(devtools)

install_github("bcallaway11/BMisc", dependencies = TRUE)
install_github("bcallaway11/did", dependencies = TRUE)
install_github("asheshrambachan/HonestDiD", dependencies = TRUE)
#-----------------------------------------------------------------------------
# Load packages
#-----------------------------------------------------------------------------
# Libraries
# Load libraries
#' @title honest_did
#'
#' @description a function to compute a sensitivity analysis
#'  using the approach of Rambachan and Roth (2022)
#' @param es an event study
honest_did <- function(es, ...) {
  UseMethod("honest_did", es)
}


#' @title honest_did.AGGTEobj
#'
#' @description a function to compute a sensitivity analysis
#'  using the approach of Rambachan and Roth (2022) when
#'  the event study is estimating using the `did` package
#'
#' @param e event time to compute the sensitivity analysis for.
#'  The default value is `e=0` corresponding to the "on impact"
#'  effect of participating in the treatment.
#' @param type Options are "smoothness" (which conducts a
#'  sensitivity analysis allowing for violations of linear trends
#'  in pre-treatment periods) or "relative_magnitude" (which
#'  conducts a sensitivity analysis based on the relative magnitudes
#'  of deviations from parallel trends in pre-treatment periods).
#' @inheritParams HonestDiD::createSensitivityResults
#' @inheritParams HonestDid::createSensitivityResults_relativeMagnitudes
honest_did.AGGTEobj <- function(es,
                                e=0,
                                type=c("smoothness", "relative_magnitude"),
                                method=NULL,
                                bound="deviation from parallel trends",
                                Mvec=NULL,
                                Mbarvec=NULL,
                                monotonicityDirection=NULL,
                                biasDirection=NULL,
                                alpha=0.05,
                                parallel=FALSE,
                                gridPoints=10^3,
                                grid.ub=NA,
                                grid.lb=NA,
                                ...) {
  
  
  type <- type[1]
  
  # make sure that user is passing in an event study
  if (es$type != "dynamic") {
    stop("need to pass in an event study")
  }
  
  # check if used universal base period and warn otherwise
  if (es$DIDparams$base_period != "universal") {
    stop("Use a universal base period for honest_did")
  }
  
  # recover influence function for event study estimates
  es_inf_func <- es$inf.function$dynamic.inf.func.e
  
  # recover variance-covariance matrix
  n <- nrow(es_inf_func)
  V <- t(es_inf_func) %*% es_inf_func / n / n
