x<-c("quantmod", "tidyquant", "lubridate", "timetk", "purrr","tidyverse", "tibble", "readr", "xts", "PerformanceAnalytics", "magrittr", "dplyr")
lapply(x, require, character.only = TRUE)
## Loading required package: quantmod
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Loading required package: tidyquant
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
## Loading required package: timetk
## Loading required package: purrr
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ stringr 1.5.1
## ✔ forcats 1.0.0 ✔ tibble 3.2.1
## ✔ ggplot2 3.5.1 ✔ 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
## Loading required package: magrittr
##
##
## Attaching package: 'magrittr'
##
##
## The following object is masked from 'package:tidyr':
##
## extract
##
##
## The following object is masked from 'package:purrr':
##
## set_names
## [[1]]
## [1] TRUE
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] TRUE
##
## [[5]]
## [1] TRUE
##
## [[6]]
## [1] TRUE
##
## [[7]]
## [1] TRUE
##
## [[8]]
## [1] TRUE
##
## [[9]]
## [1] TRUE
##
## [[10]]
## [1] TRUE
##
## [[11]]
## [1] TRUE
##
## [[12]]
## [1] TRUE
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_data <- merge(Ad(SPY), Ad(QQQ),Ad(EEM), Ad(IWM),Ad(EFA),Ad(TLT),Ad(IYR),Ad(GLD))
colnames(etf_data) <- c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
head(etf_data)
## SPY QQQ EEM IWM EFA TLT IYR
## 2010-01-04 86.86005 40.73328 31.82712 52.51539 37.52378 60.71098 28.10298
## 2010-01-05 87.08997 40.73328 32.05813 52.33483 37.55686 61.10304 28.17045
## 2010-01-06 87.15131 40.48757 32.12519 52.28559 37.71561 60.28514 28.15819
## 2010-01-07 87.51920 40.51390 31.93889 52.67136 37.57009 60.38650 28.40971
## 2010-01-08 87.81042 40.84735 32.19226 52.95864 37.86774 60.35945 28.21954
## 2010-01-11 87.93307 40.68061 32.12519 52.74524 38.17862 60.02822 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_data.xts <- xts(etf_data)
head(etf_data)
## SPY QQQ EEM IWM EFA TLT IYR
## 2010-01-04 86.86005 40.73328 31.82712 52.51539 37.52378 60.71098 28.10298
## 2010-01-05 87.08997 40.73328 32.05813 52.33483 37.55686 61.10304 28.17045
## 2010-01-06 87.15131 40.48757 32.12519 52.28559 37.71561 60.28514 28.15819
## 2010-01-07 87.51920 40.51390 31.93889 52.67136 37.57009 60.38650 28.40971
## 2010-01-08 87.81042 40.84735 32.19226 52.95864 37.86774 60.35945 28.21954
## 2010-01-11 87.93307 40.68061 32.12519 52.74524 38.17862 60.02822 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
weekly_returns <- to.weekly(etf_data.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.008117130 -0.015037138 -0.02893527 -0.01301914 -0.003493371
## 2010-01-22 -0.038982528 -0.036860025 -0.05578052 -0.03062199 -0.055740315
## 2010-01-29 -0.016665154 -0.031023374 -0.03357763 -0.02624325 -0.025803224
## 2010-02-05 -0.006798167 0.004440222 -0.02821308 -0.01397445 -0.019054781
## 2010-02-12 0.012939037 0.018148153 0.03333337 0.02952621 0.005244940
## 2010-02-19 0.028692767 0.024451759 0.02445382 0.03343154 0.022994971
## TLT IYR GLD
## 2010-01-15 2.004839e-02 -0.006304761 -0.004579349
## 2010-01-22 1.009976e-02 -0.041784837 -0.033285246
## 2010-01-29 3.370279e-03 -0.008447517 -0.011290465
## 2010-02-05 -5.501879e-05 0.003223452 -0.012080019
## 2010-02-12 -1.946079e-02 -0.007573931 0.022544905
## 2010-02-19 -8.204716e-03 0.050184879 0.022701796
monthly_returns <- to.monthly(etf_data.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.03119454 0.04603872 0.017764218 0.04475151 0.002667887
## 2010-03-31 0.06087956 0.07710933 0.081108576 0.08230709 0.063853876
## 2010-04-30 0.01547000 0.02242521 -0.001661758 0.05678435 -0.028045895
## 2010-05-28 -0.07945434 -0.07392352 -0.093935930 -0.07536649 -0.111927677
## 2010-06-30 -0.05174100 -0.05975701 -0.013986389 -0.07743395 -0.020619345
## 2010-07-30 0.06830063 0.07258275 0.109324699 0.06730918 0.116103966
## TLT IYR GLD
## 2010-02-26 -0.003424553 0.05457031 0.032748219
## 2010-03-31 -0.020573514 0.09748503 -0.004386396
## 2010-04-30 0.033217763 0.06388088 0.058834363
## 2010-05-28 0.051083426 -0.05683531 0.030513147
## 2010-06-30 0.057979423 -0.04670129 0.023553189
## 2010-07-30 -0.009464333 0.09404812 -0.050871157
fama <- read_csv("F-F_Research_Data_Factors-2.CSV")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
## dat <- vroom(...)
## problems(dat)
## Rows: 1276 Columns: 1
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): This file was created by CMPT_ME_BEME_RETS using the 202404 CRSP da...
##
## ℹ 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(fama)
## # A tibble: 6 × 1
## `This file was created by CMPT_ME_BEME_RETS using the 202404 CRSP database.`
## <chr>
## 1 The 1-month TBill return is from Ibbotson and Associates, Inc.
## 2 ,Mkt-RF,SMB,HML,RF
## 3 192607, 2.96, -2.56, -2.43, 0.22
## 4 192608, 2.64, -1.17, 3.82, 0.25
## 5 192609, 0.36, -1.40, 0.13, 0.23
## 6 192610, -3.24, -0.09, 0.70, 0.32