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:

library(pacman)
p_load(reshape2)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(dplyr)
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## 
## Attaching package: 'xts'
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
getwd()
## [1] "/cloud/project"
setwd("/cloud/project")
etfcsv <- read.table('tw_ETF_2008_2020_chinese.txt', header = TRUE)

etfcsv <- etfcsv[, -2]
colnames(etfcsv) <- c('id', 'date', 'close')

etfcsv2 <- dcast(etfcsv, date ~ id)
## Using close as value column: use value.var to override.
view(etfcsv2)
apply(is.na(etfcsv2), 2, sum)
## date   50   52   56 
##    0    0    0    0
str(etfcsv2)
## 'data.frame':    3041 obs. of  4 variables:
##  $ date: int  20080102 20080103 20080104 20080107 20080108 20080109 20080110 20080111 20080114 20080115 ...
##  $ 50  : num  39.6 39 39 37.2 37.6 ...
##  $ 52  : num  27.1 26.1 25.9 24.1 24.1 ...
##  $ 56  : num  14.6 14.4 14.4 14.2 14.4 ...
etfcsv2.xts <- xts(etfcsv2[, -1], order.by = as.Date(as.character(etfcsv2$date), format = '%Y%m%d'))

head(etfcsv2.xts)
##                 50      52      56
## 2008-01-02 39.6472 27.0983 14.5739
## 2008-01-03 38.9876 26.0676 14.3758
## 2008-01-04 38.9876 25.9346 14.4041
## 2008-01-07 37.2064 24.1391 14.1777
## 2008-01-08 37.5692 24.1391 14.3531
## 2008-01-09 38.2619 24.2721 14.4663
p_load(tidyquant)
library(tidyquant)
etfcsv2.ret <- Return.calculate(etfcsv2.xts, method = 'log') %>% 
  na.omit()
head(etfcsv2.ret)
##                      50           52           56
## 2008-01-03 -0.016776681 -0.038777831 -0.013686020
## 2008-01-04  0.000000000 -0.005115179  0.001966651
## 2008-01-07 -0.046762857 -0.071745052 -0.015842581
## 2008-01-08  0.009703776  0.000000000  0.012295639
## 2008-01-09  0.018270057  0.005494610  0.007855860
## 2008-01-10 -0.001726443  0.000000000  0.007410451
etfcsv2.w <- to.weekly(etfcsv2.xts, indexAt = "lastof", OHLC = FALSE)
## Warning in !missing(sec) && sec%%1 != 0: 'length(x) = 635 > 1' in coercion to
## 'logical(1)'
etfcsv2.m <- to.monthly(etfcsv2.xts, indexAt = "lastof", OHLC = FALSE)
head(etfcsv2.m)
##                 50      52      56
## 2008-01-31 36.2499 23.2680 12.8646
## 2008-02-29 40.1420 25.5356 14.0079
## 2008-03-31 39.8781 25.1366 14.4324
## 2008-04-30 42.1541 26.7326 14.8851
## 2008-05-31 41.0326 26.1008 14.5625
## 2008-06-30 36.2828 23.1416 12.9608
etfcsv2.ret.m <- Return.calculate(etfcsv2.m, method = 'log') %>% 
  na.omit()
head(etfcsv2.ret.m)
##                      50          52          56
## 2008-02-29  0.101986545  0.09299453  0.08514210
## 2008-03-31 -0.006595867 -0.01574861  0.02985422
## 2008-04-30  0.055504650  0.06155884  0.03088503
## 2008-05-31 -0.026965079 -0.02391783 -0.02191098
## 2008-06-30 -0.123023073 -0.12033410 -0.11652031
## 2008-07-31 -0.048424080 -0.06408000 -0.06447680
etfcsv2.ret.w <- Return.calculate(etfcsv2.w, method = 'log') %>% 
  na.omit()
head(etfcsv2.ret.w)
##                     50           52          56
## 2008-01-11 -0.02484203 -0.063789738  0.01365931
## 2008-01-18  0.01463561 -0.019593212 -0.02950189
## 2008-01-25 -0.03655644 -0.010647520 -0.05119047
## 2008-02-01  0.00000000 -0.006783635 -0.03202126
## 2008-02-15  0.01583096  0.020770112  0.01933553
## 2008-02-22  0.02584300  0.019258135  0.01268573

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.