Version 3 (155 countries & 2 Xs)

############## Version 3
#load data
combined_data <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vTCR15LDjmHoVC3cWEbYbOyXQhTgncTbqO9V-PWmYBEvNZj5fjOLYFJBgAWFB1eSgiCRxT8HtX7MTAk/pub?output=csv")

# filtering country as a unit
library(dplyr)
filter_data <- combined_data %>%
  filter(year >= 1986 & year <= 2005)  %>%
  filter(!is.na(life_expectancy))  %>%
  group_by(country) %>%
  filter(!(any(is.na(infant_mortality[year >= 1986 & year <= 1995])))) %>%
  filter(!(any(is.na(gdp_per_capita[year >= 1986 & year <= 1995])))) %>%
  filter(n() == 20) %>%
  ungroup()


# SCM
library(Synth )
filter_data <- as.data.frame(filter_data)
country_unit_all <- unique(filter_data$unit.num)
country_unit_control <- setdiff(country_unit_all, c(100,238))

dataprep.out <-
  dataprep(
    foo = filter_data,
    predictors = c("gdp_per_capita","infant_mortality"),
    predictors.op = "mean",
    dependent = "life_expectancy",
    unit.variable = "unit.num",
    unit.names.variable = "country", 
    time.variable = "year",
    treatment.identifier = 238,
    controls.identifier = country_unit_control,
    time.predictors.prior = c(1986:1995),
    time.optimize.ssr = c(1986:1995),
    time.plot = 1986:2005
  )

# # SCM result
# synth.out <- synth(dataprep.out)
# synth.tables <- synth.tab(
#   dataprep.res = dataprep.out,
#   synth.res = synth.out)
# 
# synth.tables$tab.pred
# synth.tables$tab.w
# synth.tables$tab.loss
# 
# #Plot
# 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 3"
# )
# 
# 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)

Version 4 (100 countries & 2 Xs)

############## Version 4
#load data
combined_data <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vTCR15LDjmHoVC3cWEbYbOyXQhTgncTbqO9V-PWmYBEvNZj5fjOLYFJBgAWFB1eSgiCRxT8HtX7MTAk/pub?output=csv")

# filtering country as a unit
library(dplyr)
filter_data <- combined_data %>%
  filter(year >= 1986 & year <= 2005)  %>%
  filter(!is.na(life_expectancy))  %>%
  group_by(country) %>%
  filter(!(any(is.na(infant_mortality[year >= 1986 & year <= 1995])))) %>%
  filter(!(any(is.na(gdp_per_capita[year >= 1986 & year <= 1995])))) %>%
  filter(n() == 20) %>%
  ungroup()


# SCM
library(Synth )
filter_data <- as.data.frame(filter_data)
country_unit_all <- unique(filter_data$unit.num)
country_unit_control <- setdiff(country_unit_all, c(238))

dataprep.out <-
  dataprep(
    foo = filter_data,
    predictors = c("gdp_per_capita","infant_mortality"),
    predictors.op = "mean",
    dependent = "life_expectancy",
    unit.variable = "unit.num",
    unit.names.variable = "country", 
    time.variable = "year",
    treatment.identifier = 238,
    controls.identifier = country_unit_control[1:99],
    time.predictors.prior = c(1986:1995),
    time.optimize.ssr = c(1986:1995),
    time.plot = 1986:2005
  )

# # SCM result
# synth.out <- synth(dataprep.out)
# synth.tables <- synth.tab(
#   dataprep.res = dataprep.out,
#   synth.res = synth.out)
# 
# synth.tables$tab.pred
# synth.tables$tab.w
# synth.tables$tab.loss
# 
# #Plot
# 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 4"
# )
# 
# 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)

Version 5 (13 countries & 3 Xs)

############## Version 5
#load data
combined_data <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vTCR15LDjmHoVC3cWEbYbOyXQhTgncTbqO9V-PWmYBEvNZj5fjOLYFJBgAWFB1eSgiCRxT8HtX7MTAk/pub?output=csv")

# filtering country as a unit
library(dplyr)
filter_data <- combined_data %>%
  filter(year >= 1986 & year <= 2005)  %>%
  filter(!is.na(life_expectancy))  %>%
  group_by(country) %>%
  filter(!(any(is.na(infant_mortality[year >= 1986 & year <= 1995])))) %>%
  filter(!(any(is.na(gdp_per_capita[year >= 1986 & year <= 1995])))) %>%
  filter(!(any(is.na(physicians[year >= 1986 & year <= 1995])))) %>%
  filter(n() == 20) %>%
  ungroup()


# SCM
library(Synth )
filter_data <- as.data.frame(filter_data)
country_unit_all <- unique(filter_data$unit.num)
country_unit_control <- setdiff(country_unit_all, c(100,238))

dataprep.out <-
  dataprep(
    foo = filter_data,
    predictors = c("gdp_per_capita","infant_mortality","physicians"),
    predictors.op = "mean",
    dependent = "life_expectancy",
    unit.variable = "unit.num",
    unit.names.variable = "country", 
    time.variable = "year",
    treatment.identifier = 238,
    controls.identifier = country_unit_control,
    time.predictors.prior = c(1986:1995),
    time.optimize.ssr = c(1986:1995),
    time.plot = 1986:2005
  )

# # SCM result
# synth.out <- synth(dataprep.out)
# synth.tables <- synth.tab(
#   dataprep.res = dataprep.out,
#   synth.res = synth.out)
# 
# synth.tables$tab.pred
# synth.tables$tab.w
# synth.tables$tab.loss
# 
# #Plot
# 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 5"
# )
# 
# 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)