Final Project Sustainable Finance

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

library(tidyverse) 
-- Attaching packages --------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.5     v purrr   0.3.4
v tibble  3.1.8     v dplyr   1.0.7
v tidyr   1.1.4     v stringr 1.4.0
v readr   2.1.1     v forcats 0.5.1
Warning: package 'tibble' was built under R version 4.1.3
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(janitor)
Warning: package 'janitor' was built under R version 4.1.3

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

    chisq.test, fisher.test
library(here)
Warning: package 'here' was built under R version 4.1.3
here() starts at C:/Users/Ryan/Documents/Final Project Sustainabke Finance
library(readxl)
library(tidyr)
library(dplyr)

library(rnaturalearth)
Warning: package 'rnaturalearth' was built under R version 4.1.3
library(countrycode)
Warning: package 'countrycode' was built under R version 4.1.3
library(wbstats)
Warning: package 'wbstats' was built under R version 4.1.3
library(ggplot2)
library(gganimate)
Warning: package 'gganimate' was built under R version 4.1.3
#install.packages('gganimate')
library(gapminder)
Warning: package 'gapminder' was built under R version 4.1.3
#install.packages('gifski')
library(gifski)
Warning: package 'gifski' was built under R version 4.1.3
renewable_energy <- read_csv("00_data_raw/Renewable Energy by year and country.csv")
New names:
Rows: 18374 Columns: 8
-- Column specification
-------------------------------------------------------- Delimiter: "," chr
(6): LOCATION, INDICATOR, SUBJECT, MEASURE, FREQUENCY, ...8 dbl (2): TIME,
Value
i Use `spec()` to retrieve the full column specification for this data. i
Specify the column types or set `show_col_types = FALSE` to quiet this message.
* `` -> `...8`
renewable_energy <- renewable_energy[,!names(renewable_energy) %in% c("INDICATOR", "SUBJECT", "MEASURE", "FREQUENCY", "...8")]
renewable_energy <- renewable_energy %>%  
rename("year" = "TIME", "energy_ktoe" = "Value","iso3c" = "LOCATION")
#Create iso3c index function.

iso3c_to_country_name <- function(iso3c) {
  iso3c |>
  countrycode(origin = "iso3c", 
              destination = "country.name")
}


country_name_regex_to_iso3c <- function(country_name) {
  country_name |>
    countrycode(origin = "country.name", 
                                     destination = "iso3c",
                                     origin_regex = TRUE)
}

renewable_energy <- renewable_energy %>%  
  mutate(country_name = iso3c_to_country_name(iso3c)) 
Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: EU28, G20, OEU, WLD
renewable_energy <- renewable_energy %>%  
na.omit()
co2_emissions <- read_csv("00_data_raw/CO2 Emissions by country and year .csv")
New names:
Rows: 2980 Columns: 8
-- Column specification
-------------------------------------------------------- Delimiter: "," chr
(6): LOCATION, INDICATOR, SUBJECT, MEASURE, FREQUENCY, ...8 dbl (2): TIME,
Value
i Use `spec()` to retrieve the full column specification for this data. i
Specify the column types or set `show_col_types = FALSE` to quiet this message.
* `` -> `...8`
co2_emissions <- co2_emissions[,!names(co2_emissions) %in% c("INDICATOR", "SUBJECT", "MEASURE", "FREQUENCY", "...8")]
co2_emissions <- co2_emissions %>%  
rename("year" = "TIME", "emission_mln_ton" = "Value","iso3c" = "LOCATION")
co2_emissions <- co2_emissions %>%  
mutate(country_name = iso3c_to_country_name(iso3c)) 
Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: EU27_2020, EU28, G20, OECD, OEU, WLD
co2_emissions <- co2_emissions %>%  
na.omit()
 co2_emissions_and_renewables <- 
  full_join(renewable_energy, co2_emissions, by = c("year" = "year", "country_name" = "country_name", "iso3c" = "iso3c"))
 co2_emissions_and_renewables_clean <-  co2_emissions_and_renewables %>%
   na.omit()
Environmental_Protection_Expenditures <- read_csv("00_data_raw/Environmental_Protection_Expenditures.csv")
Rows: 1523 Columns: 36
-- Column specification --------------------------------------------------------
Delimiter: ","
chr  (9): Country, ISO2, ISO3, Indicator, Source, CTS_Code, CTS_Name, CTS_Fu...
dbl (27): F1995, F1996, F1997, F1998, F1999, F2000, F2001, F2002, F2003, F20...

i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Environmental_Protection_Expenditures <- Environmental_Protection_Expenditures[,!names(Environmental_Protection_Expenditures) %in% c("ISO2", "Source", "CTS_Code", "CTS_Name", "CTS_Full_Descriptor")]
Environmental_Protection_Expenditures <- Environmental_Protection_Expenditures %>% 
  pivot_longer("F1995":"F2021",
    names_to = "year",
    values_to = "percent_of_gdp"
  )
Environmental_Protection_Expenditures <- Environmental_Protection_Expenditures %>%
  na.omit()
Environmental_Protection_Expenditures <- subset(Environmental_Protection_Expenditures, Unit!="Domestic Currency")
Environmental_Protection_Expenditures <- Environmental_Protection_Expenditures[,!names(Environmental_Protection_Expenditures) %in% c("Unit")]
Environmental_Protection_Expenditures <- Environmental_Protection_Expenditures |>
  pivot_wider(
    names_from = "Indicator",
    values_from = "percent_of_gdp")
Environmental_Protection_Expenditures <- Environmental_Protection_Expenditures |>
  rowwise() |>
  mutate(total_percent_gdp = sum(`Expenditure on biodiversity & landscape protection`, `Expenditure on environment protection`, `Expenditure on environmental protection n.e.c.`, `Expenditure on environmental protection R&D`, `Expenditure on pollution abatement`, `Expenditure on waste management`, `Expenditure on waste water management`, na.rm = TRUE)) 
Environmental_Protection_Expenditures <- Environmental_Protection_Expenditures |>
mutate(iso3c = country_name_regex_to_iso3c(Country)) |>
  mutate(country_name = iso3c_to_country_name(iso3c))
Environmental_Protection_Expenditures <- Environmental_Protection_Expenditures[,!names(Environmental_Protection_Expenditures) %in% c("Country", "ISO3")]
Environmental_Protection_Expenditures$year<-gsub("F","",as.character(Environmental_Protection_Expenditures$year))
Environmental_Protection_Expenditures$year <- as.numeric(as.character(Environmental_Protection_Expenditures$year)) 
depedent_variables <- 
  full_join(co2_emissions_and_renewables, Environmental_Protection_Expenditures, by = c("year" = "year", "country_name" = "country_name", "iso3c" = "iso3c"))
green_bonds <- read_excel("00_data_raw/Green Bonds Database.xlsx", 
    sheet = "Sheet2")
green_bonds <- green_bonds %>% 
  pivot_longer("France":"Singapore",
    names_to = "country_name",
    values_to = "issuance_billion_usd"
  )
country_name_regex_to_iso3c <- function(country_name) {
  country_name |>
    countrycode(origin = "country.name", 
                                     destination = "iso3c",
                                     origin_regex = TRUE)
}
  
green_bonds <- green_bonds %>%
  mutate(iso3c = country_name_regex_to_iso3c(country_name)) |>
  mutate(country_name = iso3c_to_country_name(iso3c)) 
clean_dataset <-
  left_join(depedent_variables, green_bonds, by = c("year" = "year", "country_name" = "country_name", "iso3c" = "iso3c"))
clean_dataset
# A tibble: 7,167 x 14
   iso3c  year energy_ktoe country_name emission_mln_ton `Expenditure on biodi~`
   <chr> <dbl>       <dbl> <chr>                   <dbl>                   <dbl>
 1 AUS    1960       4438. Australia                  NA                      NA
 2 AUS    1961       4491. Australia                  NA                      NA
 3 AUS    1962       4408. Australia                  NA                      NA
 4 AUS    1963       4629. Australia                  NA                      NA
 5 AUS    1964       4498. Australia                  NA                      NA
 6 AUS    1965       4717. Australia                  NA                      NA
 7 AUS    1966       4446. Australia                  NA                      NA
 8 AUS    1967       4575. Australia                  NA                      NA
 9 AUS    1968       4473. Australia                  NA                      NA
10 AUS    1969       4526. Australia                  NA                      NA
# ... with 7,157 more rows, and 8 more variables:
#   `Expenditure on environment protection` <dbl>,
#   `Expenditure on environmental protection n.e.c.` <dbl>,
#   `Expenditure on environmental protection R&D` <dbl>,
#   `Expenditure on pollution abatement` <dbl>,
#   `Expenditure on waste management` <dbl>,
#   `Expenditure on waste water management` <dbl>, total_percent_gdp <dbl>, ...
dataset_countries <- subset(clean_dataset, country_name %in% c("France", "Germany", "Italy", "Netherlands", "Spain", "United Kingdom", "United States", "Brazil", "Chile", "China", "India", "Mexico", "Turkey", "United Arab Emirates", "Hong Kong SAR China", "Singapore"))
Chile <- subset(dataset_countries, country_name %in% c("Chile"))




dataset_countries %>%
  ggplot(aes(x = issuance_billion_usd, y = total_percent_gdp)) +
  geom_point(na.rm = TRUE) +
  geom_smooth(na.rm = TRUE)
`geom_smooth()` using method = 'loess' and formula 'y ~ x'

Chile %>%
 ggplot(aes(x = issuance_billion_usd, y = total_percent_gdp)) +
  geom_point(na.rm = TRUE) +
  geom_smooth(na.rm = TRUE)
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : pseudoinverse used at -0.024892
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : neighborhood radius 0.52489
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : reciprocal condition number 0
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : There are other near singularities as well. 0.25
Warning in predLoess(object$y, object$x, newx = if
(is.null(newdata)) object$x else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
-0.024892
Warning in predLoess(object$y, object$x, newx = if
(is.null(newdata)) object$x else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
0.52489
Warning in predLoess(object$y, object$x, newx = if
(is.null(newdata)) object$x else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
number 0
Warning in predLoess(object$y, object$x, newx = if
(is.null(newdata)) object$x else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : There are other near
singularities as well. 0.25

dataset_countries %>%
  ggplot(aes(x = issuance_billion_usd, y = energy_ktoe)) +
  geom_point(na.rm = TRUE) +
  geom_smooth(na.rm = TRUE)
`geom_smooth()` using method = 'loess' and formula 'y ~ x'

Chile %>%
 ggplot(aes(x = issuance_billion_usd, y = energy_ktoe)) +
  geom_point(na.rm = TRUE) +
  geom_smooth(na.rm = TRUE)
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : pseudoinverse used at -0.024892
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : neighborhood radius 0.52489
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : reciprocal condition number 0
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : There are other near singularities as well. 0.25
Warning in predLoess(object$y, object$x, newx = if
(is.null(newdata)) object$x else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
-0.024892
Warning in predLoess(object$y, object$x, newx = if
(is.null(newdata)) object$x else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
0.52489
Warning in predLoess(object$y, object$x, newx = if
(is.null(newdata)) object$x else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
number 0
Warning in predLoess(object$y, object$x, newx = if
(is.null(newdata)) object$x else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : There are other near
singularities as well. 0.25

dataset_countries %>%
  ggplot(aes(x = issuance_billion_usd, y = emission_mln_ton)) +
  geom_point(na.rm = TRUE) +
  geom_smooth(na.rm = TRUE)
`geom_smooth()` using method = 'loess' and formula 'y ~ x'

Chile %>%
 ggplot(aes(x = issuance_billion_usd, y = emission_mln_ton)) +
  geom_point(na.rm = TRUE) +
  geom_smooth(na.rm = TRUE)
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : span too small. fewer data values than degrees of freedom.
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : at -0.0025
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : radius 6.25e-006
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : all data on boundary of neighborhood. make span bigger
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : pseudoinverse used at -0.0025
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : neighborhood radius 0.0025
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : reciprocal condition number 1
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : There are other near singularities as well. 0.25251
Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
parametric, : zero-width neighborhood. make span bigger
Warning: Computation failed in `stat_smooth()`:
NA/NaN/Inf in foreign function call (arg 5)

Chile %>%
  filter(year > 1999) %>%
  ggplot(aes(x = year)) + 
    geom_line(aes(y = issuance_billion_usd, colour = "issuance_billion_usd", na.rm = TRUE)) +
    geom_line(aes(y = total_percent_gdp, colour = "total_percent_gdp", na.rm = TRUE))
Warning: Ignoring unknown aesthetics: na.rm
Ignoring unknown aesthetics: na.rm
Warning: Removed 13 row(s) containing missing values (geom_path).
Warning: Removed 1 row(s) containing missing values (geom_path).

Chile %>%
  filter(year > 1999) %>%
  ggplot(aes(x = year)) + 
    geom_line(aes(y = issuance_billion_usd, colour = "issuance_billion_usd", na.rm = TRUE)) +
    geom_line(aes(y = energy_ktoe/10000, colour = "energy_ktoe", na.rm = TRUE))
Warning: Ignoring unknown aesthetics: na.rm
Ignoring unknown aesthetics: na.rm
Warning: Removed 13 row(s) containing missing values (geom_path).

Chile %>%
  filter(year > 1999) %>%
  ggplot(aes(x = year)) + 
    geom_line(aes(y = issuance_billion_usd, colour = "issuance_billion_usd", na.rm = TRUE)) +
    geom_line(aes(y = emission_mln_ton , colour = "emission_mln_ton", na.rm = TRUE))
Warning: Ignoring unknown aesthetics: na.rm
Ignoring unknown aesthetics: na.rm
Warning: Removed 13 row(s) containing missing values (geom_path).
Warning: Removed 4 row(s) containing missing values (geom_path).

dataset_countries_green_bond <- dataset_countries %>%
  arrange(issuance_billion_usd)

dataset_countries_green_bond %>% 
  ggplot(aes(x = fct_reorder(.f = country_name, .x = issuance_billion_usd), y = issuance_billion_usd)) + 
  geom_bar(stat="identity", width= .9) +
    coord_flip()
Warning: Removed 760 rows containing missing values (position_stack).

theme_set(theme_bw())

graph_chile <- Chile %>%
  filter(year > 2010)

graphic <- ggplot(graph_chile, 
  aes(x = total_percent_gdp, y=issuance_billion_usd, size = energy_ktoe)
  ) +
  geom_point(show.legend = TRUE, alpha = 0.7) +
  scale_color_viridis_d() +
  scale_size(range = c(2, 12)) +
  scale_x_log10()

graphic + transition_time(year) +
  labs(title = "Year: {frame_time}")
Warning: Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).
Removed 1 rows containing missing values (geom_point).