# Load necessary libraries
library(readr)   # For reading CSV files
library(dplyr)   # For data manipulation
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)   # For reshaping data
library(ggplot2) # For data visualization

# -----------------------------------------
# Figure 1: Bolivia's Short-term Debt (% of Total Reserves)
# -----------------------------------------

# Step 1: Load debt data and country metadata
debt_data <- read_csv("API_DT/API_DT.DOD.DSTC.IR.ZS_DS2_en_csv_v2_4023.csv", skip = 3)
## New names:
## • `` -> `...69`
## Rows: 266 Columns: 69
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (4): Country Name, Country Code, Indicator Name, Indicator Code
## dbl (54): 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, ...
## lgl (11): 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, ...69
## 
## ℹ 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.
meta_country_data <- read_csv("API_DT/Metadata_Country_API_DT.DOD.DSTC.IR.ZS_DS2_en_csv_v2_4023.csv")
## New names:
## Rows: 265 Columns: 6
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (5): Country Code, Region, IncomeGroup, SpecialNotes, TableName lgl (1): ...6
## ℹ 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.
## • `` -> `...6`
# Step 2: Filter metadata to retain only valid countries
valid_countries <- meta_country_data %>%
  filter(!is.na(Region)) %>%  # Keep rows where Region is not NA
  pull(`Country Code`)        # Extract the list of valid country codes

# Step 3: Filter debt data to include only valid countries
debt_data_filtered <- debt_data %>%
  filter(`Country Code` %in% valid_countries)

# Step 4: Filter data for Bolivia
bolivia_data <- debt_data_filtered %>%
  filter(`Country Name` == "Bolivia") %>%
  select(`Country Name`, `Country Code`, matches("^\\d{4}$"))  # Select year columns

# Step 5: Reshape data to long format for plotting
bolivia_long <- bolivia_data %>%
  pivot_longer(
    cols = matches("^\\d{4}$"),  # Select all year columns
    names_to = "Year",
    values_to = "Value"
  ) %>%
  mutate(Year = as.numeric(Year)) %>%  # Convert Year to numeric
  drop_na(Value)  # Remove rows with missing values

# Step 6: Plot Bolivia's short-term debt over time
ggplot(bolivia_long, aes(x = Year, y = Value)) +
  geom_line(color = "blue") +
  geom_point(color = "red") +
  labs(
    title = "Figure 1: Short-term Debt (% of Total Reserves) for Bolivia",
    x = "Year",
    y = "Short-term Debt (% of Total Reserves)"
  ) +
  theme_minimal()

# -----------------------------------------
# Figure 2: Proportions of Bolivia's Public Debt by Creditor
# -----------------------------------------

# Step 1: Load Bolivia's debt data by creditor
debt_data <- read_csv("Bolivia Borrowing - Sheet1.csv")
## Rows: 24 Columns: 30
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): Creditor
## num (29): 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, ...
## 
## ℹ 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.
# Step 2: Reshape data from wide to long format
long_data <- debt_data %>%
  pivot_longer(cols = -Creditor, names_to = "Year", values_to = "Value")

# Step 3: Ensure 'Value' is numeric and handle missing values
long_data <- long_data %>%
  mutate(Value = as.numeric(Value)) %>%
  drop_na(Value)

# Step 4: Reshape data back to wide format for processing
reshaped_data <- long_data %>%
  pivot_wider(names_from = Creditor, values_from = Value)
## Warning: Values from `Value` are not uniquely identified; output will contain list-cols.
## • Use `values_fn = list` to suppress this warning.
## • Use `values_fn = {summary_fun}` to summarise duplicates.
## • Use the following dplyr code to identify duplicates.
##   {data} |>
##   dplyr::summarise(n = dplyr::n(), .by = c(Year, Creditor)) |>
##   dplyr::filter(n > 1L)
# Step 5: Clean and categorize debt data
reshaped_data <- reshaped_data %>%
  select(-IMF) %>%  # Drop the original "IMF" column
  rename(
    IMF = `IMF (Allocations)`,              # Rename "IMF (Allocations)" to "IMF"
    China = `People's Republic of China`    # Rename "People's Republic of China" to "China"
  ) %>%
  mutate(across(c("Germany", "Spain", "France", "USA", "United Kingdom", "Belgium", "Japan"), as.numeric)) %>%
  rowwise() %>%
  mutate(
    `Bilateral Global North` = sum(c_across(c("Germany", "Spain", "France", "USA", "United Kingdom", "Belgium", "Japan")), na.rm = TRUE),
    `Regional Development Banks (CAF & IDB)` = sum(c_across(c("Andean Development Corporation (CAF)", "Inter-American Development Bank (IDB)")), na.rm = TRUE),
    `Breton Woods Institutions (IMF & WB)` = sum(c_across(c("IMF", "World Bank")), na.rm = TRUE)
  ) %>%
  ungroup()

# Step 6: Ensure key columns are numeric
columns_to_numeric <- c(
  "China", 
  "Bilateral Global North", 
  "Regional Development Banks (CAF & IDB)", 
  "Breton Woods Institutions (IMF & WB)", 
  "Total"
)
reshaped_data <- reshaped_data %>%
  mutate(across(all_of(columns_to_numeric), as.numeric))

# Step 7: Calculate proportions relative to 'Total'
selected_columns <- c(
  "China", 
  "Bilateral Global North", 
  "Regional Development Banks (CAF & IDB)", 
  "Breton Woods Institutions (IMF & WB)"
)
proportion_data <- reshaped_data %>%
  mutate(across(all_of(selected_columns), ~ (. / Total) * 100, .names = "prop_{col}"))  # Calculate proportions as percentages

# Step 8: Reshape data to long format for plotting
long_proportion_data <- proportion_data %>%
  pivot_longer(cols = starts_with("prop_"), 
               names_to = "Category", 
               values_to = "Proportion") %>%
  mutate(Category = gsub("prop_", "", Category),  # Remove "prop_" from category names
         Year = as.numeric(Year))  # Ensure Year is numeric
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Year = as.numeric(Year)`.
## Caused by warning:
## ! NAs introduced by coercion
# Step 9: Plot proportions of Bolivia's debt by creditor
ggplot(long_proportion_data, aes(x = Year, y = Proportion, color = Category)) +
  geom_line(size = 1) +
  labs(
    title = "Figure 2: Bolivian Public External Debt Owed to Major Creditors (% of Total)",
    x = "Year",
    y = "Proportion (%)",
    color = "Category"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 4 rows containing missing values or values outside the scale range
## (`geom_line()`).