尝试理解时间序列维度时出错
我正在尝试计算 2 个股票价格(类型 xts)、AGL 和 BIL(下面的 OHLC 数据)的滚动每日相关性:
library(RODBC)
library(quantmod)
library(xts)
library(TTR)
dput(my.AGL)
structure(c(28500, 27800, 28699, 28440, 28569, 28600, 26650,
27250, 26910, 27450, 28814, 27950, 28950, 28740, 29250, 28765,
27429, 27584, 27534, 28072, 27122, 27050, 28406, 28030, 28211,
27349, 26618, 26509, 26560, 27200, 27203, 27900, 28665, 28694,
28836, 27698, 27090, 26600, 27079, 27206), .Dim = c(10L, 4L), .Dimnames = list(
NULL, c("days.Open", "days.High", "days.Low", "days.Close"
)), index = structure(c(1312988460, 1313074860, 1313420460,
1313506860, 1313593260, 1313679660, 1314025260, 1314111660, 1314198060,
1314284460), tzone = "", tclass = c("POSIXt", "POSIXct")), class = c("xts",
"zoo"), .indexCLASS = c("POSIXt", "POSIXct"), .indexTZ = "", tclass = c("POSIXct",
"POSIXt"))
my.AGL
days.Open days.High days.Low days.Close
2011-08-10 17:01:00 28500 28814 27122 27203
2011-08-11 17:01:00 27800 27950 27050 27900
2011-08-15 17:01:00 28699 28950 28406 28665
2011-08-16 17:01:00 28440 28740 28030 28694
2011-08-17 17:01:00 28569 29250 28211 28836
2011-08-18 17:01:00 28600 28765 27349 27698
2011-08-22 17:01:00 26650 27429 26618 27090
2011-08-23 17:01:00 27250 27584 26509 26600
2011-08-24 17:01:00 26910 27534 26560 27079
2011-08-25 17:01:00 27450 28072 27200 27206
然后我使用 ROC 创建一个系列:
my.AGL.roc <- ROC(my.AGL[,4])
从下面的反馈中,我发现 ROC 与 2.13.1 不兼容,因此,为了创建日志返回,我将 ROC 函数替换为:
my.AGL.lret <- log(my.AGL[,4]) - log(lag(my.AGL[,4], 1)
将第一个 NA 观察替换为:
my.AGL.lret[ is.na(my.AGL.lret) ] <- 0
my.AGL.lret
days.Close
2011-08-10 17:01:00 0.000000000
2011-08-11 17:01:00 0.025299427
2011-08-15 17:01:00 0.027050178
2011-08-16 17:01:00 0.001011175
2011-08-17 17:01:00 0.004936565
2011-08-18 17:01:00 -0.040264398
2011-08-22 17:01:00 -0.022195552
2011-08-23 17:01:00 -0.018253440
2011-08-24 17:01:00 0.017847304
2011-08-25 17:01:00 0.004679017
但是,这两个建议在错误方面产生相同的结果。我使用 xts 的原因是我想将生成的滚动相关性与原始价格系列合并。
> rollapply(my.AGL.lret, 30, mean)
Error in `colnames<-`(`*tmp*`, value = "days.Close") :
attempt to set colnames on object with less than two dimensions
> rollmean(my.AGL.lret, 30)
Error in `colnames<-`(`*tmp*`, value = "days.Close") :
attempt to set colnames on object with less than two dimensions
我确信我在做一些愚蠢的事情,但如果有人能解释一下尺寸是如何处理的,我将不胜感激?以我有限的知识,我创建了一个返回序列,它仍然是一个时间序列。
dim(my.AGL.roc)
[1] 406 1
提前致谢 埃德
I am trying to calculate rolling daily correlations on 2 stock prices (type xts), AGL and BIL (OHLC data below):
library(RODBC)
library(quantmod)
library(xts)
library(TTR)
dput(my.AGL)
structure(c(28500, 27800, 28699, 28440, 28569, 28600, 26650,
27250, 26910, 27450, 28814, 27950, 28950, 28740, 29250, 28765,
27429, 27584, 27534, 28072, 27122, 27050, 28406, 28030, 28211,
27349, 26618, 26509, 26560, 27200, 27203, 27900, 28665, 28694,
28836, 27698, 27090, 26600, 27079, 27206), .Dim = c(10L, 4L), .Dimnames = list(
NULL, c("days.Open", "days.High", "days.Low", "days.Close"
)), index = structure(c(1312988460, 1313074860, 1313420460,
1313506860, 1313593260, 1313679660, 1314025260, 1314111660, 1314198060,
1314284460), tzone = "", tclass = c("POSIXt", "POSIXct")), class = c("xts",
"zoo"), .indexCLASS = c("POSIXt", "POSIXct"), .indexTZ = "", tclass = c("POSIXct",
"POSIXt"))
my.AGL
days.Open days.High days.Low days.Close
2011-08-10 17:01:00 28500 28814 27122 27203
2011-08-11 17:01:00 27800 27950 27050 27900
2011-08-15 17:01:00 28699 28950 28406 28665
2011-08-16 17:01:00 28440 28740 28030 28694
2011-08-17 17:01:00 28569 29250 28211 28836
2011-08-18 17:01:00 28600 28765 27349 27698
2011-08-22 17:01:00 26650 27429 26618 27090
2011-08-23 17:01:00 27250 27584 26509 26600
2011-08-24 17:01:00 26910 27534 26560 27079
2011-08-25 17:01:00 27450 28072 27200 27206
I then create a series using ROC:
my.AGL.roc <- ROC(my.AGL[,4])
From the feedback below, I gathered that ROC is not compatible with 2.13.1, so, to create log returns I replaced the ROC function with:
my.AGL.lret <- log(my.AGL[,4]) - log(lag(my.AGL[,4], 1)
replacing the first NA observation with:
my.AGL.lret[ is.na(my.AGL.lret) ] <- 0
my.AGL.lret
days.Close
2011-08-10 17:01:00 0.000000000
2011-08-11 17:01:00 0.025299427
2011-08-15 17:01:00 0.027050178
2011-08-16 17:01:00 0.001011175
2011-08-17 17:01:00 0.004936565
2011-08-18 17:01:00 -0.040264398
2011-08-22 17:01:00 -0.022195552
2011-08-23 17:01:00 -0.018253440
2011-08-24 17:01:00 0.017847304
2011-08-25 17:01:00 0.004679017
However, both suggestions yield the same result in terms of the error. The reason I am using xts, is that I want to merge my resulting rolling correlation with my original price series.
> rollapply(my.AGL.lret, 30, mean)
Error in `colnames<-`(`*tmp*`, value = "days.Close") :
attempt to set colnames on object with less than two dimensions
> rollmean(my.AGL.lret, 30)
Error in `colnames<-`(`*tmp*`, value = "days.Close") :
attempt to set colnames on object with less than two dimensions
I am sure I am doing something silly, but I would appreciate it if someone could please explain how the dimensions are handled? With my limited knowledge I have created a return series, which is still a time series.
dim(my.AGL.roc)
[1] 406 1
Thanks in advance
Ed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
R-2.13.1 下的
TTR::ROC
没有任何问题。您可以使用TTR::runCor
计算两个价格系列之间的滚动相关性。更新:
在这种情况下,zoo 的
rollapply
不适用于 xts 对象,因为 xts 和 Zoo 对象之间存在根本的设计差异。 xts 对象始终具有dim
属性,而 Zoo 对象可以是向量。rollapply
计算“下降”到最低维度,这会将输入 xts 对象减少为向量,并且您无法在向量上设置列名称。添加 xts
rollapply
方法已在我的待办事项列表中,一旦这些方法可用,这将不再是问题。There's nothing wrong with
TTR::ROC
under R-2.13.1. You can useTTR::runCor
to calculate the rolling correlation between your two price series.UPDATE:
zoo's
rollapply
doesn't work with xts objects in this case because of a fundamental design difference between xts and zoo objects. xts objects always have adim
attribute, whereas zoo objects can be a vector. Therollapply
calculation "drops" to the lowest dimension, which reduces the input xts object to a vector and you can't set column names on a vector.Adding xts
rollapply
methods has been on my to-do list and this won't be a problem once those are available.my.AGL.roc
中的第一个观察结果可能是NA
。来自
?rollmean
:rollapply(my.AGL.roc, 30, 平均值)
The first observation in
my.AGL.roc
is probablyNA
.From the
?rollmean
:rollapply(my.AGL.roc, 30, mean)