knitr::opts_chunk$set(echo = TRUE) library(xsynthdid)
Arkhangelsky et al. (2021) introduce the novel synthetic difference-in-differences (SDID) method that is implemented in the R package synthdid.
This package has the function adjust.outcome.for.x
that can be used to adjust the outcome for exogenous time-varying covariates. For more background see my vignette Synthetic Difference-in-Differences with Time-Varying Covariates (2021).
You can install the package from my r-universe repository by using the following code:
options(repos = c(skranz = 'https://skranz.r-universe.dev', CRAN = 'https://cloud.r-project.org')) install.packages("xsynthdid")
We first simulate a panel data set with a true treatment effect of tau=50
using the function xsdid.mc
in our package.
set.seed(1) library(xsynthdid) dat = xsdid.mc(N=30, T=30,tau=50,return.data = TRUE) head(dat)
We first perform SDID estimation without any adjustment for the exogenous covariate x
:
pm = panel.matrices(dat, unit="i",time = "t",outcome = "y",treatment = "treat_exp") sdid = synthdid_estimate(Y=pm$Y,N0=pm$N0,T0 = pm$T0) # inconsistent sdid
We see how the estimator is a bit off the true causal effect.
We now adjust the outcomes y
using the adjust.outcome.for.x
function and use the adjusted outcome for the SDID estimation.
dat$y.adj = adjust.outcome.for.x(dat,unit="i",time = "t", outcome = "y",treatment = "treat_exp", x="x") pm = panel.matrices(dat, unit="i",time = "t",outcome = "y.adj",treatment = "treat_exp") sdid.adj = synthdid_estimate(Y=pm$Y,N0=pm$N0,T0 = pm$T0) # inconsistent sdid.adj
Our estimate is now pretty close to the true causal effect.
Note that the shown standard errors do not account for the initial adjustment for time varying covariates. Experimentally, I have included the function xsdid_se_bootstrap
that allows to compute the standard errors using a clustered bootstrap approach similar to Algorithm 2 in Arkhangelsky et al. (2021).
To speed up the building of the README, here is an example with just B=10
bootstrap replications. Of course, you should set B
to a larger number like 500.
xsdid_se_bootstrap(dat,B=10, unit="i",time = "t",outcome = "y",treatment = "treat_exp",x = "x")
From the few experiments, I have run so far, the bootstrapped standard errors seldom exceeds the standard error that is directly returned from the call to synthdid_estimate
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.