Data Transformation- HW 2

Author

Niki Linganur

Setup/ Loading packages & Data

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
options(scipen = 999)
blackrock_etf_screener <- read_csv("https://raw.githubusercontent.com/t-emery/sais-susfin_data/main/datasets/ishares_etf_screener_as_of_2023-12-27.csv") 
Rows: 424 Columns: 18
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (10): ticker, name, asset_class, sub_asset_class, region, market, locat...
dbl   (6): gross_expense_ratio_percent, net_expense_ratio_percent, net_asset...
dttm  (2): incept_date, net_assets_as_of

ℹ 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.
blackrock_etf_screener
# A tibble: 424 × 18
   ticker name                        incept_date         gross_expense_ratio_…¹
   <chr>  <chr>                       <dttm>                               <dbl>
 1 IVV    iShares Core S&P 500 ETF    2000-05-15 00:00:00                   0.03
 2 IEFA   iShares Core MSCI EAFE ETF  2012-10-18 00:00:00                   0.07
 3 AGG    iShares Core U.S. Aggregat… 2003-09-22 00:00:00                   0.03
 4 IWF    iShares Russell 1000 Growt… 2000-05-22 00:00:00                   0.19
 5 IJR    iShares Core S&P Small-Cap… 2000-05-22 00:00:00                   0.06
 6 IJH    iShares Core S&P Mid-Cap E… 2000-05-22 00:00:00                   0.05
 7 IEMG   iShares Core MSCI Emerging… 2012-10-18 00:00:00                   0.09
 8 IWM    iShares Russell 2000 ETF    2000-05-22 00:00:00                   0.19
 9 IWD    iShares Russell 1000 Value… 2000-05-22 00:00:00                   0.19
10 TLT    iShares 20+ Year Treasury … 2002-07-22 00:00:00                   0.15
# ℹ 414 more rows
# ℹ abbreviated name: ¹​gross_expense_ratio_percent
# ℹ 14 more variables: net_expense_ratio_percent <dbl>, net_assets_usd <dbl>,
#   net_assets_as_of <dttm>, asset_class <chr>, sub_asset_class <chr>,
#   region <chr>, market <chr>, location <chr>, investment_style <chr>,
#   msci_esg_fund_rating_aaa_ccc <chr>, msci_esg_quality_score_0_10 <dbl>,
#   msci_weighted_average_carbon_intensity_tons_co2e_m_sales <dbl>, …

Question 1

library(dplyr)

# Filter ESG funds
esg_funds <- blackrock_etf_screener %>%
  filter(!is.na(sustainable_classification) & sustainable_classification != "")

# Group by sustainable classification and find the largest fund in terms of net assets
largest_funds <- esg_funds %>%
  group_by(sustainable_classification) %>%
  filter(net_assets_usd == max(net_assets_usd, na.rm = TRUE)) %>%
  ungroup()

# Calculate the total net assets for each sustainable classification
total_assets <- esg_funds %>%
  group_by(sustainable_classification) %>%
  summarize(total_net_assets = sum(net_assets_usd, na.rm = TRUE))

# Join total assets with the largest funds
largest_funds <- largest_funds %>%
  left_join(total_assets, by = "sustainable_classification") %>%
  mutate(percent_of_assets = net_assets_usd / total_net_assets * 100) %>%
  arrange(desc(percent_of_assets))

# Print the resulting tibble
print(largest_funds)
# A tibble: 4 × 20
  ticker name  incept_date         gross_expense_ratio_…¹ net_expense_ratio_pe…²
  <chr>  <chr> <dttm>                               <dbl>                  <dbl>
1 BGRN   iSha… 2018-11-13 00:00:00                   0.2                    0.2 
2 ICLN   iSha… 2008-06-24 00:00:00                   0.41                   0.41
3 XVV    iSha… 2020-09-22 00:00:00                   0.08                   0.08
4 ESGU   iSha… 2016-12-01 00:00:00                   0.15                   0.15
# ℹ abbreviated names: ¹​gross_expense_ratio_percent, ²​net_expense_ratio_percent
# ℹ 15 more variables: net_assets_usd <dbl>, net_assets_as_of <dttm>,
#   asset_class <chr>, sub_asset_class <chr>, region <chr>, market <chr>,
#   location <chr>, investment_style <chr>, msci_esg_fund_rating_aaa_ccc <chr>,
#   msci_esg_quality_score_0_10 <dbl>,
#   msci_weighted_average_carbon_intensity_tons_co2e_m_sales <dbl>,
#   msci_esg_percent_coverage <dbl>, sustainable_classification <chr>, …

Question 2

library(ggplot2)

# Sort the data by net assets in ascending order and select the 10 smallest ETFs
smallest_etfs <- blackrock_etf_screener %>%
  arrange(net_assets_usd) %>%
  head(10)

# Create a bar chart
bar_chart <- ggplot(smallest_etfs, aes(x = fct_reorder(name, net_assets_usd), y = net_assets_usd)) +
  geom_bar(stat = "identity", fill = "skyblue", width = 0.5) +
  labs(title = "Top 10 Smallest ETFs by Net Assets",
       x = "ETF Name",
       y = "Net Assets (USD)") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Display the bar chart
print(bar_chart)

Question 3

library(dplyr)

# Find the bottom quintile of the MSCI ESG quality score
bottom_quintile <- blackrock_etf_screener %>%
  filter(!is.na(msci_esg_quality_score_0_10)) %>%
  mutate(quality_quintile = ntile(msci_esg_quality_score_0_10, 5)) %>%
  filter(quality_quintile == 1)

# Print the bottom quintile of the MSCI ESG quality score
print("Bottom Quintile of MSCI ESG Quality Score:")
[1] "Bottom Quintile of MSCI ESG Quality Score:"
print(bottom_quintile)
# A tibble: 76 × 19
   ticker name                        incept_date         gross_expense_ratio_…¹
   <chr>  <chr>                       <dttm>                               <dbl>
 1 IJR    iShares Core S&P Small-Cap… 2000-05-22 00:00:00                   0.06
 2 IEMG   iShares Core MSCI Emerging… 2012-10-18 00:00:00                   0.09
 3 IWM    iShares Russell 2000 ETF    2000-05-22 00:00:00                   0.19
 4 HYG    iShares iBoxx $ High Yield… 2007-04-04 00:00:00                   0.49
 5 EEM    iShares MSCI Emerging Mark… 2003-04-07 00:00:00                   0.69
 6 EMB    iShares J.P. Morgan USD Em… 2007-12-17 00:00:00                   0.39
 7 PFF    iShares Preferred and Inco… 2007-03-26 00:00:00                   0.46
 8 IWN    iShares Russell 2000 Value… 2000-07-24 00:00:00                   0.24
 9 USHY   iShares Broad USD High Yie… 2017-10-25 00:00:00                   0.08
10 IWO    iShares Russell 2000 Growt… 2000-07-24 00:00:00                   0.24
# ℹ 66 more rows
# ℹ abbreviated name: ¹​gross_expense_ratio_percent
# ℹ 15 more variables: net_expense_ratio_percent <dbl>, net_assets_usd <dbl>,
#   net_assets_as_of <dttm>, asset_class <chr>, sub_asset_class <chr>,
#   region <chr>, market <chr>, location <chr>, investment_style <chr>,
#   msci_esg_fund_rating_aaa_ccc <chr>, msci_esg_quality_score_0_10 <dbl>,
#   msci_weighted_average_carbon_intensity_tons_co2e_m_sales <dbl>, …
# Calculate the number of funds by total assets by asset class
by_asset_class <- bottom_quintile %>%
  group_by(asset_class) %>%
  summarize(num_funds = n(),
            total_assets = sum(net_assets_usd))

# Calculate the number of funds by total assets by sub-asset class
by_sub_asset_class <- bottom_quintile %>%
  group_by(sub_asset_class) %>%
  summarize(num_funds = n(),
            total_assets = sum(net_assets_usd))

# Print the results
print("Number of funds by total assets by asset class:")
[1] "Number of funds by total assets by asset class:"
print(by_asset_class)
# A tibble: 3 × 3
  asset_class  num_funds  total_assets
  <chr>            <int>         <dbl>
1 Equity              51 327053099481.
2 Fixed Income        21  56249713527.
3 Real Estate          4   7145439523.
print("Number of funds by total assets by sub-asset class:")
[1] "Number of funds by total assets by sub-asset class:"
print(by_sub_asset_class)
# A tibble: 9 × 3
  sub_asset_class        num_funds  total_assets
  <chr>                      <int>         <dbl>
1 All Cap                       23  95486052602.
2 Corporates                     1     41339182.
3 Credit                         1    408038372.
4 Government                     3  17560711395.
5 High Yield                    16  38239624578.
6 Large Cap                      1   1852506812.
7 Large/Mid Cap                 10  42421984678.
8 Real Estate Securities         4   7145439523.
9 Small Cap                     17 187292555389.

Question 4:

a) Highlight dplyr skills

Top Asset Classes by Total Net Assets

top_asset_classes <- blackrock_etf_screener %>%
  group_by(asset_class) %>%
  summarize(total_net_assets = sum(net_assets_usd, na.rm = TRUE)) %>%
  arrange(desc(total_net_assets)) %>%
  head(5)

print(top_asset_classes)
# A tibble: 5 × 2
  asset_class  total_net_assets
  <chr>                   <dbl>
1 Equity                1.91e12
2 Fixed Income          6.33e11
3 Commodity             4.03e10
4 Real Estate           1.41e10
5 Multi Asset           6.58e 9

Average Expense Ratio by Investment Style:

expense_ratio_by_style <- blackrock_etf_screener %>%
  group_by(investment_style) %>%
  summarize(avg_expense_ratio = mean(gross_expense_ratio_percent, na.rm = TRUE))

print(expense_ratio_by_style)
# A tibble: 2 × 2
  investment_style avg_expense_ratio
  <chr>                        <dbl>
1 Active                       0.352
2 Index                        0.318

Top Sub-Asset Classes by Total Net Assets

top_sub_asset_classes <- blackrock_etf_screener %>%
  filter(asset_class == "Equity") %>%
  group_by(sub_asset_class) %>%
  summarize(total_net_assets = sum(net_assets_usd, na.rm = TRUE)) %>%
  arrange(desc(total_net_assets)) %>%
  head(5)

print(top_sub_asset_classes)
# A tibble: 5 × 2
  sub_asset_class total_net_assets
  <chr>                      <dbl>
1 Large/Mid Cap      536807760165.
2 All Cap            508312247338.
3 Large Cap          505828620151.
4 Small Cap          201397234132.
5 Mid Cap            155402513637.

Trend in Net Assets Over Time

net_assets_trend <- blackrock_etf_screener %>%
  mutate(year = lubridate::year(incept_date)) %>%
  group_by(year) %>%
  summarize(total_net_assets = sum(net_assets_usd, na.rm = TRUE))

print(net_assets_trend)
# A tibble: 25 × 2
    year total_net_assets
   <dbl>            <dbl>
 1  1996          3.01e10
 2  2000          1.03e12
 3  2001          1.58e11
 4  2002          1.37e11
 5  2003          1.58e11
 6  2004          6.19e10
 7  2005          6.09e10
 8  2006          3.60e10
 9  2007          2.31e11
10  2008          4.61e10
# ℹ 15 more rows

b) Visualizations

1) Distribution of ESG Fund Ratings:

library(ggplot2)

# Create bar plot for MSCI ESG Fund Ratings
ggplot(data = blackrock_etf_screener, aes(x = msci_esg_fund_rating_aaa_ccc)) +
  geom_bar(fill = "skyblue", color = "black") +
  labs(title = "Distribution of ESG Fund Ratings",
       x = "MSCI ESG Fund Rating",
       y = "Count")

2) Comparison of Expense Ratios:

# Create boxplot for comparison of gross expense ratio percent by asset class
ggplot(data = blackrock_etf_screener, aes(x = asset_class, y = gross_expense_ratio_percent)) +
  geom_boxplot(fill = "lightgreen", color = "black") +
  labs(title = "Comparison of Gross Expense Ratios by Asset Class",
       x = "Asset Class",
       y = "Gross Expense Ratio (%)")

3) Geographical Distribution of ESG Funds:

# Create bar chart for number of ESG funds by region
ggplot(data = blackrock_etf_screener, aes(x = region)) +
  geom_bar(fill = "orange") +
  labs(title = "Number of ESG Funds by Region",
       x = "Region",
       y = "Number of Funds") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

4) Trend in Net Assets Over Time:

# Create line chart for trend in net assets over time
ggplot(data = blackrock_etf_screener, aes(x = incept_date, y = net_assets_usd)) +
  geom_line(color = "blue") +
  labs(title = "Trend in Net Assets of ESG Funds Over Time",
       x = "Inception Date",
       y = "Net Assets (USD)")

5) Top Sub-Asset Classes by Total Net Assets:

library(dplyr)

# Calculate total net assets by sub-asset class
net_assets_by_sub_asset <- blackrock_etf_screener %>%
  group_by(sub_asset_class) %>%
  summarize(total_net_assets = sum(net_assets_usd, na.rm = TRUE)) %>%
  top_n(10, total_net_assets)  # Select top 10 sub-asset classes

# Create horizontal bar chart for top sub-asset classes by total net assets
ggplot(data = net_assets_by_sub_asset, aes(x = total_net_assets, y = fct_reorder(sub_asset_class, total_net_assets))) +
  geom_bar(stat = "identity", fill = "purple") +
  labs(title = "Top Sub-Asset Classes by Total Net Assets",
       x = "Total Net Assets (USD)",
       y = "Sub-Asset Class") +
  theme(axis.text.y = element_text(size = 8))