performance

# Import required libraries
library(readxl)
library(dplyr)

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

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tidyr)

# Read data
AUM_ESG <- read_excel("/Users/hahazhou/Desktop/Final Project/00_data_raw/AUM_ESG.xlsx")
AUM_Thematic <- read_excel("/Users/hahazhou/Desktop/Final Project/00_data_raw/AUM_Thematic ETF.xlsx")

# Add "Type" column and fill values
AUM_ESG <- AUM_ESG %>% mutate(Type = "ESG")
AUM_Thematic <- AUM_Thematic %>% mutate(Type = "Thematic")

# Combine datasets
Combined_Volume <- bind_rows(AUM_ESG, AUM_Thematic)

# Handle duplicates
Combined_Volume <- Combined_Volume %>%
  group_by(Ticker) %>%
  mutate(duplicate = n() > 1) %>%
  ungroup() %>%
  filter(!duplicate | Type == "ESG") %>%
  mutate(Type = ifelse(duplicate, "ESG and Thematic", Type)) %>%
  select(-duplicate)

# Keep only the first 4 letters in the Ticker column
Combined_Volume$Ticker <- substr(Combined_Volume$Ticker, 1, 4)

# Transform to long format
Combined_Volume_long <- Combined_Volume %>%
  pivot_longer(cols = starts_with("20"), names_to = "Quarter", values_to = "AUM")
library(plotly)
Loading required package: ggplot2

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
ggplot(Combined_Volume_long) +
  aes(x = Quarter, y = AUM, fill = Ticker) +
  geom_col() +
  scale_fill_hue(direction = 1) +
  labs(
    x = "Quarter",
    y = "AUM",
    title = "AUM for ETFs by Type and Ticker",
    caption = "Data Source: Bloomberg"
  ) +
  theme_minimal() +
  facet_wrap(vars(Type))
Warning: Removed 2449 rows containing missing values (`position_stack()`).

ggplotly()
Warning: Removed 2449 rows containing missing values (`position_stack()`).
# Read data
Volume_ESG <- read_excel("/Users/hahazhou/Desktop/Final Project/00_data_raw/Volume_ESG.xlsx")
Volume_Thematic <- read_excel("/Users/hahazhou/Desktop/Final Project/00_data_raw/Volume_Thematic ETF.xlsx")

# Add "Type" column and fill values
Volume_ESG <- Volume_ESG %>% mutate(Type = "ESG")
Volume_Thematic <- Volume_Thematic %>% mutate(Type = "Thematic")

# Combine datasets
Combined_V <- bind_rows(Volume_ESG, Volume_Thematic)

# Handle duplicates
Combined_V <- Combined_V %>%
  group_by(Ticker) %>%
  mutate(duplicate = n() > 1) %>%
  ungroup() %>%
  filter(!duplicate | Type == "ESG") %>%
  mutate(Type = ifelse(duplicate, "ESG and Thematic", Type)) %>%
  select(-duplicate)

# Keep only the first 4 letters in the Ticker column
Combined_V$Ticker <- substr(Combined_V$Ticker, 1, 4)

# Transform to long format
Combined_V_long <- Combined_V %>%
  pivot_longer(cols = starts_with("20"), names_to = "Quarter", values_to = "$ Volume")
ggplot(Combined_V_long) +
  aes(x = Quarter, y = `$ Volume`, fill = Ticker) +
  geom_col() +
  scale_fill_hue(direction = 1) +
  labs(
    x = "Quarter",
    y = "$ Volume",
    title = "$ Volume by Type and Ticker",
    caption = "Data source: Bloomberg"
  ) +
  theme_minimal() +
  facet_wrap(vars(Type))
Warning: Removed 2455 rows containing missing values (`position_stack()`).

ggplotly()
Warning: Removed 2455 rows containing missing values (`position_stack()`).
# Read data
FF_ESG <- read_excel("/Users/hahazhou/Desktop/Final Project/00_data_raw/Fund Flow_ESG.xlsx")
FF_Thematic <- read_excel("/Users/hahazhou/Desktop/Final Project/00_data_raw/Fund Flow_Thematic ETF.xlsx")

# Add "Type" column and fill values
FF_ESG <- FF_ESG %>% mutate(Type = "ESG")
FF_Thematic <- FF_Thematic %>% mutate(Type = "Thematic")

# Combine datasets
Combined_FF <- bind_rows(FF_ESG, FF_Thematic)

# Handle duplicates
Combined_FF <- Combined_FF %>%
  group_by(Ticker) %>%
  mutate(duplicate = n() > 1) %>%
  ungroup() %>%
  filter(!duplicate | Type == "ESG") %>%
  mutate(Type = ifelse(duplicate, "ESG and Thematic", Type)) %>%
  select(-duplicate)

# Keep only the first 4 letters in the Ticker column
Combined_FF$Ticker <- substr(Combined_FF$Ticker, 1, 4)

# Transform to long format
Combined_FF_long <- Combined_FF %>%
  pivot_longer(cols = starts_with("20"), names_to = "Quarter", values_to = "Fund Flow")

# Replace NA with 0
Combined_FF_long$`Fund Flow`[is.na(Combined_FF_long$`Fund Flow`)] <- 0
ggplot(Combined_FF_long) +
  aes(x = Quarter, y = `Fund Flow`, fill = Ticker) +
  geom_col() +
  scale_fill_hue(direction = 1) +
  labs(
    x = "Quarter",
    y = "Fund Flow",
    title = "Fund Flow by Type and Ticker",
    caption = "Data source: Bloomberg"
  ) +
  theme_minimal() +
  facet_wrap(vars(Type))

ggplotly()
library(tidyquant)
Loading required package: lubridate

Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
Loading required package: PerformanceAnalytics
Loading required package: xts
Loading required package: zoo

Attaching package: 'zoo'
The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

######################### Warning from 'xts' package ##########################
#                                                                             #
# The dplyr lag() function breaks how base R's lag() function is supposed to  #
# work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
# source() into this session won't work correctly.                            #
#                                                                             #
# Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
# conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
# dplyr from breaking base R's lag() function.                                #
#                                                                             #
# Code in packages is not affected. It's protected by R's namespace mechanism #
# Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
#                                                                             #
###############################################################################

Attaching package: 'xts'
The following objects are masked from 'package:dplyr':

    first, last

Attaching package: 'PerformanceAnalytics'
The following object is masked from 'package:graphics':

    legend
Loading required package: quantmod
Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
library(dplyr)

Thematic_tickers <- read.csv("/Users/hahazhou/Desktop/Final Project/03_data_processed/Volume_Thematic_Cleaned.csv") %>%
  distinct(Ticker)

ESG_tickers <- read.csv("/Users/hahazhou/Desktop/Final Project/03_data_processed/Volume_ESG_Cleaned.csv") %>%
  distinct(Ticker)

Combined_tickers <- full_join(
  mutate(Thematic_tickers, Type = "Thematic"),
  mutate(ESG_tickers, Type = "ESG"),
  by = "Ticker"
) %>%
  mutate(Type = ifelse(is.na(Type.x) & is.na(Type.y), NA,
                       ifelse(!is.na(Type.x) & !is.na(Type.y), "ESG and Thematic",
                              coalesce(Type.x, Type.y)))) %>%
  select(Ticker, Type)

combined_returns <- Combined_tickers %>%
  tq_get(get = "stock.prices",
         from = "2018-01-01",
         collapse = "monthly") %>%
  group_by(Ticker, Type) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = "monthly",
               col_rename = "monthly.returns")
Warning: There were 26 warnings in `dplyr::mutate()`.
The first warning was:
ℹ In argument: `data.. = purrr::map(...)`.
Caused by warning:
! x = 'PBW ', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "PBW ", env = <environment>, verbose = FALSE, : Unable to import "PBW ".
URL using bad/illegal format or missing URL
 Removing PBW .
ℹ Run `dplyr::last_dplyr_warnings()` to see the 25 remaining warnings.
Warning: There were 40 warnings in `dplyr::mutate()`.
The first warning was:
ℹ In argument: `nested.col = purrr::map(...)`.
ℹ In group 5: `Ticker = "AVDG"` and `Type = "ESG"`.
Caused by warning in `to_period()`:
! missing values removed from data
ℹ Run `dplyr::last_dplyr_warnings()` to see the 39 remaining warnings.
library(ggplot2)
library(plotly)
library(dplyr)

ggplot(combined_returns) +
  aes(x = date, y = monthly.returns, colour = Ticker) +
  geom_line() +
  scale_color_viridis_d(option = "viridis", direction = 1) +
  labs(
    x = "Year",
    y = "Monthly Returns",
    title = "Monthly Returns by Type and Ticker",
    caption = "Data Source: Bloomberg"
  ) +
  theme_gray() +
  facet_wrap(vars(Type))

ggplotly()