---
title: "Replicating the Basque Study"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Replicating the Basque Study}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 7, fig.align = "center")
```

## Introduction

In order to demonstrate `SCtools` it is useful to start with a replication of the cannonical Basque study from Abadie and Gardeazabal (2003).

```{r setup}
library(SCtools)
library(Synth)
```

Now we can load the `basque` data set.

```{r}
data("basque")
```

As per the normal `Synth` workflow, we need to reformat our data using the `dataprep` function in which we specify our counterfactuals and our response variables. 

```{r}
dataprep.out <- dataprep(
  foo = basque,
  predictors = c("school.illit", "school.prim", "school.med",
    "school.high", "school.post.high", "invest"),
  predictors.op = "mean",
  time.predictors.prior = 1964:1969,
  special.predictors = list(
    list("gdpcap", 1960:1969 ,"mean"),
    list("sec.agriculture", seq(1961, 1969, 2), "mean"),
    list("sec.energy", seq(1961, 1969, 2), "mean"),
    list("sec.industry", seq(1961, 1969, 2), "mean"),
    list("sec.construction", seq(1961, 1969, 2), "mean"),
    list("sec.services.venta", seq(1961, 1969, 2), "mean"),
    list("sec.services.nonventa", seq(1961, 1969, 2), "mean"),
    list("popdens",               1969,               "mean")),
  dependent = "gdpcap",
  unit.variable = "regionno",
  unit.names.variable = "regionname",
  time.variable = "year",
  treatment.identifier = 17,
  controls.identifier = c(2:16, 18),
  time.optimize.ssr = 1960:1969,
  time.plot = 1955:1997)

```

Now, we can run the SCM algorithm using the `synth` function.

```{r echo=FALSE}
synth.out <- readRDS("synth_out.rds")
```


```{r eval=FALSE}
synth.out <- synth(data.prep.obj = dataprep.out, method = "BFGS")
```

`Synth` provides some additional helper functions to extract information from the outputted object including the ability to analyze the outputs:

```{r}
gaps <- dataprep.out$Y1plot - (dataprep.out$Y0plot %*% synth.out$solution.w)
```

And generate plots of the counterfactual:

```{r}
path.plot(synth.res = synth.out, dataprep.res = dataprep.out,
          Ylab = "real per-capita GDP (1986 USD, thousand)", Xlab = "year",
          Ylim = c(0, 12), Legend = c("Basque country",
                                      "synthetic Basque country"), 
          Legend.position = "bottomright")

```

## Extending the Analysis

At this point, `SCtools` extends the analysis from `Synth`, While `Synth` generates an analysis on one configured dataset, `SCtools` provides the tooling to permute the dataset and generate multiple placebos to test the sensitivity of our SCM output.

```{r echo=FALSE}
placebo <- readRDS("basque_placebo.rds")
```


```{r eval=FALSE}
placebo <- generate.placebos(dataprep.out = dataprep.out,
                             synth.out = synth.out, strategy = "multiprocess")
```

We can then use the `plot_placebos` to run a placebo test for the findings in Abadie and Gardeazabal (2003).

```{r}
plot_placebos(placebo)
```

Finally, we can also run the `mspe_plot` function to run a post/pre MPSE test for that case, and find how unlikely it would be to find by chance the effects identified.

```{r}
mspe_plot(placebo)
```