1. Import data

# Load libraries
library(tidyquant)
library(lubridate)
library(timetk)
library(purrr)
#Define the symbols we want to take 
symbols <- c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")

#Extract data from yahoo finance and feed the data in prices
prices <- 
  getSymbols(symbols, src = 'yahoo', from = "2010-01-01", 
             auto.assign = TRUE, warnings = FALSE) %>% 
  map(~Ad(get(.))) %>% 
  reduce(merge) %>%
  `colnames<-`(symbols)
#Show the data prices
head(prices)
##                 SPY      QQQ      EEM      IWM      EFA      TLT      IYR
## 2010-01-04 88.11787 41.00547 32.42901 53.08978 37.99448 63.06117 28.77184
## 2010-01-05 88.35115 41.00547 32.66441 52.90722 38.02797 63.46844 28.84092
## 2010-01-06 88.41335 40.75813 32.73273 52.85745 38.18871 62.61877 28.82836
## 2010-01-07 88.78660 40.78463 32.54292 53.24745 38.04136 62.72415 29.08587
## 2010-01-08 89.08205 41.12032 32.80108 53.53788 38.34275 62.69608 28.89116
## 2010-01-11 89.20643 40.95248 32.73273 53.32213 38.65753 62.35197 29.02934
##               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.Calculate weekly and monthly returns using log returns

#Change data from daily to weekly
prices_weekly <- to.weekly(prices, indexAt = "last", OHLC = FALSE)

#Change data from daily to monthly
prices_monthly <- to.monthly(prices, indexAt = "last", OHLC = FALSE)

#Calculate weekly retun
asset_returns_wk_xts <- na.omit(Return.calculate(prices_weekly))

#Calculate monthly return
asset_returns_mon_xts <- na.omit(Return.calculate(prices_monthly))
#Monthly return
head(asset_returns_mon_xts)
##                    SPY         QQQ          EEM         IWM          EFA
## 2010-02-26  0.03119479  0.04603912  0.017764120  0.04475106  0.002667612
## 2010-03-31  0.06087978  0.07710910  0.081108163  0.08230686  0.063854308
## 2010-04-30  0.01547009  0.02242541 -0.001661391  0.05678471 -0.028046007
## 2010-05-28 -0.07945479 -0.07392359 -0.093936013 -0.07536612 -0.111928087
## 2010-06-30 -0.05174064 -0.05975711 -0.013986237 -0.07743407 -0.020619288
## 2010-07-30  0.06830041  0.07258269  0.109324578  0.06730896  0.116103977
##                     TLT         IYR          GLD
## 2010-02-26 -0.003424463  0.05457117  0.032748219
## 2010-03-31 -0.020573443  0.09748428 -0.004386396
## 2010-04-30  0.033217323  0.06388123  0.058834363
## 2010-05-28  0.051083883 -0.05683516  0.030513147
## 2010-06-30  0.057978208 -0.04670117  0.023553189
## 2010-07-30 -0.009463235  0.09404778 -0.050871157
#Weekly return
head(asset_returns_wk_xts)
##                     SPY          QQQ         EEM         IWM          EFA
## 2010-01-15 -0.008117736 -0.015037880 -0.02893568 -0.01301944 -0.003493471
## 2010-01-22 -0.038982494 -0.036859780 -0.05578038 -0.03062148 -0.055740656
## 2010-01-29 -0.016665279 -0.031023396 -0.03357737 -0.02624343 -0.025802774
## 2010-02-05 -0.006797251  0.004440723 -0.02821335 -0.01397431 -0.019055102
## 2010-02-12  0.012937772  0.018147549  0.03333364  0.02952577  0.005244799
## 2010-02-19  0.028693087  0.024451794  0.02445346  0.03343193  0.022995059
##                      TLT          IYR          GLD
## 2010-01-15  2.004642e-02 -0.006304362 -0.004579349
## 2010-01-22  1.010064e-02 -0.041785243 -0.033285246
## 2010-01-29  3.370102e-03 -0.008447773 -0.011290465
## 2010-02-05 -5.449855e-05  0.003224107 -0.012080019
## 2010-02-12 -1.946069e-02 -0.007573715  0.022544905
## 2010-02-19 -8.205366e-03  0.050184009  0.022701796

#3. Convert weekly,monthly returns into tibble format.