My Second Quarto Document

Assignment 2

Homework problem 1:

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
github_url <- "https://raw.githubusercontent.com/t-emery/sais-susfin_data/main/datasets/etf_comparison-2022-10-03.csv"
blackrock_esg_vs_non_esg_etf <- github_url |> 
  read_csv() |> 
  select(company_name:standard_etf)
Rows: 537 Columns: 14
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (4): ticker, company_name, sector, esg_uw_ow
dbl (7): esg_etf, standard_etf, esg_tilt, esg_tilt_z_score, esg_tilt_rank, e...
lgl (3): in_esg_only, in_standard_only, in_on_index_only

ℹ 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.
ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(x = esg_etf, y = standard_etf, colour = sector) +
  geom_point(shape = "circle", size = 1.5) +
  geom_smooth(span = 0.75) +
  scale_color_viridis_d(option = "plasma", direction = 1) +
  scale_x_continuous(trans = "log10") +
  scale_y_continuous(trans = "log10") +
  labs(
    x = "ESGU",
    y = "IVV",
    title = "ESG vs NON-ESG Funds",
    caption = "Yin Guan"
  ) +
  theme_minimal() +
  facet_wrap(vars(sector))
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).

Homework Problem 2:

blackrock_esg_vs_non_esg_etf_long <- blackrock_esg_vs_non_esg_etf |> 
  pivot_longer(cols = contains("etf"), names_to = "fund_type", values_to = "weight") |> 
  mutate(fund_type = case_when(fund_type == "esg_etf" ~ "ESG ETF (ESGU)",
                               fund_type == "standard_etf" ~ "Standard ETF (IVV)"))

blackrock_esg_vs_non_esg_etf_long # Read the data
# A tibble: 1,074 × 4
   company_name                  sector                 fund_type         weight
   <chr>                         <chr>                  <chr>              <dbl>
 1 PRUDENTIAL FINANCIAL INC      Financials             ESG ETF (ESGU)    0.537 
 2 PRUDENTIAL FINANCIAL INC      Financials             Standard ETF (IV… 0.106 
 3 GENERAL MILLS INC             Consumer Staples       ESG ETF (ESGU)    0.552 
 4 GENERAL MILLS INC             Consumer Staples       Standard ETF (IV… 0.151 
 5 KELLOGG                       Consumer Staples       ESG ETF (ESGU)    0.453 
 6 KELLOGG                       Consumer Staples       Standard ETF (IV… 0.0592
 7 AUTOMATIC DATA PROCESSING INC Information Technology ESG ETF (ESGU)    0.649 
 8 AUTOMATIC DATA PROCESSING INC Information Technology Standard ETF (IV… 0.312 
 9 ECOLAB INC                    Materials              ESG ETF (ESGU)    0.441 
10 ECOLAB INC                    Materials              Standard ETF (IV… 0.118 
# ℹ 1,064 more rows
blackrock_esg_vs_non_esg_etf_long %>%  
 filter(weight >= 1L & weight <= 7L) %>%  # Find out outlier data
 ggplot() +
# Variables
  aes(
    x = weight,
    y = company_name,
    colour = fund_type,
    size = weight
  ) +
  geom_point(shape = "circle") +
  scale_color_manual(
# Color
    values = c(`ESG ETF (ESGU)` = "Green",
    `Standard ETF (IVV)` = "Gray")
  ) +
# Lables & Title
  labs(
    x = "Weight",
    y = "Company",
    title = "Weight for Outlier Company",
    caption = "Yin Guan"
  ) +
  theme_minimal()

#This chart shows the funds weight of esg and non esg
#respectively on the outlier company, outlier company represent #for the company with fund weight higher than 1%, green dots #represent esg and gray dots represnts for non-esg. The graph #shows that the company with high esg weight ususally have higher #non-esg as well

Homework Problem 3:

# Chart 1
github_url <- "https://raw.githubusercontent.com/t-emery/sais-susfin_data/main/datasets/country_features_2022-10.csv"

country_features <- github_url |> 
  read_csv() |> 
  select(country_name:population_mn)
Rows: 217 Columns: 8
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (4): country_name, iso3c, wb_income_group, wb_region
dbl (4): debt_gross_percent_of_gdp, nominal_gdp_bn_ppp, nominal_gdp_per_capi...

ℹ 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.
ggplot(country_features) +
  aes(
    x = debt_gross_percent_of_gdp,
    y = nominal_gdp_per_capita_ppp
  ) +
  geom_point(shape = "circle", size = 1.5, colour = "#112446") +
  geom_smooth(span = 0.75) +
  labs(
    x = "Debt gross percentage of GDP",
    y = "Nominal GDP per Capita",
    title = "Debt gross percentage of GDP vs. Nominal GDP per Capita",
    caption = "Yin Guan"
  ) +
  theme_minimal()
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 27 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 27 rows containing missing values (`geom_point()`).

# This chart shows the relationship between Debt gross percentage #of GDP vs. Nominal GDP per Capita, from the graph we can see #there seems no obvious relationship between Debt gross #percentage of GDP vs. Nominal GDP per Capita.

#Chart 2
ggplot(country_features) +
  aes(
    x = debt_gross_percent_of_gdp,
    y = nominal_gdp_per_capita_ppp,
    colour = wb_income_group
  ) +
  geom_point(shape = "circle", size = 1.5) +
  geom_smooth(span = 0.75) +
  scale_color_hue(direction = 1) +
  labs(
    x = "Debt gross percentage of GDP",
    y = "Nominal GDP per Capita",
    title = "Debt gross percentage of GDP vs. Nominal GDP per Capita",
    caption = "Yin Guan"
  ) +
  theme_minimal() +
  facet_wrap(vars(wb_income_group))
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 27 rows containing non-finite values (`stat_smooth()`).
Removed 27 rows containing missing values (`geom_point()`).

# We can keep classify the data according to imcome level of #countries, to observe if this relationship will differ with each #income level.

Homework 4:

Chart 1:

ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(x = esg_etf, y = standard_etf) +
  geom_point(mapping = aes(colour = sector, shape = "circle", size = 1.5)) +
  geom_smooth(span = 0.75) +
  scale_color_hue(direction = 1) +
  scale_x_continuous(trans = "log10") +
  scale_y_continuous(trans = "log10") +
  theme_minimal()
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).

# Use the sector to define the color but does not classify the #data.

Chart 2:

ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(x = esg_etf, y = standard_etf, colour = sector) +
  geom_point(shape = "circle", size = 1.5) +
  geom_smooth(span = 0.75) +
  scale_color_hue(direction = 1) +
  scale_x_continuous(trans = "log10") +
  scale_y_continuous(trans = "log10") +
  theme_minimal()
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).

# Use sector to classify the data into different groups

Chart 3:

ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(x = esg_etf, y = standard_etf) +
  geom_point(shape = "circle", size = 1.5, colour = "purple") +
  geom_smooth(span = 0.75) +
  scale_x_continuous(trans = "log10") +
  scale_y_continuous(trans = "log10") +
  theme_minimal()
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).

# Sectors are not included.

Homework problem 5:

blackrock_esg_vs_non_esg_etf %>%
 filter(esg_etf >= 1L & esg_etf <= 7L) %>%
 ggplot() +
  aes(x = company_name, y = esg_etf) +
  geom_col(fill = "Black") +
  labs(
    x = "Company",
    y = "ESG",
    title = "The esg weight of outlier company",
    caption = "Yin Guan"
  ) +
  theme_minimal()

# This Bar chart tells us the ESG weight of outlier company.

Homework Problem 6

library(ggplot2)
library(ggrepel)
set.seed(42)
ggplot(blackrock_esg_vs_non_esg_etf, aes(esg_etf, standard_etf, label = company_name)) +
  geom_point(color = "red") +
  geom_text_repel(point.size = NA)
Warning: ggrepel: 533 unlabeled data points (too many overlaps). Consider
increasing max.overlaps

# This chart shows the ESG vs. Non-ESG fund weight of different #company, we can see the location of each company on the degree #of two funds profortlio.