1. Import ETF Data
## 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
##
## ######################### 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: 'dplyr'
## The following objects are masked from 'package:xts':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## # A tibble: 6 × 13
## date SPY QQQ EEM IWM EFA TLT IYR GLD
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2010-02-01 0.0307 0.0450 0.0176 0.0438 0.00266 -0.00343 0.0531 0.0322
## 2 2010-03-01 0.0591 0.0743 0.0780 0.0791 0.0619 -0.0208 0.0930 -0.00440
## 3 2010-04-01 0.0154 0.0222 -0.00166 0.0552 -0.0284 0.0327 0.0619 0.0572
## 4 2010-05-01 -0.0828 -0.0768 -0.0986 -0.0784 -0.119 0.0498 -0.0585 0.0301
## 5 2010-06-01 -0.0531 -0.0616 -0.0141 -0.0806 -0.0208 0.0564 -0.0478 0.0233
## 6 2010-07-01 0.0661 0.0701 0.104 0.0651 0.110 -0.00951 0.0899 -0.0522
## # ℹ 4 more variables: Mkt.RF <dbl>, SMB <dbl>, HML <dbl>, RF <dbl>
## SPY QQQ EEM IWM EFA
## SPY 0.0013789638 0.0014370260 0.0017213324 0.0017634445 0.001596164
## QQQ 0.0014370260 0.0017632631 0.0017694536 0.0018116909 0.001658240
## EEM 0.0017213324 0.0017694536 0.0033901689 0.0023127655 0.002495557
## IWM 0.0017634445 0.0018116909 0.0023127655 0.0026629365 0.001964868
## EFA 0.0015961637 0.0016582399 0.0024955573 0.0019648682 0.002425401
## TLT -0.0009663069 -0.0009584948 -0.0011821310 -0.0013080786 -0.001103945
## IYR 0.0011816465 0.0012041793 0.0017966283 0.0015876817 0.001547163
## GLD 0.0002043555 0.0003904105 0.0009310444 0.0005394857 0.000441711
## TLT IYR GLD
## SPY -0.0009663069 0.0011816465 0.0002043555
## QQQ -0.0009584948 0.0012041793 0.0003904105
## EEM -0.0011821310 0.0017966283 0.0009310444
## IWM -0.0013080786 0.0015876817 0.0005394857
## EFA -0.0011039447 0.0015471635 0.0004417110
## TLT 0.0015464594 -0.0003632156 0.0001797626
## IYR -0.0003632156 0.0019945624 0.0005353328
## GLD 0.0001797626 0.0005353328 0.0029025882
2. Calculate Weekly and Monthly Returns Using Log Returns
## # A tibble: 6 × 9
## date SPY QQQ EEM IWM EFA TLT IYR GLD
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2010-02-01 0.0307 0.0450 0.0176 0.0438 0.00266 -0.00343 0.0531 0.0322
## 2 2010-03-01 0.0591 0.0743 0.0780 0.0791 0.0619 -0.0208 0.0930 -0.00440
## 3 2010-04-01 0.0154 0.0222 -0.00166 0.0552 -0.0284 0.0327 0.0619 0.0572
## 4 2010-05-01 -0.0828 -0.0768 -0.0986 -0.0784 -0.119 0.0498 -0.0585 0.0301
## 5 2010-06-01 -0.0531 -0.0616 -0.0141 -0.0806 -0.0208 0.0564 -0.0478 0.0233
## 6 2010-07-01 0.0661 0.0701 0.104 0.0651 0.110 -0.00951 0.0899 -0.0522
4. Download Fama French 3 Factors Data
## [1] TRUE
## 'data.frame': 1274 obs. of 5 variables:
## $ X : chr "192607" "192608" "192609" "192610" ...
## $ Mkt.RF: chr " 2.96" " 2.64" " 0.36" " -3.24" ...
## $ SMB : chr " -2.56" " -1.17" " -1.40" " -0.09" ...
## $ HML : chr " -2.43" " 3.82" " 0.13" " 0.70" ...
## $ RF : chr " 0.22" " 0.25" " 0.23" " 0.32" ...
6. Compute Covariance Matrix Based on CAPM Model:
## SPY QQQ EEM IWM EFA
## SPY 0.0013789638 0.0014370260 0.0017213324 0.0017634445 0.001596164
## QQQ 0.0014370260 0.0017632631 0.0017694536 0.0018116909 0.001658240
## EEM 0.0017213324 0.0017694536 0.0033901689 0.0023127655 0.002495557
## IWM 0.0017634445 0.0018116909 0.0023127655 0.0026629365 0.001964868
## EFA 0.0015961637 0.0016582399 0.0024955573 0.0019648682 0.002425401
## TLT -0.0009663069 -0.0009584948 -0.0011821310 -0.0013080786 -0.001103945
## IYR 0.0011816465 0.0012041793 0.0017966283 0.0015876817 0.001547163
## GLD 0.0002043555 0.0003904105 0.0009310444 0.0005394857 0.000441711
## TLT IYR GLD
## SPY -0.0009663069 0.0011816465 0.0002043555
## QQQ -0.0009584948 0.0012041793 0.0003904105
## EEM -0.0011821310 0.0017966283 0.0009310444
## IWM -0.0013080786 0.0015876817 0.0005394857
## EFA -0.0011039447 0.0015471635 0.0004417110
## TLT 0.0015464594 -0.0003632156 0.0001797626
## IYR -0.0003632156 0.0019945624 0.0005353328
## GLD 0.0001797626 0.0005353328 0.0029025882