Multiple Treated

Background

In this vignette I show how {bsynth} can be used for causal estimation when you have multiple treated accounts. In particular, I use the synthetic data exaple from Xu, Yiqing, 2017. In this example, the treatment starts in period 21 and increases by one each period (e.g. the effect is 5 period 25 and 10 in period 30).

Figure 1: Xu, Yiqing, 2017

Figure 1: Xu, Yiqing, 2017

Bayesian Synthetic Control with Covariates

library(bsynth)
ci_width <- 0.95
data(gsynth, package = "gsynth")
dplyr::glimpse(simdata)
#> Rows: 1,500
#> Columns: 15
#> $ id    <dbl> 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101,…
#> $ time  <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1…
#> $ Y     <dbl> 6.2109977, 4.0271059, 8.8771874, 11.5153460, 5.9715262, 8.237905…
#> $ D     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1…
#> $ X1    <dbl> 0.3776736, 1.7332009, 1.8580159, 1.3943369, 2.3636963, 0.5370867…
#> $ X2    <dbl> -0.1732470, -0.4945009, 0.4984432, 1.1272713, -0.1535215, 0.8774…
#> $ eff   <dbl> 0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000…
#> $ error <dbl> 0.29822759, 0.63656967, -0.48378063, 0.51686204, 0.36896453, -0.…
#> $ mu    <dbl> 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5…
#> $ alpha <dbl> -0.06052364, -0.06052364, -0.06052364, -0.06052364, -0.06052364,…
#> $ xi    <dbl> 1.1313372, -1.4606401, 0.7399475, 1.9091036, -1.4438932, 0.70178…
#> $ F1    <dbl> 0.25331851, -0.02854676, -0.04287046, 1.36860228, -0.22577099, 1…
#> $ L1    <dbl> -0.04303273, -0.04303273, -0.04303273, -0.04303273, -0.04303273,…
#> $ F2    <dbl> 0.005764186, 0.385280401, -0.370660032, 0.644376549, -0.22048656…
#> $ L2    <dbl> -0.8804667, -0.8804667, -0.8804667, -0.8804667, -0.8804667, -0.8…

outcome_data <- simdata %>% 
  dplyr::select(time, id, D, Y)

covariates <- simdata %>% 
  dplyr::select(time, id, X1, X2)

synth <-
  bsynth::bayesianSynth$new(
    data = outcome_data,
    time = time,
    id = id,
    treated = D,
    outcome = Y, 
    ci_width = ci_width,
    covariates = covariates
  )

synth$timeTiles +
  ggplot2::theme(text = ggplot2::element_text(size=6))

Fit

synth$fit()

Visualize the synthetic controls for each treated unit

synth$synthetic  

### Visualize the treatment effect

synth$effectPlot(subset = c("Average"), facet = FALSE) + 
  ggplot2::scale_y_continuous(breaks=seq(-2,12,2))