-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscpi_illustration_plot-multi.R
62 lines (46 loc) · 2.31 KB
/
scpi_illustration_plot-multi.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
################################################################################
## SCPI R Package
## R-file for Empirical Illustration - Multiple Treated Units
## Authors: Matias D. Cattaneo, Yingjie Feng, Filippo Palomba, and Rocio Titiunik
################################################################################
### Clear R environment
rm(list=ls(all=TRUE))
### Install R library
#install.packages('scpi')
### Load packages
library(scpi)
library(ggplot2)
set.seed(8894)
theme_set(theme_bw())
###############################################################################
# MULTIPLE TREATED UNITS
###############################################################################
### Load data
data <- scpi_germany
data$treatment <- 0
data[(data$country == "West Germany" & data$year >= 1991), 'treatment'] <- 1
data[(data$country == "Italy" & data$year >= 1992), 'treatment'] <- 1
###############################################
# unit-time treatment effect
###############################################
df <- scdataMulti(data, id.var = "country", outcome.var = "gdp",
treatment.var = "treatment", time.var = "year", constant = TRUE,
cointegrated.data = T, features = list(c("gdp","trade")),
cov.adj = list(c("constant", "trend")))
res.pi <- scpi(df, w.constr = list("name" = "simplex"), cores = 1, sims = 50,
e.method = "gaussian")
# plot series
scplotMulti(res.pi, type = "series", joint = TRUE, save.data = '__scpi_data')
load('__scpi_data.RData')
plot <- ggplot(toplot) + xlab("Date") + ylab("Outcome") +
geom_line(aes(x=Time, y=Y, colour=Type)) +
geom_point(aes(x=Time, y=Y, colour=Type), size=1.5) +
geom_vline(aes(xintercept=Tdate)) +
facet_wrap(~ID, ncol = 2) + theme(legend.position="bottom") +
scale_color_manual(name = "", values = c("black", "blue"),
labels = c("Treated", "Synthetic Control"))
plot.w1 <- plot + geom_errorbar(data = toplot,
aes(x = Time, ymin = lb.gaussian, ymax = ub.gaussian), colour = "blue",
width = 0.5, linetype = 1) + ggtitle("In and Out of Sample Uncertainty - Subgaussian Bounds")
plotdf <- subset(toplot, Type == "Synthetic")
plot.w1 + geom_ribbon(data=plotdf, aes(x=Time, ymin=lb.joint, ymax=ub.joint), fill="blue", alpha=0.1)