r - N 个块中的块时间序列数据集,用于比较均值和方差
问题描述
我正在做一个分析时间序列数据的项目。这是从 2018 年 1 月 1 日到 2019 年 12 月 31 日的苹果股票。从数据集中,我选择了两列“日期”和“Ajd.close”。我在下面附上了一个小数据集。(或者:您可以直接从雅虎财经下载数据。蓝色按钮“应用”下方有下载链接。)
我用 adf.test() 测试了数据集。它不是静止的。现在我想尝试另一种方式,将数据集分成 24 个周期(月),然后比较这些分块数据的均值和方差。我尝试使用 chunker() 但似乎没有用。我该怎么做?谢谢!
这是数据集的较短版本:
Date Adj.Close
1 2018-01-02 41.38024
2 2018-01-03 41.37303
3 2018-01-04 41.56522
4 2018-01-05 42.03845
5 2018-01-08 41.88231
6 2018-01-09 41.87751
7 2018-01-10 41.86789
8 2018-01-11 42.10571
9 2018-01-12 42.54050
10 2018-01-16 42.32431
11 2018-01-17 43.02335
12 2018-01-18 43.06179
13 2018-01-19 42.86961
14 2018-01-22 42.51889
15 2018-01-23 42.52850
16 2018-01-24 41.85107
17 2018-01-25 41.10399
18 2018-01-26 41.20008
19 2018-01-29 40.34730
20 2018-01-30 40.10948
21 2018-01-31 40.21999
22 2018-02-01 40.30407
23 2018-02-02 38.55526
24 2018-02-05 37.59198
解决方案
您可能想要检查回报的平稳性,而不是原始价格。使用最后 Note 中的数据将其转换为 zoo 类,计算收益,按年/月计算平均值和 sd 统计汇总并绘图。如果您更喜欢年/季度,请替换as.yearmon
为as.yearqtr
.
library(zoo)
aapl <- read.zoo(aapl.df)
aapl.ret <- diff(aapl, arith = FALSE) - 1
stats <- function(x) c(mean = mean(x), sd = sd(x))
aapl.ret.stats <- aggregate(aapl.ret, as.yearmon, stats)
plot(aapl.ret.stats, main = "AAPL Adj Returns")
要使用任意长度的块,这里是 10,我们可以使用 rollapplyr:
na.omit(rollapplyr(drop(aapl.ad.ret), 10, by = 10, stats))
雅虎数据
问题中没有足够的数据来真正显示上述内容,但使用 quantmod 我们可以下载更长的系列并执行相同的操作,给出代码后显示的图。我们还展示了一些可以使用数据运行的测试。
library(quantmod)
getSymbols("AAPL")
aapl.ad <- Ad(AAPL)
aapl.ad.ret <- diff(aapl.ad, arith = FALSE) - 1
stats <- function(x) c(mean = mean(x), sd = sd(x))
aapl.ret.stats <- aggregate(aapl.ad.ret, as.yearmon, stats)
# plot shown after code
plot(aapl.ret.stats, main = "AAPL Adj Returns")
# some additional things to try -- output not shown
aapl.ad.ret.na <- na.omit(aapl.ad.ret)
acf(aapl.ad.ret.na)
Box.test(aapl.ad.ret.na)
library(tseries)
adf.test(aapl.ad.ret.na)
kpss.test(aapl.ad.ret.na, null = "Level")
kpss.test(aapl.ad.ret.na, null = "Trend")
笔记
可重现形式的输入:
Lines <- " Date Adj.Close
1 2018-01-02 41.38024
2 2018-01-03 41.37303
3 2018-01-04 41.56522
4 2018-01-05 42.03845
5 2018-01-08 41.88231
6 2018-01-09 41.87751
7 2018-01-10 41.86789
8 2018-01-11 42.10571
9 2018-01-12 42.54050
10 2018-01-16 42.32431
11 2018-01-17 43.02335
12 2018-01-18 43.06179
13 2018-01-19 42.86961
14 2018-01-22 42.51889
15 2018-01-23 42.52850
16 2018-01-24 41.85107
17 2018-01-25 41.10399
18 2018-01-26 41.20008
19 2018-01-29 40.34730
20 2018-01-30 40.10948
21 2018-01-31 40.21999
22 2018-02-01 40.30407
23 2018-02-02 38.55526
24 2018-02-05 37.59198"
aapl.df <- read.table(text = Lines)
推荐阅读
- javascript - Mongoose/Mongodb 类似 trello 的基本方案问题,在 vue 中呈现
- swift - 使用完成处理程序进行异步调用的多个 URLSession dataTask 导致内存上升
- c# - 使用 c# 打开包含具有正确编码的图像的 .prn 文件
- java - Selenium + sikuli 可运行的罐子
- r - 在 R 中跳过 travis 上的特定测试文件
- docker-swarm - 在 Docker Swarm 中运行 Traefik 时如何解决“使用空前端规则过滤容器”的问题
- sql-server - 如何在 SQL Server 2017 中使用 MFA 创建 Azure SQL 数据库的链接服务器
- excel - 导入文件并将字符集更改为 UTF-8
- prestashop - 即使通过后台管理员添加,优惠券也不会反映在电子商务网站中
- botframework - 非美国 LUIS 应用的语音启动