润滑不匹配的索引 class

lubridate mismatched index class

在开始之前,我想声明我已经在 https://tsibble.tidyverts.org/articles/faq.html 看到了常见问题解答条目,但是我仍然无法找到可行的解决方案。

我正在使用 polygon.io (https://polygon.io/docs/get_v2_aggs_ticker__forexTicker__range__multiplier___timespan___from___to__anchor)

的“聚合(条)”输出

由于 licensing/copyright 限制,我无法 post 此处的数据,但是如果您转到上面的文档 link,他们那里有示例(无需登录) .

Polygon.io 提供如下时间戳:

t integer The Unix Msec timestamp for the start of the aggregate window.

到目前为止我的尝试是这样的:

library(fpp3)
library(fable.prophet)
library(jsonlite)
library(curl)
library(tidyverse)
library(lubridate)

myURL <-  # *N.B. For sample data please see doc link in SO question*
myData <- fromJSON(myURL)
myData$ticker

myData$results

parse1 <- myData$results %>% select(t,c) %>% 
  mutate(dt = as_datetime(t/1000),.keep="unused",.before=1)

print(head(parse1))

parse2 <- as_tsibble(parse1,index=dt)

然而这会产生:

Error: Can't obtain the interval due to the mismatched index class.

该问题似乎与 Datetime 列 'dt' 的 regular 区间有关。我们可以使用 as.Date 将其转换为 Date class 并且有效

library(dplyr)
library(tsibble)
parse1 %>%
      mutate(dt = as.Date(dt)) %>%
       as_tsibble(index = dt)

-输出

# A tsibble: 120 x 2 [1D]
   dt             c
   <date>     <dbl>
 1 2021-01-03  1.22
 2 2021-01-04  1.23
 3 2021-01-05  1.23
 4 2021-01-06  1.23
 5 2021-01-07  1.23
 6 2021-01-08  1.22
 7 2021-01-09  1.22
 8 2021-01-10  1.22
 9 2021-01-11  1.22
10 2021-01-12  1.22
# … with 110 more rows

能够在 OP post

中复制相同的错误
as_tsibble(parse1,index=dt)
Error: Can't obtain the interval due to the mismatched index class.
ℹ Please see `vignette("FAQ")` for details.
Run `rlang::last_error()` to see where the error occurred.

问题出在 as_tsibble

中的代码段
...
  if (unknown_interval(interval) && (nrows > vec_size(key_data))) {
    abort(c(
      "Can't obtain the interval due to the mismatched index class.",
      i = "Please see `vignette(\"FAQ\")` for details."
    ))
  }
...

有一个参数可以指定 interval 是否为 regular。默认情况下,它是 TRUE。这里,间隔不是规则的。所以,我们需要

as_tsibble(parse1,index=dt)
Error: Can't obtain the interval due to the mismatched index class.
ℹ Please see `vignette("FAQ")` for details.
Run `rlang::last_error()` to see where the error occurred.

因此,将 regular 更改为 FALSE

as_tsibble(parse1,index=dt, regular = FALSE)
# A tsibble: 120 x 2 [!] <GMT>
   dt                      c
   <dttm>              <dbl>
 1 2021-01-03 00:00:00  1.22
 2 2021-01-04 00:00:00  1.23
 3 2021-01-05 00:00:00  1.23
 4 2021-01-06 00:00:00  1.23
 5 2021-01-07 00:00:00  1.23
 6 2021-01-08 00:00:00  1.22
 7 2021-01-09 00:00:00  1.22
 8 2021-01-10 00:00:00  1.22
 9 2021-01-11 00:00:00  1.22
10 2021-01-12 00:00:00  1.22
# … with 110 more rows