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(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.5 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.4 ✓ stringr 1.4.0
## ✓ readr 2.0.2 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(dplyr)
library(tidyquant)
## 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: 'xts'
## The following objects are masked from 'package:dplyr':
##
## first, last
##
## 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
## ══ Need to Learn tidyquant? ════════════════════════════════════════════════════
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(timetk)
library(readr)
test_tej <- read_csv("test_tej.csv")
## Rows: 980 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): CoName
## dbl (3): CO_ID, Date, Close
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
test_tej$CO_ID<-as.character(test_tej$CO_ID)
test_tej$Date<-as.character(test_tej$Date)
test_tej$Date<-as.Date(test_tej$Date, '%Y%m%d')
test_tejin<-select(test_tej,CO_ID,Date,Close,CoName)
ret_day <- test_tejin %>% tk_xts(select = -Date, date_var = Date) %>% Return.calculate(method = "log")
## Warning: Non-numeric columns being dropped: CO_ID, CoName
head(ret_day,10)
## Close
## 2020-12-30 NA
## 2020-12-30 -1.4399463
## 2020-12-30 0.2940439
## 2020-12-30 -0.1587541
## 2020-12-31 1.3099875
## 2020-12-31 -1.4395834
## 2020-12-31 0.3033404
## 2020-12-31 -0.1756096
## 2021-01-04 1.3288841
## 2021-01-04 -1.4569473
ret_mon <- test_tej %>%tk_xts(select = -Date, date_var = Date) %>%to.period(period = "months",indexAt = "lastof",OHLC = FALSE) %>% Return.calculate(method = 'log')
## Warning: Non-numeric columns being dropped: CO_ID, CoName
head(ret_mon,10)
## Close
## 2020-12-31 NA
## 2021-01-31 -0.006868588
## 2021-02-28 0.017083814
## 2021-03-31 0.063248037
## 2021-04-30 0.037451185
## 2021-05-31 -0.007266661
## 2021-06-30 0.029572419
## 2021-07-31 0.023150428
## 2021-08-31 0.027039316
## 2021-09-30 -0.026773253
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.