This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#1. Import data
# Load libraries
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
##
## 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(lubridate)
library(timetk)
library(purrr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ stringr 1.5.0
## ✔ forcats 1.0.0 ✔ tibble 3.2.1
## ✔ ggplot2 3.4.4 ✔ tidyr 1.3.1
## ✔ readr 2.1.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(utils)
library(tibble)
library(readr)
library(xts)
library(PerformanceAnalytics)
library(magrittr)
##
## Attaching package: 'magrittr'
##
## The following object is masked from 'package:tidyr':
##
## extract
##
## The following object is masked from 'package:purrr':
##
## set_names
library(dplyr)
tickers <- c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
data = new.env()
getSymbols(tickers, src = 'yahoo', from = '2010-01-01', to = '2021-04-14', auto.assign = TRUE)
## [1] "SPY" "QQQ" "EEM" "IWM" "EFA" "TLT" "IYR" "GLD"
etf_data1<- merge(Ad(SPY), Ad(QQQ),Ad(EEM), Ad(IWM),Ad(EFA),Ad(TLT),Ad(IYR),Ad(GLD))
colnames(etf_data1) <- c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
head(etf_data1)
## SPY QQQ EEM IWM EFA TLT IYR
## 2010-01-04 86.86005 40.73329 31.82712 52.51540 37.52378 61.13187 28.10298
## 2010-01-05 87.08998 40.73329 32.05812 52.33482 37.55686 61.52666 28.17046
## 2010-01-06 87.15128 40.48758 32.12519 52.28558 37.71562 60.70304 28.15820
## 2010-01-07 87.51920 40.51390 31.93890 52.67136 37.57010 60.80513 28.40972
## 2010-01-08 87.81044 40.84735 32.19226 52.95864 37.86774 60.77788 28.21953
## 2010-01-11 87.93311 40.68062 32.12519 52.74523 38.17862 60.44436 28.35450
## GLD
## 2010-01-04 109.80
## 2010-01-05 109.70
## 2010-01-06 111.51
## 2010-01-07 110.82
## 2010-01-08 111.37
## 2010-01-11 112.85
etf_data1.xts <- xts(etf_data1)
head(etf_data1.xts)
## SPY QQQ EEM IWM EFA TLT IYR
## 2010-01-04 86.86005 40.73329 31.82712 52.51540 37.52378 61.13187 28.10298
## 2010-01-05 87.08998 40.73329 32.05812 52.33482 37.55686 61.52666 28.17046
## 2010-01-06 87.15128 40.48758 32.12519 52.28558 37.71562 60.70304 28.15820
## 2010-01-07 87.51920 40.51390 31.93890 52.67136 37.57010 60.80513 28.40972
## 2010-01-08 87.81044 40.84735 32.19226 52.95864 37.86774 60.77788 28.21953
## 2010-01-11 87.93311 40.68062 32.12519 52.74523 38.17862 60.44436 28.35450
## GLD
## 2010-01-04 109.80
## 2010-01-05 109.70
## 2010-01-06 111.51
## 2010-01-07 110.82
## 2010-01-08 111.37
## 2010-01-11 112.85
#2. Weekly and monthly returns
weekly_returns <- to.weekly(etf_data1.xts, indexAt = "last", OHLC = FALSE)
etf_weekly_returns <- na.omit(Return.calculate(weekly_returns, method = "discrete"))
head(etf_weekly_returns)
## SPY QQQ EEM IWM EFA
## 2010-01-15 -0.008117041 -0.015037047 -0.02893492 -0.01301907 -0.003493573
## 2010-01-22 -0.038983040 -0.036859554 -0.05578087 -0.03062199 -0.055740321
## 2010-01-29 -0.016664883 -0.031023952 -0.03357711 -0.02624310 -0.025803120
## 2010-02-05 -0.006797703 0.004440325 -0.02821360 -0.01397460 -0.019055001
## 2010-02-12 0.012938097 0.018148155 0.03333357 0.02952629 0.005245053
## 2010-02-19 0.028693146 0.024451563 0.02445355 0.03343139 0.022995085
## TLT IYR GLD
## 2010-01-15 2.004738e-02 -0.006304156 -0.004579349
## 2010-01-22 1.010106e-02 -0.041785172 -0.033285246
## 2010-01-29 3.369553e-03 -0.008447377 -0.011290465
## 2010-02-05 -5.457932e-05 0.003223810 -0.012080019
## 2010-02-12 -1.946056e-02 -0.007574429 0.022544905
## 2010-02-19 -8.205194e-03 0.050185609 0.022701796
monthly_returns <- to.monthly(etf_data1.xts, indexAt = "last", OHLC = FALSE)
etf_monthly_returns <- na.omit(Return.calculate(monthly_returns, method = "discrete"))
head(etf_monthly_returns)
## SPY QQQ EEM IWM EFA
## 2010-02-26 0.03119435 0.04603883 0.017763540 0.04475127 0.002667777
## 2010-03-31 0.06088002 0.07710885 0.081108652 0.08230717 0.063853883
## 2010-04-30 0.01546999 0.02242577 -0.001661941 0.05678422 -0.028045691
## 2010-05-28 -0.07945490 -0.07392370 -0.093935710 -0.07536631 -0.111927877
## 2010-06-30 -0.05174047 -0.05975703 -0.013986590 -0.07743402 -0.020619408
## 2010-07-30 0.06830013 0.07258297 0.109325062 0.06730895 0.116104170
## TLT IYR GLD
## 2010-02-26 -0.003424473 0.05457067 0.032748219
## 2010-03-31 -0.020573518 0.09748473 -0.004386396
## 2010-04-30 0.033217852 0.06388100 0.058834363
## 2010-05-28 0.051084459 -0.05683512 0.030513147
## 2010-06-30 0.057977580 -0.04670140 0.023553189
## 2010-07-30 -0.009463713 0.09404778 -0.050871157
#3. Download Fama French 3 factors data
famafrench <- read_csv("C:\\Users\\User\\Downloads\\F-F_Research_Data_Factors_CSV\\F-F_Research_Data_Factors.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
## dat <- vroom(...)
## problems(dat)
## Rows: 1272 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): date
## dbl (4): Mkt-RF, SMB, HML, RF
##
## ℹ 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.
head(famafrench)
## # A tibble: 6 × 5
## date `Mkt-RF` SMB HML RF
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 192607 2.96 -2.56 -2.43 0.22
## 2 192608 2.64 -1.17 3.82 0.25
## 3 192609 0.36 -1.4 0.13 0.23
## 4 192610 -3.24 -0.09 0.7 0.32
## 5 192611 2.53 -0.1 -0.51 0.31
## 6 192612 2.62 -0.03 -0.05 0.28
# Converting Fama/French 3 factor returns to digit numbers (not in percentage)
famafrench[, -1] <- famafrench[, -1] / 100