A time series platform for the tidyverse
本文是学习 time_series_platform_for_tidyverse.pdf 的笔记。介绍了 tibbletime 包的使用示例。
首先我们需要为 tibble 数据框指定日期索引生成 time tibble 数据框:
1 | library(tibbletime) |
计算收益率:
1 | library(dplyr) |
选择列:
1 | FB_time %>% |
tibbletime 最好用的功能应该算是时间范围的筛选了:
1 | FB_time %>% |
再例如,选择 2013 年 2 月的数据:
1 | FB_time %>% |
collapse_by()
可以把日期索引变成每年的最后一个观测值对应的日期:
1 | (FB_yearly <- collapse_by(FB_time, period = "year")) |
这样我们就可以通过下面的方式把高频数据汇总为低频数据:
1 | FB_time %>% |
再例如计算所有变量半年期的均值:
1 | FB_time %>% |
还可以和 group_by()
一起使用:
1 | data("FANG") |
和 tidyfinance 一起使用:
计算日度收益率:
1 | # devtools::install_github("DavisVaughan/tidyfinance") |
计算年度收益:
1 | FANG_time %>% |
计算累计收益率和回撤指标:
1 | FANG_return <- FANG_time %>% |
计算月度总收益:
1 | FANG_return_monthly <- FANG_return %>% |
绘图:
1 | # 累积收益率 |
A time series platform for the tidyverse
You need to set
install_url
to use ShareThis. Please set it in _config.yml
.