【问题标题】:Spotvol calculation on xts object keeps producing POSIX and Error in set errorsxts 对象上的 Spotvol 计算在设置错误中不断产生 POSIX 和错误
【发布时间】:2020-05-18 23:37:50
【问题描述】:

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

数据集的样本:

2015-01-02 10:00:00  80.15
2015-01-02 11:00:00  81.00
2015-01-02 12:00:00  80.50
2015-01-02 13:00:00  80.40
2015-01-02 14:00:00  80.15
2015-01-02 15:00:00  81.00
2015-01-02 16:00:00  80.50
2015-01-05 10:00:00  80.40
2015-01-05 11:00:00  80.95
2015-01-05 12:00:00  81.05
2015-01-05 13:00:00  80.85
2015-01-05 14:00:00  80.50
2015-01-05 15:00:00  80.40
2015-01-05 16:00:00  80.95

我用来转换成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)

尝试从1小时时间段计算每日spotvol的代码如下: 注意:我的实际数据属于高频性质,因此我提供了 2 天内每小时的汇总数据。

  spotvol(vol_df3, method = "detper", on = "hours", k = 1,
          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

编辑:我现在收到的唯一错误是列表中关于设置中的错误的最后一个错误...当我将 @987654332 的 aggregatePrice 部分中的 fill=TRUE 更改为 fill=FALSE 时@函数spotvol似乎想计算一下,但仍然抛出Error in set错误。

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

【问题讨论】:

    标签: r timestamp time-series xts


    【解决方案1】:

    问题出在这行代码:

    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,它将是一个字符矩阵。您需要像这样排除时间戳列:

    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 不会返回错误。

    【讨论】:

    • 感谢您的回答@phiver,这是有道理的。不幸的是,spotvol 仍然返回一个错误(上面提到的最后一个)。我认为这可能与 NA 有关,但是在转换之前 vol_df2 中的 NA 为零。
    • 在这种情况下,我需要更多数据。您可以添加为您提供数据的代码吗?或者以会产生错误的方式复制数据?否则很难看出错误的来源。
    • 好的,所以我编辑了问题以反映我目前只看到 Error in set 错误的事实。 @phiver 您用于转换为 xts 对象的代码适用于 POSIX 错误,但不是最近的错误。我今天设法在我的数据上使用了aggregatePrice,所以我在反映每小时汇总的问题中添加了一些额外的虚拟数据。这有帮助吗?干杯:)
    • 代码的spotvol 部分是产生Error in set 错误的原因。此外,当我使用 aggregatePrice 聚合到 5 分钟时间段时,有多个条目表示 10:00:0010:05:00 等等等。这会有所不同吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    相关资源
    最近更新 更多