lubridate mismatched index class

Viewed 426

Before I start, I would like to state that I have seen the FAQ entry at https://tsibble.tidyverts.org/articles/faq.html, however I am still unable to get to a workable solution.

I am using the "Aggregates (Bars)" output from polygon.io (https://polygon.io/docs/get_v2_aggs_ticker__forexTicker__range__multiplier___timespan___from___to__anchor)

Due to licensing/copyright restrictions I am unable to post the data here, but if you go to the docs link above, they have a sample there (no login required).

Polygon.io provide timestamp as follows:

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

My attempt so far looks like this :

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)

However this yields:

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

The issue seems to be related to the regular interval of Datetime column 'dt'. We can convert it to Date class with as.Date and it works

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

-output

# 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

Was able to replicate the same error in the OP's 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.

The issue is with the piece of code in 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."
    ))
  }
...

There is an argument to specify if the interval is regular or not. By default, it is TRUE. Here, the interval is not regular. So, we need

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.

Thus, change the regular to 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
Related