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.0013789599 0.0014370273 0.0017213346 0.0017634431 0.0015961633
## QQQ 0.0014370273 0.0017632670 0.0017694599 0.0018116935 0.0016582447
## EEM 0.0017213346 0.0017694599 0.0033901721 0.0023127676 0.0024955572
## IWM 0.0017634431 0.0018116935 0.0023127676 0.0026629359 0.0019648699
## EFA 0.0015961633 0.0016582447 0.0024955572 0.0019648699 0.0024254009
## TLT -0.0009663054 -0.0009584946 -0.0011821308 -0.0013080797 -0.0011039439
## IYR 0.0011816460 0.0012041814 0.0017966314 0.0015876791 0.0015471666
## GLD 0.0002043581 0.0003904141 0.0009310458 0.0005394831 0.0004417107
## TLT IYR GLD
## SPY -0.0009663054 0.0011816460 0.0002043581
## QQQ -0.0009584946 0.0012041814 0.0003904141
## EEM -0.0011821308 0.0017966314 0.0009310458
## IWM -0.0013080797 0.0015876791 0.0005394831
## EFA -0.0011039439 0.0015471666 0.0004417107
## TLT 0.0015464538 -0.0003632176 0.0001797631
## IYR -0.0003632176 0.0019945674 0.0005353330
## GLD 0.0001797631 0.0005353330 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.0013789599 0.0014370273 0.0017213346 0.0017634431 0.0015961633
## QQQ 0.0014370273 0.0017632670 0.0017694599 0.0018116935 0.0016582447
## EEM 0.0017213346 0.0017694599 0.0033901721 0.0023127676 0.0024955572
## IWM 0.0017634431 0.0018116935 0.0023127676 0.0026629359 0.0019648699
## EFA 0.0015961633 0.0016582447 0.0024955572 0.0019648699 0.0024254009
## TLT -0.0009663054 -0.0009584946 -0.0011821308 -0.0013080797 -0.0011039439
## IYR 0.0011816460 0.0012041814 0.0017966314 0.0015876791 0.0015471666
## GLD 0.0002043581 0.0003904141 0.0009310458 0.0005394831 0.0004417107
## TLT IYR GLD
## SPY -0.0009663054 0.0011816460 0.0002043581
## QQQ -0.0009584946 0.0012041814 0.0003904141
## EEM -0.0011821308 0.0017966314 0.0009310458
## IWM -0.0013080797 0.0015876791 0.0005394831
## EFA -0.0011039439 0.0015471666 0.0004417107
## TLT 0.0015464538 -0.0003632176 0.0001797631
## IYR -0.0003632176 0.0019945674 0.0005353330
## GLD 0.0001797631 0.0005353330 0.0029025882