The following is a short analysis output of the publicly-available IMF’s MONA dataset to visualize the 10 largest IMF programs in Africa. This analysis is a part of the larger project on analyzing the trends in IMF Programs around the world undertaken by the Finance for Development Lab which the author was a part of.
We first load the required libraries:
library(writexl)
library(htmltools)
library(haven)
library(tidyverse)
library(tidyr)
library(readxl)
library(writexl)
library(lubridate)
library(countrycode)
library(tidyquant)
library(ggplot2)
library(xtable)
library(lubridate)
library(rdbnomics)
library(patchwork)
library(RColorBrewer)
library(tinytex)
library(stringr)
library(jtools)
library(huxtable)
library(ggpubr)
library(readr)
library(showtext)
library(extrafont)
library(tibble)
library(openxlsx)
library(dplyr)
library(DT)
library(plotly)
Next, we load the datasets from the IMF’s MONA website - ‘Description’ and ‘Mecon’ calling it ‘programs1’ and ‘programs3’ respectively. We also load an external ‘Country Classification’ file from the World Bank Country and Lending Groups dataset calling it ‘countryclassification’ which sorts the different countries into various regions. All the required excel files can be downladed through the links given if required.
Creating a dataframe to sort different IMF program into different groups for easy analysis
# Note: Creating a 'tibble' to sort different IMF program into Groups
program_types <- tibble("arrangement_type" = c("PRGF", "ECF","PRGF-EFF","EFF",
"SBA","ECF-EFF","PSI",
"PCI","FCL","SLL","ESF","SBA-SCF"
,"PLL","SBA-ESF","SCF","PCL"),
"Groups" = c("PRGT, Protracted", "PRGT, Protracted",
"Combined, Protracted","GRA, Protracted",
"GRA, Short-term","Combined, Protracted",
"Precautionary","Precautionary","Precautionary",
"Precautionary","PRGT, Short-term",
"Combined, Short-term","Precautionary",
"Combined, Short-term","PRGT, Short-term",
"Precautionary" )) %>%
arrange(Groups)
datatable(program_types, extensions = 'FixedColumns',
options = list(
dom = 't',
scrollX = TRUE,
scrollCollapse = TRUE
))
We begin doing our analysis:
# Note**: We first only work with the 'Description' dataset that looks at total access
imf_programs1_ssa <- programs1 %>%
# Rename 'Review Type' to 'reviewtype'
rename(reviewtype = `Review Type`) %>%
# Extract ISO3 and ISO2 codes from 'Country Name' using countryname() function
mutate(iso3 = countryname(`Country Name`, destination = 'iso3c'),
iso2 = countryname(`Country Name`, destination = 'iso2c')) %>%
# Rename 'Arrangement Type' to 'arrangement_type'
rename(arrangement_type = "Arrangement Type") %>%
# Left join with 'program_types' data frame based on 'arrangement_type'
left_join(program_types, by = "arrangement_type") %>%
mutate(CancelledY = case_when(is.na(Cancelled) ~ "N",
Cancelled == "N" ~ "N", Cancelled=="Y" ~ "Y")) %>%
# Filter rows where 'iso3' is not NA, and 'CancelledY' is "N"
filter(!is.na(iso3), CancelledY == "N") %>%
# Convert date columns to date format using as.Date()
mutate(start_date = as.Date(`Approval Date`, format='%Y-%m-%d'),
end_date = as.Date(`Initial End date`, format='%Y-%m-%d'),
revised_end_date = as.Date(`Revised End Date`, format='%Y-%m-%d'),
arrt_id = as.character(`Arrangement Number`),
review_date = as.Date(`Board Action Date`, format='%Y-%m-%d')) %>%
# Group by 'arrt_id' to calculate max('Revised End Date', 'Initial End date')
group_by(arrt_id) %>%
filter(Totalaccess == max(Totalaccess)) %>% # Filter to keep only the row with the maximum 'Totalaccess' for each 'arrt_id'
mutate(end_date = max(`Revised End Date`, `Initial End date`, na.rm = TRUE)) %>%
# Filter rows with max('Sort')
filter(Sort == max(Sort)) %>%
ungroup() %>%
# Extract year from 'start_date' and 'end_date'
mutate(start_year = year(start_date), end_year = year(end_date),
review_year = year(review_date) ) %>%
# Select specific columns
select(arrt_id, iso3, iso2, start_date, start_year, end_date, end_year,
review_date, review_year,arrangement_type, Groups, reviewtype,
Totalaccess) %>%
# Left join with 'countryclassification' data frame based on 'iso3'
left_join(countryclassification, by = "iso3") %>%
filter(start_year >= 2002) %>% # Restricting the dataset to first review in the year 2002 and beyond (earlier pgms are done under the archival dataset)
# Replace 'Kosovo' with 'XK' in 'iso2', 'XXK' in 'iso3' and filling in the datapoints
mutate(iso2 = case_when(iso2 == "Kosovo" ~ "XK", TRUE ~ iso2),
iso3 = case_when(iso3 == "Kosovo" ~ "XXK", TRUE ~ iso3),
country = case_when(iso3 == "XXK" ~ "Kosovo", TRUE ~ country),
REG = case_when(iso3 == "XXK" ~ "Europe & Central Asia", TRUE ~ REG),
INC = case_when(iso3 == "XXK" ~ "Upper middle income", TRUE ~ INC),
LEND = case_when(iso3 == "XXK" ~ "IDA", TRUE ~ LEND)) %>%
mutate(start_date = ymd(start_date),start_month = floor_date(as_date(start_date), "month"),
review_date = ymd(review_date),review_month = floor_date(as_date(review_date), "month")) %>%
#Converting Totalaccess into numeric for further analysis
mutate(Totalaccess = as.numeric(Totalaccess))
datatable(imf_programs1_ssa, extensions = 'FixedColumns',
options = list(
dom = 't',
scrollX = TRUE,
scrollCollapse = TRUE
))
We use the ‘Macroeconomic indicators’ dataset to get the values for NGDP
########## Getting NGDP values from MEcon database ##########
NGDP = programs3 %>%
mutate(iso3 =countryname(`Country Name`, destination = 'iso3c')) %>%
# Units in NCU billions
mutate(iso3 = case_when(iso3 == "Kosovo" ~ "XXK", TRUE ~ iso3)) %>%
filter(!is.na(iso3), Mneumonic == "NGDP") %>%
rename(arrt_id ="Arrangement Number", country = "Country Name",
imfcode = "Country Code", arrangement_type = "Arrangement Type",
varcode = Mneumonic, varname = Description, PY = "Program year",
curr = "Indicator Currency", review_date = "Board Action Date",
reviewtype = "Review Type")%>%
mutate(review_date = ymd(review_date)) %>%
mutate(arrt_id = as.character(arrt_id)) %>%
pivot_longer(c(`T-3`:`T+4`),names_to = "Time", values_to = "NGDP") %>%
filter(Time == "T") %>%
#select(arrt_id, country, review_month, reviewtype, NGDP) %>%
mutate(review_month = floor_date(as_date(review_date), "month")) %>%
select(arrt_id, iso3,review_month, NGDP) %>%
mutate(NGDP = as.numeric(sprintf("%.2f", NGDP))) %>%
mutate(NGDP = ifelse(arrt_id == "811", 14782.343, NGDP)) #Changing Sudan 2021
datatable(NGDP, extensions = 'FixedColumns',
options = list(
dom = 't',
scrollX = TRUE,
scrollCollapse = TRUE
))
We also load the required exchange rate datasets using DBnomics and IMF’s World Economic Outlook (WEO). We use the WEO values for ‘NGDP’ to fill in datapoints in case the ‘MEcon’ dataset from MONA has missing or ‘0’ values (as there a significant number of such missing values)
# Additional NGDP datasets also used for later creating Kenya 2023 program
NGDP_WEO = rdb("IMF", "WEO:2023-04", mask = ".NGDP.national_currency") %>%
#NGDP NCU - WEO (billions)
select("weo-country", "original_period", "original_value") %>%
filter (original_period > 1999) %>%
rename(iso3 = "weo-country", review_year = "original_period",
NGDP_WEO = "original_value") %>%
mutate(review_year = as.numeric(review_year),NGDP_WEO = as.numeric(NGDP_WEO))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `NGDP_WEO = as.numeric(NGDP_WEO)`.
## Caused by warning:
## ! NAs introduced by coercion
NGDPD_WEO = rdb("IMF", "WEO:2023-04", mask = ".NGDPD.us_dollars") %>%
#NGDPD USD - WEO (billions)
select("weo-country", "original_period", "value") %>%
filter (original_period > 1999) %>%
rename(iso3 = "weo-country", review_year = "original_period", NGDPD_WEO = "value") %>%
mutate(review_year = as.numeric(review_year),NGDPD_WEO = as.numeric(NGDPD_WEO))
#################### Working with Variables in USD ######################################
############### Get SDR/USD exchange rate ##################
#Getting SDR in USD and generating a year and month column
USD_SDR = rdb("IMF", "IFS", mask = "M..EDSE_USD_XDR_RATE") %>%
# Monthly Exchange Rates, USD per SDR, End of Period
select(original_value,original_period) %>%
rename(usd_to_sdr = original_value,review_month = original_period) %>%
# Let's format our date in the same manner as above
mutate(review_month = as.Date(as.yearmon(review_month, "%Y-%m"))) %>%
mutate(usd_to_sdr = as.numeric(usd_to_sdr))
############### Get Quotas (USD) ##################
## Get quota values for each country and merging it with imf_programs1 (at the time of program and in USD and SDR)
quotas_USD = rdb("IMF", "IFS", mask = "M..HPQ_USD") %>%
# Monthly Fund Accounts, Quota, in millions of USD
select(REF_AREA, original_value, original_period) %>%
rename(iso2 = REF_AREA, quotas_USD = original_value,review_month = original_period) %>%
mutate(review_month = as.Date(as.yearmon(review_month, "%Y-%m"))) %>%
mutate(quotas_USD = as.numeric(quotas_USD))
quotas_SDR = rdb("IMF", "IFS", mask = "M..HPQ_XDR") %>%
# Monthly Fund Accounts, Quota, in millions of SDR
select(REF_AREA, original_value, original_period) %>%
rename(iso2 = REF_AREA, quotas_SDR = original_value,review_month = original_period) %>%
mutate(review_month = as.Date(as.yearmon(review_month, "%Y-%m"))) %>%
mutate(quotas_SDR = as.numeric(quotas_SDR))
#################### Working with Variables in NCU ######################################
# Note**: The idea here is to get exchange rate values and to merge them with the imf_programs1 dataset
############### Get SDR/NCU exchange rate ##################
#Getting SDR in NCU, and generating a year and month column
LCL_SDR_XR = rdb("IMF", "IFS", mask = "M..ENSE_XDC_XDR_RATE") %>%
# Monthly Exchange Rates, SDR per Domestic Currency, End of Period %>%
select(REF_AREA, original_value, original_period) %>%
rename(iso2 = REF_AREA, lcl_sdr = original_value, review_month = original_period) %>%
mutate(review_month = as.Date(as.yearmon(review_month, "%Y-%m")))
#Generating the USD/NCU exchange rate dataset, merging with imf_programs1 and multiplying to find NGDP_USD
USD_to_xr_end = rdb("IMF", "IFS", mask = "M..EDNE_USD_XDC_RATE") %>%
# Monthly Exchange Rates, U.S. Dollar per Domestic Currency , End of Period
select(REF_AREA, original_value, original_period) %>%
rename(iso2 = REF_AREA, usd_to_ncu = original_value,review_month = original_period)%>%
mutate(review_month = as.Date(as.yearmon(review_month, "%Y-%m")))
imf_programs1_ssa <- imf_programs1_ssa %>%
left_join(USD_SDR, by = "review_month") %>%
left_join(quotas_USD, by = c("iso2","review_month")) %>%
left_join(quotas_SDR, by = c("iso2","review_month")) %>%
mutate(access_to_quotas_sdr = round((Totalaccess * 100) / quotas_SDR, 2),
access_to_quotas_usd =
round((Totalaccess* usd_to_sdr * 100) / quotas_USD, 2)) %>%
left_join(NGDP_WEO, by=c("iso3", "review_year")) %>%
left_join(NGDP, by = c("arrt_id","iso3","review_month" ) ) %>%
mutate(NGDP_combined = ifelse(!is.na(NGDP) & NGDP != 0, NGDP, NGDP_WEO)) %>%
left_join(LCL_SDR_XR, by = c("iso2","review_month")) %>%
left_join(USD_to_xr_end, by = c("iso2","review_month")) %>%
mutate(lcl_sdr = as.numeric(lcl_sdr),usd_to_ncu = as.numeric(usd_to_ncu),
access_usd = round(Totalaccess * usd_to_sdr, 2),
NGDP_usd = round(NGDP_combined * usd_to_ncu, 2)) %>%
# NGDP_usd in USD billion
# Below, adjusting NGDP_usd for arrt_id 811 or Sudan 2021 program manually for later analysis
mutate(NGDP_usd = ifelse(arrt_id == "811", 34.738, NGDP_usd)) %>%
mutate(access_to_gdp = round(access_usd*100 / (1000*NGDP_usd), 2)) %>% # millions now
#Converted the above to percentages for easy comparisons
filter(Totalaccess!=0, !is.na(review_year)) %>%
# For this exercise we remove no-access programs (FCLs, etc,)
select(-c(Other,start_date,end_date)) %>%
relocate(arrt_id, iso3, iso2, country, review_year, start_month, end_year,
arrangement_type, Groups, REG, INC, LEND) %>%
mutate(access_bil = round(Totalaccess/1000,2)) %>%
filter(arrt_id != "851") # 851 Mauritania program for mismatched data w official IMF document
datatable(imf_programs1_ssa, extensions = 'FixedColumns',
options = list(
dom = 't',
scrollX = TRUE,
scrollCollapse = TRUE
))
At the time of preparing this markdown file, the IMF had provided one of the largest programs to Kenya amounting to about $3.9 billion in 2023 which was not updated in the MONA dataset until the analysis was undertaken. Thus, we manually create a new row for the Kenya (2023) program and merge it with the main dataframe for later visualizations:
# Manually creating a new row for Kenya (2023) program:
new_row_KEN_2023 <- data.frame(REG = "Sub-Saharan Africa",
country = "Kenya",
iso3 = "KEN",
iso2 = "KE",
review_year = 2023,
arrangement_type = "ECF-EFF",
INC = "Lower middle income",
review_year = 2023,
Totalaccess = 3340,
access_bil = 3.34, # ECF-EFF and RST both
Groups = "PRGF-EFF",
access_usd = 4430.0, # From Nov16 Press Release
NGDP_usd = 118.1, # NGDP_usd values (in bil) from NGDPD_WEO dataset above for 2023
quotas_SDR = 542.8, # Taking value at Oct 23 - only one available
#quotas_USD = 764.7354, # Also taking the value at Oct 23 - only one available
access_to_quotas_sdr = 539.79 , # (Totalaccess/quotas_SDR)*100
access_to_gdp = 3.75, # access_usd*100 / (1000*NGDP_usd) where NGDP_usd is 118130 million USD
iso3_year = "KEN-2023",
country_year = "Kenya-2023")
# Analysis
africa_prog <- imf_programs1_ssa %>%
filter(arrt_id != "807") %>% # Removing KEN 2021 as we add it manually later
filter(REG == "Sub-Saharan Africa") %>%
top_n(11, access_bil) %>%
# Create a new column with country and year below
mutate(iso3_year = paste(iso3, start_year, sep = "-"),
country_year = paste(country, start_year, sep = "-")) %>%
bind_rows(new_row_KEN_2023) # Binding the new row created above
datatable(africa_prog, extensions = 'FixedColumns',
options = list(
dom = 't',
scrollX = TRUE,
scrollCollapse = TRUE
))
Now, we start with plotting the required graphs:
# 1) Total access
plot_access <- ggplot(africa_prog,
aes(x = reorder(iso3_year, access_bil),
y = access_bil, fill = Groups)) +
geom_bar(stat = "identity") +
labs(title = "Largest IMF programs (Sub-Saharan Africa)",
x = "",
y = "Access in Billions (SDR)") +
geom_text(aes(label = access_bil), position = position_stack(vjust = 0.5),
size = 2.5, color = "black") +
theme_FDL() +
#coord_flip() +
scale_y_continuous(breaks = seq(0, 3.5, by = 0.5), limits = c(0, 3.5)) +
#theme(plot.title = element_text(hjust = 0.5)) +
scale_fill_manual(values = FDL_color) +
theme(plot.title = element_text(hjust = 0.0),
axis.text.x = element_text(
angle = 90.0, # Set the angle of x-axis labels to 0 degrees
hjust = 0.5, # Center-align x-axis labels
size = 7.5 # Adjust the font size here
)) +
labs(caption = "Note: Kenya-2023 includes a ECF-EFF equal to SDR 2.93 billion and RST equal to SDR 0.407 billion") +
theme(plot.caption = element_text(hjust = 0, size = 7))
ggplotly(plot_access)
plot_PctQuota <-
ggplot(africa_prog, aes(x = reorder(iso3_year, access_to_quotas_sdr),
y = access_to_quotas_sdr, fill = Groups)) +
geom_bar(stat = "identity") +
labs(title = "Largest IMF programs (Sub-Saharan Africa) - % Quotas",
x = "",
y = "Access as a % of Quota") +
geom_text(aes(label = access_to_quotas_sdr), position = position_stack(vjust = 0.5),
size = 2.5, color = "black") +
theme_FDL() +
#coord_flip() +
scale_y_continuous(breaks = seq(0, 650, by = 50), limits = c(0, 650)) +
#theme(plot.title = element_text(hjust = 0.5)) +
scale_fill_manual(values = FDL_color) +
theme(plot.title = element_text(hjust = 0.0),
axis.text.x = element_text(
angle = 90.0, # Set the angle of x-axis labels to 0 degrees
hjust = 0.5, # Center-align x-axis labels
size = 7.5 # Adjust the font size here
)) +
labs(caption = "Note: Kenya-2023 includes a ECF-EFF equal to SDR 2.93 billion and RST equal to SDR 0.407 billion") +
theme(plot.caption = element_text(hjust = 0, size = 7))
ggplotly(plot_PctQuota)