尝试理解时间序列维度时出错

发布于 2024-12-02 00:50:43 字数 2798 浏览 0 评论 0原文

我正在尝试计算 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最终幸福 2024-12-09 00:50:43

R-2.13.1 下的 TTR::ROC 没有任何问题。您可以使用 TTR::runCor 计算两个价格系列之间的滚动相关性。

library(quantmod)
my.AGL <-
structure(c(32020L, 32810L, 33000L, 33394L, 33650L, 34205L, 34140L, 
33400L, 34300L, 32975L, 33179L, 33450L, 33700L, 34180L, 35000L, 
34140L, 33600L, 34300L, 32020L, 32460L, 32811L, 33157L, 33599L, 
34205L, 33299L, 33155L, 33106L, 32850L, 33020L, 33400L, 33539L, 
34000L, 34461L, 33480L, 33400L, 33250L), .Dim = c(9L, 4L), .Dimnames = list(
    NULL, c("days.Open", "days.High", "days.Low", "days.Close"
    )), index = structure(c(1262646025, 1262732404, 1262818810, 
1262905220, 1262991623, 1263250801, 1263337207, 1263423608, 1263510020
), tzone = "", tclass = c("POSIXct", "POSIXt")), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "", class = c("xts", "zoo"))

my.AGL.roc <- ROC(Cl(my.AGL))
my.BIL.roc <- ROC(Op(my.AGL))  # since OP didn't provide BIL data
runCor(x=my.AGL.roc,y=my.BIL.roc,n=3)
#                              [,1]
# 2010-01-04 17:00:25            NA
# 2010-01-05 17:00:04            NA
# 2010-01-06 17:00:10            NA
# 2010-01-07 17:00:20 -0.6614544157
# 2010-01-08 17:00:23 -0.8643698058
# 2010-01-11 17:00:01  0.0001661546
# 2010-01-12 17:00:07  0.8768496736
# 2010-01-13 17:00:08  0.3459987310
# 2010-01-14 17:00:20  0.0289108044

更新:

在这种情况下,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 use TTR::runCor to calculate the rolling correlation between your two price series.

library(quantmod)
my.AGL <-
structure(c(32020L, 32810L, 33000L, 33394L, 33650L, 34205L, 34140L, 
33400L, 34300L, 32975L, 33179L, 33450L, 33700L, 34180L, 35000L, 
34140L, 33600L, 34300L, 32020L, 32460L, 32811L, 33157L, 33599L, 
34205L, 33299L, 33155L, 33106L, 32850L, 33020L, 33400L, 33539L, 
34000L, 34461L, 33480L, 33400L, 33250L), .Dim = c(9L, 4L), .Dimnames = list(
    NULL, c("days.Open", "days.High", "days.Low", "days.Close"
    )), index = structure(c(1262646025, 1262732404, 1262818810, 
1262905220, 1262991623, 1263250801, 1263337207, 1263423608, 1263510020
), tzone = "", tclass = c("POSIXct", "POSIXt")), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "", class = c("xts", "zoo"))

my.AGL.roc <- ROC(Cl(my.AGL))
my.BIL.roc <- ROC(Op(my.AGL))  # since OP didn't provide BIL data
runCor(x=my.AGL.roc,y=my.BIL.roc,n=3)
#                              [,1]
# 2010-01-04 17:00:25            NA
# 2010-01-05 17:00:04            NA
# 2010-01-06 17:00:10            NA
# 2010-01-07 17:00:20 -0.6614544157
# 2010-01-08 17:00:23 -0.8643698058
# 2010-01-11 17:00:01  0.0001661546
# 2010-01-12 17:00:07  0.8768496736
# 2010-01-13 17:00:08  0.3459987310
# 2010-01-14 17:00:20  0.0289108044

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 a dim attribute, whereas zoo objects can be a vector. The rollapply 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.

牛↙奶布丁 2024-12-09 00:50:43

my.AGL.roc 中的第一个观察结果可能是 NA

来自 ?rollmean

rollmean 的默认方法不处理包含 NA 的输入。在这种情况下,请改用 rollapply。

rollapply(my.AGL.roc, 30, 平均值)

The first observation in my.AGL.roc is probably NA.

From the ?rollmean:

The default method of rollmean does not handle inputs that contain NAs. In such cases, use rollapply instead.

rollapply(my.AGL.roc, 30, mean)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文