SusFin_Assignment4_BGao

Author

Boyuan Gao

Getting Started

library(remotes)
install_github('vincentarelbundock/countrycode')
Skipping install of 'countrycode' from a github remote, the SHA1 (ef867ffb) has not changed since last install.
  Use `force = TRUE` to force installation
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(janitor)

Attaching package: 'janitor'

The following objects are masked from 'package:stats':

    chisq.test, fisher.test
library(countrycode)

imf_climate_dashboards_green_debt_url <- "https://opendata.arcgis.com/datasets/8e2772e0b65f4e33a80183ce9583d062_0.csv"

green_debt <- imf_climate_dashboards_green_debt_url |> 
  read_csv() 
Rows: 355 Columns: 42
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (12): Country, ISO2, ISO3, Indicator, Unit, Source, CTS_Code, CTS_Name, ...
dbl (30): ObjectId, F1985, F1986, F1987, F1990, F1991, F1992, F1993, F1994, ...

ℹ 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.
# we want to compare these two indicators
indicators_we_want <- c("Green Bond Issuances by Country", "Sovereign Green Bond Issuances")

green_debt_subset <- green_debt |> 
  # from the janitor package -- makes variables snake_case so they are easier to work with
  clean_names() |> 
  # filter for the vector of indicators we defined above
  filter(indicator %in% indicators_we_want) |> 
  # "f\\d{4}" is a regular expression (regex) that searches for all columns that are f + four digits.
  # Ask ChatGPT to explain this to you.
  select(country, iso3, indicator, matches("f\\d{4}")) 

Problem 1

Step 1: Use the countrycode package to add the region as a variable to green_debt_subset.

Step 2: Calculate the cumulative issuance of green bonds by region.

Step 3: Use ggplot2 to make a data visualization comparing regional cumulative issuance.

Step 4: Make your plot look professionally presentable, as we did above.

green_debt_subset_with_region <- green_debt_subset |> 
  mutate(region = countrycode(
    sourcevar = country,
    origin = "country.name", 
    destination = "region"
    )) |> 
  relocate(region, .after = iso3)
green_bonds_with_region_tidy <- green_debt_subset_with_region |> 
  pivot_longer(
    cols = matches("f\\d{4}"),
    names_to = "year",
    values_to = "issuance_bn_usd",
    names_transform = readr::parse_number,
    values_drop_na = TRUE
  )

green_bonds_with_region_cumulative <- green_bonds_with_region_tidy |> 
  select(-iso3) |> 
  group_by(region) |> 
  summarize(green_bonds_issuance_region_cumulative = sum(issuance_bn_usd))
ggplot(green_bonds_with_region_cumulative, 
         aes(x = green_bonds_issuance_region_cumulative,
             y = reorder(region, green_bonds_issuance_region_cumulative),
             fill = region)) +
  geom_bar(stat = "identity") +
  theme_minimal() +
  scale_x_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(title = "Cumulative Green Bonds Issuance by Region",
       x = "Cumulative Issuance (USD)",
       y = "Region",
       caption = "Boyuan Gao")

Problem 2

Step 1: Use the full green_debt dataset

Step 2: Use clean_names() to make the variable names snake_case

Step 3: Filter out observations where type_of_issuer is “Not Applicable”

Step 4: Use the tools taught in this chapter to provide a compelling data visualization and some repeatable factoids that provide actionable insights about green bond issuers.

green_debt_with_issuer <- green_debt |> 
  # from the janitor package -- makes variables snake_case so they are easier to work with
  clean_names() |> 
  # filter out observations where type_of_issuer is “Not Applicable”
  filter(!type_of_issuer == "Not Applicable")

green_debt_with_issuer_tidy <- green_debt_with_issuer |> 
  pivot_longer(
    cols = matches("f\\d{4}"),
    names_to = "year",
    values_to = "issuance_bn_usd",
    names_transform = readr::parse_number,
    values_drop_na = TRUE
  )

green_debt_with_issuer_cumulative <- green_debt_with_issuer_tidy |> 
  group_by(type_of_issuer) |> 
  mutate(green_debt_issuer_cumulative = sum(issuance_bn_usd))

ggplot(green_debt_with_issuer_cumulative, 
         aes(x = green_debt_issuer_cumulative,
             y = type_of_issuer,
             fill = type_of_issuer)) +
  geom_bar(stat = "identity") +
  theme_minimal() +
  scale_x_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(title = "Cumulative Green Debt Issuance by Issuer",
       subtitle = "Nonfinancial corporations and banks are the main issuers of green debt",
       x = "Cumulative Issuance (USD)",
       y = "Issuer",
       caption = "Boyuan Gao")

Problem 3

Repeat the process from problem 2 for use_of_proceed and for principal_currency

  1. What are green bond proceeds used for?
green_debt_with_use_of_proceed <- green_debt |> 
  # from the janitor package -- makes variables snake_case so they are easier to work with
  clean_names() |> 
  # filter out observations where type_of_issuer is “Not Applicable”
  filter(!use_of_proceed == "Not Applicable")

green_debt_with_use_of_proceed |> 
  distinct(use_of_proceed)
# A tibble: 69 × 1
   use_of_proceed                                                  
   <chr>                                                           
 1 Access to Essential Services                                    
 2 Acquiring and distribution of vaccine                           
 3 Acquisition                                                     
 4 Affordable Basic Infrastructure                                 
 5 Agriculture                                                     
 6 Alternative Energy                                              
 7 Aquatic Biodiversity Conservation                               
 8 Capital expenditure                                             
 9 Capital expenditure/Financing expenses                          
10 Carbon reduction through reforestation and avoided deforestation
# ℹ 59 more rows
  1. What do we know about the currency of issuance? Is that changing over time?
green_debt_with_currency_of_issuance <- green_debt |> 
  # from the janitor package -- makes variables snake_case so they are easier to work with
  clean_names() |> 
  # filter out observations where type_of_issuer is “Not Applicable”
  filter(!principal_currency == "Not Applicable")

green_debt_with_currency_of_issuance_tidy <- green_debt_with_currency_of_issuance |> 
    pivot_longer(
    cols = matches("f\\d{4}"),
    names_to = "year",
    values_to = "issuance_bn_usd",
    names_transform = readr::parse_number,
  )

cumulative_issuance_by_currency <- green_debt_with_currency_of_issuance_tidy |> 
  arrange(principal_currency, year) |> 
  group_by(principal_currency, year) |> 
  mutate(cumulative_bn_usd = cumsum(issuance_bn_usd)) |> 
  filter(!is.na(cumulative_bn_usd))

biggest_green_bond_currency <- cumulative_issuance_by_currency |> 
  group_by(principal_currency) |> 
  slice_max(order_by = year) |> 
  arrange(cumulative_bn_usd|> desc()) |> 
  select(principal_currency, cumulative_bn_usd) |> 
  ungroup()

top_10_chart <- biggest_green_bond_currency |> 
  # take the top 10, ordered by cumulative issuance
  slice_max(order_by = cumulative_bn_usd, n = 10) |> 
  ggplot(aes(x = cumulative_bn_usd, 
             # order countries by cumulative issuance
             y = fct_reorder(.f = principal_currency, .x = cumulative_bn_usd,),
             fill = principal_currency
  )) +
  geom_col() 

top_10_chart +
  theme_minimal() +
  scale_x_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(title = "Top 10 Green Bond Currencies",
       subtitle = "Euro takes the lead, followed by US Dollar and Chinese Yuan",
       x = "Cumulative Issuance (USD)",
       y = "Currency",
       caption = "Boyuan Gao")