Sais_Susfin_Homework4_JunFang

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
# for clean_names() - makes variable names snake_case
library(janitor) 

Attaching package: 'janitor'

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

    chisq.test, fisher.test

Problem 1

library(countrycode)
#Read in the .csv file directly from the internet
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>, …
# 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}")) 

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_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(
    # select all coluns with f + 4 numbers 
    cols = matches("f\\d{4}"),
    
    # change from default ("names")
    names_to = "year",
    # same with the values
    values_to = "issuance_bn_usd",
    
    # readr::parse_number is a handy function that changes the character string 
    # "f2222" into the number 2222. Very useful! 
    names_transform = readr::parse_number,
    
    # green bonds are new-ish, we can drop all those NA values in the 80s and 90s for now.
    values_drop_na = TRUE
  )

green_bonds_with_region_tidy
# A tibble: 465 × 6
   country   iso3  region                    indicator      year issuance_bn_usd
   <chr>     <chr> <chr>                     <chr>         <dbl>           <dbl>
 1 Argentina ARG   Latin America & Caribbean Green Bond I…  2017          0.974 
 2 Argentina ARG   Latin America & Caribbean Green Bond I…  2020          0.0500
 3 Argentina ARG   Latin America & Caribbean Green Bond I…  2021          0.916 
 4 Argentina ARG   Latin America & Caribbean Green Bond I…  2022          0.207 
 5 Australia AUS   East Asia & Pacific       Green Bond I…  2014          0.526 
 6 Australia AUS   East Asia & Pacific       Green Bond I…  2015          0.413 
 7 Australia AUS   East Asia & Pacific       Green Bond I…  2016          0.531 
 8 Australia AUS   East Asia & Pacific       Green Bond I…  2017          2.53  
 9 Australia AUS   East Asia & Pacific       Green Bond I…  2018          2.22  
10 Australia AUS   East Asia & Pacific       Green Bond I…  2019          1.98  
# ℹ 455 more rows
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 = "Top 10 Green Bond Issuance By Region",
       x = "Cumulative Issuance (USD)",
       y = "Region",
       caption = "Jun!")

Problem 2

green_debt_issuer_filter <-green_debt |> 
  # from the janitor package -- makes variables snake_case so they are easier to work with
  clean_names() |> 
  # filter for the type_of_issuer is "Not Applicable"
  filter(type_of_issuer != "Not Applicable")
green_debt_issuer_filter
# A tibble: 7 × 42
  object_id country iso2  iso3  indicator         unit  source cts_code cts_name
      <dbl> <chr>   <chr> <chr> <chr>             <chr> <chr>  <chr>    <chr>   
1       347 World   <NA>  WLD   Green Bond Issua… Bill… Refin… ECFFI    Green B…
2       348 World   <NA>  WLD   Green Bond Issua… Bill… Refin… ECFFI    Green B…
3       349 World   <NA>  WLD   Green Bond Issua… Bill… Refin… ECFFI    Green B…
4       350 World   <NA>  WLD   Green Bond Issua… Bill… Refin… ECFFI    Green B…
5       351 World   <NA>  WLD   Green Bond Issua… Bill… Refin… ECFFI    Green B…
6       352 World   <NA>  WLD   Green Bond Issua… Bill… Refin… ECFFI    Green B…
7       353 World   <NA>  WLD   Green Bond Issua… Bill… Refin… ECFFI    Green B…
# ℹ 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>,
#   f2016 <dbl>, f2017 <dbl>, f2018 <dbl>, f2019 <dbl>, f2020 <dbl>, …
green_debt_issuer_filter_tidy <- green_debt_issuer_filter |> 
  pivot_longer(
    cols = matches("f\\d{4}"),
    names_to = "year",
    values_to = "issuance_bn_usd",
    names_transform = readr::parse_number,
    values_drop_na = TRUE
  )
#Import data into {esquisse}
library(tidyverse)
library(esquisse)
library(ggplot2)
ggplot(green_debt_issuer_filter_tidy) +
 aes(x = issuance_bn_usd, y = year, fill = type_of_issuer, colour = type_of_issuer) +
 geom_point(shape = "circle") +
 scale_fill_brewer(palette = "BrBG", direction = 1) +
 scale_color_brewer(palette = "BrBG", direction = 1) +
 labs(x = "Issuance (bn USD)", y = "Type of Issuer", 
 title = "Green Debt Issuance by Type of Issuer") +
#Change the x and y axis to log10.
 scale_x_continuous(trans = "log10") +
 scale_y_continuous(trans = "log10") +
 theme_minimal()
Warning: Transformation introduced infinite values in continuous x-axis

green_debt_issuer_filter_tidy_short <- green_debt_issuer_filter_tidy |> 
  filter(type_of_issuer %in% c("Banks", "Nonfinancial corporations")) |> 
  group_by(year,type_of_issuer) |> 
  summarize(issuance_bn_usd = sum(issuance_bn_usd, na.rm = TRUE), .groups = 'drop') |>
  pivot_wider(names_from = type_of_issuer,
              values_from = issuance_bn_usd,
              values_fill = 0) |> 
  ungroup() |> 
  clean_names() 

green_debt_issuer_filter_tidy_short
# A tibble: 21 × 3
    year  banks nonfinancial_corporations
   <dbl>  <dbl>                     <dbl>
 1  1985 0.03                       0.025
 2  1986 0.3                        0    
 3  1987 0.0751                     0    
 4  1990 0                          0    
 5  1993 0                          0.168
 6  1999 0.0482                     0    
 7  2000 0.0533                     0    
 8  2002 0.148                      0    
 9  2003 0.01                       0.172
10  2004 0.109                      0    
# ℹ 11 more rows
nonfinancial_corporations_vs_Banks_repeatable_factoids <- green_debt_issuer_filter_tidy_short |> 
  mutate(
    
    # multiples:
    # "nonfinancial corporations issued x times more than Banks"
    nonfinancial_corporations_x_than_Banks = nonfinancial_corporations/banks,
    
    # percent more:
    # "nonfinancial corporations issued x% more than Banks"
    nonfinancial_corporations_pct_more_than_banks = (nonfinancial_corporations
/banks-1) * 100,
         
         )

nonfinancial_corporations_vs_Banks_repeatable_factoids
# A tibble: 21 × 5
    year  banks nonfinancial_corporations nonfinancial_corporations_x_than_Banks
   <dbl>  <dbl>                     <dbl>                                  <dbl>
 1  1985 0.03                       0.025                                  0.833
 2  1986 0.3                        0                                      0    
 3  1987 0.0751                     0                                      0    
 4  1990 0                          0                                    NaN    
 5  1993 0                          0.168                                Inf    
 6  1999 0.0482                     0                                      0    
 7  2000 0.0533                     0                                      0    
 8  2002 0.148                      0                                      0    
 9  2003 0.01                       0.172                                 17.2  
10  2004 0.109                      0                                      0    
# ℹ 11 more rows
# ℹ 1 more variable: nonfinancial_corporations_pct_more_than_banks <dbl>
factoid_2013 <- nonfinancial_corporations_vs_Banks_repeatable_factoids |> 
  filter(year == 2013) |> 
  pull(nonfinancial_corporations_x_than_Banks) |> 
  round()

factoid_2013
[1] 3

Note:

factoid_2022 <- nonfinancial_corporations_vs_Banks_repeatable_factoids |> 
  filter(year == 2013) |> 
  pull(nonfinancial_corporations_pct_more_than_banks) |> 
  round()

factoid_2022
[1] 166

Problem 3.1-use_of_proceed

green_debt_use_of_proceed<-green_debt |> 
  clean_names() 
green_debt_use_of_proceed
# A tibble: 355 × 42
   object_id 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>, …
green_debt_use_of_proceed |>
  distinct(use_of_proceed) |>
  filter(use_of_proceed != "Not Applicable")
# 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

Problem 3.2-principal_currency

#Read in the .csv file directly from the internet
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>, …
green_debt_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_currency_of_issuance_tidy <- green_debt_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_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 Principal Currencies",
       x = "Cumulative Issuance (USD)",
       y = "Principal Currency",
       caption = "Jun")