# print(getwd())
# source(file = "../functions/cate_dext.R")
# source('~/GitHub/cap_estudio_2017/functions/cate_dext.R')
source('V:/USR/RMAYER/cw/cap_estudio_2017/functions/cate_dext.R')
# source(paste0(pre_path, "functions/cate_dext.R"))
load(paste0(pre_path, "produced_data/cepal_19_countries"))
load(paste0(pre_path, "produced_data/cepal_33_countries"))


# load("./produced_data/cepal_19_countries")
# load("./produced_data/cepal_33_countries")
# load("../produced_data/cepal_19_countries")
# load("../produced_data/cepal_33_countries")

# load("~/GitHub/cap_estudio_2017/produced_data/cepal_19_countries")
# load("~/GitHub/cap_estudio_2017/produced_data/cepal_33_countries")

# load("V:/USR/RMAYER/cw/cap_estudio_2017/produced_data/cepal_19_countries")
# load("V:/USR/RMAYER/cw/cap_estudio_2017/produced_data/cepal_33_countries")
coi_19 <- cepal_19_countries$iso3c
coi_18 <- coi_19[-which(coi_19 %in% c("CUB"))]
coi_18_minus_br_mx_ven <-  coi_18[-which(coi_18 %in% c("BRA", "MEX", "VEN"))]
coi_sa <-  c("ARG", "BOL" ,"BRA", "CHL", "COL", "ECU",  "PER", "PRY", "URY", "VEN")
coi_sa_minus_BRA_VEN <- coi_sa[-which(coi_sa %in% c("BRA","VEN"))]
coi_ca <-  c("CRI", "DOM", "HND" ,"GTM", "PAN", "NIC", "SLV", "MEX")
coi_ca_minus_mex <- coi_ca[-which(coi_ca %in% c("MEX"))]

coi <- coi_sa_minus_BRA_VEN

1 Debt

1.1 Total external debt

# load("./produced_data/data_with_basic_wrangling/cs_deuda_externa")
# load('V:/USR/RMAYER/cw/cap_estudio_2017/produced_data/data_with_basic_wrangling/cs_deuda_externa')

load(paste0(pre_path, 
            "produced_data/data_with_basic_wrangling/cs_deuda_externa"))

1.1.1 Latin America

dext_to_gdp_18 <- dext_to_gdp %>% 
  filter(iso3c %in% coi_18)

dext_to_gdp_18_xts <- cate_dext_to_xts_wide(dext_to_gdp_18, 
                                            is_full = TRUE, is_med = FALSE)

dext_to_gdp_18_m_bmv <- dext_to_gdp_18 %>% 
  filter(iso3c %in% coi_18_minus_br_mx_ven)

dext_to_gdp_18_m_bmv_xts <- cate_dext_to_xts_wide(dext_to_gdp_18_m_bmv,
                                                is_full = TRUE, is_med = FALSE)

dext_18_class_med_1990_2015 <- cate_dext(dext_to_gdp_18)
dext_18_class_pct3_1990_2015 <- cate_dext(dext_to_gdp_18, 
                                          is_med = FALSE, is_pct3 = TRUE)
dext_18_class_pct4_1990_2015 <- cate_dext(dext_to_gdp_18,
                                          is_med = FALSE, is_pct3 = FALSE,
                                          is_pct4 = TRUE)

dext_18_class_med_1990_2001_2015 <- cate_dext(dext_to_gdp_18,
                                              time_breaks = 2001)
dext_18_class_pct3_1990_2001_2015 <- cate_dext(dext_to_gdp_18, 
                                               time_breaks = 2001,
                                               is_med = FALSE,
                                               is_pct3 = TRUE)
dext_18_class_pct4_1990_2001_2015 <- cate_dext(dext_to_gdp_18,
                                               time_breaks = 2001,
                                               is_med = FALSE, is_pct3 = FALSE,
                                               is_pct4 = TRUE)

dext_18_med_1990_2015_xts <- cate_dext_to_xts_wide(dext_18_class_med_1990_2015)
dext_18_pct3_1990_2015_xts <- cate_dext_to_xts_wide(dext_18_class_pct3_1990_2015,
                                                      is_med = FALSE,
                                                      is_pct3 = TRUE)
dext_18_pct4_1990_2015_xts <- cate_dext_to_xts_wide(dext_18_class_pct4_1990_2015,
                                                      is_med = FALSE,
                                                      is_pct3 = FALSE,
                                                      is_pct4 = TRUE)

dext_18_med_1990_2001_2015_xts_1 <- cate_dext_to_xts_wide(dext_18_class_med_1990_2001_2015[[1]])
dext_18_med_1990_2001_2015_xts_2 <- cate_dext_to_xts_wide(dext_18_class_med_1990_2001_2015[[2]])


dext_18_pct3_1990_2001_2015_xts_1 <- cate_dext_to_xts_wide(dext_18_class_pct3_1990_2001_2015[[1]],
                                                      is_med = FALSE,
                                                      is_pct3 = TRUE)
dext_18_pct3_1990_2001_2015_xts_2 <- cate_dext_to_xts_wide(dext_18_class_pct3_1990_2001_2015[[2]],
                                                      is_med = FALSE,
                                                      is_pct3 = TRUE)


dext_18_pct4_1990_2001_2015_xts_1 <- cate_dext_to_xts_wide(dext_18_class_pct4_1990_2001_2015[[1]],
                                                      is_med = FALSE,
                                                      is_pct3 = FALSE,
                                                      is_pct4 = TRUE)
dext_18_pct4_1990_2001_2015_xts_2 <- cate_dext_to_xts_wide(dext_18_class_pct4_1990_2001_2015[[2]],
                                                      is_med = FALSE,
                                                      is_pct3 = FALSE,
                                                      is_pct4 = TRUE)

1.1.1.1 All (18) countries

#using dygraph
dext_title_18 <- "(Gross) External debt, as % of GDP (E18)"
dext_title_18mbmv <- "(Gross) External debt, as % of GDP (E18mBMV)"

dg_18 <- dygraph(dext_to_gdp_18_xts, main = dext_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dg_18

1.1.1.2 Country debt above and below group median

1.1.1.2.1 Based on 1990-2015 values
#using dygraph
dext_title_18 <- "(Gross) External debt, as % of GDP (E18)"
dext_title_18mbmv <- "(Gross) External debt, as % of GDP (E18mBMV)"

dg_18_med_a <- dygraph(dext_18_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_med_b <- dygraph(dext_18_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dg_18_med_a
dg_18_med_b
1.1.1.2.2 Based on 1990-2001 and 2002-2015 values

Period: 1990 - 2001

dg_18_med_2001_a_period1 <- dygraph(dext_18_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_med_2001_b_period1 <- dygraph(dext_18_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dg_18_med_2001_a_period1
dg_18_med_2001_b_period1

Period: 2002-2015

dg_18_med_2001_a_period2 <- dygraph(dext_18_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_med_2001_b_period2 <- dygraph(dext_18_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dg_18_med_2001_a_period2
dg_18_med_2001_b_period2

1.1.1.3 Country debt above and below percentiles 33 and 66

1.1.1.3.1 Based on 1990-2015 values
#using dygraph
dext_title_18 <- "(Gross) External debt, as % of GDP (E18)"
dext_title_18mbmv <- "(Gross) External debt, as % of GDP (E18mBMV)"

dg_18_pct3_b33 <- dygraph(dext_18_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct3_33_to_66 <- dygraph(dext_18_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct3_a66 <- dygraph(dext_18_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct3_a66
dg_18_pct3_33_to_66
dg_18_pct3_b33
1.1.1.3.2 Based on 1990-2001 and 2002-2015 values

Period: 1990 - 2001

dg_18_pct3_b33_2001_period1 <- dygraph(dext_18_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct3_33_to_66_2001_period1 <- dygraph(dext_18_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct3_a66_2001_period1 <- dygraph(dext_18_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct3_a66_2001_period1
dg_18_pct3_33_to_66_2001_period1
dg_18_pct3_b33_2001_period1

Period: 2002 - 2015

dg_18_pct3_b33_2001_period2 <- dygraph(dext_18_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct3_33_to_66_2001_period2 <- dygraph(dext_18_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct3_a66_2001_period2 <- dygraph(dext_18_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct3_a66_2001_period2
dg_18_pct3_33_to_66_2001_period2
dg_18_pct3_b33_2001_period2

1.1.1.4 Country debt above and below percentiles 25, 50 and 75

1.1.1.4.1 Based on 1990-2015 values
#using dygraph
dext_title_18 <- "(Gross) External debt, as % of GDP (E18)"
dext_title_18mbmv <- "(Gross) External debt, as % of GDP (E18mBMV)"

dg_18_pct4_b25 <- dygraph(dext_18_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_25_to_50 <- dygraph(dext_18_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_50_to_75 <- dygraph(dext_18_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_a75 <- dygraph(dext_18_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_a75
dg_18_pct4_50_to_75
dg_18_pct4_25_to_50
dg_18_pct4_b25
1.1.1.4.2 Based on 1990-2001 ans 2002-2015 values

Period 1990 - 2001

dg_18_pct4_b25_2001_period1 <- dygraph(dext_18_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_25_to_50_2001_period1 <- dygraph(dext_18_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_50_to_75_2001_period1 <- dygraph(dext_18_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_a75_2001_period1 <- dygraph(dext_18_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_a75_2001_period1
dg_18_pct4_50_to_75_2001_period1
dg_18_pct4_25_to_50_2001_period1
dg_18_pct4_b25_2001_period1

Period: 2002-2015

dg_18_pct4_b25_2001_period2 <- dygraph(dext_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_25_to_50_2001_period2 <- dygraph(dext_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_50_to_75_2001_period2 <- dygraph(dext_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_a75_2001_period2 <- dygraph(dext_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_18_pct4_a75_2001_period2
dg_18_pct4_50_to_75_2001_period2
dg_18_pct4_25_to_50_2001_period2
dg_18_pct4_b25_2001_period2

1.1.2 South America

dext_to_gdp_sa <- dext_to_gdp %>% 
  filter(iso3c %in% coi_sa)

dext_to_gdp_sa_xts <- cate_dext_to_xts_wide(dext_to_gdp_sa, 
                                               is_med = FALSE, is_full = TRUE)

dext_sa_class_med_1990_2015 <- cate_dext(dext_to_gdp_sa)
dext_sa_class_pct3_1990_2015 <- cate_dext(dext_to_gdp_sa, 
                                          is_med = FALSE, is_pct3 = TRUE)
dext_sa_class_pct4_1990_2015 <- cate_dext(dext_to_gdp_sa,
                                          is_med = FALSE, is_pct3 = FALSE,
                                          is_pct4 = TRUE)

dext_sa_class_med_1990_2001_2015 <- cate_dext(dext_to_gdp_sa,
                                              time_breaks = 2001)
dext_sa_class_med_1990_2001_2015 <- cate_dext(dext_to_gdp_sa,
                                              time_breaks = 2001)
dext_sa_class_pct3_1990_2001_2015 <- cate_dext(dext_to_gdp_sa, 
                                               time_breaks = 2001,
                                               is_med = FALSE,
                                               is_pct3 = TRUE)
dext_sa_class_pct4_1990_2001_2015 <- cate_dext(dext_to_gdp_sa,
                                               time_breaks = 2001,
                                               is_med = FALSE, is_pct3 = FALSE,
                                               is_pct4 = TRUE)

dext_sa_med_1990_2015_xts <- cate_dext_to_xts_wide(dext_sa_class_med_1990_2015)
dext_sa_pct3_1990_2015_xts <- cate_dext_to_xts_wide(dext_sa_class_pct3_1990_2015,
                                                      is_med = FALSE,
                                                      is_pct3 = TRUE)
dext_sa_pct4_1990_2015_xts <- cate_dext_to_xts_wide(dext_sa_class_pct4_1990_2015,
                                                      is_med = FALSE,
                                                      is_pct3 = FALSE,
                                                      is_pct4 = TRUE)

dext_sa_med_1990_2001_2015_xts_1 <- cate_dext_to_xts_wide(dext_sa_class_med_1990_2001_2015[[1]])
dext_sa_med_1990_2001_2015_xts_2 <- cate_dext_to_xts_wide(dext_sa_class_med_1990_2001_2015[[2]])

dext_sa_pct3_1990_2001_2015_xts_1 <- cate_dext_to_xts_wide(dext_sa_class_pct3_1990_2001_2015[[1]],
                                                      is_med = FALSE,
                                                      is_pct3 = TRUE)
dext_sa_pct3_1990_2001_2015_xts_2 <- cate_dext_to_xts_wide(dext_sa_class_pct3_1990_2001_2015[[2]],
                                                      is_med = FALSE,
                                                      is_pct3 = TRUE)

dext_sa_pct4_1990_2001_2015_xts_1 <- cate_dext_to_xts_wide(dext_sa_class_pct4_1990_2001_2015[[1]],
                                                      is_med = FALSE,
                                                      is_pct3 = FALSE,
                                                      is_pct4 = TRUE)

dext_sa_pct4_1990_2001_2015_xts_2 <- cate_dext_to_xts_wide(dext_sa_class_pct4_1990_2001_2015[[2]],
                                                      is_med = FALSE,
                                                      is_pct3 = FALSE,
                                                      is_pct4 = TRUE)
# dext_to_gdp_sa_mbv <- dext_to_gdp_sa %>% 
#   filter(iso3c %in% coi_sa_minus_BRA_VEN)
# 
# dext_to_gdp_sambv_xts <- cate_dext_to_xts_wide(dext_to_gdp_sa_mbv, 
#                                                is_med = FALSE, is_full = TRUE)

1.1.2.1 All South America

#using dygraph

dext_title_sa <- "(Gross) External debt, as % of GDP (SA)"

dg_sa <- dygraph(dext_to_gdp_sa_xts, main = dext_title_sa) %>%
dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dg_sa

1.1.2.2 Country debt above and below group median

1.1.2.2.1 Based on 1990-2015 values
#using dygraph

dg_sa_med_a <- dygraph(dext_sa_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_med_b <- dygraph(dext_sa_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dg_sa_med_a
dg_sa_med_b
1.1.2.2.2 Based on 1990-2001 and 2002-2015 values

Period: 1990 - 2001

dg_sa_med_2001_a_period1 <- dygraph(dext_sa_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_med_2001_b_period1 <- dygraph(dext_sa_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_med_2001_a_period1
dg_sa_med_2001_b_period1

Period: 2002 - 2015

dg_sa_med_2001_a_period2 <- dygraph(dext_sa_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_med_2001_b_period2 <- dygraph(dext_sa_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_med_2001_a_period2
dg_sa_med_2001_b_period2

1.1.2.3 Country debt above and below percentiles 33 and 66

1.1.2.3.1 Based on 1990-2015 values
#using dygraph

dg_sa_pct3_b33 <- dygraph(dext_sa_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct3_33_to_66 <- dygraph(dext_sa_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct3_a66 <- dygraph(dext_sa_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct3_a66
dg_sa_pct3_33_to_66
dg_sa_pct3_b33
1.1.2.3.2 Based on 1990-2001 ans 2002-2015 values

Period: 1990 - 2001

dg_sa_pct3_b33_2001_period1 <- dygraph(dext_sa_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct3_33_to_66_2001_period1 <- dygraph(dext_sa_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct3_a66_2001_period1 <- dygraph(dext_sa_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct3_a66_2001_period1
dg_sa_pct3_33_to_66_2001_period1
dg_sa_pct3_b33_2001_period1

Period: 2002 - 2015

dg_sa_pct3_b33_2001_period2 <- dygraph(dext_sa_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct3_33_to_66_2001_period2 <- dygraph(dext_sa_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct3_a66_2001_period2 <- dygraph(dext_sa_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct3_a66_2001_period2
dg_sa_pct3_33_to_66_2001_period2
dg_sa_pct3_b33_2001_period2

1.1.2.4 Country debt above and below percentiles 25, 50 and 75

1.1.2.4.1 Based on 1990-2015 values
dg_sa_pct4_b25 <- dygraph(dext_sa_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_25_to_50 <- dygraph(dext_sa_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_50_to_75 <- dygraph(dext_sa_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_a75 <- dygraph(dext_sa_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_a75
dg_sa_pct4_50_to_75
dg_sa_pct4_25_to_50
dg_sa_pct4_b25
1.1.2.4.2 Based on 1990-2001 ans 2002-2015 values

Period: 1990 - 2001

dg_sa_pct4_b25_2001_period1 <- dygraph(dext_sa_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_25_to_50_2001_period1 <- dygraph(dext_sa_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_50_to_75_2001_period1 <- dygraph(dext_sa_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_a75_2001_period1 <- dygraph(dext_sa_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_a75_2001_period1
dg_sa_pct4_50_to_75_2001_period1
dg_sa_pct4_25_to_50_2001_period1
dg_sa_pct4_b25_2001_period1

Period: 2002 - 2015

dg_sa_pct4_b25_2001_period2 <- dygraph(dext_sa_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_25_to_50_2001_period2 <- dygraph(dext_sa_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_50_to_75_2001_period2 <- dygraph(dext_sa_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_a75_2001_period2 <- dygraph(dext_sa_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_sa_pct4_a75_2001_period2
dg_sa_pct4_50_to_75_2001_period2
dg_sa_pct4_25_to_50_2001_period2
dg_sa_pct4_b25_2001_period2

1.1.3 Central America

dext_to_gdp_ca <- dext_to_gdp %>% 
  filter(iso3c %in% coi_ca)

dext_to_gdp_ca_xts <- cate_dext_to_xts_wide(dext_to_gdp_ca, 
                                               is_med = FALSE, is_full = TRUE)

dext_ca_class_med_1990_2015 <- cate_dext(dext_to_gdp_ca)
dext_ca_class_pct3_1990_2015 <- cate_dext(dext_to_gdp_ca, 
                                          is_med = FALSE, is_pct3 = TRUE)
dext_ca_class_pct4_1990_2015 <- cate_dext(dext_to_gdp_ca,
                                          is_med = FALSE, is_pct3 = FALSE,
                                          is_pct4 = TRUE)

dext_ca_class_med_1990_2001_2015 <- cate_dext(dext_to_gdp_ca,
                                              time_breaks = 2001)
dext_ca_class_pct3_1990_2001_2015 <- cate_dext(dext_to_gdp_ca, 
                                               time_breaks = 2001,
                                               is_med = FALSE,
                                               is_pct3 = TRUE)
dext_ca_class_pct4_1990_2001_2015 <- cate_dext(dext_to_gdp_ca,
                                               time_breaks = 2001,
                                               is_med = FALSE, is_pct3 = FALSE,
                                               is_pct4 = TRUE)

dext_ca_med_1990_2015_xts <- cate_dext_to_xts_wide(dext_ca_class_med_1990_2015)
dext_ca_pct3_1990_2015_xts <- cate_dext_to_xts_wide(dext_ca_class_pct3_1990_2015,
                                                      is_med = FALSE,
                                                      is_pct3 = TRUE)
dext_ca_pct4_1990_2015_xts <- cate_dext_to_xts_wide(dext_ca_class_pct4_1990_2015,
                                                      is_med = FALSE,
                                                      is_pct3 = FALSE,
                                                      is_pct4 = TRUE)

dext_ca_med_1990_2001_2015_xts_1 <- cate_dext_to_xts_wide(dext_ca_class_med_1990_2001_2015[[1]])
dext_ca_med_1990_2001_2015_xts_2 <- cate_dext_to_xts_wide(dext_ca_class_med_1990_2001_2015[[2]])

dext_ca_pct3_1990_2001_2015_xts_1 <- cate_dext_to_xts_wide(dext_ca_class_pct3_1990_2001_2015[[1]],
                                                      is_med = FALSE,
                                                      is_pct3 = TRUE)
dext_ca_pct3_1990_2001_2015_xts_2 <- cate_dext_to_xts_wide(dext_ca_class_pct3_1990_2001_2015[[2]],
                                                      is_med = FALSE,
                                                      is_pct3 = TRUE)

dext_ca_pct4_1990_2001_2015_xts_1 <- cate_dext_to_xts_wide(dext_ca_class_pct4_1990_2001_2015[[1]],
                                                      is_med = FALSE,
                                                      is_pct3 = FALSE,
                                                      is_pct4 = TRUE)
dext_ca_pct4_1990_2001_2015_xts_2 <- cate_dext_to_xts_wide(dext_ca_class_pct4_1990_2001_2015[[2]],
                                                      is_med = FALSE,
                                                      is_pct3 = FALSE,
                                                      is_pct4 = TRUE)

# dext_to_gdp_ca_mm <- dext_to_gdp_ca %>% 
#   filter(iso3c %in% coi_ca_minus_mex)

1.1.3.1 All Central America

#using dygraph

dext_title_ca <- "(Gross) External debt, as % of GDP (CA)"

dg_ca <- dygraph(dext_to_gdp_ca_xts, main = dext_title_ca) %>%
dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dg_ca

1.1.3.2 Country debt above and below group median

1.1.3.2.1 Based on 1990-2015 values
#using dygraph

dg_ca_med_a <- dygraph(dext_ca_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_med_b <- dygraph(dext_ca_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dg_ca_med_a
dg_ca_med_b
1.1.3.2.2 Based on 1990-2001 and 2002-2015 values

Period: 1990 - 2001

dg_ca_med_2001_a_period1 <- dygraph(dext_ca_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_med_2001_b_period1 <- dygraph(dext_ca_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_med_2001_a_period1
dg_ca_med_2001_b_period1

Period: 2002 - 2015

dg_ca_med_2001_a_period2 <- dygraph(dext_ca_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_med_2001_b_period2 <- dygraph(dext_ca_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_med_2001_a_period2
dg_ca_med_2001_b_period2

1.1.3.3 Country debt above and below percentiles 33 and 66

1.1.3.3.1 Based on 1990-2015 values
#using dygraph

dg_ca_pct3_b33 <- dygraph(dext_ca_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct3_33_to_66 <- dygraph(dext_ca_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct3_a66 <- dygraph(dext_ca_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct3_a66
dg_ca_pct3_33_to_66
dg_ca_pct3_b33
1.1.3.3.2 Based on 1990-2001 ans 2002-2015 values

Period: 1990 - 2001

dg_ca_pct3_b33_2001_period1 <- dygraph(dext_ca_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct3_33_to_66_2001_period1 <- dygraph(dext_ca_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct3_a66_2001_period1 <- dygraph(dext_ca_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct3_a66_2001_period1
dg_ca_pct3_33_to_66_2001_period1
dg_ca_pct3_b33_2001_period1

Period: 2002 - 2015

dg_ca_pct3_b33_2001_period2 <- dygraph(dext_ca_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct3_33_to_66_2001_period2 <- dygraph(dext_ca_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct3_a66_2001_period2 <- dygraph(dext_ca_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct3_a66_2001_period2
dg_ca_pct3_33_to_66_2001_period2
dg_ca_pct3_b33_2001_period2

1.1.3.4 Countries sorted into quartiles of the Y-axis variable

1.1.3.4.1 Based on 1990-2015 values
dg_ca_pct4_b25 <- dygraph(dext_ca_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_25_to_50 <- dygraph(dext_ca_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_50_to_75 <- dygraph(dext_ca_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_a75 <- dygraph(dext_ca_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_a75
dg_ca_pct4_50_to_75
dg_ca_pct4_25_to_50
dg_ca_pct4_b25
1.1.3.4.2 Based on 1990-2001 ans 2002-2015 values

Period: 1990 - 2001

dg_ca_pct4_b25_2001_period1 <- dygraph(dext_ca_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_25_to_50_2001_period1 <- dygraph(dext_ca_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_50_to_75_2001_period1 <- dygraph(dext_ca_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_a75_2001_period1 <- dygraph(dext_ca_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_a75_2001_period1
dg_ca_pct4_50_to_75_2001_period1
dg_ca_pct4_25_to_50_2001_period1
dg_ca_pct4_b25_2001_period1

Period: 2002 - 2015

dg_ca_pct4_b25_2001_period2 <- dygraph(dext_ca_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_25_to_50_2001_period2 <- dygraph(dext_ca_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_50_to_75_2001_period2 <- dygraph(dext_ca_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_a75_2001_period2 <- dygraph(dext_ca_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dg_ca_pct4_a75_2001_period2
dg_ca_pct4_50_to_75_2001_period2
dg_ca_pct4_25_to_50_2001_period2
dg_ca_pct4_b25_2001_period2

1.2 Public debt, domestic and external:

# load("./produced_data/data_with_basic_wrangling/cs_sector_publico")
# load("V:/USR/RMAYER/cw/cap_estudio_2017/produced_data/data_with_basic_wrangling/cs_sector_publico")

load(paste0(pre_path, 
            "produced_data/data_with_basic_wrangling/cs_sector_publico"))

1.2.1 Latin America

gobcen_deuda_18 <- gcentral_deuda_pib %>% 
  filter(iso3c %in% coi_18)

gobcen_deuda_tot_18_xts <- cate_gen_to_xts_wide(gobcen_deuda_18 %>% select(iso3c, year, total_p), is_full = TRUE, is_med = FALSE, value_col_name = "total_p")

gobcen_deuda_dom_18_xts <- cate_gen_to_xts_wide(gobcen_deuda_18 %>% select(iso3c, year, interna_p), is_full = TRUE, is_med = FALSE, value_col_name = "interna_p")

gobcen_deuda_ext_18_xts <- cate_gen_to_xts_wide(gobcen_deuda_18 %>% select(iso3c, year, externa_p), is_full = TRUE, is_med = FALSE, value_col_name = "externa_p")

# dext_to_gdp_18_m_bmv <- dext_to_gdp_18 %>% 
#   filter(iso3c %in% coi_18_minus_br_mx_ven)
# 
# dext_to_gdp_18_m_bmv_xts <- cate_dext_to_xts_wide(dext_to_gdp_18_m_bmv,
#                                                 is_full = TRUE, is_med = FALSE)

gobcen_deuda_tot_18_class_med_1990_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, total_p),
  value_col_name = "total_p")

gobcen_deuda_dom_18_class_med_1990_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p")

gobcen_deuda_ext_18_class_med_1990_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p")

gobcen_deuda_tot_18_class_pct3_1990_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, total_p),
  value_col_name = "total_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_18_class_pct3_1990_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_18_class_pct3_1990_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", is_med = FALSE, is_pct3 = TRUE)


gobcen_deuda_tot_18_class_pct4_1990_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, total_p),
  value_col_name = "total_p", is_med = FALSE,  is_pct4 = TRUE)

gobcen_deuda_dom_18_class_pct4_1990_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", is_med = FALSE,  is_pct4 = TRUE)

gobcen_deuda_ext_18_class_pct4_1990_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", is_med = FALSE,  is_pct4 = TRUE)


gobcen_deuda_tot_18_class_med_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, total_p),
  value_col_name = "total_p", time_breaks = default_time_break)

gobcen_deuda_dom_18_class_med_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", time_breaks = default_time_break)

gobcen_deuda_ext_18_class_med_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", time_breaks = default_time_break)

gobcen_deuda_tot_18_class_pct3_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, total_p),
  value_col_name = "total_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct3 = TRUE)

gobcen_deuda_dom_18_class_pct3_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct3 = TRUE)

gobcen_deuda_ext_18_class_pct3_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct3 = TRUE)

gobcen_deuda_tot_18_class_pct4_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, total_p),
  value_col_name = "total_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct4 = TRUE)

gobcen_deuda_dom_18_class_pct4_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct4 = TRUE)

gobcen_deuda_ext_18_class_pct4_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_18 %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct4 = TRUE)



gobcen_deuda_tot_18_med_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_tot_18_class_med_1990_2015, value_col_name = "total_p")

gobcen_deuda_dom_18_med_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_dom_18_class_med_1990_2015, value_col_name = "interna_p")

gobcen_deuda_ext_18_med_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_ext_18_class_med_1990_2015, value_col_name = "externa_p")

gobcen_deuda_tot_18_pct3_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_tot_18_class_pct3_1990_2015,
  value_col_name = "total_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_18_pct3_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_dom_18_class_pct3_1990_2015,
  value_col_name = "interna_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_18_pct3_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_ext_18_class_pct3_1990_2015,
  value_col_name = "externa_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_tot_18_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_tot_18_class_pct4_1990_2015,
  value_col_name = "total_p", is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_dom_18_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_dom_18_class_pct4_1990_2015,
  value_col_name = "interna_p", is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_ext_18_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_ext_18_class_pct4_1990_2015,
  value_col_name = "externa_p", is_med = FALSE, is_pct4 = TRUE)


gobcen_deuda_tot_18_med_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_tot_18_class_med_1990_2001_2015[[1]], 
                    value_col_name = "total_p")

gobcen_deuda_tot_18_med_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_tot_18_class_med_1990_2001_2015[[2]], 
                    value_col_name = "total_p")

gobcen_deuda_tot_18_pct3_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_tot_18_class_pct3_1990_2001_2015[[1]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_tot_18_pct3_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_tot_18_class_pct3_1990_2001_2015[[2]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_tot_18_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_tot_18_class_pct4_1990_2001_2015[[1]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_tot_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_tot_18_class_pct4_1990_2001_2015[[2]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_dom_18_med_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_dom_18_class_med_1990_2001_2015[[1]], 
                    value_col_name = "interna_p")

gobcen_deuda_dom_18_med_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_dom_18_class_med_1990_2001_2015[[2]], 
                    value_col_name = "interna_p")

gobcen_deuda_dom_18_pct3_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_dom_18_class_pct3_1990_2001_2015[[1]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_18_pct3_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_dom_18_class_pct3_1990_2001_2015[[2]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_18_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_dom_18_class_pct4_1990_2001_2015[[1]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_dom_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_dom_18_class_pct4_1990_2001_2015[[2]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_ext_18_med_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_ext_18_class_med_1990_2001_2015[[1]], 
                    value_col_name = "externa_p")

gobcen_deuda_ext_18_med_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_ext_18_class_med_1990_2001_2015[[2]], 
                    value_col_name = "externa_p")

gobcen_deuda_ext_18_pct3_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_ext_18_class_pct3_1990_2001_2015[[1]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_18_pct3_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_ext_18_class_pct3_1990_2001_2015[[2]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_18_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_ext_18_class_pct4_1990_2001_2015[[1]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_ext_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_ext_18_class_pct4_1990_2001_2015[[2]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct4 = TRUE)

1.2.1.1 All (18) countries

#using dygraph
dgcd_tot_title_18 <- "(Gross) Total Public Debt, as % of GDP"

dgcd_tot_18 <- dygraph(gobcen_deuda_tot_18_xts, main = dgcd_tot_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dgcd_tot_18
dgcd_dom_title_18 <- "(Gross) Domestic Public Debt, as % of GDP"

dgcd_dom_18 <- dygraph(gobcen_deuda_dom_18_xts, main = dgcd_dom_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dgcd_dom_18
dgcd_ext_title_18 <- "(Gross) External Public Debt, as % of GDP"

dgcd_ext_18 <- dygraph(gobcen_deuda_ext_18_xts, main = dgcd_ext_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dgcd_ext_18

1.2.1.2 Country debt above and below group median

1.2.1.2.1 Based on 1990-2015 values
dgcd_tot_18_med_a <- dygraph(gobcen_deuda_tot_18_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_med_b <- dygraph(gobcen_deuda_tot_18_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_med_a
dgcd_tot_18_med_b
dgcd_dom_18_med_a <- dygraph(gobcen_deuda_dom_18_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_med_b <- dygraph(gobcen_deuda_dom_18_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_med_a
dgcd_dom_18_med_b
dgcd_ext_18_med_a <- dygraph(gobcen_deuda_ext_18_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_med_b <- dygraph(gobcen_deuda_ext_18_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_med_a
dgcd_ext_18_med_b
1.2.1.2.2 Based on 1990-2001 and 2002-2015 values

Period: 1990 - 2001

dgcd_tot_18_med_2001_a_period1 <- dygraph(gobcen_deuda_tot_18_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_med_2001_b_period1 <- dygraph(gobcen_deuda_tot_18_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_med_2001_a_period1
dgcd_tot_18_med_2001_b_period1
dgcd_dom_18_med_2001_a_period1 <- dygraph(gobcen_deuda_dom_18_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_med_2001_b_period1 <- dygraph(gobcen_deuda_dom_18_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_med_2001_a_period1
dgcd_dom_18_med_2001_b_period1
dgcd_ext_18_med_2001_a_period1 <- dygraph(gobcen_deuda_ext_18_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_med_2001_b_period1 <- dygraph(gobcen_deuda_ext_18_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_med_2001_a_period1
dgcd_ext_18_med_2001_b_period1

Period: 2002-2015

dgcd_tot_18_med_2001_a_period2 <- dygraph(gobcen_deuda_tot_18_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_med_2001_b_period2 <- dygraph(gobcen_deuda_tot_18_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_med_2001_a_period2
dgcd_tot_18_med_2001_b_period2
dgcd_dom_18_med_2001_a_period2 <- dygraph(gobcen_deuda_dom_18_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_med_2001_b_period2 <- dygraph(gobcen_deuda_dom_18_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_med_2001_a_period2
dgcd_dom_18_med_2001_b_period2
dgcd_ext_18_med_2001_a_period2 <- dygraph(gobcen_deuda_ext_18_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_med_2001_b_period2 <- dygraph(gobcen_deuda_ext_18_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_med_2001_a_period2
dgcd_ext_18_med_2001_b_period2

1.2.1.3 Country debt above and below percentiles 33 and 66

1.2.1.3.1 Based on 1990-2015 values
dgcd_tot_18_pct3_b33 <- dygraph(gobcen_deuda_tot_18_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct3_33_to_66 <- dygraph(gobcen_deuda_tot_18_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct3_a66 <- dygraph(gobcen_deuda_tot_18_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct3_a66
dgcd_tot_18_pct3_33_to_66
dgcd_tot_18_pct3_b33
dgcd_dom_18_pct3_b33 <- dygraph(gobcen_deuda_dom_18_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct3_33_to_66 <- dygraph(gobcen_deuda_dom_18_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct3_a66 <- dygraph(gobcen_deuda_dom_18_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct3_a66
dgcd_dom_18_pct3_33_to_66
dgcd_dom_18_pct3_b33
dgcd_ext_18_pct3_b33 <- dygraph(gobcen_deuda_ext_18_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct3_33_to_66 <- dygraph(gobcen_deuda_ext_18_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct3_a66 <- dygraph(gobcen_deuda_ext_18_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct3_a66
dgcd_ext_18_pct3_33_to_66
dgcd_ext_18_pct3_b33
1.2.1.3.2 Based on 1990-2001 and 2002-2015 values

Period: 1990 - 2001

dgcd_tot_18_pct3_b33_2001_period1 <- dygraph(gobcen_deuda_tot_18_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct3_33_to_66_2001_period1 <- dygraph(gobcen_deuda_tot_18_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct3_a66_2001_period1 <- dygraph(gobcen_deuda_tot_18_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct3_a66_2001_period1
dgcd_tot_18_pct3_33_to_66_2001_period1
dgcd_tot_18_pct3_b33_2001_period1
dgcd_dom_18_pct3_b33_2001_period1 <- dygraph(gobcen_deuda_dom_18_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct3_33_to_66_2001_period1 <- dygraph(gobcen_deuda_dom_18_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct3_a66_2001_period1 <- dygraph(gobcen_deuda_dom_18_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct3_a66_2001_period1
dgcd_dom_18_pct3_33_to_66_2001_period1
dgcd_dom_18_pct3_b33_2001_period1
dgcd_ext_18_pct3_b33_2001_period1 <- dygraph(gobcen_deuda_ext_18_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct3_33_to_66_2001_period1 <- dygraph(gobcen_deuda_ext_18_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct3_a66_2001_period1 <- dygraph(gobcen_deuda_ext_18_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct3_a66_2001_period1
dgcd_ext_18_pct3_33_to_66_2001_period1
dgcd_ext_18_pct3_b33_2001_period1

Period: 2002 - 2015

dgcd_tot_18_pct3_b33_2001_period2 <- dygraph(gobcen_deuda_tot_18_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct3_33_to_66_2001_period2 <- dygraph(gobcen_deuda_tot_18_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct3_a66_2001_period2 <- dygraph(gobcen_deuda_tot_18_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct3_a66_2001_period2
dgcd_tot_18_pct3_33_to_66_2001_period2
dgcd_tot_18_pct3_b33_2001_period2
dgcd_dom_18_pct3_b33_2001_period2 <- dygraph(gobcen_deuda_dom_18_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct3_33_to_66_2001_period2 <- dygraph(gobcen_deuda_dom_18_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct3_a66_2001_period2 <- dygraph(gobcen_deuda_dom_18_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct3_a66_2001_period2
dgcd_dom_18_pct3_33_to_66_2001_period2
dgcd_dom_18_pct3_b33_2001_period2
dgcd_ext_18_pct3_b33_2001_period2 <- dygraph(gobcen_deuda_ext_18_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct3_33_to_66_2001_period2 <- dygraph(gobcen_deuda_ext_18_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct3_a66_2001_period2 <- dygraph(gobcen_deuda_ext_18_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct3_a66_2001_period2
dgcd_ext_18_pct3_33_to_66_2001_period2
dgcd_ext_18_pct3_b33_2001_period2

1.2.1.4 Countries sorted into quartiles of the Y-axis variable

1.2.1.4.1 Based on 1990-2015 values
dgcd_tot_18_pct4_b25 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct4_25_to_50 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct4_50_to_75 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct4_a75 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_18_pct4_a75
dgcd_tot_18_pct4_50_to_75
dgcd_tot_18_pct4_25_to_50
dgcd_tot_18_pct4_b25
dgcd_dom_18_pct4_b25 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct4_25_to_50 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct4_50_to_75 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct4_a75 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_18_pct4_a75
dgcd_dom_18_pct4_50_to_75
dgcd_dom_18_pct4_25_to_50
dgcd_dom_18_pct4_b25
dgcd_ext_18_pct4_b25 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct4_25_to_50 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct4_50_to_75 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct4_a75 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_18_pct4_a75
dgcd_ext_18_pct4_50_to_75
dgcd_ext_18_pct4_25_to_50
dgcd_ext_18_pct4_b25
1.2.1.4.2 Based on 1990-2001 ans 2002-2015 values

Period 1990 - 2001

dgcg_tot_18_pct4_b25_2001_period1 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_18_pct4_25_to_50_2001_period1 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_18_pct4_50_to_75_2001_period1 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_18_pct4_a75_2001_period1 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_18_pct4_a75_2001_period1
dgcg_tot_18_pct4_50_to_75_2001_period1
dgcg_tot_18_pct4_25_to_50_2001_period1
dgcg_tot_18_pct4_b25_2001_period1
dgcg_dom_18_pct4_b25_2001_period1 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_18_pct4_25_to_50_2001_period1 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_18_pct4_50_to_75_2001_period1 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_18_pct4_a75_2001_period1 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_18_pct4_a75_2001_period1
dgcg_dom_18_pct4_50_to_75_2001_period1
dgcg_dom_18_pct4_25_to_50_2001_period1
dgcg_dom_18_pct4_b25_2001_period1
dgcg_ext_18_pct4_b25_2001_period1 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_18_pct4_25_to_50_2001_period1 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_18_pct4_50_to_75_2001_period1 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_18_pct4_a75_2001_period1 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_18_pct4_a75_2001_period1
dgcg_ext_18_pct4_50_to_75_2001_period1
dgcg_ext_18_pct4_25_to_50_2001_period1
dgcg_ext_18_pct4_b25_2001_period1

Period: 2002-2015

dgcg_tot_18_pct4_b25_2001_period2 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_18_pct4_25_to_50_2001_period2 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_18_pct4_50_to_75_2001_period2 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_18_pct4_a75_2001_period2 <- dygraph(gobcen_deuda_tot_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_18_pct4_a75_2001_period2
dgcg_tot_18_pct4_50_to_75_2001_period2
dgcg_tot_18_pct4_25_to_50_2001_period2
dgcg_tot_18_pct4_b25_2001_period2
dgcg_dom_18_pct4_b25_2001_period2 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_18_pct4_25_to_50_2001_period2 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_18_pct4_50_to_75_2001_period2 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_18_pct4_a75_2001_period2 <- dygraph(gobcen_deuda_dom_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_18_pct4_a75_2001_period2
dgcg_dom_18_pct4_50_to_75_2001_period2
dgcg_dom_18_pct4_25_to_50_2001_period2
dgcg_dom_18_pct4_b25_2001_period2
dgcg_ext_18_pct4_b25_2001_period2 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_18_pct4_25_to_50_2001_period2 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_18_pct4_50_to_75_2001_period2 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_18_pct4_a75_2001_period2 <- dygraph(gobcen_deuda_ext_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_18_pct4_a75_2001_period2
dgcg_ext_18_pct4_50_to_75_2001_period2
dgcg_ext_18_pct4_25_to_50_2001_period2
dgcg_ext_18_pct4_b25_2001_period2

1.2.2 South America

gobcen_deuda_sa <- gcentral_deuda_pib %>% 
  filter(iso3c %in% coi_sa)

gobcen_deuda_tot_sa_xts <- cate_gen_to_xts_wide(gobcen_deuda_sa %>% select(iso3c, year, total_p), is_full = TRUE, is_med = FALSE, value_col_name = "total_p")

gobcen_deuda_dom_sa_xts <- cate_gen_to_xts_wide(gobcen_deuda_sa %>% select(iso3c, year, interna_p), is_full = TRUE, is_med = FALSE, value_col_name = "interna_p")

gobcen_deuda_ext_sa_xts <- cate_gen_to_xts_wide(gobcen_deuda_sa %>% select(iso3c, year, externa_p), is_full = TRUE, is_med = FALSE, value_col_name = "externa_p")

# dext_to_gdp_sa_m_bmv <- dext_to_gdp_sa %>% 
#   filter(iso3c %in% coi_sa_minus_br_mx_ven)
# 
# dext_to_gdp_sa_m_bmv_xts <- cate_dext_to_xts_wide(dext_to_gdp_sa_m_bmv,
#                                                 is_full = TRUE, is_med = FALSE)

gobcen_deuda_tot_sa_class_med_1990_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, total_p),
  value_col_name = "total_p")

gobcen_deuda_dom_sa_class_med_1990_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p")

gobcen_deuda_ext_sa_class_med_1990_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p")

gobcen_deuda_tot_sa_class_pct3_1990_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, total_p),
  value_col_name = "total_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_sa_class_pct3_1990_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_sa_class_pct3_1990_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", is_med = FALSE, is_pct3 = TRUE)


gobcen_deuda_tot_sa_class_pct4_1990_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, total_p),
  value_col_name = "total_p", is_med = FALSE,  is_pct4 = TRUE)

gobcen_deuda_dom_sa_class_pct4_1990_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", is_med = FALSE,  is_pct4 = TRUE)

gobcen_deuda_ext_sa_class_pct4_1990_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", is_med = FALSE,  is_pct4 = TRUE)


gobcen_deuda_tot_sa_class_med_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, total_p),
  value_col_name = "total_p", time_breaks = default_time_break)

gobcen_deuda_dom_sa_class_med_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", time_breaks = default_time_break)

gobcen_deuda_ext_sa_class_med_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", time_breaks = default_time_break)

gobcen_deuda_tot_sa_class_pct3_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, total_p),
  value_col_name = "total_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct3 = TRUE)

gobcen_deuda_dom_sa_class_pct3_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct3 = TRUE)

gobcen_deuda_ext_sa_class_pct3_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct3 = TRUE)

gobcen_deuda_tot_sa_class_pct4_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, total_p),
  value_col_name = "total_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct4 = TRUE)

gobcen_deuda_dom_sa_class_pct4_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct4 = TRUE)

gobcen_deuda_ext_sa_class_pct4_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_sa %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct4 = TRUE)



gobcen_deuda_tot_sa_med_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_tot_sa_class_med_1990_2015, value_col_name = "total_p")

gobcen_deuda_dom_sa_med_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_dom_sa_class_med_1990_2015, value_col_name = "interna_p")

gobcen_deuda_ext_sa_med_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_ext_sa_class_med_1990_2015, value_col_name = "externa_p")

gobcen_deuda_tot_sa_pct3_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_tot_sa_class_pct3_1990_2015,
  value_col_name = "total_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_sa_pct3_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_dom_sa_class_pct3_1990_2015,
  value_col_name = "interna_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_sa_pct3_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_ext_sa_class_pct3_1990_2015,
  value_col_name = "externa_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_tot_sa_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_tot_sa_class_pct4_1990_2015,
  value_col_name = "total_p", is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_dom_sa_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_dom_sa_class_pct4_1990_2015,
  value_col_name = "interna_p", is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_ext_sa_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_ext_sa_class_pct4_1990_2015,
  value_col_name = "externa_p", is_med = FALSE, is_pct4 = TRUE)


gobcen_deuda_tot_sa_med_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_tot_sa_class_med_1990_2001_2015[[1]], 
                    value_col_name = "total_p")

gobcen_deuda_tot_sa_med_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_tot_sa_class_med_1990_2001_2015[[2]], 
                    value_col_name = "total_p")

gobcen_deuda_tot_sa_pct3_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_tot_sa_class_pct3_1990_2001_2015[[1]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_tot_sa_pct3_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_tot_sa_class_pct3_1990_2001_2015[[2]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_tot_sa_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_tot_sa_class_pct4_1990_2001_2015[[1]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_tot_sa_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_tot_sa_class_pct4_1990_2001_2015[[2]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_dom_sa_med_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_dom_sa_class_med_1990_2001_2015[[1]], 
                    value_col_name = "interna_p")

gobcen_deuda_dom_sa_med_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_dom_sa_class_med_1990_2001_2015[[2]], 
                    value_col_name = "interna_p")

gobcen_deuda_dom_sa_pct3_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_dom_sa_class_pct3_1990_2001_2015[[1]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_sa_pct3_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_dom_sa_class_pct3_1990_2001_2015[[2]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_sa_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_dom_sa_class_pct4_1990_2001_2015[[1]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_dom_sa_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_dom_sa_class_pct4_1990_2001_2015[[2]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_ext_sa_med_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_ext_sa_class_med_1990_2001_2015[[1]], 
                    value_col_name = "externa_p")

gobcen_deuda_ext_sa_med_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_ext_sa_class_med_1990_2001_2015[[2]], 
                    value_col_name = "externa_p")

gobcen_deuda_ext_sa_pct3_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_ext_sa_class_pct3_1990_2001_2015[[1]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_sa_pct3_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_ext_sa_class_pct3_1990_2001_2015[[2]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_sa_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_ext_sa_class_pct4_1990_2001_2015[[1]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_ext_sa_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_ext_sa_class_pct4_1990_2001_2015[[2]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct4 = TRUE)

1.2.2.1 All (sa) countries

#using dygraph
dgcd_tot_title_sa <- "(Gross) Total Public Debt, as % of GDP"

dgcd_tot_sa <- dygraph(gobcen_deuda_tot_sa_xts, main = dgcd_tot_title_sa) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dgcd_tot_sa
dgcd_dom_title_sa <- "(Gross) Domestic Public Debt, as % of GDP"

dgcd_dom_sa <- dygraph(gobcen_deuda_dom_sa_xts, main = dgcd_dom_title_sa) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dgcd_dom_sa
dgcd_ext_title_sa <- "(Gross) External Public Debt, as % of GDP"

dgcd_ext_sa <- dygraph(gobcen_deuda_ext_sa_xts, main = dgcd_ext_title_sa) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dgcd_ext_sa

1.2.2.2 Country debt above and below group median

1.2.2.2.1 Based on 1990-2015 values
dgcd_tot_sa_med_a <- dygraph(gobcen_deuda_tot_sa_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_med_b <- dygraph(gobcen_deuda_tot_sa_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_med_a
dgcd_tot_sa_med_b
dgcd_dom_sa_med_a <- dygraph(gobcen_deuda_dom_sa_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_med_b <- dygraph(gobcen_deuda_dom_sa_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_med_a
dgcd_dom_sa_med_b
dgcd_ext_sa_med_a <- dygraph(gobcen_deuda_ext_sa_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_med_b <- dygraph(gobcen_deuda_ext_sa_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_med_a
dgcd_ext_sa_med_b
1.2.2.2.2 Based on 1990-2001 and 2002-2015 values

Period: 1990 - 2001

dgcd_tot_sa_med_2001_a_period1 <- dygraph(gobcen_deuda_tot_sa_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_med_2001_b_period1 <- dygraph(gobcen_deuda_tot_sa_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_med_2001_a_period1
dgcd_tot_sa_med_2001_b_period1
dgcd_dom_sa_med_2001_a_period1 <- dygraph(gobcen_deuda_dom_sa_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_med_2001_b_period1 <- dygraph(gobcen_deuda_dom_sa_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_med_2001_a_period1
dgcd_dom_sa_med_2001_b_period1
dgcd_ext_sa_med_2001_a_period1 <- dygraph(gobcen_deuda_ext_sa_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_med_2001_b_period1 <- dygraph(gobcen_deuda_ext_sa_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_med_2001_a_period1
dgcd_ext_sa_med_2001_b_period1

Period: 2002-2015

dgcd_tot_sa_med_2001_a_period2 <- dygraph(gobcen_deuda_tot_sa_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_med_2001_b_period2 <- dygraph(gobcen_deuda_tot_sa_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_med_2001_a_period2
dgcd_tot_sa_med_2001_b_period2
dgcd_dom_sa_med_2001_a_period2 <- dygraph(gobcen_deuda_dom_sa_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_med_2001_b_period2 <- dygraph(gobcen_deuda_dom_sa_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_med_2001_a_period2
dgcd_dom_sa_med_2001_b_period2
dgcd_ext_sa_med_2001_a_period2 <- dygraph(gobcen_deuda_ext_sa_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_med_2001_b_period2 <- dygraph(gobcen_deuda_ext_sa_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_med_2001_a_period2
dgcd_ext_sa_med_2001_b_period2

1.2.2.3 Country debt above and below percentiles 33 and 66

1.2.2.3.1 Based on 1990-2015 values
dgcd_tot_sa_pct3_b33 <- dygraph(gobcen_deuda_tot_sa_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct3_33_to_66 <- dygraph(gobcen_deuda_tot_sa_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct3_a66 <- dygraph(gobcen_deuda_tot_sa_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct3_a66
dgcd_tot_sa_pct3_33_to_66
dgcd_tot_sa_pct3_b33
dgcd_dom_sa_pct3_b33 <- dygraph(gobcen_deuda_dom_sa_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct3_33_to_66 <- dygraph(gobcen_deuda_dom_sa_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct3_a66 <- dygraph(gobcen_deuda_dom_sa_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct3_a66
dgcd_dom_sa_pct3_33_to_66
dgcd_dom_sa_pct3_b33
dgcd_ext_sa_pct3_b33 <- dygraph(gobcen_deuda_ext_sa_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct3_33_to_66 <- dygraph(gobcen_deuda_ext_sa_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct3_a66 <- dygraph(gobcen_deuda_ext_sa_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct3_a66
dgcd_ext_sa_pct3_33_to_66
dgcd_ext_sa_pct3_b33
1.2.2.3.2 Based on 1990-2001 and 2002-2015 values

Period: 1990 - 2001

dgcd_tot_sa_pct3_b33_2001_period1 <- dygraph(gobcen_deuda_tot_sa_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct3_33_to_66_2001_period1 <- dygraph(gobcen_deuda_tot_sa_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct3_a66_2001_period1 <- dygraph(gobcen_deuda_tot_sa_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct3_a66_2001_period1
dgcd_tot_sa_pct3_33_to_66_2001_period1
dgcd_tot_sa_pct3_b33_2001_period1
dgcd_dom_sa_pct3_b33_2001_period1 <- dygraph(gobcen_deuda_dom_sa_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct3_33_to_66_2001_period1 <- dygraph(gobcen_deuda_dom_sa_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct3_a66_2001_period1 <- dygraph(gobcen_deuda_dom_sa_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct3_a66_2001_period1
dgcd_dom_sa_pct3_33_to_66_2001_period1
dgcd_dom_sa_pct3_b33_2001_period1
dgcd_ext_sa_pct3_b33_2001_period1 <- dygraph(gobcen_deuda_ext_sa_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct3_33_to_66_2001_period1 <- dygraph(gobcen_deuda_ext_sa_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct3_a66_2001_period1 <- dygraph(gobcen_deuda_ext_sa_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct3_a66_2001_period1
dgcd_ext_sa_pct3_33_to_66_2001_period1
dgcd_ext_sa_pct3_b33_2001_period1

Period: 2002 - 2015

dgcd_tot_sa_pct3_b33_2001_period2 <- dygraph(gobcen_deuda_tot_sa_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct3_33_to_66_2001_period2 <- dygraph(gobcen_deuda_tot_sa_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct3_a66_2001_period2 <- dygraph(gobcen_deuda_tot_sa_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct3_a66_2001_period2
dgcd_tot_sa_pct3_33_to_66_2001_period2
dgcd_tot_sa_pct3_b33_2001_period2
dgcd_dom_sa_pct3_b33_2001_period2 <- dygraph(gobcen_deuda_dom_sa_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct3_33_to_66_2001_period2 <- dygraph(gobcen_deuda_dom_sa_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct3_a66_2001_period2 <- dygraph(gobcen_deuda_dom_sa_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct3_a66_2001_period2
dgcd_dom_sa_pct3_33_to_66_2001_period2
dgcd_dom_sa_pct3_b33_2001_period2
dgcd_ext_sa_pct3_b33_2001_period2 <- dygraph(gobcen_deuda_ext_sa_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct3_33_to_66_2001_period2 <- dygraph(gobcen_deuda_ext_sa_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct3_a66_2001_period2 <- dygraph(gobcen_deuda_ext_sa_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct3_a66_2001_period2
dgcd_ext_sa_pct3_33_to_66_2001_period2
dgcd_ext_sa_pct3_b33_2001_period2

1.2.2.4 Countries sorted into quartiles of the Y-axis variable

1.2.2.4.1 Based on 1990-2015 values
dgcd_tot_sa_pct4_b25 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct4_25_to_50 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct4_50_to_75 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct4_a75 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_sa_pct4_a75
dgcd_tot_sa_pct4_50_to_75
dgcd_tot_sa_pct4_25_to_50
dgcd_tot_sa_pct4_b25
dgcd_dom_sa_pct4_b25 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct4_25_to_50 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct4_50_to_75 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct4_a75 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_sa_pct4_a75
dgcd_dom_sa_pct4_50_to_75
dgcd_dom_sa_pct4_25_to_50
dgcd_dom_sa_pct4_b25
dgcd_ext_sa_pct4_b25 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct4_25_to_50 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct4_50_to_75 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct4_a75 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_sa_pct4_a75
dgcd_ext_sa_pct4_50_to_75
dgcd_ext_sa_pct4_25_to_50
dgcd_ext_sa_pct4_b25
1.2.2.4.2 Based on 1990-2001 ans 2002-2015 values

Period 1990 - 2001

dgcg_tot_sa_pct4_b25_2001_period1 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_sa_pct4_25_to_50_2001_period1 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_sa_pct4_50_to_75_2001_period1 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_sa_pct4_a75_2001_period1 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_sa_pct4_a75_2001_period1
dgcg_tot_sa_pct4_50_to_75_2001_period1
dgcg_tot_sa_pct4_25_to_50_2001_period1
dgcg_tot_sa_pct4_b25_2001_period1
dgcg_dom_sa_pct4_b25_2001_period1 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_sa_pct4_25_to_50_2001_period1 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_sa_pct4_50_to_75_2001_period1 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_sa_pct4_a75_2001_period1 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_sa_pct4_a75_2001_period1
dgcg_dom_sa_pct4_50_to_75_2001_period1
dgcg_dom_sa_pct4_25_to_50_2001_period1
dgcg_dom_sa_pct4_b25_2001_period1
dgcg_ext_sa_pct4_b25_2001_period1 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_sa_pct4_25_to_50_2001_period1 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_sa_pct4_50_to_75_2001_period1 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_sa_pct4_a75_2001_period1 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_sa_pct4_a75_2001_period1
dgcg_ext_sa_pct4_50_to_75_2001_period1
dgcg_ext_sa_pct4_25_to_50_2001_period1
dgcg_ext_sa_pct4_b25_2001_period1

Period: 2002-2015

dgcg_tot_sa_pct4_b25_2001_period2 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_sa_pct4_25_to_50_2001_period2 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_sa_pct4_50_to_75_2001_period2 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_sa_pct4_a75_2001_period2 <- dygraph(gobcen_deuda_tot_sa_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_sa_pct4_a75_2001_period2
dgcg_tot_sa_pct4_50_to_75_2001_period2
dgcg_tot_sa_pct4_25_to_50_2001_period2
dgcg_tot_sa_pct4_b25_2001_period2
dgcg_dom_sa_pct4_b25_2001_period2 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_sa_pct4_25_to_50_2001_period2 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_sa_pct4_50_to_75_2001_period2 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_sa_pct4_a75_2001_period2 <- dygraph(gobcen_deuda_dom_sa_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_sa_pct4_a75_2001_period2
dgcg_dom_sa_pct4_50_to_75_2001_period2
dgcg_dom_sa_pct4_25_to_50_2001_period2
dgcg_dom_sa_pct4_b25_2001_period2
dgcg_ext_sa_pct4_b25_2001_period2 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_sa_pct4_25_to_50_2001_period2 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_sa_pct4_50_to_75_2001_period2 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_sa_pct4_a75_2001_period2 <- dygraph(gobcen_deuda_ext_sa_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_sa_pct4_a75_2001_period2
dgcg_ext_sa_pct4_50_to_75_2001_period2
dgcg_ext_sa_pct4_25_to_50_2001_period2
dgcg_ext_sa_pct4_b25_2001_period2

1.2.3 Central America

gobcen_deuda_ca <- gcentral_deuda_pib %>% 
  filter(iso3c %in% coi_ca)

gobcen_deuda_tot_ca_xts <- cate_gen_to_xts_wide(gobcen_deuda_ca %>% select(iso3c, year, total_p), is_full = TRUE, is_med = FALSE, value_col_name = "total_p")

gobcen_deuda_dom_ca_xts <- cate_gen_to_xts_wide(gobcen_deuda_ca %>% select(iso3c, year, interna_p), is_full = TRUE, is_med = FALSE, value_col_name = "interna_p")

gobcen_deuda_ext_ca_xts <- cate_gen_to_xts_wide(gobcen_deuda_ca %>% select(iso3c, year, externa_p), is_full = TRUE, is_med = FALSE, value_col_name = "externa_p")

# dext_to_gdp_ca_m_bmv <- dext_to_gdp_ca %>% 
#   filter(iso3c %in% coi_ca_minus_br_mx_ven)
# 
# dext_to_gdp_ca_m_bmv_xts <- cate_dext_to_xts_wide(dext_to_gdp_ca_m_bmv,
#                                                 is_full = TRUE, is_med = FALSE)

gobcen_deuda_tot_ca_class_med_1990_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, total_p),
  value_col_name = "total_p")

gobcen_deuda_dom_ca_class_med_1990_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p")

gobcen_deuda_ext_ca_class_med_1990_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p")

gobcen_deuda_tot_ca_class_pct3_1990_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, total_p),
  value_col_name = "total_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_ca_class_pct3_1990_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_ca_class_pct3_1990_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", is_med = FALSE, is_pct3 = TRUE)


gobcen_deuda_tot_ca_class_pct4_1990_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, total_p),
  value_col_name = "total_p", is_med = FALSE,  is_pct4 = TRUE)

gobcen_deuda_dom_ca_class_pct4_1990_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", is_med = FALSE,  is_pct4 = TRUE)

gobcen_deuda_ext_ca_class_pct4_1990_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", is_med = FALSE,  is_pct4 = TRUE)


gobcen_deuda_tot_ca_class_med_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, total_p),
  value_col_name = "total_p", time_breaks = default_time_break)

gobcen_deuda_dom_ca_class_med_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", time_breaks = default_time_break)

gobcen_deuda_ext_ca_class_med_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", time_breaks = default_time_break)

gobcen_deuda_tot_ca_class_pct3_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, total_p),
  value_col_name = "total_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct3 = TRUE)

gobcen_deuda_dom_ca_class_pct3_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct3 = TRUE)

gobcen_deuda_ext_ca_class_pct3_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct3 = TRUE)

gobcen_deuda_tot_ca_class_pct4_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, total_p),
  value_col_name = "total_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct4 = TRUE)

gobcen_deuda_dom_ca_class_pct4_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, interna_p),
  value_col_name = "interna_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct4 = TRUE)

gobcen_deuda_ext_ca_class_pct4_1990_2001_2015 <- cate_gen(
  df = gobcen_deuda_ca %>% select(iso3c, year, externa_p),
  value_col_name = "externa_p", time_breaks = default_time_break, is_med = FALSE,
                                               is_pct4 = TRUE)



gobcen_deuda_tot_ca_med_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_tot_ca_class_med_1990_2015, value_col_name = "total_p")

gobcen_deuda_dom_ca_med_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_dom_ca_class_med_1990_2015, value_col_name = "interna_p")

gobcen_deuda_ext_ca_med_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_ext_ca_class_med_1990_2015, value_col_name = "externa_p")

gobcen_deuda_tot_ca_pct3_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_tot_ca_class_pct3_1990_2015,
  value_col_name = "total_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_ca_pct3_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_dom_ca_class_pct3_1990_2015,
  value_col_name = "interna_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_ca_pct3_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_ext_ca_class_pct3_1990_2015,
  value_col_name = "externa_p", is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_tot_ca_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_tot_ca_class_pct4_1990_2015,
  value_col_name = "total_p", is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_dom_ca_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_dom_ca_class_pct4_1990_2015,
  value_col_name = "interna_p", is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_ext_ca_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  gobcen_deuda_ext_ca_class_pct4_1990_2015,
  value_col_name = "externa_p", is_med = FALSE, is_pct4 = TRUE)


gobcen_deuda_tot_ca_med_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_tot_ca_class_med_1990_2001_2015[[1]], 
                    value_col_name = "total_p")

gobcen_deuda_tot_ca_med_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_tot_ca_class_med_1990_2001_2015[[2]], 
                    value_col_name = "total_p")

gobcen_deuda_tot_ca_pct3_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_tot_ca_class_pct3_1990_2001_2015[[1]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_tot_ca_pct3_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_tot_ca_class_pct3_1990_2001_2015[[2]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_tot_ca_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_tot_ca_class_pct4_1990_2001_2015[[1]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_tot_ca_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_tot_ca_class_pct4_1990_2001_2015[[2]], 
                    value_col_name = "total_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_dom_ca_med_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_dom_ca_class_med_1990_2001_2015[[1]], 
                    value_col_name = "interna_p")

gobcen_deuda_dom_ca_med_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_dom_ca_class_med_1990_2001_2015[[2]], 
                    value_col_name = "interna_p")

gobcen_deuda_dom_ca_pct3_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_dom_ca_class_pct3_1990_2001_2015[[1]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_ca_pct3_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_dom_ca_class_pct3_1990_2001_2015[[2]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_dom_ca_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_dom_ca_class_pct4_1990_2001_2015[[1]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_dom_ca_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_dom_ca_class_pct4_1990_2001_2015[[2]], 
                    value_col_name = "interna_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_ext_ca_med_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_ext_ca_class_med_1990_2001_2015[[1]], 
                    value_col_name = "externa_p")

gobcen_deuda_ext_ca_med_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_ext_ca_class_med_1990_2001_2015[[2]], 
                    value_col_name = "externa_p")

gobcen_deuda_ext_ca_pct3_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_ext_ca_class_pct3_1990_2001_2015[[1]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_ca_pct3_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_ext_ca_class_pct3_1990_2001_2015[[2]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct3 = TRUE)

gobcen_deuda_ext_ca_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(gobcen_deuda_ext_ca_class_pct4_1990_2001_2015[[1]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct4 = TRUE)

gobcen_deuda_ext_ca_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(gobcen_deuda_ext_ca_class_pct4_1990_2001_2015[[2]], 
                    value_col_name = "externa_p",is_med = FALSE, is_pct4 = TRUE)

1.2.3.1 All (ca) countries

#using dygraph
dgcd_tot_title_ca <- "(Gross) Total Public Debt, as % of GDP"

dgcd_tot_ca <- dygraph(gobcen_deuda_tot_ca_xts, main = dgcd_tot_title_ca) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dgcd_tot_ca
dgcd_dom_title_ca <- "(Gross) Domestic Public Debt, as % of GDP"

dgcd_dom_ca <- dygraph(gobcen_deuda_dom_ca_xts, main = dgcd_dom_title_ca) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dgcd_dom_ca
dgcd_ext_title_ca <- "(Gross) External Public Debt, as % of GDP"

dgcd_ext_ca <- dygraph(gobcen_deuda_ext_ca_xts, main = dgcd_ext_title_ca) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dgcd_ext_ca

1.2.3.2 Country debt above and below group median

1.2.3.2.1 Based on 1990-2015 values
dgcd_tot_ca_med_a <- dygraph(gobcen_deuda_tot_ca_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_med_b <- dygraph(gobcen_deuda_tot_ca_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_med_a
dgcd_tot_ca_med_b
dgcd_dom_ca_med_a <- dygraph(gobcen_deuda_dom_ca_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_med_b <- dygraph(gobcen_deuda_dom_ca_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_med_a
dgcd_dom_ca_med_b
dgcd_ext_ca_med_a <- dygraph(gobcen_deuda_ext_ca_med_1990_2015_xts[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_med_b <- dygraph(gobcen_deuda_ext_ca_med_1990_2015_xts[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_med_a
dgcd_ext_ca_med_b
1.2.3.2.2 Based on 1990-2001 and 2002-2015 values

Period: 1990 - 2001

dgcd_tot_ca_med_2001_a_period1 <- dygraph(gobcen_deuda_tot_ca_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_med_2001_b_period1 <- dygraph(gobcen_deuda_tot_ca_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_med_2001_a_period1
dgcd_tot_ca_med_2001_b_period1
dgcd_dom_ca_med_2001_a_period1 <- dygraph(gobcen_deuda_dom_ca_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_med_2001_b_period1 <- dygraph(gobcen_deuda_dom_ca_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_med_2001_a_period1
dgcd_dom_ca_med_2001_b_period1
dgcd_ext_ca_med_2001_a_period1 <- dygraph(gobcen_deuda_ext_ca_med_1990_2001_2015_xts_1[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_med_2001_b_period1 <- dygraph(gobcen_deuda_ext_ca_med_1990_2001_2015_xts_1[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_med_2001_a_period1
dgcd_ext_ca_med_2001_b_period1

Period: 2002-2015

dgcd_tot_ca_med_2001_a_period2 <- dygraph(gobcen_deuda_tot_ca_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_med_2001_b_period2 <- dygraph(gobcen_deuda_tot_ca_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_med_2001_a_period2
dgcd_tot_ca_med_2001_b_period2
dgcd_dom_ca_med_2001_a_period2 <- dygraph(gobcen_deuda_dom_ca_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_med_2001_b_period2 <- dygraph(gobcen_deuda_dom_ca_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_med_2001_a_period2
dgcd_dom_ca_med_2001_b_period2
dgcd_ext_ca_med_2001_a_period2 <- dygraph(gobcen_deuda_ext_ca_med_1990_2001_2015_xts_2[["above_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_med_2001_b_period2 <- dygraph(gobcen_deuda_ext_ca_med_1990_2001_2015_xts_2[["below_median"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_med_2001_a_period2
dgcd_ext_ca_med_2001_b_period2

1.2.3.3 Country debt above and below percentiles 33 and 66

1.2.3.3.1 Based on 1990-2015 values
dgcd_tot_ca_pct3_b33 <- dygraph(gobcen_deuda_tot_ca_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct3_33_to_66 <- dygraph(gobcen_deuda_tot_ca_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct3_a66 <- dygraph(gobcen_deuda_tot_ca_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct3_a66
dgcd_tot_ca_pct3_33_to_66
dgcd_tot_ca_pct3_b33
dgcd_dom_ca_pct3_b33 <- dygraph(gobcen_deuda_dom_ca_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct3_33_to_66 <- dygraph(gobcen_deuda_dom_ca_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct3_a66 <- dygraph(gobcen_deuda_dom_ca_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct3_a66
dgcd_dom_ca_pct3_33_to_66
dgcd_dom_ca_pct3_b33
dgcd_ext_ca_pct3_b33 <- dygraph(gobcen_deuda_ext_ca_pct3_1990_2015_xts[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct3_33_to_66 <- dygraph(gobcen_deuda_ext_ca_pct3_1990_2015_xts[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct3_a66 <- dygraph(gobcen_deuda_ext_ca_pct3_1990_2015_xts[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct3_a66
dgcd_ext_ca_pct3_33_to_66
dgcd_ext_ca_pct3_b33
1.2.3.3.2 Based on 1990-2001 and 2002-2015 values

Period: 1990 - 2001

dgcd_tot_ca_pct3_b33_2001_period1 <- dygraph(gobcen_deuda_tot_ca_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct3_33_to_66_2001_period1 <- dygraph(gobcen_deuda_tot_ca_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct3_a66_2001_period1 <- dygraph(gobcen_deuda_tot_ca_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct3_a66_2001_period1
dgcd_tot_ca_pct3_33_to_66_2001_period1
dgcd_tot_ca_pct3_b33_2001_period1
dgcd_dom_ca_pct3_b33_2001_period1 <- dygraph(gobcen_deuda_dom_ca_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct3_33_to_66_2001_period1 <- dygraph(gobcen_deuda_dom_ca_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct3_a66_2001_period1 <- dygraph(gobcen_deuda_dom_ca_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct3_a66_2001_period1
dgcd_dom_ca_pct3_33_to_66_2001_period1
dgcd_dom_ca_pct3_b33_2001_period1
dgcd_ext_ca_pct3_b33_2001_period1 <- dygraph(gobcen_deuda_ext_ca_pct3_1990_2001_2015_xts_1[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct3_33_to_66_2001_period1 <- dygraph(gobcen_deuda_ext_ca_pct3_1990_2001_2015_xts_1[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct3_a66_2001_period1 <- dygraph(gobcen_deuda_ext_ca_pct3_1990_2001_2015_xts_1[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct3_a66_2001_period1
dgcd_ext_ca_pct3_33_to_66_2001_period1
dgcd_ext_ca_pct3_b33_2001_period1

Period: 2002 - 2015

dgcd_tot_ca_pct3_b33_2001_period2 <- dygraph(gobcen_deuda_tot_ca_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct3_33_to_66_2001_period2 <- dygraph(gobcen_deuda_tot_ca_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct3_a66_2001_period2 <- dygraph(gobcen_deuda_tot_ca_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct3_a66_2001_period2
dgcd_tot_ca_pct3_33_to_66_2001_period2
dgcd_tot_ca_pct3_b33_2001_period2
dgcd_dom_ca_pct3_b33_2001_period2 <- dygraph(gobcen_deuda_dom_ca_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct3_33_to_66_2001_period2 <- dygraph(gobcen_deuda_dom_ca_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct3_a66_2001_period2 <- dygraph(gobcen_deuda_dom_ca_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct3_a66_2001_period2
dgcd_dom_ca_pct3_33_to_66_2001_period2
dgcd_dom_ca_pct3_b33_2001_period2
dgcd_ext_ca_pct3_b33_2001_period2 <- dygraph(gobcen_deuda_ext_ca_pct3_1990_2001_2015_xts_2[["below_p33"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct3_33_to_66_2001_period2 <- dygraph(gobcen_deuda_ext_ca_pct3_1990_2001_2015_xts_2[["p33_to_66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct3_a66_2001_period2 <- dygraph(gobcen_deuda_ext_ca_pct3_1990_2001_2015_xts_2[["above_p66"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct3_a66_2001_period2
dgcd_ext_ca_pct3_33_to_66_2001_period2
dgcd_ext_ca_pct3_b33_2001_period2

1.2.3.4 Countries sorted into quartiles of the Y-axis variable

1.2.3.4.1 Based on 1990-2015 values
dgcd_tot_ca_pct4_b25 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct4_25_to_50 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct4_50_to_75 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct4_a75 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_tot_ca_pct4_a75
dgcd_tot_ca_pct4_50_to_75
dgcd_tot_ca_pct4_25_to_50
dgcd_tot_ca_pct4_b25
dgcd_dom_ca_pct4_b25 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct4_25_to_50 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct4_50_to_75 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct4_a75 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_dom_ca_pct4_a75
dgcd_dom_ca_pct4_50_to_75
dgcd_dom_ca_pct4_25_to_50
dgcd_dom_ca_pct4_b25
dgcd_ext_ca_pct4_b25 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2015_xts[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct4_25_to_50 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2015_xts[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct4_50_to_75 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2015_xts[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct4_a75 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2015_xts[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcd_ext_ca_pct4_a75
dgcd_ext_ca_pct4_50_to_75
dgcd_ext_ca_pct4_25_to_50
dgcd_ext_ca_pct4_b25
1.2.3.4.2 Based on 1990-2001 ans 2002-2015 values

Period 1990 - 2001

dgcg_tot_ca_pct4_b25_2001_period1 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_ca_pct4_25_to_50_2001_period1 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_ca_pct4_50_to_75_2001_period1 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_ca_pct4_a75_2001_period1 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_ca_pct4_a75_2001_period1
dgcg_tot_ca_pct4_50_to_75_2001_period1
dgcg_tot_ca_pct4_25_to_50_2001_period1
dgcg_tot_ca_pct4_b25_2001_period1
dgcg_dom_ca_pct4_b25_2001_period1 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_ca_pct4_25_to_50_2001_period1 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_ca_pct4_50_to_75_2001_period1 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_ca_pct4_a75_2001_period1 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_ca_pct4_a75_2001_period1
dgcg_dom_ca_pct4_50_to_75_2001_period1
dgcg_dom_ca_pct4_25_to_50_2001_period1
dgcg_dom_ca_pct4_b25_2001_period1
dgcg_ext_ca_pct4_b25_2001_period1 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2001_2015_xts_1[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_ca_pct4_25_to_50_2001_period1 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2001_2015_xts_1[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_ca_pct4_50_to_75_2001_period1 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2001_2015_xts_1[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_ca_pct4_a75_2001_period1 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2001_2015_xts_1[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_ca_pct4_a75_2001_period1
dgcg_ext_ca_pct4_50_to_75_2001_period1
dgcg_ext_ca_pct4_25_to_50_2001_period1
dgcg_ext_ca_pct4_b25_2001_period1

Period: 2002-2015

dgcg_tot_ca_pct4_b25_2001_period2 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_ca_pct4_25_to_50_2001_period2 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_ca_pct4_50_to_75_2001_period2 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_ca_pct4_a75_2001_period2 <- dygraph(gobcen_deuda_tot_ca_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_tot_ca_pct4_a75_2001_period2
dgcg_tot_ca_pct4_50_to_75_2001_period2
dgcg_tot_ca_pct4_25_to_50_2001_period2
dgcg_tot_ca_pct4_b25_2001_period2
dgcg_dom_ca_pct4_b25_2001_period2 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_ca_pct4_25_to_50_2001_period2 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_ca_pct4_50_to_75_2001_period2 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_ca_pct4_a75_2001_period2 <- dygraph(gobcen_deuda_dom_ca_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_dom_ca_pct4_a75_2001_period2
dgcg_dom_ca_pct4_50_to_75_2001_period2
dgcg_dom_ca_pct4_25_to_50_2001_period2
dgcg_dom_ca_pct4_b25_2001_period2
dgcg_ext_ca_pct4_b25_2001_period2 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_ca_pct4_25_to_50_2001_period2 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_ca_pct4_50_to_75_2001_period2 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_ca_pct4_a75_2001_period2 <- dygraph(gobcen_deuda_ext_ca_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dgcg_ext_ca_pct4_a75_2001_period2
dgcg_ext_ca_pct4_50_to_75_2001_period2
dgcg_ext_ca_pct4_25_to_50_2001_period2
dgcg_ext_ca_pct4_b25_2001_period2

1.3 Cartera vencida

# load("./produced_data/data_with_basic_wrangling/monetary_fin_tidy")
# load("./produced_data/data_with_basic_wrangling/cs_gdp_currentlc_q_gasto")

load(paste0(pre_path, 
            "produced_data/data_with_basic_wrangling/monetary_fin_tidy"))

load(paste0(pre_path, 
            "produced_data/data_with_basic_wrangling/cs_gdp_currentlc_q_gasto"))

cv_december <- cartera_vencida_33_tidy %>% 
  filter(iso3c %in% coi) %>% 
  filter( month == 12 ) %>% 
    mutate(year = as.Date(as.character(year), format = "%Y"))


cv_3_6_9_12 <- cartera_vencida_33_tidy %>% 
  filter(iso3c %in% coi) %>% 
  filter( month  %in%  c(3,6,9,12))

cartera_vencida_qtr_with_gdp <- cs_gdp_currentlc_q_gasto %>%
  select(-c(iso3c, nombre_pais, Rubro)) %>%
  left_join( cartera_vencida_33_tidy ,  by = c("iso2c", "date")) %>%
  filter( !is.na(cartera_vencida_percent) ) %>% 
  filter(iso3c %in% coi)

mr_3_cv_anual <- cv_december %>%
  filter(!is.na(cartera_vencida_percent)) %>% 
  group_by(iso3c) %>% 
  arrange(year) %>% 
  summarise(year_mr = max(year),
            cv_mr = last(cartera_vencida_percent),
            avg_mr_3 = mean(tail(cartera_vencida_percent, 3) , rm.na = TRUE)
  ) %>% 
  arrange(desc(cv_mr)) %>% 
  mutate(rnk_mr_cv = min_rank(-cv_mr),
         rnk_mr_3_cv = min_rank(-avg_mr_3)) %>% 
  arrange(iso3c)

1.3.1 Latin America

cv_18 <- cv_december %>% 
  filter(iso3c %in% coi_18)

cv_18_xts <- cate_gen_to_xts_wide(cv_18 %>% select(iso3c, year, cartera_vencida_percent)    , value_col_name = "cartera_vencida_percent",  is_full = TRUE, is_med = FALSE)

cv_18_class_pct4_1990_2001_2015 <- cate_gen(cv_18 %>% 
    select(iso3c, year, cartera_vencida_percent), value_col_name = "cartera_vencida_percent", time_breaks = default_time_break, is_med = FALSE, is_pct3 = FALSE,
                                               is_pct4 = TRUE)

# cv_18_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(cv_18_class_pct4_1990_2001_2015[[1]] %>% filter(!is.na(gen_group)), 
#     value_col_name = "cartera_vencida_percent", is_med = FALSE, is_pct4 = TRUE)

cv_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(cv_18_class_pct4_1990_2001_2015[[2]], 
    value_col_name = "cartera_vencida_percent", is_med = FALSE, is_pct4 = TRUE)

1.3.1.1 All (18) countries

#using dygraph
dcv_title_18 <- "Cartera Vencida,  % de cartera (E18)"

dcv_18 <- dygraph(cv_18_xts, main = dcv_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dcv_18

1.3.1.2 Countries sorted into quartiles of the Y-axis variable

dcv_18_pct4_b25_2001_period2 <- dygraph(
  cv_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dcv_18_pct4_25_to_50_2001_period2 <- dygraph(cv_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dcv_18_pct4_50_to_75_2001_period2 <- dygraph(cv_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dcv_18_pct4_a75_2001_period2 <- dygraph(cv_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dcv_18_pct4_a75_2001_period2
dcv_18_pct4_50_to_75_2001_period2
dcv_18_pct4_25_to_50_2001_period2
dcv_18_pct4_b25_2001_period2

1.4 Crédito interno

credito_interno_qtr <- cs_gdp_currentlc_q_gasto %>% 
  select(-c(iso3c, nombre_pais, Rubro, year)) %>% 
  left_join( credito_interno_33_tidy ,  by = c("iso2c", "date")) %>% 
  filter( !is.na(total) ) %>% 
  mutate(total_to_gdp_seas = total/gdp_sa_seas,
         al_spub_gdp_seas = al.sector.publico/gdp_sa_seas,
         al_spriv_gdp_seas = al.sector.privado/gdp_sa_seas,
         al_gob_gdp_seas = gobierno/gdp_sa_seas,
         a_otros_gdp_seas = otros/gdp_sa_seas,
         total_to_gdp_stl = total/gdp_sa_stl,
         al_spub_gdp_stl = al.sector.publico/gdp_sa_stl,
         al_spriv_gdp_stl = al.sector.privado/gdp_sa_stl,
         al_gob_gdp_stl = gobierno/gdp_sa_stl,
         a_otros_gdp_stl = otros/gdp_sa_stl) %>% 
  dplyr::select(- ends_with("_stl")) %>% 
  select(- dplyr::contains("al.")) %>% 
  select(- c(otros, otros.1, gdp,  total, gobierno, month))


tab_ci_qrt_gdp <- credito_interno_qtr %>% 
  group_by(iso2c, year) %>%
  summarise(nobs = n(), 
            num_countries = length(unique(credito_interno_qtr$iso2c)),
            avg_spu_gdp = mean(al_spub_gdp_seas, rm.na = TRUE),
            avg_spr_gdp = mean(al_spriv_gdp_seas, rm.na = TRUE),
            avg_tot_gdp = mean(total_to_gdp_seas, rm.na = TRUE),
            avg_gob_gdp = mean(al_gob_gdp_seas, rm.na = TRUE),
            n_gob_gdp = sum(!is.na(al_gob_gdp_seas)),
            n_tot_gdp = sum(!is.na(total_to_gdp_seas)),
            n_spub_gdp = sum(!is.na(al_spub_gdp_seas)),
            n_spr_gdp = sum(!is.na(al_spriv_gdp_seas))
  )


mr_3_ci_to_gdp <- tab_ci_qrt_gdp %>% 
  group_by(iso2c) %>% 
  arrange(year) %>% 
  summarise(year_mr = max(year),
            ci_total_mr = last(avg_tot_gdp),
            avg_mr_ci_tot_3 = mean(tail(avg_tot_gdp, 3) , rm.na = TRUE),
            ci_spu_mr = last(avg_spu_gdp),
            avg_mr_ci_spu_3 = mean(tail(avg_spu_gdp, 3) , rm.na = TRUE),
            ci_spr_mr = last(avg_spr_gdp),
            avg_mr_ci_spr_3 = mean(tail(avg_spr_gdp, 3) , rm.na = TRUE)
  ) %>% 
  arrange(desc(ci_total_mr)) %>% 
  mutate(rnk_mr_tot = min_rank(-ci_total_mr),
         rnk_mr_3_tot = min_rank(-avg_mr_ci_tot_3)) %>% 
  arrange(iso2c)

1.4.1 Latin America

ci_time_break <- as.Date("2004-10-01", format = "%Y-%m-%d") # 4th quarter of 2004

ci_18 <- credito_interno_qtr %>% 
  filter(iso3c %in% coi_18) %>% 
  filter( iso3c != "COL") # remove colombia until fix in ci or gdp data (ratio is off by 3 orders of magnitude)

ci_tot_18_xts <- cate_gen_to_xts_wide(ci_18 %>% select(iso3c, date, total_to_gdp_seas), is_full = TRUE, is_med = FALSE, value_col_name = "total_to_gdp_seas", dating_col_name = "date")

ci_spu_18_xts <- cate_gen_to_xts_wide(ci_18 %>% select(iso3c, date, al_spub_gdp_seas), is_full = TRUE, is_med = FALSE, value_col_name = "al_spub_gdp_seas", dating_col_name = "date")

ci_spri_18_xts <- cate_gen_to_xts_wide(ci_18 %>% select(iso3c, date, al_spriv_gdp_seas), is_full = TRUE, is_med = FALSE, value_col_name = "al_spriv_gdp_seas", dating_col_name = "date")


ci_tot_18_class_pct4_1990_2015 <- cate_gen(
  df = ci_18 %>% select(iso3c, date, total_to_gdp_seas),
  value_col_name = "total_to_gdp_seas", is_med = FALSE,  is_pct4 = TRUE)

ci_spu_18_class_pct4_1990_2015 <- cate_gen(
  df = ci_18 %>% select(iso3c, date, al_spub_gdp_seas),
  value_col_name = "al_spub_gdp_seas", is_med = FALSE,  is_pct4 = TRUE)

ci_spri_18_class_pct4_1990_2015 <- cate_gen(
  df = ci_18 %>% select(iso3c, date, al_spriv_gdp_seas),
  value_col_name = "al_spriv_gdp_seas", is_med = FALSE,  is_pct4 = TRUE)



ci_tot_18_class_pct4_1990_2001_2015 <- cate_gen(
  df = ci_18 %>% select(iso3c, date, total_to_gdp_seas),
  value_col_name = "total_to_gdp_seas", time_breaks = ci_time_break, is_med = FALSE,
                 is_pct4 = TRUE, dating_col_name = "date")

ci_spu_18_class_pct4_1990_2001_2015 <- cate_gen(
  df = ci_18 %>% select(iso3c, date, al_spub_gdp_seas),
  value_col_name = "al_spub_gdp_seas", time_breaks = ci_time_break, is_med = FALSE,
                     is_pct4 = TRUE, dating_col_name = "date")

ci_spri_18_class_pct4_1990_2001_2015 <- cate_gen(
  df = ci_18 %>% select(iso3c, date, al_spriv_gdp_seas),
  value_col_name = "al_spriv_gdp_seas", time_breaks = ci_time_break, is_med = FALSE,
                    is_pct4 = TRUE, dating_col_name = "date")


ci_tot_18_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  ci_tot_18_class_pct4_1990_2015,
  value_col_name = "total_to_gdp_seas",
  dating_col_name = "date", is_med = FALSE, is_pct4 = TRUE)

ci_spu_18_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  ci_spu_18_class_pct4_1990_2015,
  value_col_name = "al_spub_gdp_seas", 
  dating_col_name = "date", is_med = FALSE, is_pct4 = TRUE)

ci_spri_18_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  ci_spri_18_class_pct4_1990_2015,
  value_col_name = "al_spriv_gdp_seas", dating_col_name = "date",
  is_med = FALSE, is_pct4 = TRUE)


ci_tot_18_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(
  ci_tot_18_class_pct4_1990_2001_2015[[1]], dating_col_name = "date",
  value_col_name = "total_to_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

ci_tot_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(
  ci_tot_18_class_pct4_1990_2001_2015[[2]], dating_col_name = "date",
                    value_col_name = "total_to_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

ci_spu_18_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(
  ci_spu_18_class_pct4_1990_2001_2015[[1]], dating_col_name = "date", 
                    value_col_name = "al_spub_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

ci_spu_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(
  ci_spu_18_class_pct4_1990_2001_2015[[2]],  dating_col_name = "date",
                    value_col_name = "al_spub_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

ci_spri_18_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(
  ci_spri_18_class_pct4_1990_2001_2015[[1]],  dating_col_name = "date",
                    value_col_name = "al_spriv_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

ci_spri_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(
  ci_spri_18_class_pct4_1990_2001_2015[[2]],  dating_col_name = "date", 
                    value_col_name = "al_spriv_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

1.4.1.1 All (18) countries

#using dygraph
ci_tot_title_18 <- "Domestic credit, as % of GDP"

ci_tot_18 <- dygraph(ci_tot_18_xts, main = ci_tot_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
ci_tot_18
ci_spu_title_18 <- "Domestic credit to public sector, as % of GDP"

ci_spu_18 <- dygraph(ci_spu_18_xts, main = ci_spu_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
ci_spu_18
ci_spri_title_18 <- "Domestic credit to private sector, as % of GDP"

ci_spri_18 <- dygraph(ci_spri_18_xts, main = ci_spri_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
ci_spri_18

1.4.1.2 Countries sorted into quartiles of the Y-axis variable

dci_tot_18_pct4_b25_2001_period2 <- dygraph(ci_tot_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_tot_18_pct4_25_to_50_2001_period2 <- dygraph(ci_tot_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_tot_18_pct4_50_to_75_2001_period2 <- dygraph(ci_tot_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_tot_18_pct4_a75_2001_period2 <- dygraph(ci_tot_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_tot_18_pct4_a75_2001_period2
dci_tot_18_pct4_50_to_75_2001_period2
dci_tot_18_pct4_25_to_50_2001_period2
dci_tot_18_pct4_b25_2001_period2
dci_spu_18_pct4_b25_2001_period2 <- dygraph(ci_spu_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_spu_18_pct4_25_to_50_2001_period2 <- dygraph(ci_spu_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_spu_18_pct4_50_to_75_2001_period2 <- dygraph(ci_spu_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_spu_18_pct4_a75_2001_period2 <- dygraph(ci_spu_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_spu_18_pct4_a75_2001_period2
dci_spu_18_pct4_50_to_75_2001_period2
dci_spu_18_pct4_25_to_50_2001_period2
dci_spu_18_pct4_b25_2001_period2
dci_spri_18_pct4_b25_2001_period2 <- dygraph(ci_spri_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_spri_18_pct4_25_to_50_2001_period2 <- dygraph(ci_spri_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_spri_18_pct4_50_to_75_2001_period2 <- dygraph(ci_spri_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_spri_18_pct4_a75_2001_period2 <- dygraph(ci_spri_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dci_spri_18_pct4_a75_2001_period2
dci_spri_18_pct4_50_to_75_2001_period2
dci_spri_18_pct4_25_to_50_2001_period2
dci_spri_18_pct4_b25_2001_period2

1.5 Préstamos Bancarios

prestamos_bancarios_qtr <- cs_gdp_currentlc_q_gasto %>% 
    select(-c(iso3c, nombre_pais, Rubro, year)) %>% 
    left_join(prestamos_bancarios_33_tidy,  by = c("iso2c", "date")) %>% 
    filter( !is.na(total) ) %>% 
    mutate(total_to_gdp_seas = total/gdp_sa_seas,
           consumo_gdp_seas = consumo/gdp_sa_seas,
           hipotecario_gdp_seas = hipotecario/gdp_sa_seas,
           industrial_gdp_seas = industrial/gdp_sa_seas,
           comercial_gdp_seas = comercial/gdp_sa_seas)



tab_pb_qtr <- prestamos_bancarios_qtr %>% group_by(iso2c, year) %>% 
  group_by(iso2c, year) %>%
  filter(iso2c != "CO") %>% 
  summarise(nobs = n(), 
            num_countries = length(unique(prestamos_bancarios_qtr$iso2c)),
            avg_tot_to_gdp = mean(total_to_gdp_seas),
            avg_hip_to_gdp = mean(hipotecario_gdp_seas, rm.na=TRUE),
            avg_con_to_gdp = mean(consumo_gdp_seas, rm.na=TRUE),
            n_hip_to_gdp = sum(!is.na(hipotecario_gdp_seas)),
            n_con_to_gdp = sum(!is.na(consumo_gdp_seas))
  )

1.5.1 Latin America

pb_time_break <- as.Date("2004-10-01", format = "%Y-%m-%d") # 4th quarter of 2004

pb_18 <- prestamos_bancarios_qtr %>% 
  filter(iso3c %in% coi_18) %>% 
  filter( iso3c != "COL") # remove colombia until fix in ci or gdp data (ratio is off by 3 orders of magnitude)

pb_tot_18_xts <- cate_gen_to_xts_wide(pb_18 %>% select(iso3c, date, total_to_gdp_seas), is_full = TRUE, is_med = FALSE, value_col_name = "total_to_gdp_seas", dating_col_name = "date")

pb_hip_18_xts <- cate_gen_to_xts_wide(pb_18 %>% select(iso3c, date, hipotecario_gdp_seas), is_full = TRUE, is_med = FALSE, value_col_name = "hipotecario_gdp_seas", dating_col_name = "date")

pb_con_18_xts <- cate_gen_to_xts_wide(pb_18 %>% select(iso3c, date, consumo_gdp_seas), is_full = TRUE, is_med = FALSE, value_col_name = "consumo_gdp_seas", dating_col_name = "date")


pb_tot_18_class_pct4_1990_2015 <- cate_gen(
  df = pb_18 %>% select(iso3c, date, total_to_gdp_seas),
  value_col_name = "total_to_gdp_seas", is_med = FALSE,  is_pct4 = TRUE)

pb_hip_18_class_pct4_1990_2015 <- cate_gen(
  df = pb_18 %>% select(iso3c, date, hipotecario_gdp_seas),
  value_col_name = "hipotecario_gdp_seas", is_med = FALSE,  is_pct4 = TRUE)

pb_con_18_class_pct4_1990_2015 <- cate_gen(
  df = pb_18 %>% select(iso3c, date, consumo_gdp_seas),
  value_col_name = "consumo_gdp_seas", is_med = FALSE,  is_pct4 = TRUE)



pb_tot_18_class_pct4_1990_2001_2015 <- cate_gen(
  df = pb_18 %>% select(iso3c, date, total_to_gdp_seas),
  value_col_name = "total_to_gdp_seas", time_breaks = pb_time_break, is_med = FALSE,
                 is_pct4 = TRUE, dating_col_name = "date")

pb_hip_18_class_pct4_1990_2001_2015 <- cate_gen(
  df = pb_18 %>% select(iso3c, date, hipotecario_gdp_seas),
  value_col_name = "hipotecario_gdp_seas", time_breaks = pb_time_break, is_med = FALSE,
                     is_pct4 = TRUE, dating_col_name = "date")

pb_con_18_class_pct4_1990_2001_2015 <- cate_gen(
  df = pb_18 %>% select(iso3c, date, consumo_gdp_seas),
  value_col_name = "consumo_gdp_seas", time_breaks = pb_time_break, is_med = FALSE,
                    is_pct4 = TRUE, dating_col_name = "date")


pb_tot_18_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  pb_tot_18_class_pct4_1990_2015,
  value_col_name = "total_to_gdp_seas",
  dating_col_name = "date", is_med = FALSE, is_pct4 = TRUE)

pb_hip_18_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  pb_hip_18_class_pct4_1990_2015,
  value_col_name = "hipotecario_gdp_seas", 
  dating_col_name = "date", is_med = FALSE, is_pct4 = TRUE)

pb_con_18_pct4_1990_2015_xts <- cate_gen_to_xts_wide(
  pb_con_18_class_pct4_1990_2015,
  value_col_name = "consumo_gdp_seas", dating_col_name = "date",
  is_med = FALSE, is_pct4 = TRUE)


pb_tot_18_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(
  pb_tot_18_class_pct4_1990_2001_2015[[1]], dating_col_name = "date",
  value_col_name = "total_to_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

pb_tot_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(
  pb_tot_18_class_pct4_1990_2001_2015[[2]], dating_col_name = "date",
                    value_col_name = "total_to_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

pb_hip_18_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(
  pb_hip_18_class_pct4_1990_2001_2015[[1]], dating_col_name = "date", 
                    value_col_name = "hipotecario_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

pb_hip_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(
  pb_hip_18_class_pct4_1990_2001_2015[[2]],  dating_col_name = "date",
                    value_col_name = "hipotecario_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

pb_con_18_pct4_1990_2001_2015_xts_1 <- cate_gen_to_xts_wide(
  pb_con_18_class_pct4_1990_2001_2015[[1]],  dating_col_name = "date",
                    value_col_name = "consumo_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

pb_con_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(
  pb_con_18_class_pct4_1990_2001_2015[[2]],  dating_col_name = "date", 
                    value_col_name = "consumo_gdp_seas",is_med = FALSE, is_pct4 = TRUE)

1.5.1.1 All (18) countries together

#using dygraph
pb_tot_title_18 <- "Domestic credit, as % of GDP"

pb_tot_18 <- dygraph(pb_tot_18_xts, main = pb_tot_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
pb_tot_18
pb_hip_title_18 <- "Domestic credit to public sector, as % of GDP"

pb_hip_18 <- dygraph(pb_hip_18_xts, main = pb_hip_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
pb_hip_18
pb_con_title_18 <- "Domestic credit to private sector, as % of GDP"

pb_con_18 <- dygraph(pb_con_18_xts, main = pb_con_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
pb_con_18

1.5.1.2 Countries sorted into quartiles of the Y-axis variable

dpb_tot_18_pct4_b25_2001_period2 <- dygraph(pb_tot_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_tot_18_pct4_25_to_50_2001_period2 <- dygraph(pb_tot_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_tot_18_pct4_50_to_75_2001_period2 <- dygraph(pb_tot_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_tot_18_pct4_a75_2001_period2 <- dygraph(pb_tot_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_tot_18_pct4_a75_2001_period2
dpb_tot_18_pct4_50_to_75_2001_period2
dpb_tot_18_pct4_25_to_50_2001_period2
dpb_tot_18_pct4_b25_2001_period2
dpb_hip_18_pct4_b25_2001_period2 <- dygraph(pb_hip_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_hip_18_pct4_25_to_50_2001_period2 <- dygraph(pb_hip_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_hip_18_pct4_50_to_75_2001_period2 <- dygraph(pb_hip_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_hip_18_pct4_a75_2001_period2 <- dygraph(pb_hip_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_hip_18_pct4_a75_2001_period2
dpb_hip_18_pct4_50_to_75_2001_period2
dpb_hip_18_pct4_25_to_50_2001_period2
dpb_hip_18_pct4_b25_2001_period2
dpb_con_18_pct4_b25_2001_period2 <- dygraph(pb_con_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_con_18_pct4_25_to_50_2001_period2 <- dygraph(pb_con_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_con_18_pct4_50_to_75_2001_period2 <- dygraph(pb_con_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_con_18_pct4_a75_2001_period2 <- dygraph(pb_con_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dpb_con_18_pct4_a75_2001_period2
dpb_con_18_pct4_50_to_75_2001_period2
dpb_con_18_pct4_25_to_50_2001_period2
dpb_con_18_pct4_b25_2001_period2

2 Exportaciones

2.1 Concentracion de exportaciones

Índice de Herfindahl, concentración al interior de los 10 principales productos de cada país, en cada año.

# load("./produced_data/data_with_basic_wrangling/cs_x_m_10_ppales")
load(paste0(pre_path, 
            "produced_data/data_with_basic_wrangling/cs_x_m_10_ppales"))


exp_10_ppales <- cs_x_m_10_ppales %>%
  unite(productos_principales,  starts_with("Produ")) %>% 
  mutate(productos_principales = str_replace_all(productos_principales,
                                                 "_", "") %>% 
           str_replace_all("n/a", "") %>% 
           str_replace_all("NA", ""))
  
exp_10_ppales_by_iso <- exp_10_ppales %>% 
  mutate(year_as_date = as.Date(as.character(year), format = "%Y")) %>% 
  group_by(iso3c, year_as_date) %>% 
  summarise(concentracion = conc(valor, type = "Herfindahl")) %>% 
  arrange(iso3c, year_as_date)  

2.1.1 Latin America

hx_18 <- exp_10_ppales_by_iso%>% 
  filter(iso3c %in% coi_18)

hx_18_xts <- cate_gen_to_xts_wide(hx_18 %>% select(iso3c, year_as_date, concentracion),
                                  value_col_name = "concentracion",
                                  is_full = TRUE, is_med = FALSE,
                                  dating_col_name = "year_as_date")

hx_18_class_pct4_1990_2001_2015 <- cate_gen(hx_18 %>% 
    select(iso3c, year_as_date, concentracion), value_col_name = "concentracion", time_breaks = default_time_break, is_med = FALSE, is_pct3 = FALSE, is_pct4 = TRUE, dating_col_name = "year_as_date")

hx_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(hx_18_class_pct4_1990_2001_2015[[2]], 
    value_col_name = "concentracion", is_med = FALSE, is_pct4 = TRUE, dating_col_name = "year_as_date")

2.1.1.1 All (18) countries

#using dygraph
dhx_title_18 <- "Export concentration, Herfindahl index (E18)"

dhx_18 <- dygraph(hx_18_xts, main = dhx_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dhx_18

2.1.1.2 Countries sorted into quartiles of the Y-axis variable

dhx_18_pct4_b25_2001_period2 <- dygraph(
  hx_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dhx_18_pct4_25_to_50_2001_period2 <- dygraph(hx_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dhx_18_pct4_50_to_75_2001_period2 <- dygraph(hx_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dhx_18_pct4_a75_2001_period2 <- dygraph(hx_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dhx_18_pct4_a75_2001_period2
dhx_18_pct4_50_to_75_2001_period2
dhx_18_pct4_25_to_50_2001_period2
dhx_18_pct4_b25_2001_period2

2.2 Shares en exportaciones de 3 categorías a priori

Datos mensuales, promediados por año

# load("./produced_data/data_with_basic_wrangling/x_m_locations_products_tidy")
load(paste0(pre_path, 
            "produced_data/data_with_basic_wrangling/x_m_locations_products_tidy"))

imp_by_prod_to_join <- imp_by_prod_tidy %>% 
   filter(iso3c %in% coi) %>% 
  rename(producto_m = producto) 

exp_by_prod_to_join <- exp_by_prod_tidy %>% 
   filter(iso3c %in% coi) %>% 
  rename(producto_x = producto) %>% 
  mutate(producto_x = stri_trans_general(producto_x, "Latin-ASCII") %>% 
           str_to_lower() %>% str_replace_all(" ", "_"))

exp_by_prod_wide <- exp_by_prod_to_join %>% 
   filter(iso3c %in% coi) %>% 
  spread(producto_x, value) %>% 
  mutate(agro_agropec_share = productos_agricolas_y_agropecuarios/total,
         mineria_y_petro_share = mineria_y_petroleo/total,
         manufacturas_share = manufacturas/total) %>% 
  select(-c(productos_agricolas_y_agropecuarios,mineria_y_petroleo,
            manufacturas))

exp_by_prod_wide_anual <- exp_by_prod_wide %>% 
  mutate(year = year(date)) %>% 
  group_by(iso3c, year) %>% 
  mutate(avg_share_agr = mean(agro_agropec_share, rm.na = TRUE),
         avg_share_min_pet = mean(mineria_y_petro_share, rm.na = TRUE),
         avg_share_manuf = mean(manufacturas_share, rm.na = TRUE)) %>% 
  arrange(year, desc(avg_share_min_pet))


exp_by_prod_shares <- exp_by_prod_wide_anual %>% 
  rename(agro_pec=agro_agropec_share, mineria_y_petro=mineria_y_petro_share,
         manuf=manufacturas_share) %>% 
  gather(key = product, value = share, agro_pec, mineria_y_petro, manuf ) %>% 
  select(-c(avg_share_agr, avg_share_min_pet, avg_share_manuf, total)) %>% 
  arrange(iso3c, year, product)

exp_by_prod_shares_anual_avg <- exp_by_prod_shares %>% 
  mutate(year_as_date = as.Date(as.character(year), format = "%Y")) %>% 
  group_by(iso3c, year_as_date, product) %>% 
  summarise(share_anual = mean(share, rm.na=TRUE)) 

exp_agro_pec_shares_anual_avg <- exp_by_prod_shares_anual_avg %>% 
  filter(product == "agro_pec")


exp_manuf_shares_anual_avg <- exp_by_prod_shares_anual_avg %>% 
  filter(product == "manuf")

exp_mineria_y_petro_pec_shares_anual_avg <- exp_by_prod_shares_anual_avg %>% 
  filter(product == "mineria_y_petro")

2.2.1 Latin America

minsh_18 <- exp_mineria_y_petro_pec_shares_anual_avg  %>% 
  filter(iso3c %in% coi_18)

# minsh_18_xts <- cate_gen_to_xts_wide(minsh_18 %>% select(iso3c, year_as_date, share_anual),
#                                   value_col_name = "share_anual",
#                                   is_full = TRUE, is_med = FALSE,
#                                   dating_col_name = "year_as_date")

minsh_18_sp <- minsh_18 %>% select(iso3c, year_as_date, share_anual) %>% spread(key = iso3c, value = share_anual)
minsh_18_xts <- minsh_18_sp %>% select(- year_as_date) %>% xts(order.by = minsh_18_sp$year_as_date) 
## Adding missing grouping variables: `year_as_date`
minsh_18_xts$year_as_date <- NULL



minsh_18_class_pct4_1990_2001_2015 <- cate_gen(minsh_18 %>% 
    select(iso3c, year_as_date, share_anual), value_col_name = "share_anual",
    time_breaks = default_time_break, is_med = FALSE,  is_pct4 = TRUE, dating_col_name = "year_as_date")

minsh_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(minsh_18_class_pct4_1990_2001_2015[[2]], 
    value_col_name = "share_anual", is_med = FALSE, is_pct4 = TRUE, dating_col_name = "year_as_date")


manush_18 <- exp_manuf_shares_anual_avg  %>% 
  filter(iso3c %in% coi_18)

manush_18_sp <- manush_18 %>% select(iso3c, year_as_date, share_anual) %>% spread(key = iso3c, value = share_anual)
manush_18_xts <- manush_18_sp %>% select(- year_as_date) %>% xts(order.by = minsh_18_sp$year_as_date) 
## Adding missing grouping variables: `year_as_date`
manush_18_xts$year_as_date <- NULL

# 
# manush_18_xts <- cate_gen_to_xts_wide(manush_18 %>% select(iso3c, year_as_date, share_anual),
#                                   value_col_name = "share_anual",
#                                   is_full = TRUE, is_med = FALSE,
#                                   dating_col_name = "year_as_date")

manush_18_class_pct4_1990_2001_2015 <- cate_gen(manush_18 %>% 
    select(iso3c, year_as_date, share_anual), value_col_name = "share_anual",
    time_breaks = default_time_break, is_med = FALSE,  is_pct4 = TRUE, dating_col_name = "year_as_date")

manush_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(manush_18_class_pct4_1990_2001_2015[[2]], 
    value_col_name = "share_anual", is_med = FALSE, is_pct4 = TRUE, dating_col_name = "year_as_date")


agropec_18 <- exp_agro_pec_shares_anual_avg %>% 
  filter(iso3c %in% coi_18)

agropec_18_xts <- cate_gen_to_xts_wide(agropec_18 %>% select(iso3c, year_as_date, share_anual),
                                  value_col_name = "share_anual",
                                  is_full = TRUE, is_med = FALSE,
                                  dating_col_name = "year_as_date")
## Adding missing grouping variables: `year_as_date`
agropec_18_class_pct4_1990_2001_2015 <- cate_gen(agropec_18 %>% 
    select(iso3c, year_as_date, share_anual), value_col_name = "share_anual",
    time_breaks = default_time_break, is_med = FALSE,  is_pct4 = TRUE, dating_col_name = "year_as_date")

agropec_18_pct4_1990_2001_2015_xts_2 <- cate_gen_to_xts_wide(agropec_18_class_pct4_1990_2001_2015[[2]], 
    value_col_name = "share_anual", is_med = FALSE, is_pct4 = TRUE, dating_col_name = "year_as_date")

2.2.1.1 All (18) countries

dminsh_title_18 <- "Export shares, mining and oil (E18)"

dminsh_18 <- dygraph(minsh_18_xts, main = dminsh_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dminsh_18
dagropec_title_18 <- "Export shares, agro and agropec (E18)"

dagropec_18 <- dygraph(agropec_18_xts, main = dagropec_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dagropec_18
dmanush_title_18 <- "Export shares, manufactures (E18)"

dmanush_18 <- dygraph(manush_18_xts, main = dmanush_title_18) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)
dmanush_18

2.2.1.2 Countries sorted into quartiles of the Y-axis variable

dmanush_18_pct4_b25_2001_period2 <- dygraph(
  manush_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dmanush_18_pct4_25_to_50_2001_period2 <- dygraph(manush_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dmanush_18_pct4_50_to_75_2001_period2 <- dygraph(manush_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dmanush_18_pct4_a75_2001_period2 <- dygraph(manush_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dmanush_18_pct4_a75_2001_period2
dmanush_18_pct4_50_to_75_2001_period2
dmanush_18_pct4_25_to_50_2001_period2
dmanush_18_pct4_b25_2001_period2
dagropec_18_pct4_b25_2001_period2 <- dygraph(
  agropec_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dagropec_18_pct4_25_to_50_2001_period2 <- dygraph(agropec_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dagropec_18_pct4_50_to_75_2001_period2 <- dygraph(agropec_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dagropec_18_pct4_a75_2001_period2 <- dygraph(agropec_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dagropec_18_pct4_a75_2001_period2
dagropec_18_pct4_50_to_75_2001_period2
dagropec_18_pct4_25_to_50_2001_period2
dagropec_18_pct4_b25_2001_period2
dminsh_18_pct4_b25_2001_period2 <- dygraph(
  minsh_18_pct4_1990_2001_2015_xts_2[["below_p25"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dminsh_18_pct4_25_to_50_2001_period2 <- dygraph(minsh_18_pct4_1990_2001_2015_xts_2[["p25_to_50"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dminsh_18_pct4_50_to_75_2001_period2 <- dygraph(minsh_18_pct4_1990_2001_2015_xts_2[["p50_to_75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dminsh_18_pct4_a75_2001_period2 <- dygraph(minsh_18_pct4_1990_2001_2015_xts_2[["above_p75"]]) %>%
  dyRangeSelector() %>%
dyHighlight(highlightCircleSize = 4,
            highlightSeriesBackgroundAlpha = 0.3,
            hideOnMouseOut = FALSE)

dminsh_18_pct4_a75_2001_period2
dminsh_18_pct4_50_to_75_2001_period2
dminsh_18_pct4_25_to_50_2001_period2
dminsh_18_pct4_b25_2001_period2