SCM version 1(only KOR, LUX)

### load data from google sheet
combined_data <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vRe67N_K9M-u4GgzLxD310RbZgaRTjk4wXYch-TQT2e29TXciZ2Pdz8vr50p8_LMfw_TrvOhpSNZ2yF/pub?gid=1175897076&single=true&output=csv")

##### SCM Version 1
######################
#install.packages("Synth", repos="http://cran.rstudio.com/")
library(Synth )
## ##
## ## Synth Package: Implements Synthetic Control Methods.
## ## See https://web.stanford.edu/~jhain/synthpage.html for additional information.
#data("synth.data")  # Example dataset included in Synth package

country.list <- unique(combined_data$unit.num)
list <- setdiff(country.list, c(10,22,30,38,40,41,46,49,56, 72,76,90,100,107,110,112,116,117,144, 146,177,181,187,195,202,203,204,223,224,225,246,253,265))
#list <- setdiff(country.list, c(41,100,107,110,146,202,246,265))

dataprep.out <-
  dataprep(
    foo = combined_data,
    predictors = c("alcohol","GDP","infant_mortality","cancer_death","doctors"),
    predictors.op = "mean",
    dependent = "life_expectancy_at_birth",
    unit.variable = "unit.num",
    unit.names.variable = "REF_AREA",   
    time.variable = "TIME_PERIOD",
    treatment.identifier = 202,
    controls.identifier = list,
    time.predictors.prior = c(1986:1995),
    time.optimize.ssr = c(1986:1995),
    time.plot = 1986:2005
  )

synth.out <- synth(dataprep.out)
## 
## X1, X0, Z1, Z0 all come directly from dataprep object.
## 
## 
## **************** 
##  searching for synthetic control unit  
##  
## 
## **************** 
## **************** 
## **************** 
## 
## MSPE (LOSS V): 0.3085476 
## 
## solution.v:
##  0.2521439 0.05408721 0.02503257 0.026605 0.6421313 
## 
## solution.w:
##  5.5e-09 6e-09 2.1e-09 2e-09 5.1e-09 4.1e-09 3.655e-07 5.55e-08 2.9e-09 1.12e-08 3e-10 5e-10 0.4671621 0.5328374 1.3e-09 4e-10 3.8e-09
synth.tables <- synth.tab(
  dataprep.res = dataprep.out,
  synth.res = synth.out)

synth.tables$tab.pred
##                   Treated Synthetic Sample Mean
## alcohol            35.300    12.156      10.345
## GDP              8797.800 21240.993   16985.929
## infant_mortality    4.368     9.373      10.136
## cancer_death       98.889  1292.199    1274.033
## doctors             1.123     1.485       2.304
synth.tables$tab.w
##     w.weights unit.names unit.numbers
## 14      0.000        AUS           14
## 15      0.000        AUT           15
## 18      0.000        BEL           18
## 36      0.000        CAN           36
## 55      0.000        CZE           55
## 59      0.000        DNK           59
## 71      0.000        ESP           71
## 78      0.000        FRA           78
## 82      0.000        GBR           82
## 102     0.000        HUN          102
## 115     0.000        ISL          115
## 120     0.000        JPN          120
## 127     0.467        KOR          127
## 145     0.533        LUX          145
## 155     0.000        MEX          155
## 178     0.000        NOR          178
## 191     0.000        POL          191
synth.tables$tab.loss
##        Loss W    Loss V
## [1,] 3.548052 0.3085476
path.plot(dataprep.res = dataprep.out,
          synth.res = synth.out, 
          Ylab="Life expectancy at birth",
          Xlab="Year",
          Ylim = c(70,82),
          Legend=c("Taiwan","Synthetic Taiwan"),
          Legend.position="bottomright",
          Main = "SCM Version 1"
)
abline(v=1995, lty=2)
arrows(1995, 78, 1993, 78,
       col    = "black",
       length = .1)
text(1991, 78,
     "NHI Policy",
     cex = 1)

gaps.plot(dataprep.res = dataprep.out,
          synth.res = synth.out,
          Ylab="Gap of life expectancy",
          Xlab="Year"
)
abline(v=1995, lty=2)
arrows(1995, 1, 1993, 1,
       col    = "black",
       length = .1)
text(1991, 1,
     "NHI Policy",
     cex = 1)

SCM version 2(KOR, LUX, ESP)

##### SCM Version 2
### load data from google sheet
combined_data <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vRe67N_K9M-u4GgzLxD310RbZgaRTjk4wXYch-TQT2e29TXciZ2Pdz8vr50p8_LMfw_TrvOhpSNZ2yF/pub?gid=1175897076&single=true&output=csv")

#install.packages("Synth", repos="http://cran.rstudio.com/")
library(Synth )
#data("synth.data")  # Example dataset included in Synth package

country.list <- unique(combined_data$unit.num)
list <- setdiff(country.list, c(10,22,30,38,40,41,46,49,90,100,107,110,112,116,117, 146,177,181,187,195,202,203,204,223,224,246,265))
#list <- setdiff(country.list, c(41,100,107,110,146,202,246,265))

dataprep.out <-
  dataprep(
    foo = combined_data,
    predictors = c("alcohol","GDP","infant_mortality","cancer_death","doctors"),
    predictors.op = "mean",
    dependent = "life_expectancy_at_birth",
    unit.variable = "unit.num",
    unit.names.variable = "REF_AREA",   
    time.variable = "TIME_PERIOD",
    treatment.identifier = 202,
    controls.identifier = list,
    time.predictors.prior = c(1986:1995),
    time.optimize.ssr = c(1986:1995),
    time.plot = 1986:2005
  )

synth.out <- synth(dataprep.out)
## 
## X1, X0, Z1, Z0 all come directly from dataprep object.
## 
## 
## **************** 
##  searching for synthetic control unit  
##  
## 
## **************** 
## **************** 
## **************** 
## 
## MSPE (LOSS V): 0.2985003 
## 
## solution.v:
##  0.1701266 0.2136082 0.007266856 0.0001097161 0.6088886 
## 
## solution.w:
##  1.6676e-06 2.0092e-06 7.313e-07 8.193e-07 7.321e-07 4.5923e-06 2.4761e-06 0.2228325 3.131e-07 8.016e-07 2.52643e-05 1.7278e-06 2.51635e-05 2.008e-07 6.9e-09 0.5727735 2.052e-07 0.2043249 3.748e-07 2.254e-07 7.675e-07 2.512e-07 7.643e-07
synth.tables <- synth.tab(
  dataprep.res = dataprep.out,
  synth.res = synth.out)

synth.tables$tab.pred
##                   Treated Synthetic Sample Mean
## alcohol            35.300    11.160      10.024
## GDP              8797.800 13666.109   16897.810
## infant_mortality    4.368     9.663       9.851
## cancer_death       98.889  1255.513    1396.026
## doctors             1.123     1.373       2.445
synth.tables$tab.w
##     w.weights unit.names unit.numbers
## 14      0.000        AUS           14
## 15      0.000        AUT           15
## 18      0.000        BEL           18
## 36      0.000        CAN           36
## 55      0.000        CZE           55
## 56      0.000        DEU           56
## 59      0.000        DNK           59
## 71      0.223        ESP           71
## 72      0.000        EST           72
## 76      0.000        FIN           76
## 78      0.000        FRA           78
## 82      0.000        GBR           82
## 102     0.000        HUN          102
## 115     0.000        ISL          115
## 120     0.000        JPN          120
## 127     0.573        KOR          127
## 144     0.000        LTU          144
## 145     0.204        LUX          145
## 155     0.000        MEX          155
## 178     0.000        NOR          178
## 191     0.000        POL          191
## 225     0.000        SWE          225
## 253     0.000        USA          253
synth.tables$tab.loss
##        Loss W    Loss V
## [1,] 2.870037 0.2985003
path.plot(dataprep.res = dataprep.out,
          synth.res = synth.out, 
          Ylab="Life expectancy at birth",
          Xlab="Year",
          Ylim = c(70,82),
          Legend=c("Taiwan","Synthetic Taiwan"),
          Legend.position="bottomright",
          Main = "SCM Version 2"
)
abline(v=1995, lty=2)
arrows(1995, 78, 1993, 78,
       col    = "black",
       length = .1)
text(1991, 78,
     "NHI Policy",
     cex = 1)

gaps.plot(dataprep.res = dataprep.out,
          synth.res = synth.out,
          Ylab="Gap of life expectancy",
          Xlab="Year"
)
abline(v=1995, lty=2)
arrows(1995, 1, 1993, 1,
       col    = "black",
       length = .1)
text(1991, 1,
     "NHI Policy",
     cex = 1)