library(rvest)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.2
##
## 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
data(cars)
str(cars)
## 'data.frame': 50 obs. of 2 variables:
## $ speed: num 4 4 7 7 8 9 10 10 10 11 ...
## $ dist : num 2 10 4 22 16 10 18 26 34 17 ...
median(cars[, 1]) # first column = speed
## [1] 15
url <- "https://en.wikipedia.org/wiki/Player_efficiency_rating"
tables <- read_html(url) %>% html_table()
per <- tables[[which(sapply(tables, function(t) "PER" %in% names(t)))[1]]]
per <- as_tibble(per)
head(per)
## # A tibble: 6 × 5
## Rank Player Pos `Team(s) played for (years)[a]` PER
## <int> <chr> <chr> <chr> <dbl>
## 1 1 Nikola Jokić^ C "Denver Nuggets (2015–present)" 28.8
## 2 2 Joel Embiid^ C "Philadelphia 76ers (2014–present)" 28.0
## 3 3 Michael Jordan* SG "Chicago Bulls (1984–1993, 1995–1998)Wash… 27.9
## 4 4 LeBron James^ SF "Cleveland Cavaliers (2003–2010, 2014–201… 26.7
## 5 5 Anthony Davis^ PF/C "New Orleans Hornets/Pelicans (2012–2019)… 26.7
## 6 6 Shaquille O'Neal* C "Orlando Magic (1992–1996)Los Angeles Lak… 26.4
Research questions
This pulls daily prices from Yahoo Finance for SPY (the S&P 500) and 8 sector ETFs:
library(tidyquant)
## Warning: package 'tidyquant' was built under R version 4.5.2
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Warning: package 'xts' was built under R version 4.5.2
## Warning: package 'quantmod' was built under R version 4.5.2
## Warning: package 'PerformanceAnalytics' was built under R version 4.5.2
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.12 ──
## ✔ PerformanceAnalytics 2.1.0 ✔ TTR 0.24.4
## ✔ quantmod 0.4.29 ✔ xts 0.14.3
## ── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date() masks base::as.Date()
## ✖ zoo::as.Date.numeric() masks base::as.Date.numeric()
## ✖ dplyr::filter() masks stats::filter()
## ✖ xts::first() masks dplyr::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ xts::last() masks dplyr::last()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary() masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
etfs <- c("SPY", "XLK", "XLF", "XLE", "XLV", "XLY", "XLP", "XLU", "XLI")
prices <- tq_get(etfs, from = "2015-01-01", to = "2025-12-31")
glimpse(prices)
## Rows: 24,885
## Columns: 8
## $ symbol <chr> "SPY", "SPY", "SPY", "SPY", "SPY", "SPY", "SPY", "SPY", "SPY"…
## $ date <date> 2015-01-02, 2015-01-05, 2015-01-06, 2015-01-07, 2015-01-08, …
## $ open <dbl> 206.38, 204.17, 202.09, 201.42, 204.01, 206.40, 204.41, 204.1…
## $ high <dbl> 206.88, 204.37, 202.72, 202.72, 206.16, 206.42, 204.60, 205.4…
## $ low <dbl> 204.18, 201.35, 198.86, 200.88, 203.99, 203.51, 201.92, 200.5…
## $ close <dbl> 205.43, 201.72, 199.82, 202.31, 205.90, 204.25, 202.65, 202.0…
## $ volume <dbl> 121465900, 169632600, 209151400, 125346700, 147217800, 158567…
## $ adjusted <dbl> 169.2675, 166.2106, 164.6451, 166.6967, 169.6548, 168.2952, 1…
count(prices, symbol)
## # A tibble: 9 × 2
## symbol n
## <chr> <int>
## 1 SPY 2765
## 2 XLE 2765
## 3 XLF 2765
## 4 XLI 2765
## 5 XLK 2765
## 6 XLP 2765
## 7 XLU 2765
## 8 XLV 2765
## 9 XLY 2765
colSums(is.na(prices))
## symbol date open high low close volume adjusted
## 0 0 0 0 0 0 0 0
prices <- prices %>% select(symbol, date, adjusted) %>% na.omit()
returns <- prices %>% group_by(symbol) %>% tq_transmute(select = adjusted, mutate_fun = monthlyReturn)
head(returns)
## # A tibble: 6 × 3
## # Groups: symbol [1]
## symbol date monthly.returns
## <chr> <date> <dbl>
## 1 SPY 2015-01-30 -0.0291
## 2 SPY 2015-02-27 0.0562
## 3 SPY 2015-03-31 -0.0157
## 4 SPY 2015-04-30 0.00983
## 5 SPY 2015-05-29 0.0129
## 6 SPY 2015-06-30 -0.0203
Cleaning decisions
returns %>% summarise(avg_return = mean(monthly.returns) * 12, volatility = sd(monthly.returns) * sqrt(12)) %>% arrange(desc(avg_return))
## # A tibble: 9 × 3
## symbol avg_return volatility
## <chr> <dbl> <dbl>
## 1 XLK 0.209 0.191
## 2 XLY 0.141 0.197
## 3 SPY 0.138 0.150
## 4 XLF 0.129 0.192
## 5 XLI 0.127 0.181
## 6 XLV 0.102 0.144
## 7 XLU 0.0978 0.150
## 8 XLE 0.0926 0.289
## 9 XLP 0.0781 0.124
The data is clean and has the same number of rows for every ETF, so it’s ready for the visualization and dashboard phases.