HW4

Homework 4 - Xiaorui Zhang

options(repos = c(CRAN = "https://cran.rstudio.com/"))
install.packages("countrycode")

The downloaded binary packages are in
    /var/folders/pj/5nkf5s7n7jg1582h2g7x8hlc0000gn/T//RtmpzzBfna/downloaded_packages
library(tidyverse) 
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ 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)
library(ggplot2)

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.
green_debt
# A tibble: 355 × 42
   ObjectId Country         ISO2  ISO3  Indicator Unit  Source CTS_Code CTS_Name
      <dbl> <chr>           <chr> <chr> <chr>     <chr> <chr>  <chr>    <chr>   
 1        1 Argentina       AR    ARG   Green Bo… Bill… Refin… ECFFI    Green B…
 2        2 Australia       AU    AUS   Green Bo… Bill… Refin… ECFFI    Green B…
 3        3 Austria         AT    AUT   Green Bo… Bill… Refin… ECFFI    Green B…
 4        4 Austria         AT    AUT   Sovereig… Bill… Refin… ECFF     Green B…
 5        5 Bangladesh      BD    BGD   Green Bo… Bill… Refin… ECFFI    Green B…
 6        6 Belarus, Rep. … BY    BLR   Green Bo… Bill… Refin… ECFFI    Green B…
 7        7 Belarus, Rep. … BY    BLR   Sovereig… Bill… Refin… ECFF     Green B…
 8        8 Belgium         BE    BEL   Green Bo… Bill… Refin… ECFFI    Green B…
 9        9 Belgium         BE    BEL   Sovereig… Bill… Refin… ECFF     Green B…
10       10 Bermuda         BM    BMU   Green Bo… Bill… Refin… ECFFI    Green B…
# ℹ 345 more rows
# ℹ 33 more variables: CTS_Full_Descriptor <chr>, Type_of_Issuer <chr>,
#   Use_of_Proceed <chr>, Principal_Currency <chr>, F1985 <dbl>, F1986 <dbl>,
#   F1987 <dbl>, F1990 <dbl>, F1991 <dbl>, F1992 <dbl>, F1993 <dbl>,
#   F1994 <dbl>, F1999 <dbl>, F2000 <dbl>, F2002 <dbl>, F2003 <dbl>,
#   F2004 <dbl>, F2007 <dbl>, F2008 <dbl>, F2009 <dbl>, F2010 <dbl>,
#   F2011 <dbl>, F2012 <dbl>, F2013 <dbl>, F2014 <dbl>, F2015 <dbl>, …
#Creating green_debt_subset
indicators_we_want <- c("Green Bond Issuances by Country", "Sovereign Green Bond Issuances")

green_debt_subset <- green_debt |> 
  clean_names() |> 
  filter(indicator %in% indicators_we_want) |> 
  select(country, iso3, indicator, matches("f\\d{4}")) 

green_debt_subset 
# A tibble: 107 × 32
   country iso3  indicator f1985 f1986 f1987 f1990 f1991 f1992 f1993 f1994 f1999
   <chr>   <chr> <chr>     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 Argent… ARG   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 2 Austra… AUS   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 3 Austria AUT   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 4 Austria AUT   Sovereig…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 5 Bangla… BGD   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 6 Belaru… BLR   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 7 Belaru… BLR   Sovereig…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 8 Belgium BEL   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 9 Belgium BEL   Sovereig…    NA    NA    NA    NA    NA    NA    NA    NA    NA
10 Bermuda BMU   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
# ℹ 97 more rows
# ℹ 20 more variables: f2000 <dbl>, f2002 <dbl>, f2003 <dbl>, f2004 <dbl>,
#   f2007 <dbl>, f2008 <dbl>, f2009 <dbl>, f2010 <dbl>, f2011 <dbl>,
#   f2012 <dbl>, f2013 <dbl>, f2014 <dbl>, f2015 <dbl>, f2016 <dbl>,
#   f2017 <dbl>, f2018 <dbl>, f2019 <dbl>, f2020 <dbl>, f2021 <dbl>,
#   f2022 <dbl>
green_debt_subset <- green_debt_subset %>%
  mutate(region = countrycode(iso3, "iso3c", "region"))

green_debt_subset |> 
  pivot_longer(cols = matches("f\\d{4}"))
# A tibble: 3,103 × 6
   country   iso3  indicator                       region            name  value
   <chr>     <chr> <chr>                           <chr>             <chr> <dbl>
 1 Argentina ARG   Green Bond Issuances by Country Latin America & … f1985    NA
 2 Argentina ARG   Green Bond Issuances by Country Latin America & … f1986    NA
 3 Argentina ARG   Green Bond Issuances by Country Latin America & … f1987    NA
 4 Argentina ARG   Green Bond Issuances by Country Latin America & … f1990    NA
 5 Argentina ARG   Green Bond Issuances by Country Latin America & … f1991    NA
 6 Argentina ARG   Green Bond Issuances by Country Latin America & … f1992    NA
 7 Argentina ARG   Green Bond Issuances by Country Latin America & … f1993    NA
 8 Argentina ARG   Green Bond Issuances by Country Latin America & … f1994    NA
 9 Argentina ARG   Green Bond Issuances by Country Latin America & … f1999    NA
10 Argentina ARG   Green Bond Issuances by Country Latin America & … f2000    NA
# ℹ 3,093 more rows
green_bonds_tidy <- green_debt_subset |> 
  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_tidy
# A tibble: 465 × 6
   country   iso3  indicator                       region   year issuance_bn_usd
   <chr>     <chr> <chr>                           <chr>   <dbl>           <dbl>
 1 Argentina ARG   Green Bond Issuances by Country Latin …  2017          0.974 
 2 Argentina ARG   Green Bond Issuances by Country Latin …  2020          0.0500
 3 Argentina ARG   Green Bond Issuances by Country Latin …  2021          0.916 
 4 Argentina ARG   Green Bond Issuances by Country Latin …  2022          0.207 
 5 Australia AUS   Green Bond Issuances by Country East A…  2014          0.526 
 6 Australia AUS   Green Bond Issuances by Country East A…  2015          0.413 
 7 Australia AUS   Green Bond Issuances by Country East A…  2016          0.531 
 8 Australia AUS   Green Bond Issuances by Country East A…  2017          2.53  
 9 Australia AUS   Green Bond Issuances by Country East A…  2018          2.22  
10 Australia AUS   Green Bond Issuances by Country East A…  2019          1.98  
# ℹ 455 more rows
green_debt_cumulative_by_region <- green_bonds_tidy %>%
  filter(indicator == "Green Bond Issuances by Country") |> 
  group_by(region, year) %>%
  summarize(annual_issuance = sum(issuance_bn_usd, na.rm = TRUE)) %>%
  arrange(region, year) %>%
  group_by(region) %>%
  mutate(cumulative_issuance = cumsum(annual_issuance)) %>%
  ungroup()
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
green_bond_region <- green_debt_cumulative_by_region |> 
  group_by(region) |> 
  slice_max(order_by = year) |> 
  arrange(cumulative_issuance|> desc()) |> 
  select(region, cumulative_issuance) |> 
  ungroup()

green_bond_region_chart <- ggplot(green_bond_region,aes(x =cumulative_issuance, 
             y = fct_reorder(.f = region, .x=cumulative_issuance)
             )) +
  geom_col(fill = "forestgreen") 

green_bond_region_chart

green_bond_region_chart +
  theme_minimal() +
  scale_x_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(title = "Cumulative Green Bonds Issurance By Region",
       subtitle = "EU and Central Asia is leading the green bonds insurance",
       x = "Cumulative Issuance (USD)",
       y = "",
       caption = "Data: IMF Climate Change Dashboard | Insight: Sherri")

green_debt_rename <- green_debt %>%
  clean_names() %>%
  filter(type_of_issuer != "Not Applicable")

green_debt_rename_tidy <- green_debt_rename |> 
  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_cumulative_issuer <- green_debt_rename_tidy %>%
  group_by(type_of_issuer, year) %>%
  summarize(annual_issuance = sum(issuance_bn_usd, na.rm = TRUE), .groups = 'drop') %>%
  arrange(type_of_issuer, year) %>%
  group_by(type_of_issuer) %>%
  mutate(cumulative_issuance = cumsum(annual_issuance)) %>%
  ungroup()

green_debt_issuer <- green_debt_cumulative_issuer |> 
  group_by(type_of_issuer) |> 
  slice_max(order_by = year) |> 
  arrange(cumulative_issuance|> desc()) |> 
  select(type_of_issuer, cumulative_issuance) |> 
  ungroup()

green_bond_issuer_chart <- ggplot(green_debt_issuer,aes(x =cumulative_issuance, 
             y = fct_reorder(.f = type_of_issuer, .x=cumulative_issuance)
             )) +
  geom_col(fill = "forestgreen") +
  theme_minimal() +
  scale_x_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(title = "Cumulative Green Bonds Issurance By Issuer",
       subtitle = " 
Nonfinancial corporations is leading the green bonds insurance",
       x = "Cumulative Issuance (USD)",
       y = "",
       caption = "Data: IMF Climate Change Dashboard | Insight: Sherri")

green_bond_issuer_chart

# Find the issuer type with the highest cumulative issuance in the latest year (2022) available in the dataset
top_issuer_latest_year <- green_debt_cumulative_issuer %>%
filter(year == max(year)) %>%
arrange(desc(cumulative_issuance)) %>%
slice(1) %>%
pull(type_of_issuer)
#The answer is also nonfinancial corporations
#use_of_proceed

green_debt_use <- green_debt %>%
  clean_names() %>%
  filter(use_of_proceed != "Not Applicable")

green_debt_use_tidy <- green_debt_use |> 
  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_cumulative_use <- green_debt_use_tidy %>%
  group_by(use_of_proceed, year) %>%
  summarize(annual_issuance = sum(issuance_bn_usd, na.rm = TRUE), .groups = 'drop') %>%
  arrange(use_of_proceed, year) %>%
  group_by(use_of_proceed) %>%
  mutate(cumulative_issuance = cumsum(annual_issuance)) %>%
  ungroup()

green_debt_use <- green_debt_cumulative_use |> 
  group_by(use_of_proceed) |> 
  arrange(cumulative_issuance|> desc()) |> 
  select(use_of_proceed, cumulative_issuance) |> 
  ungroup()

green_bond_use_chart <- green_debt_use %>%
  slice_max(order_by = cumulative_issuance, n = 10) 

green_bond_use_chart_t10 <- ggplot(green_bond_use_chart,aes(x =cumulative_issuance, 
             y = fct_reorder(.f = use_of_proceed, .x=cumulative_issuance)
             )) +
  geom_col(fill = "forestgreen") +
  theme_minimal() +
  scale_x_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(title = "Cumulative Green Bonds Issurance By Use of Proceed (Top 10)",
       subtitle = " 
Most of green bonds proceed were invested in clean transport",
       x = "Cumulative Issuance (USD)",
       y = "",
       caption = "Data: IMF Climate Change Dashboard | Insight: Sherri")

green_bond_use_chart_t10

#currency

green_debt_cur <- green_debt %>%
  clean_names() %>%
  filter(principal_currency != "Not Applicable")

green_debt_cur_tidy <- green_debt_cur |> 
  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_cumulative_cur <- green_debt_cur_tidy %>%
  group_by(principal_currency, year) %>%
  summarize(annual_issuance = sum(issuance_bn_usd, na.rm = TRUE), .groups = 'drop') %>%
  arrange(principal_currency, year) %>%
  group_by(principal_currency) %>%
  mutate(cumulative_issuance = cumsum(annual_issuance)) %>%
  ungroup()

green_debt_cur <- green_debt_cumulative_cur |> 
  group_by(principal_currency) |> 
  arrange(cumulative_issuance|> desc()) |> 
  select(principal_currency, cumulative_issuance) |> 
  ungroup()

green_debt_cur_chart <- green_debt_cur %>%
  slice_max(order_by = cumulative_issuance, n = 10) 

green_debt_cur_chart_t10 <- ggplot(green_debt_cur_chart,aes(x =cumulative_issuance, 
             y = fct_reorder(.f = principal_currency, .x=cumulative_issuance)
             )) +
  geom_col(fill = "forestgreen") +
  theme_minimal() +
  scale_x_continuous(labels = scales::label_dollar(suffix = " bn"),
                     expand = c(0,0)) +
  labs(title = "Cumulative Green Bonds Issurance By Currency (Top 10)",
       subtitle = " 
Euro if the dominant currency in Green Bonds",
       x = "Cumulative Issuance (USD)",
       y = "",
       caption = "Data: IMF Climate Change Dashboard | Insight: Sherri")

green_debt_cur_chart_t10

1 + 1
[1] 2
1 + 1
[1] 2