xts对象上的Spotvol计算不断产生POSIX错误

问题描述 投票:0回答:1

我正在尝试通过使用高频软件包中的spotvol函数来计算大型股票数据集的每日实现/历史波动率。由于某些原因,我不断收到POSIX(时间戳)错误,并且尝试了许多不同的方法将样本转换为xts对象。

数据集样本:

2015-01-02 10:02:00  80.15
2015-01-02 11:15:00  81.00
2015-01-02 12:31:00  80.50
2015-01-02 15:59:00  80.40
2015-01-05 10:01:00  80.40
2015-01-05 11:24:00  80.95
2015-01-05 14:21:00  81.05
2015-01-05 15:59:00  80.85

我用来转换为xts对象的代码如下:

vol_df2 <- vol_df1 %>%
  mutate(time_stamp = as.POSIXct(time_stamp, format = "%Y-%m-%d %H:%M:%S"))

vol_df3 <- as.xts(vol_df2, order.by = vol_df2$time_stamp, tz = "", unique = F)

用于尝试从5分钟的时间段中计算每日Spotvol的代码如下:注意:我意识到我提供的样本数据可能不适用于参数k=5,因为每天只有4个观测值。但是,我的实际数据具有高频性质。

  spotvol(vol_df3, method = "detper", on = "minutes", k = 5,
          marketopen = "10:00:00", marketclose = "16:00:00")

我得到的错误如下:

Error in as.POSIXlt.character(x, tz, ...): 
character string is not in a standard unambiguous format
  do not know how to convert 'time' to class “POSIXct”
In addition: Warning messages:
1: All formats failed to parse. No formats found. 
2: All formats failed to parse. No formats found. 
3: In min.default(numeric(0), na.rm = FALSE) :
  no non-missing arguments to min; returning Inf
4: In max.default(numeric(0), na.rm = FALSE) :
  no non-missing arguments to max; returning -Inf
5: All formats failed to parse. No formats found.

Error in set(x, j = name, value = value) : 
Supplied 1054304 items to be assigned to 1054377 items of column
'PRICE'. If you wish to 'recycle' the RHS please use rep() to make this
intent clear to readers of your code.

到目前为止,我所做的研究没有得到任何结果,因为我仅遵循有关转换时间戳记以及转换为xts对象的说明。当我运行Linux系统时,遇到了以下问题:https://github.com/Rdatatable/data.table/issues/1619

我也尝试过使用data.table方法,这也会产生与POSIX时间戳的明确格式有关的错误。

就像我在Spotvol的命令中缺少参数一样,但是,我无法从此处找到的文档中找出是否可以更改默认值:https://www.rdocumentation.org/packages/highfrequency/versions/0.6.3/topics/spotvol

在此方面的任何帮助将不胜感激。干杯:)

r timestamp time-series xts
1个回答
0
投票

问题出在这行代码中:

vol_df3 <- as.xts(vol_df2, order.by = vol_df2$time_stamp, tz = "", unique = F)

如果您查看其中包含的内容,则是这样:

                    time_stamp            PRICE  
2015-01-02 10:02:00 "2015-01-02 10:02:00" "80.15"
2015-01-02 11:15:00 "2015-01-02 11:15:00" "81.00"
2015-01-02 12:31:00 "2015-01-02 12:31:00" "80.50"
2015-01-02 15:59:00 "2015-01-02 15:59:00" "80.40"
2015-01-05 10:01:00 "2015-01-05 10:01:00" "80.40"
2015-01-05 11:24:00 "2015-01-05 11:24:00" "80.95"
2015-01-05 14:21:00 "2015-01-05 14:21:00" "81.05"
2015-01-05 15:59:00 "2015-01-05 15:59:00" "80.85"

xts对象是一个矩阵,如果仅选择包括时间戳在内的整个data.frame,它将是一个字符矩阵。您需要像这样排除timestamp列:

vol_df3 <- as.xts(vol_df2[, names(vol_df2) != "time_stamp"], 
                          order.by = vol_df2$time_stamp, tz = "", unique = F)

这导致此:

                     [,1]
2015-01-02 10:02:00 80.15
2015-01-02 11:15:00 81.00
2015-01-02 12:31:00 80.50
2015-01-02 15:59:00 80.40
2015-01-05 10:01:00 80.40
2015-01-05 11:24:00 80.95
2015-01-05 14:21:00 81.05
2015-01-05 15:59:00 80.85

然后spotvol将不会返回错误。

© www.soinside.com 2019 - 2024. All rights reserved.