## While synth() can be used to construct synthetic control groups
## direcly, by providing a X1,X0,Z1, and Z0 matrix. We strongly
## recommend to first run dataprep() to setup the data.
## Two extensive examples illustrating the whole sequence of
## of commands with:
## 1. dataprep() for matrix-extraction
## 2. synth() for the construction of the synthetic control group
## 3. synth.tab(), gaps.plot(), and path.plot() to summarize the results
## are provided in the examples for the dataprep() function
## please take a close look there.
## Here we provide a stand-alone example, for synth()
## that does not require the user to run dataprep first.
## This example is a generic version of the original estimation
## of economic impact of terrorism in the Basque country
## presented in Abadie and Gardeazabal (2003).
## See the paper for details and the example in dataprep() for the exact replication.
## Here we just use a subset of the predictors.
## Notice the various matrix extractions below;
## these are all taken care of by running dataprep() first.
# load data
data(basque)
# Construct matrix of predictors
predictors = c(
"school.illit","school.prim","school.med","school.high",
"invest","gdpcap","popdens"
)
# for the treated unit (Basque country)
# (predictors are averaged for the 1964:1969 period)
X1 <-
as.matrix(apply(
basque[
basque$regionname == "Basque Country (Pais Vasco)" &
is.element(basque$year,1964:1969),
predictors
]
,2,mean,na.rm=TRUE))
# and the control units (other Spanish regions)
controls <- c(2:16,18)
X0 <- basque[
is.element(basque$regionno,controls) &
is.element(basque$year,1964:1969),
c(predictors,"regionno")
]
X0 <- split(X0, X0[,dim(X0)[2]])
X0 <- sapply(X0, apply, 2, mean, na.rm = TRUE)
X0 <- as.matrix(X0[-dim(X0)[1],])
# get matrix of pre-intervention values of the outcome variable (GDP)
# over which mean squared prediction error should be minimized
# treated unit
Z1 <-
matrix(basque$gdpcap[
is.element(basque$year,1960:1969) &
basque$regionname == "Basque Country (Pais Vasco)"
])
Z0 <-
matrix(basque$gdpcap[
is.element(basque$year,1960:1969) &
is.element(basque$regionno,controls)
],ncol=length(controls))
rownames(Z0) <- rownames(Z1) <- 1960:1969
colnames(Z0) <- colnames(X0) <- controls
# now construct a synthetic basque country
synth.out <- synth(X1=X1,X0=X0,Z1=Z1,Z0=Z0,method="BFGS")
# examine the weights associated with each of the control units
# with region_numbers
round(synth.out$solution.w,3)
# or regionnames
data.frame(round(synth.out$solution.w,3),row.names = unique(basque$regionname)[c(-1,-17)])
# compare the predictor values for the treated unit and its synthetic counterpart
tab <- cbind(X1,X0%*%synth.out$solution.w)
colnames(tab) <- c("Basque Country","Synthetic Basque country")
tab
# plot the pre-terrorism trajectory for the treated unit and its synthetic counterpart
# over with the mean squared prediction error was minimized
matplot(cbind(1960:1969,1960:1969),
cbind(Z1,Z0%*%as.matrix(synth.out$solution.w)),type="l",xlab="year",ylab="GDPcap")
legend("topleft",legend=c("Basque","Synthetic Basque"),col=c("black","red"),lty=c(1,2))
# plot the whole pre and post terrorism period
Y1 <-
matrix(basque$gdpcap[
is.element(basque$year,1955:1997) &
basque$regionname == "Basque Country (Pais Vasco)"
])
Y0 <-
matrix(basque$gdpcap[
is.element(basque$year,1955:1997) &
is.element(basque$regionno,controls)
],ncol=length(controls))
matplot(cbind(1955:1997,1955:1997),
cbind(Y1,Y0%*%as.matrix(synth.out$solution.w)),type="l",xlab="year",ylab="GDPcap",ylim=c(0,11.5))
legend("topleft",legend=c("Basque","Synthetic Basque"),col=c("black","red"),lty=c(1,2))
## to run placebo studies, simply re-run synth
## and change the treated unit ot time of intervention (see references for details)
Run the code above in your browser using DataLab