How do I get around implicit gaps in time when there are actually no gaps in time?

I am new to the tsibble package. I have monthly data that I coerced to a tsibble to use the fable package. A few issues I am having

  • It appears the index variable (from my testing) is not of class date even though I applied
    lubridate's ymd function to it.
  • has_gaps function returns FALSE but when I model on the data, I get the error that ".data contains
    implicit gaps in time"
library(dplyr)
library(tsibble)
library(fable)
library(lubridate)

test <- data.frame(
   YearMonth = c(20160101, 20160201, 20160301, 20160401, 20160501, 20160601,
                 20160701, 20160801, 20160901, 20161001, 20161101, 20161201),
      Claims = c(13032647, 1668005, 24473616, 13640769, 17891432, 11596556,
                 23176360, 7885872, 11948461, 16194792, 4971310, 18032363),
     Revenue = c(12603367, 18733242, 5862766, 3861877, 15407158, 24534258,
                 15633646, 13720258, 24944078, 13375742, 4537475, 22988443)
)

test_ts <- test %>% 
  mutate(YearMonth = ymd(YearMonth)) %>% 
  as_tsibble(
    index = YearMonth,
    regular = FALSE       #because it picks up gaps when I set it to TRUE
    )

# Are there any gaps?
has_gaps(grp_ts, .full = T)

model_new <- test_ts %>% 
  model(
  snaive = SNAIVE(Claims))

Warning messages:
1: 1 error encountered for snaive
[1] .data contains implicit gaps in time. You should check your data and convert implicit gaps into explicit missing values using `tsibble::fill_gaps()` if required.
 

Any help will appreciated.

I am not familiar with the tsibble package, so this is just a guess. Your YearMonth variable is not regularly spaced in terms of number of days between values. There are 31 days from Jan 1 to Feb. 1 but only 29 days from there to Mar 1. Try spacing the data evenly in terms of days and see if the error disappears.

You are using a daily index, but you want a monthly index. Try this:

library(dplyr)
library(tsibble)

test <- data.frame(
  YearMonth = c(20160101, 20160201, 20160301, 20160401, 20160501, 20160601,
    20160701, 20160801, 20160901, 20161001, 20161101, 20161201),
  Claims = c(13032647, 1668005, 24473616, 13640769, 17891432, 11596556,
    23176360, 7885872, 11948461, 16194792, 4971310, 18032363),
  Revenue = c(12603367, 18733242, 5862766, 3861877, 15407158, 24534258,
    15633646, 13720258, 24944078, 13375742, 4537475, 22988443)
)

test_ts <- test %>%
  mutate(YearMonth = yearmonth(as.character(YearMonth))) %>%
  as_tsibble(index = YearMonth)
1 Like

That solved the issue. Thanks very much.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.