library(tidyverse) 
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## 
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## 
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## 
## Attaching package: 'xts'
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## 
## 
## Attaching package: 'PerformanceAnalytics'
## 
## The following object is masked from 'package:graphics':
## 
##     legend
## 
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(lubridate)
library(dplyr)
library(readxl)
library(plotly)
## Warning: package 'plotly' was built under R version 4.2.2
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(esquisse)
library(here)
## here() starts at C:/Users/james/OneDrive/Documents/R Projects/Final_Project
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(ggthemes)
library(ggrepel)
library(gt)
library(countrycode)
library(wbstats)
library(ggridges)
library(here)
library(countrycode)
library(viridis)
## Warning: package 'viridis' was built under R version 4.2.2
## Loading required package: viridisLite
library(hrbrthemes)
## Warning: package 'hrbrthemes' was built under R version 4.2.2
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library(plotly)
library(RColorBrewer)
character_num_to_numeric <- function(character_num) {
  character_num %>%
  # take out the commas
  str_remove_all(pattern = ",") %>%
  # take out any blank spaces before or after the number
  str_trim() %>%
  # coerce to numeric
  as.numeric()
}
gain_IDS <- here("001_data_processed", 
                    "gain_IDS.csv") %>% 
  read_csv()
## Warning: One or more parsing issues, see `problems()` for details
## Rows: 104580 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): ISO3, Name, region, Income.group, Series.Name
## dbl (2): Year, Value
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
IDS_clean <- here("001_data_processed", 
                    "IDS_clean.csv") %>% 
  read_csv()
## Warning: One or more parsing issues, see `problems()` for details
## Rows: 87193 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Name, ISO3, region, Income.group, Series.Name
## dbl (2): Year, Value
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
basic_stats <- function(income_group, series_name){

gain_IDS %>% 
  
  filter(Income.group == income_group) %>%
  filter(Series.Name == series_name) %>%
  mutate(Value = as.numeric(as.character(Value))) %>%

      mutate(Value_z_score = (Value-mean(Value))/sd(Value),
         Value_rank = rank(Value),
         Value_percentile = percent_rank(Value),
         Value_ntile_5 = ntile(Value, 5))

}

gain_stats_lowincome <- basic_stats("Low income", "Gain Score")

gain_stats_lowmidincome <- basic_stats("Lower middle income", "Gain Score")
gain_IDS %>%
    pivot_wider(names_from = Series.Name, values_from = Value) %>%
  filter(Year == 2020, Income.group == c("Low income", "Lower middle income")) %>%
  mutate_if(is.character, ~replace(., is.na(.), 0)) %>%
   mutate(across(.fns = as.numeric)) %>%

 ggplot(aes(x=`Gain Score`, y=(`Total Debt`/100))) + 
  geom_point( color="#69b3a2") +
  theme_clean()
## Warning in Income.group == c("Low income", "Lower middle income"): longer object
## length is not a multiple of shorter object length
## Warning in mask$eval_all_mutate(quo): NAs introduced by coercion

## Warning in mask$eval_all_mutate(quo): NAs introduced by coercion

## Warning in mask$eval_all_mutate(quo): NAs introduced by coercion

## Warning in mask$eval_all_mutate(quo): NAs introduced by coercion

An intersting thing to note is the massive amount of variable rate external debt exposure owned by low and lower middle income countries in Asia.

gain_IDS %>%
  filter(region %in% c("East Asia & Pacific","Middle East & North Africa","Sub-Saharan Africa","Latin America & Caribbean","Europe & Central Asia","South Asia")) %>%
  filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name == "External debt stocks, variable rate (DOD, current US$)") %>%
 filter(Year >= 2010) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.integer(as.character(`Year`))) %>%
  #as.integer("Year") 
  #na.omit() %>%
      

  ggplot(aes(x=`Year`, y=Value/1000000000, fill=region, text=region)) +
  geom_col() +
  scale_fill_viridis(discrete = TRUE) +
  ggtitle("Variable Rate External Debt Stocks by Region ($bns)") +
  labs(x = "Year", 
       y = "Total ($Bns)",
       subtitle = "The dollar value of external debt with a variable interest rate") +
  scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(100,1500, by = 100)) +
  theme_clean() 

And this trend continues for external debt as a whole in Asia. Let’s take a look at South Asia.

gain_IDS %>%
  filter(region %in% c("East Asia & Pacific","Middle East & North Africa","Sub-Saharan Africa","Latin America & Caribbean","Europe & Central Asia","South Asia")) %>%
  filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name== "External debt stocks, total (DOD, current US$)") %>%
 filter(Year >= "2010") %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
  na.omit() %>%


  ggplot(aes(x=Year, y=Value/1000000000, fill=region, text=region)) +
  geom_col() +
  scale_fill_viridis(discrete = TRUE) +
  ggtitle("External Debt Stocks by Region ($bns)") +
  labs(x = "Year", 
       y = "Total ($Bns)",
       subtitle = "The dollar value of external debt for low and lower-middle income countries, 2010-2020") +
  scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(100,3000, by = 500)) +
  theme_clean() 

It seems that Indonesia and India share a large portion of external debt exposure in their respective regions.

gain_IDS %>%
  filter(region %in% c("South Asia", "East Asia & Pacific")) %>%
  filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name== "External debt stocks, total (DOD, current US$)") %>%
 filter(Year == "2020") %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
  na.omit() %>%


  ggplot(aes(x=ISO3, y=Value/1000000000)) +
  geom_col(fill = "#0099f9") +
  #scale_fill_viridis(discrete = TRUE) +
  ggtitle("External Debt Stocks in Asia ($bns)") +
  labs(x = "Year", 
       y = "Total ($Bns)",
       subtitle = "The dollar value of external debt") +
  #scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(100,550, by = 100)) +
  theme_clean()

Let’s see how much they’re paying on this external debt. It looks like about 14% of the total for India and 16% of the total for Indonesia.

gain_IDS %>%
  filter(Name %in% c("India","Indonesia")) %>%
  filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name %in% c("External debt stocks, total (DOD, current US$)", "Debt service on external debt, total (TDS, current US$)")) %>%
 filter(Year == 2020) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
  na.omit() %>%


  ggplot(aes(x=Name, y=Value/1000000000, fill = Series.Name)) +
  geom_col(position = position_dodge()) +
  geom_text(aes(label=Value/1000000000,), position = position_dodge(width = 0.9),vjust=-0.25) +
  #scale_fill_viridis(discrete = TRUE) +
  ggtitle("External Debt Stocks in Asia ($bns)") +
  labs(x = "Country", 
       y = "Total ($Bns)",
       subtitle = "The dollar value of external debt") +
  #scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(100,10000, by = 1000)) +
  scale_fill_discrete(name = "Indicator", labels = c("Debt Service","External Debt")) +
  
  theme_clean()

How does their overall debt look as a percentage of their GDP?

IDS_clean %>%
  filter(Name %in% c("India","Indonesia","Philippines","Pakistan")) %>%
  #filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name == "General government gross debt_GGXWDG_NGDP") %>%
 filter(Year >= 2010) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
  na.omit() %>%


  ggplot(aes(x=Year, y=Value, group=Name)) +
  geom_line(aes(color = Name),size = 2 ) +
  #geom_text(aes(label=Value/100000000,), position = position_dodge(width = 0.9),vjust=-0.25) +
  #scale_fill_viridis(discrete = TRUE) +
  ggtitle("Government Debt as Percent of GDP") +
  labs(x = "Year", 
       y = "Percent of GDP",
       fill = "Country") +
  scale_x_continuous(breaks = seq(2010,2022, by = 2)) +
  #scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  #scale_y_continuous(breaks = seq(100,10000, by = 1000)) +
  #scale_fill_discrete(name = "Indicator", labels = c("Debt Service","External Debt")) +
  
  theme_clean()

Looking at total debt by region, we see that South Asia has a huge proportion, most certainly influenced by India’s debt levels compared to others in the region

gain_IDS %>%
  filter(region %in% c("East Asia & Pacific","Middle East & North Africa","Sub-Saharan Africa","Latin America & Caribbean","Europe & Central Asia","South Asia")) %>%
  filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name== "Total Debt") %>%
 filter(Year >= "2010") %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
    mutate(Value = Value/1000000000) %>%
  na.omit() %>%


  ggplot(aes(x=Year, y=Value, fill=region, text=region)) +
  geom_col() +
   scale_fill_discrete(name = "Region") +
  scale_fill_viridis(discrete = TRUE) +
  ggtitle("Total Debt by Region ($Bn)") +
  labs(x = "Year", 
       y = "Total ($Bns)",
       subtitle = "Lower and lower-middle income countries, 2010-2020") +
  scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(1000,100000000, by = 1000)) +

  theme_clean()
## Scale for 'fill' is already present. Adding another scale for 'fill', which
## will replace the existing scale.

As we see, excluding India significantly drops this proportion

gain_IDS %>%
  filter(region %in% c("East Asia & Pacific","Middle East & North Africa","Sub-Saharan Africa","Latin America & Caribbean","Europe & Central Asia","South Asia")) %>%
  filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name== "Total Debt") %>%
  filter(Year >= "2010") %>%
  filter_all(all_vars(.!= "India")) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
    mutate(Value = Value/1000000000) %>%

  na.omit() %>%


  ggplot(aes(x=Year, y=Value, fill=region, text=region)) +
  geom_col() +
   scale_fill_discrete(name = "Region") +
  scale_fill_viridis(discrete = TRUE) +
  ggtitle("Total Debt by Region ($Bn)") +
  labs(x = "Year", 
       y = "Total ($Bns)",
       subtitle = "Lower and lower-middle income countries excluding India, 2010-2020") +
  scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(100,100000, by = 1000)) +

  theme_clean()
## Scale for 'fill' is already present. Adding another scale for 'fill', which
## will replace the existing scale.

We can now see that proportionally, Indonesia now dominates

gain_IDS %>%
  filter(region %in% c("South Asia", "East Asia & Pacific")) %>%
  filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name== "Total Debt") %>%
 filter(Year == "2020") %>%
  filter_all(all_vars(.!= "India")) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
      mutate(Value = Value/1000000000) %>%

  na.omit() %>%


  ggplot(aes(x=ISO3, y=Value)) +
  geom_col(fill = "#0099f9") +
  #scale_fill_viridis(discrete = TRUE) +
  ggtitle("Total Debt in Asia ($bns)") +
  labs(x = "Year", 
       y = "Total ($Bns)",
       subtitle = "Lower and lower-middle income countries excluding India, 2010-2020") +
  #scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(50,700, by = 50)) +
  theme_clean()

Looking at external debt stock vs total, we see that almost the entire stock of Indonesia’s debt is external facing

gain_IDS %>%
  filter(Name %in% c("India","Indonesia")) %>%
  filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name %in% c("External debt stocks, total (DOD, current US$)", "Total Debt")) %>%
 filter(Year == 2020) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
  na.omit() %>%


  ggplot(aes(x=Name, y=(Value/1000000000), fill = Series.Name)) +
  geom_col(position = position_dodge()) +
  geom_text(aes(label=Value/1000000000), position = position_dodge(width = 0.9),vjust=-0.25) +
  #scale_fill_viridis(discrete = TRUE) +
  ggtitle("External Debt Stocks in Asia ($bns)") +
  labs(x = "Country", 
       y = "Total ($Bns)",
       subtitle = "The dollar value of debt, 2020") +
  #scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(100,10000, by = 1000)) +
  scale_fill_discrete(name = "Indicator", labels = c("External Debt","Total Debt")) +
  
  theme_clean()

Let’s turn our attention to Africa. We’re excluding Iran for these purposes and looking at countries with debt greater than $50 billion

gain_IDS %>%
  filter(region %in% c("Middle East & North Africa", "Sub-Saharan Africa")) %>%
  filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name== "Total Debt") %>%
 filter(Year == "2020") %>%
  filter_all(all_vars(.!= "Islamic Republic of Iran")) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
  na.omit() %>%
  mutate(Value = Value/1000000000) %>%
    filter(`Value` > 50) %>%



  ggplot(aes(x=Name, y=Value)) +
  geom_col(fill = "#0099f9") +
  #scale_fill_viridis(discrete = TRUE) +
  ggtitle("Total Debt among select African Countries") +
  labs(x = "Country", 
       y = "Total ($Bns)",
       subtitle = "Lower and lower-middle income countries, 2020") +
  #scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(100,700, by = 100)) +
  theme_clean()

Looking at debt vs GDP

gain_IDS %>%
  filter(region %in% c("Middle East & North Africa", "Sub-Saharan Africa")) %>%
  filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name %in% c("Gross domestic product, current prices_NGDPD", "Total Debt")) %>%
 filter(Year == "2020") %>%
  filter_all(all_vars(.!= "Islamic Republic of Iran")) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
    mutate(Value = Value/1000000000) %>%

  na.omit() %>%
  #mutate(Value = Value/10000000) %>%
    filter(`Value` > 50) %>%


  ggplot(aes(x=Name, y=Value, fill = Series.Name)) +
  geom_col(position = position_dodge()) +
  #geom_text(aes(label=Value), position = position_dodge(width = 0.9),vjust=-0.25) +
  #scale_fill_viridis(discrete = TRUE) +
  ggtitle("GDP vs Total Debt ($bns)") +
  labs(x = "Country", 
       y = "Total ($Bns)",
       subtitle = "Select African Countries, 2020") +
  #scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(100,1000, by = 100)) +
  scale_fill_discrete(name = "Indicator", labels = c("GDP","Total Debt")) +
  
  theme_clean()

Let’s look at debt as percent of GDP. We’re going to focus on those with a debt higher than their GDP

IDS_clean %>%
  filter(Name %in% c("Angola","Sudan","Nigeria","Algeria","Morocco")) %>%
  #filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name == "General government gross debt_GGXWDG_NGDP") %>%
 filter(Year >= 2010) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
  na.omit() %>%


  ggplot(aes(x=Year, y=Value, group=Name)) +
  geom_line(aes(color = Name),size = 2 ) +
  #geom_text(aes(label=Value/100000000,), position = position_dodge(width = 0.9),vjust=-0.25) +
  #scale_fill_viridis(discrete = TRUE) +
  ggtitle("Government Debt as Percent of GDP") +
  labs(x = "Year", 
       y = "Percent of GDP",
       fill = "Country") +
  scale_x_continuous(breaks = seq(2010,2022, by = 2)) +
  #scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  #scale_y_continuous(breaks = seq(100,10000, by = 1000)) +
  #scale_fill_discrete(name = "Indicator", labels = c("Debt Service","External Debt")) +
  
  theme_clean()

An annoying theme is that the IDS database does not have 2021 or 2022 external debt numbers. Yet it somehow has debt service numbers for external debt… So we’ll stick with 2020 for this data

gain_IDS %>%
  filter(Name %in% c("Angola","Sudan","Nigeria","Algeria","Morocco")) %>%
  #filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name %in% c("Gross domestic product, current prices_NGDPD", "Total Debt")) %>%
 filter(Year == "2020") %>%
  filter_all(all_vars(.!= "Islamic Republic of Iran")) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
    mutate(Value = Value/1000000000) %>%

  na.omit() %>%
  #mutate(Value = Value/10000000) %>%
#    filter(`Value` > 50) %>%


  ggplot(aes(x=Name, y=Value, fill = Series.Name)) +
  geom_col(position = position_dodge()) +
  #geom_text(aes(label=Value), position = position_dodge(width = 0.9),vjust=-0.25) +
  #scale_fill_viridis(discrete = TRUE) +
  ggtitle("GDP vs Total Debt vs External Debt ($bns)") +
  labs(x = "Country", 
       y = "Total ($Bns)",
       subtitle = "Select African Countries, 2020") +
  #scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(100,10000, by = 1000)) +
  scale_fill_discrete(name = "Indicator", labels = c("GDP","Total Debt")) +
  
  theme_clean()

Comparing GDP with total debt and external facing

gain_IDS %>%
  filter(Name %in% c("Angola","Sudan","Nigeria","Algeria","Morocco")) %>%
  #filter(Income.group %in% c("Low income","Lower middle income")) %>%
  filter(Series.Name %in% c("Gross domestic product, current prices_NGDPD", "Total Debt","External debt stocks, total (DOD, current US$)")) %>%
 filter(Year == "2020") %>%
  filter_all(all_vars(.!= "Islamic Republic of Iran")) %>%
   mutate(Value = as.numeric(as.character(Value))) %>%
  mutate(Year = as.numeric(as.character(Year))) %>%
    mutate(Value = Value/1000000000) %>%

  na.omit() %>%
  #mutate(Value = Value/10000000) %>%
#    filter(`Value` > 50) %>%


  ggplot(aes(x=Name, y=Value, fill = Series.Name)) +
  geom_col(position = position_dodge()) +
  #geom_text(aes(label=Value), position = position_dodge(width = 0.9),vjust=-0.25) +
  #scale_fill_viridis(discrete = TRUE) +
  ggtitle("GDP vs Total Debt vs External Debt ($bns)") +
  labs(x = "Country", 
       y = "Total ($Bns)",
       subtitle = "Select African Countries, 2020") +
  #scale_x_continuous(breaks = seq(2010,2020, by = 2)) +
  scale_y_continuous(breaks = seq(100,1000, by = 100)) +
  scale_fill_discrete(name = "Indicator", labels = c("External Debt","GDP", "Total Debt")) +
  
  theme_clean()

gain_IDS_categories_2021 <- unique(IDS_clean$Series.Name)

gain_IDS_categories_2021  
##  [1] "External debt stocks, total (DOD, current US$)"         
##  [2] "External debt stocks, variable rate (DOD, current US$)" 
##  [3] "GG, bonds (DOD, current US$)"                           
##  [4] "GG, official creditors (DOD, current US$)"              
##  [5] "GG, official creditors (INT, current US$)"              
##  [6] "Interest arrears, private creditors (current US$)"      
##  [7] "Total amount of debt rescheduled (current US$)"         
##  [8] "Total change in external debt stocks (current US$)"     
##  [9] "Total reserves (% of total external debt)"              
## [10] "Total reserves (includes gold, current US$)"            
## [11] "Debt service on external debt, total (TDS, current US$)"
## [12] "Gross domestic product, current prices_NGDPD"           
## [13] "Gross domestic product, constant prices_NGDP_RPCH"      
## [14] "Population_LP"                                          
## [15] "Unemployment rate_LUR"                                  
## [16] "General government gross debt_GGXWDG_NGDP"              
## [17] "Total Debt"