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:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

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.

#install.packages("quantmod")
# Import data from yahoo finance
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
## Version 0.4-0 included new data defaults. See ?getSymbols.
tickers = c("PEP", "AAPL", "SNE")
getSymbols(tickers, from = "2007-01-01", to= "2018-01-01", auto.assign = TRUE)
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "PEP"  "AAPL" "SNE"
data = new.env()
getSymbols(tickers, from = "2007-01-01", env = data , auto.assign = TRUE)
## [1] "PEP"  "AAPL" "SNE"
ls(data)
## [1] "AAPL" "PEP"  "SNE"
names(data)
## [1] "PEP"         "SNE"         "AAPL"        ".getSymbols"
head(data$PEP)
##            PEP.Open PEP.High PEP.Low PEP.Close PEP.Volume PEP.Adjusted
## 2007-01-03    62.70    63.35   62.45     62.72    6161600     43.76391
## 2007-01-04    62.70    63.25   62.50     63.15    5414300     44.06393
## 2007-01-05    62.70    63.22   62.70     62.95    4542400     43.92438
## 2007-01-08    63.00    63.27   62.86     63.09    6122600     44.02206
## 2007-01-09    63.05    63.47   63.01     63.35    4916500     44.20349
## 2007-01-10    63.00    64.29   62.29     64.15    6427100     44.76170
#
str(PEP)
## An 'xts' object on 2007-01-03/2017-12-29 containing:
##   Data: num [1:2769, 1:6] 62.7 62.7 62.7 63 63 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "PEP.Open" "PEP.High" "PEP.Low" "PEP.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2019-04-03 22:34:39"
class(PEP)
## [1] "xts" "zoo"
tail(PEP,3)
##            PEP.Open PEP.High PEP.Low PEP.Close PEP.Volume PEP.Adjusted
## 2017-12-27   118.87   119.40  118.80    119.30    2465100     114.5373
## 2017-12-28   119.33   119.53  119.05    119.35    1999300     114.5853
## 2017-12-29   119.46   120.57  119.42    119.92    3116400     115.1325
names(PEP)
## [1] "PEP.Open"     "PEP.High"     "PEP.Low"      "PEP.Close"   
## [5] "PEP.Volume"   "PEP.Adjusted"
ls(PEP)
## [1] "PEP.Adjusted" "PEP.Close"    "PEP.High"     "PEP.Low"     
## [5] "PEP.Open"     "PEP.Volume"
head(PEP$PEP.Close)
##            PEP.Close
## 2007-01-03     62.72
## 2007-01-04     63.15
## 2007-01-05     62.95
## 2007-01-08     63.09
## 2007-01-09     63.35
## 2007-01-10     64.15
PEP2007_15=PEP['2007/2018']
#Extract closing price: Cl(), 
# Adjusted price: Ad()
PEP.ad<-Ad(PEP)
head(PEP.ad)
##            PEP.Adjusted
## 2007-01-03     43.76391
## 2007-01-04     44.06393
## 2007-01-05     43.92438
## 2007-01-08     44.02206
## 2007-01-09     44.20349
## 2007-01-10     44.76170
class(PEP.ad)
## [1] "xts" "zoo"
# write function to gather adjusted prices together
firm3<-merge(Ad(PEP), Ad(AAPL), Ad(SNE))
head(firm3)
##            PEP.Adjusted AAPL.Adjusted SNE.Adjusted
## 2007-01-03     43.76391      7.982585     38.33660
## 2007-01-04     44.06393      8.159763     39.13174
## 2007-01-05     43.92438      8.101658     40.02516
## 2007-01-08     44.02206      8.141665     40.03409
## 2007-01-09     44.20349      8.817995     41.45463
## 2007-01-10     44.76170      9.239983     40.90070
colnames(firm3)<-c("PEP", "AAPL", "SNE")
head(firm3)
##                 PEP     AAPL      SNE
## 2007-01-03 43.76391 7.982585 38.33660
## 2007-01-04 44.06393 8.159763 39.13174
## 2007-01-05 43.92438 8.101658 40.02516
## 2007-01-08 44.02206 8.141665 40.03409
## 2007-01-09 44.20349 8.817995 41.45463
## 2007-01-10 44.76170 9.239983 40.90070
library(magrittr)
simple_returns01<-function(x) {
  coredata(x[-1,])/coredata(x[-length(x),]) - 1 %>% 
    na.omit()
}
head(simple_returns01(PEP.ad))
##      PEP.Adjusted
## [1,]  0.006855558
## [2,] -0.003167080
## [3,]  0.002223981
## [4,]  0.004121183
## [5,]  0.012628325
## [6,]  0.012470348
# or to keep the index of dates
simple_returns02<-function(x) {
  na.omit(x/lag(x) - 1)
}
head(simple_returns02(PEP.ad))
##            PEP.Adjusted
## 2007-01-04  0.006855558
## 2007-01-05 -0.003167080
## 2007-01-08  0.002223981
## 2007-01-09  0.004121183
## 2007-01-10  0.012628325
## 2007-01-11  0.012470348
#
#install.packages("PerformanceAnalytics")
library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
head(na.omit(Return.calculate(firm3)))
##                     PEP         AAPL           SNE
## 2007-01-04  0.006855558  0.022195567  0.0207411993
## 2007-01-05 -0.003167080 -0.007120918  0.0228310295
## 2007-01-08  0.002223981  0.004938125  0.0002231097
## 2007-01-09  0.004121183  0.083070232  0.0354831577
## 2007-01-10  0.012628325  0.047855323 -0.0133621993
## 2007-01-11  0.012470348 -0.012371127 -0.0028396089
firm3.week<-firm3 %>% to.weekly(indexAt = "lastof", OHLC=FALSE) %>% 
  Return.calculate()
head(firm3.week)
##                     PEP         AAPL          SNE
## 2007-01-05           NA           NA           NA
## 2007-01-12  0.027799962  0.112521536  0.064285561
## 2007-01-19  0.001854649 -0.064679530 -0.013003295
## 2007-01-26 -0.005707795 -0.035254045  0.016362248
## 2007-02-02  0.009154046 -0.007378989 -0.004599995
## 2007-02-09 -0.017373753 -0.017463226  0.031296167
firm3.day<-firm3 %>% to.daily(indexAt = "lastof", OHLC=FALSE) %>% 
  Return.calculate()
head(firm3.day)
##                     PEP         AAPL           SNE
## 2007-01-31           NA           NA            NA
## 2007-01-31  0.006855558  0.022195567  0.0207411993
## 2007-01-31 -0.003167080 -0.007120918  0.0228310295
## 2007-01-31  0.002223981  0.004938125  0.0002231097
## 2007-01-31  0.004121183  0.083070232  0.0354831577
## 2007-01-31  0.012628325  0.047855323 -0.0133621993
# Main problem: the return series are shown in calendar dates, 
# not in trading dates
# Therefore, we use package PMwR instead!
#install.packages("PMwR")
library(PMwR)
head(returns(firm3))
##                     PEP         AAPL           SNE
## 2007-01-04  0.006855558  0.022195567  0.0207411993
## 2007-01-05 -0.003167080 -0.007120918  0.0228310295
## 2007-01-08  0.002223981  0.004938125  0.0002231097
## 2007-01-09  0.004121183  0.083070232  0.0354831577
## 2007-01-10  0.012628325  0.047855323 -0.0133621993
## 2007-01-11  0.012470348 -0.012371127 -0.0028396089
firm3.mon1<-returns(firm3, period = "month")
#options(digits = 5)
head(firm3.mon1)
## [1]  0.04017827 -0.03203558  0.01128819  0.03980503  0.03389308 -0.04563658
firm3.mon1
##             PEP    AAPL   SNE  
## 2007-01-31    4.0    2.3    8.0
## 2007-02-28   -3.2   -1.3   11.7
## 2007-03-30    1.1    9.8   -2.2
## 2007-04-30    4.0    7.4    5.5
## 2007-05-31    3.4   21.4    8.3
## 2007-06-29   -4.6    0.7  -11.0
## 2007-07-31    1.2    8.0    2.7
## 2007-08-31    3.7    5.1   -9.4
## 2007-09-28    8.3   10.8    0.8
## 2007-10-31    0.6   23.8    2.9
## 2007-11-30    4.7   -4.1    9.1
## 2007-12-31   -1.2    8.7    0.6
## 2008-01-31  -10.3  -31.7  -12.5
## 2008-02-29    2.2   -7.6   -0.6
## 2008-03-31    4.4   14.8  -14.9
## 2008-04-30   -5.1   21.2   14.3
## 2008-05-30   -0.3    8.5   10.0
## 2008-06-30   -6.3  -11.3  -13.2
## 2008-07-31    4.7   -5.1  -13.9
## 2008-08-29    2.9    6.7    1.4
## 2008-09-30    4.7  -33.0  -18.6
## 2008-10-31  -20.0   -5.3  -24.7
## 2008-11-28   -0.5  -13.9  -16.6
## 2008-12-31   -2.6   -7.9   12.8
## 2009-01-30   -8.3    5.6  -12.1
## 2009-02-27   -4.2   -0.9  -13.8
## 2009-03-31    7.9   17.7   25.3
## 2009-04-30   -3.3   19.7   25.4
## 2009-05-29    4.6    7.9    1.4
## 2009-06-30    6.5    4.9   -1.4
## 2009-07-31    3.3   14.7    8.1
## 2009-08-31   -0.1    3.0   -4.4
## 2009-09-30    4.3   10.2    9.7
## 2009-10-30    3.2    1.7    0.7
## 2009-11-30    2.8    6.1   -9.2
## 2009-12-31   -1.6    5.4    8.7
## 2010-01-29   -1.9   -8.9   14.6
## 2010-02-26    4.8    6.5    2.6
## 2010-03-31    6.7   14.8   12.7
## 2010-04-30   -1.4   11.1  -10.7
## 2010-05-28   -3.6   -1.6  -10.1
## 2010-06-30   -2.3   -2.1  -13.3
## 2010-07-30    6.5    2.3   17.0
## 2010-08-31   -1.1   -5.5  -10.3
## 2010-09-30    5.1   16.7   11.1
## 2010-10-29   -1.7    6.1    9.4
## 2010-11-30   -1.0    3.4    4.8
## 2010-12-31    1.8    3.7    0.6
## 2011-01-31   -1.6    5.2   -3.8
## 2011-02-28   -1.4    4.1    7.3
## 2011-03-31    2.3   -1.3  -13.1
## 2011-04-29    7.0    0.5  -11.1
## 2011-05-31    3.2   -0.7   -5.5
## 2011-06-30   -0.2   -3.5   -1.3
## 2011-07-29   -9.1   16.3   -4.9
## 2011-08-31    1.4   -1.4  -12.5
## 2011-09-30   -3.9   -0.9  -12.7
## 2011-10-31    1.7    6.2   10.4
## 2011-11-30    2.5   -5.6  -13.9
## 2011-12-30    3.7    6.0   -0.1
## 2012-01-31   -1.0   12.7    1.0
## 2012-02-29   -3.4   18.8   17.4
## 2012-03-30    5.4   10.5   -2.0
## 2012-04-30   -0.5   -2.6  -22.0
## 2012-05-31    3.6   -1.1  -18.3
## 2012-06-29    4.1    1.1    7.6
## 2012-07-31    2.9    4.6  -14.7
## 2012-08-31   -0.4   12.3   -6.9
## 2012-09-28   -1.6    0.3    4.7
## 2012-10-31   -2.2  -10.8    0.3
## 2012-11-30    1.4    1.5  -17.0
## 2012-12-31   -1.8   -9.1   15.0
## 2013-01-31    6.5  -14.4   33.4
## 2013-02-28    4.8    1.0   -2.4
## 2013-03-28    4.4    0.3   20.4
## 2013-04-30    4.2    0.0   -5.6
## 2013-05-31   -2.1    6.5   22.6
## 2013-06-28    2.0  -11.8    5.2
## 2013-07-31    2.1   14.1   -0.7
## 2013-08-30   -4.6   12.8   -5.1
## 2013-09-30    0.4   -2.1    8.5
## 2013-10-31    5.8    9.6  -19.8
## 2013-11-29    0.4   10.9    6.1
## 2013-12-31   -1.1    0.9   -5.5
## 2014-01-31   -3.1  -10.8   -8.9
## 2014-02-28   -0.4    9.7   11.4
## 2014-03-31    5.0    2.0    9.7
## 2014-04-30    2.9    9.9   -7.7
## 2014-05-30    2.8   11.6   -8.3
## 2014-06-30    1.9    2.8    3.6
## 2014-07-31   -1.4    2.9    9.9
## 2014-08-29    5.0    7.8    3.7
## 2014-09-30    1.4   -1.7   -5.6
## 2014-10-31    3.3    7.2    9.9
## 2014-11-28    4.1   10.6   10.9
## 2014-12-31   -4.9   -7.2   -6.9
## 2015-01-30   -0.8    6.1   13.8
## 2015-02-27    5.5   10.1   21.6
## 2015-03-31   -2.7   -3.1   -5.4
## 2015-04-30   -0.5    0.6   12.9
## 2015-05-29    1.4    4.5    2.3
## 2015-06-30   -2.5   -3.7   -8.2
## 2015-07-31    3.2   -3.3   -0.1
## 2015-08-31   -3.5   -6.6   -9.1
## 2015-09-30    2.3   -2.2   -4.7
## 2015-10-30    8.4    8.3   15.9
## 2015-11-30   -2.0   -0.6   -8.7
## 2015-12-31    0.5  -11.0   -5.1
## 2016-01-29   -0.6   -7.5   -3.0
## 2016-02-29   -1.5   -0.1  -11.6
## 2016-03-31    5.5   12.7   22.2
## 2016-04-29    0.5  -14.0   -6.3
## 2016-05-31   -1.7    7.2   15.6
## 2016-06-30    5.5   -4.3    5.3
## 2016-07-29    2.8    9.0   13.8
## 2016-08-31   -1.3    2.4   -3.7
## 2016-09-30    1.9    6.6    3.5
## 2016-10-31   -1.4    0.4   -5.7
## 2016-11-30   -5.9   -2.2   -7.2
## 2016-12-30    4.5    4.8   -3.6
## 2017-01-31   -0.8    4.8    8.0
## 2017-02-28    6.4   13.4    2.3
## 2017-03-31    2.0    4.9    9.3
## 2017-04-28    1.3    0.0    2.3
## 2017-05-31    3.9    6.8    6.1
## 2017-06-30   -1.2   -5.7    4.3
## 2017-07-31    1.0    3.3    7.5
## 2017-08-31   -0.1   10.7   -3.5
## 2017-09-29   -3.7   -6.0   -5.5
## 2017-10-31   -1.1    9.7   16.3
## 2017-11-30    6.4    2.0    7.8
## 2017-12-29    2.9   -1.5   -4.0
firm3.day<-returns(firm3)
head(firm3.day)
##                     PEP         AAPL           SNE
## 2007-01-04  0.006855558  0.022195567  0.0207411993
## 2007-01-05 -0.003167080 -0.007120918  0.0228310295
## 2007-01-08  0.002223981  0.004938125  0.0002231097
## 2007-01-09  0.004121183  0.083070232  0.0354831577
## 2007-01-10  0.012628325  0.047855323 -0.0133621993
## 2007-01-11  0.012470348 -0.012371127 -0.0028396089
head(merge(firm3, firm3.day))
##                 PEP     AAPL      SNE        PEP.1       AAPL.1
## 2007-01-03 43.76391 7.982585 38.33660           NA           NA
## 2007-01-04 44.06393 8.159763 39.13174  0.006855558  0.022195567
## 2007-01-05 43.92438 8.101658 40.02516 -0.003167080 -0.007120918
## 2007-01-08 44.02206 8.141665 40.03409  0.002223981  0.004938125
## 2007-01-09 44.20349 8.817995 41.45463  0.004121183  0.083070232
## 2007-01-10 44.76170 9.239983 40.90070  0.012628325  0.047855323
##                    SNE.1
## 2007-01-03            NA
## 2007-01-04  0.0207411993
## 2007-01-05  0.0228310295
## 2007-01-08  0.0002231097
## 2007-01-09  0.0354831577
## 2007-01-10 -0.0133621993
head(100* cumprod(1+firm3.day))
##                 PEP     AAPL      SNE
## 2007-01-04 100.6856 102.2196 102.0741
## 2007-01-05 100.3667 101.4917 104.4046
## 2007-01-08 100.5899 101.9928 104.4279
## 2007-01-09 101.0044 110.4654 108.1333
## 2007-01-10 102.2800 115.7518 106.6884
## 2007-01-11 103.5554 114.3198 106.3854
firm3.p.ret<-firm3.day %>% merge(firm3) 
head(firm3.p.ret)
##                   PEP..       AAPL..         SNE.. PEP.firm3 AAPL.firm3
## 2007-01-03           NA           NA            NA  43.76391   7.982585
## 2007-01-04  0.006855558  0.022195567  0.0207411993  44.06393   8.159763
## 2007-01-05 -0.003167080 -0.007120918  0.0228310295  43.92438   8.101658
## 2007-01-08  0.002223981  0.004938125  0.0002231097  44.02206   8.141665
## 2007-01-09  0.004121183  0.083070232  0.0354831577  44.20349   8.817995
## 2007-01-10  0.012628325  0.047855323 -0.0133621993  44.76170   9.239983
##            SNE.firm3
## 2007-01-03  38.33660
## 2007-01-04  39.13174
## 2007-01-05  40.02516
## 2007-01-08  40.03409
## 2007-01-09  41.45463
## 2007-01-10  40.90070