R Markdown

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-4
## [1] -3
library(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
library(tidyquant)
## Warning: package 'tidyquant' was built under R version 4.3.3
## Loading required package: lubridate
## Warning: package 'lubridate' was built under R version 4.3.3
## 
## 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
library(lubridate)
library(timetk)
## Warning: package 'timetk' was built under R version 4.3.3
library(purrr)
## Warning: package 'purrr' was built under R version 4.3.3
library(tibble)
library(PerformanceAnalytics)
library(magrittr)
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
## 
##     set_names
library(readr)
library(xts)
library(dplyr)
## 
## ######################### 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
library(ggplot2)
tickers <- c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
data = new.env()
getSymbols(tickers, src = 'yahoo', from = '2010-01-01', to = Sys.Date(), auto.assign = TRUE)
## [1] "SPY" "QQQ" "EEM" "IWM" "EFA" "TLT" "IYR" "GLD"
ETFList <- merge(Ad(SPY), Ad(QQQ),Ad(EEM), Ad(IWM),Ad(EFA),Ad(TLT),Ad(IYR),Ad(GLD))
colnames(ETFList) <-c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
head(ETFList)
##                 SPY      QQQ      EEM      IWM      EFA      TLT      IYR
## 2010-01-04 86.86007 40.73328 31.82711 52.51541 37.52378 61.13187 28.10299
## 2010-01-05 87.09000 40.73328 32.05812 52.33482 37.55686 61.52666 28.17045
## 2010-01-06 87.15132 40.48759 32.12519 52.28558 37.71561 60.70302 28.15820
## 2010-01-07 87.51920 40.51390 31.93890 52.67135 37.57009 60.80507 28.40972
## 2010-01-08 87.81048 40.84734 32.19226 52.95864 37.86774 60.77789 28.21954
## 2010-01-11 87.93306 40.68062 32.12519 52.74524 38.17862 60.44438 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
tail(ETFList)
##               SPY    QQQ   EEM    IWM   EFA   TLT   IYR    GLD
## 2024-04-05 518.43 440.47 41.25 204.45 78.97 91.39 87.19 215.14
## 2024-04-08 518.72 440.60 41.52 205.57 79.36 91.38 88.17 216.48
## 2024-04-09 519.32 442.23 41.80 206.38 79.35 92.23 89.23 217.67
## 2024-04-10 514.12 438.37 41.23 200.93 78.30 90.22 85.61 215.61
## 2024-04-11 518.00 445.37 41.48 202.29 78.51 89.81 85.71 219.80
## 2024-04-12 510.85 438.27 40.53 198.69 77.24 90.29 84.80 216.89
ETFList.xts <- xts(ETFList)
head(ETFList.xts)
##                 SPY      QQQ      EEM      IWM      EFA      TLT      IYR
## 2010-01-04 86.86007 40.73328 31.82711 52.51541 37.52378 61.13187 28.10299
## 2010-01-05 87.09000 40.73328 32.05812 52.33482 37.55686 61.52666 28.17045
## 2010-01-06 87.15132 40.48759 32.12519 52.28558 37.71561 60.70302 28.15820
## 2010-01-07 87.51920 40.51390 31.93890 52.67135 37.57009 60.80507 28.40972
## 2010-01-08 87.81048 40.84734 32.19226 52.95864 37.86774 60.77789 28.21954
## 2010-01-11 87.93306 40.68062 32.12519 52.74524 38.17862 60.44438 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
ETFList.weekly <- to.weekly(ETFList.xts, indexAt = "last", OHLC = FALSE)
ETFList.weekly
##                  SPY       QQQ      EEM       IWM      EFA      TLT      IYR
## 2010-01-08  87.81048  40.84734 32.19226  52.95864 37.86774 60.77789 28.21954
## 2010-01-15  87.09767  40.23310 31.26077  52.26917 37.73545 61.99628 28.04163
## 2010-01-22  83.70237  38.75015 29.51703  50.66858 35.63206 62.62252 26.86991
## 2010-01-29  82.30745  37.54797 28.52592  49.33887 34.71264 62.83354 26.64292
## 2010-02-05  81.74796  37.71470 27.72112  48.64938 34.05119 62.83016 26.72881
## 2010-02-12  82.80561  38.39914 28.64514  50.08581 34.22979 61.60742 26.52636
## 2010-02-19  85.18156  39.33806 29.34563  51.76027 35.01691 61.10188 27.85760
## 2010-02-26  84.87502  39.27665 29.03264  51.54685 34.80524 62.61840 28.09685
## 2010-03-05  87.56520  40.75082 30.51558  54.68234 36.36626 61.85772 29.10293
## 2010-03-12  88.49253  41.55813 30.82856  55.58523 36.81604 62.00165 30.09062
##        ...                                                                  
## 2024-02-09 499.64758 436.47284 39.58000 198.83511 75.30000 93.24754 86.81923
## 2024-02-16 497.96283 430.00143 40.08000 201.14925 76.07000 92.16454 86.76942
## 2024-02-23 506.27698 436.20322 40.60000 199.38373 77.40000 93.26742 87.43687
## 2024-03-01 511.26147 445.02155 40.48000 205.36853 78.00000 94.15850 89.22008
## 2024-03-08 510.13498 438.44025 40.82000 206.30615 79.25000 95.41434 90.50518
## 2024-03-15 509.82999 433.34702 40.77000 201.89734 78.89000 92.63354 87.90509
## 2024-03-22 521.21002 446.38000 40.86000 205.07001 79.64000 93.67011 87.89000
## 2024-03-28 523.07001 444.01001 41.08000 210.30000 79.86000 94.30801 89.90000
## 2024-04-05 518.42999 440.47000 41.25000 204.45000 78.97000 91.39000 87.19000
## 2024-04-12 510.85001 438.26999 40.53000 198.69000 77.24000 90.29000 84.80000
##               GLD
## 2010-01-08 111.37
## 2010-01-15 110.86
## 2010-01-22 107.17
## 2010-01-29 105.96
## 2010-02-05 104.68
## 2010-02-12 107.04
## 2010-02-19 109.47
## 2010-02-26 109.43
## 2010-03-05 110.81
## 2010-03-12 107.95
##        ...       
## 2024-02-09 187.60
## 2024-02-16 186.34
## 2024-02-23 188.62
## 2024-03-01 192.89
## 2024-03-08 201.63
## 2024-03-15 199.71
## 2024-03-22 200.35
## 2024-03-28 205.72
## 2024-04-05 215.14
## 2024-04-12 216.89
ETFList.monthly <- to.monthly(ETFList.xts, indexAt = "last", OHLC = FALSE)
ETFList.monthly
##                  SPY       QQQ      EEM       IWM      EFA      TLT      IYR
## 2010-01-29  82.30745  37.54797 28.52592  49.33887 34.71264 62.83354 26.64292
## 2010-02-26  84.87502  39.27665 29.03264  51.54685 34.80524 62.61840 28.09685
## 2010-03-31  90.04217  42.30522 31.38745  55.78952 37.02770 61.33009 30.83586
## 2010-04-30  91.43510  43.25394 31.33529  58.95751 35.98922 63.36732 32.80568
## 2010-05-28  84.17016  40.05643 28.39178  54.51410 31.96103 66.60441 30.94116
## 2010-06-30  79.81512  37.66281 27.99468  50.29283 31.30201 70.46602 29.49618
## 2010-07-30  85.26654  40.39647 31.05519  53.67800 34.93630 69.79914 32.27023
## 2010-08-31  81.43121  38.32416 30.05003  49.68230 33.61046 75.65596 31.85163
## 2010-09-30  88.72378  43.37254 33.58312  55.86542 36.96209 73.74915 33.32626
## 2010-10-29  92.11320  46.12146 34.59578  58.18277 38.36869 70.45306 34.63712
##        ...                                                                  
## 2023-07-31 452.93155 381.65601 41.17138 196.56015 73.53755 97.25170 86.00313
## 2023-08-31 445.57050 375.99603 38.44297 186.56940 70.64384 94.19598 83.35612
## 2023-09-29 424.43509 356.89590 37.24562 175.64836 68.06618 86.70633 77.19147
## 2023-10-31 415.22119 349.52432 36.01882 163.50385 66.09096 81.96941 74.41558
## 2023-11-30 453.14908 387.33871 38.82574 178.55034 71.52282 90.10395 83.56317
## 2023-12-29 473.83777 408.97919 40.21000 200.20164 75.35000 97.93017 91.06306
## 2024-01-31 481.38434 416.41937 38.39000 192.39148 75.01000 95.73149 86.42075
## 2024-02-29 506.50626 438.42029 39.99000 203.21399 77.25000 93.57542 88.26373
## 2024-03-28 523.07001 444.01001 41.08000 210.30000 79.86000 94.30801 89.90000
## 2024-04-12 510.85001 438.26999 40.53000 198.69000 77.24000 90.29000 84.80000
##               GLD
## 2010-01-29 105.96
## 2010-02-26 109.43
## 2010-03-31 108.95
## 2010-04-30 115.36
## 2010-05-28 118.88
## 2010-06-30 121.68
## 2010-07-30 115.49
## 2010-08-31 122.08
## 2010-09-30 127.91
## 2010-10-29 132.62
##        ...       
## 2023-07-31 182.35
## 2023-08-31 180.02
## 2023-09-29 171.45
## 2023-10-31 184.09
## 2023-11-30 188.75
## 2023-12-29 191.17
## 2024-01-31 188.45
## 2024-02-29 189.31
## 2024-03-28 205.72
## 2024-04-12 216.89
ETFList.price <- cumprod(ETFList.monthly+1)*100
head(ETFList.price)
##                     SPY          QQQ          EEM          IWM          EFA
## 2010-01-29 8.330745e+03 3.854797e+03 2.952592e+03 5.033887e+03 3.571264e+03
## 2010-02-26 7.154029e+05 1.552583e+05 8.867413e+04 2.645149e+05 1.278700e+05
## 2010-03-31 6.513183e+07 6.723495e+06 2.871929e+06 1.502167e+07 4.862601e+06
## 2010-04-30 6.020468e+09 2.975412e+08 9.286467e+07 9.006621e+08 1.798638e+08
## 2010-05-28 5.127642e+11 1.221598e+10 2.729458e+09 4.999945e+10 5.928497e+09
## 2010-06-30 4.143910e+13 4.723041e+11 7.913977e+10 2.564613e+12 1.915024e+11
##                     TLT          IYR          GLD
## 2010-01-29 6.383354e+03 2.764292e+03 1.069600e+04
## 2010-02-26 4.060988e+05 8.043218e+04 1.181159e+06
## 2010-03-31 2.531218e+07 2.560628e+06 1.298685e+08
## 2010-04-30 1.629277e+09 8.656378e+07 1.511149e+10
## 2010-05-28 1.101463e+11 2.764947e+09 1.811566e+12
## 2010-06-30 7.871717e+12 8.432032e+10 2.222429e+14
tail(ETFList.price)
##            SPY QQQ           EEM IWM           EFA TLT           IYR GLD
## 2023-11-30 Inf Inf 7.698268e+262 Inf 4.006442e+288 Inf 6.428231e+299 Inf
## 2023-12-29 Inf Inf 3.172456e+264 Inf 3.058919e+290 Inf 5.918026e+301 Inf
## 2024-01-31 Inf Inf 1.249630e+266 Inf 2.325084e+292 Inf 5.173583e+303 Inf
## 2024-02-29 Inf Inf 5.122235e+267 Inf 1.819378e+294 Inf 4.618133e+305 Inf
## 2024-03-28 Inf Inf 2.155437e+269 Inf 1.471149e+296 Inf 4.197883e+307 Inf
## 2024-04-12 Inf Inf 8.951528e+270 Inf 1.151027e+298 Inf           Inf Inf
ETFList.price.sample <- ETFList.price['201001/202105']
ETFList.price.sample
##                      SPY           QQQ           EEM           IWM
## 2010-01-29  8.330745e+03  3.854797e+03  2.952592e+03  5.033887e+03
## 2010-02-26  7.154029e+05  1.552583e+05  8.867413e+04  2.645149e+05
## 2010-03-31  6.513183e+07  6.723495e+06  2.871929e+06  1.502167e+07
## 2010-04-30  6.020468e+09  2.975412e+08  9.286467e+07  9.006621e+08
## 2010-05-28  5.127642e+11  1.221598e+10  2.729458e+09  4.999945e+10
## 2010-06-30  4.143910e+13  4.723041e+11  7.913977e+10  2.564613e+12
## 2010-07-30  3.574808e+15  1.955172e+13  2.536840e+12  1.402279e+14
## 2010-08-31  2.946758e+17  7.688550e+14  7.876896e+13  7.107074e+15
## 2010-09-30  2.643942e+19  3.411605e+16  2.724076e+15  4.041467e+17
## 2010-10-29  2.461859e+21  1.607598e+18  9.696562e+16  2.391852e+19
##        ...                                                        
## 2020-08-31 1.742978e+286 2.982294e+255 1.374231e+199 3.070988e+257
## 2020-09-30 5.559965e+288 8.134177e+257 5.733166e+200 4.431681e+259
## 2020-10-30 1.729503e+291 2.151282e+260 2.424653e+202 6.535184e+261
## 2020-11-30 5.963178e+293 6.325879e+262 1.115446e+204 1.138341e+264
## 2020-12-31 2.132004e+296 1.951016e+265 5.489434e+205 2.153297e+266
## 2021-01-29 7.545059e+298 6.032965e+267 2.785514e+207 4.269520e+268
## 2021-02-26 2.744195e+301 1.863041e+270 1.424375e+209 8.987962e+270
## 2021-03-31 1.043272e+304 5.851710e+272 7.231720e+210 1.918367e+273
## 2021-04-30 4.175555e+306 1.946267e+275 3.714816e+212 4.167372e+275
## 2021-05-28           Inf 6.395716e+277 1.939090e+214 9.077447e+277
##                      EFA           TLT           IYR           GLD
## 2010-01-29  3.571264e+03  6.383354e+03  2.764292e+03  1.069600e+04
## 2010-02-26  1.278700e+05  4.060988e+05  8.043218e+04  1.181159e+06
## 2010-03-31  4.862601e+06  2.531218e+07  2.560628e+06  1.298685e+08
## 2010-04-30  1.798638e+08  1.629277e+09  8.656378e+07  1.511149e+10
## 2010-05-28  5.928497e+09  1.101463e+11  2.764947e+09  1.811566e+12
## 2010-06-30  1.915024e+11  7.871717e+12  8.432032e+10  2.222429e+14
## 2010-07-30  6.881887e+12  5.573108e+14  2.805357e+12  2.588907e+16
## 2010-08-31  2.381853e+14  4.272120e+16  9.216054e+13  3.186427e+18
## 2010-09-30  9.042010e+15  3.193373e+18  3.163527e+15  4.107624e+20
## 2010-10-29  3.559721e+17  2.281763e+20  1.127390e+17  5.488607e+22
##        ...                                                        
## 2020-08-31 1.069979e+217 2.825080e+256 8.236426e+223 1.319472e+273
## 2020-09-30 6.275076e+218 4.258063e+258 6.085276e+225 2.350243e+275
## 2020-10-30 3.551684e+220 6.202088e+260 4.366523e+227 4.164631e+277
## 2020-11-30 2.292031e+222 9.182940e+262 3.398292e+229 6.982837e+279
## 2020-12-31 1.552166e+224 1.343066e+265 2.710115e+231 1.252442e+282
## 2021-01-29 1.043038e+226 1.893452e+267 2.152078e+233 2.174364e+284
## 2021-02-26 7.163617e+227 2.517436e+269 1.749905e+235 3.540082e+286
## 2021-03-31 5.041841e+229 3.172812e+271 1.504037e+237 5.698116e+288
## 2021-04-30 3.651788e+231 4.097788e+273 1.393466e+239 9.496480e+290
## 2021-05-28 2.735811e+233 5.292536e+275 1.303898e+241 1.703479e+293
ETF.monthly.returns <- na.omit(Return.calculate(ETFList.monthly, method = "log"))
head(ETF.monthly.returns)
##                    SPY         QQQ         EEM         IWM          EFA
## 2010-02-26  0.03071824  0.04501077  0.01760775  0.04377899  0.002664006
## 2010-03-31  0.05909823  0.07428044  0.07798737  0.07909483  0.061898376
## 2010-04-30  0.01535139  0.02217791 -0.00166308  0.05523101 -0.028447010
## 2010-05-28 -0.08278903 -0.07679910 -0.09864533 -0.07835761 -0.118701956
## 2010-06-30 -0.05312743 -0.06161626 -0.01408533 -0.08059682 -0.020834957
## 2010-07-30  0.06606910  0.07006931  0.10375148  0.06514071  0.109844045
##                     TLT         IYR          GLD
## 2010-02-26 -0.003429802  0.05313409  0.032223422
## 2010-03-31 -0.020788655  0.09302095 -0.004396045
## 2010-04-30  0.032677647  0.06192337  0.057168645
## 2010-05-28  0.049822539 -0.05851442  0.030056879
## 2010-06-30  0.056359824 -0.04782672  0.023280093
## 2010-07-30 -0.009508887  0.08988461 -0.052210723
tail(ETF.monthly.returns)
##                    SPY         QQQ         EEM         IWM          EFA
## 2023-11-30  0.08740979  0.10272639  0.07504172  0.08803406  0.078984614
## 2023-12-29  0.04464384  0.05436473  0.03503235  0.11445451  0.052127352
## 2024-01-31  0.01580099  0.01802859 -0.04631871 -0.03979283 -0.004522438
## 2024-02-29  0.05087068  0.05148515  0.04083247  0.05472731  0.029425449
## 2024-03-28  0.03217864  0.01266909  0.02689196  0.03427554  0.033228193
## 2024-04-12 -0.02363930 -0.01301197 -0.01347902 -0.05678926 -0.033357679
##                     TLT         IYR          GLD
## 2023-11-30  0.094617835  0.11593755  0.024998640
## 2023-12-29  0.083290645  0.08594939  0.012739687
## 2024-01-31 -0.022707350 -0.05232452 -0.014330371
## 2024-02-29 -0.022779518  0.02110145  0.004553167
## 2024-03-28  0.007798301  0.01836875  0.083130157
## 2024-04-12 -0.043539366 -0.05840238  0.052874283
ETF.monthly.returns.tibble <- as_tibble(ETF.monthly.returns)
ETF.monthly.returns.tibble
## # A tibble: 171 × 8
##        SPY      QQQ      EEM     IWM      EFA      TLT     IYR      GLD
##      <dbl>    <dbl>    <dbl>   <dbl>    <dbl>    <dbl>   <dbl>    <dbl>
##  1  0.0307  0.0450   0.0176   0.0438  0.00266 -0.00343  0.0531  0.0322 
##  2  0.0591  0.0743   0.0780   0.0791  0.0619  -0.0208   0.0930 -0.00440
##  3  0.0154  0.0222  -0.00166  0.0552 -0.0284   0.0327   0.0619  0.0572 
##  4 -0.0828 -0.0768  -0.0986  -0.0784 -0.119    0.0498  -0.0585  0.0301 
##  5 -0.0531 -0.0616  -0.0141  -0.0806 -0.0208   0.0564  -0.0478  0.0233 
##  6  0.0661  0.0701   0.104    0.0651  0.110   -0.00951  0.0899 -0.0522 
##  7 -0.0460 -0.0527  -0.0329  -0.0774 -0.0387   0.0806  -0.0131  0.0555 
##  8  0.0858  0.124    0.111    0.117   0.0951  -0.0255   0.0453  0.0467 
##  9  0.0375  0.0615   0.0297   0.0406  0.0373  -0.0457   0.0386  0.0362 
## 10  0      -0.00173 -0.0295   0.0343 -0.0494  -0.0170  -0.0160  0.0209 
## # ℹ 161 more rows
setwd("C:/Users/daavka/Documents")
famafrench <- read.csv("C:/Users/daavka/Downloads/F-F_Research_Data_Factors_CSV.zip",  header = TRUE)
## Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
## line 1 appears to contain embedded nulls
## Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
## incomplete final line found by readTableHeader on
## 'C:/Users/daavka/Downloads/F-F_Research_Data_Factors_CSV.zip'
str(famafrench)
## 'data.frame':    0 obs. of  1 variable:
##  $ PK...: logi
head(famafrench)
## [1] PK...
## <0 rows> (or 0-length row.names)
colnames<- paste(c("date","Mkt-RF","SMB","HML","RF"))

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.