tsibble——当没有隐式间隙时,如何解决隐式间隙

Pun*_*ney 7 r time-series fable tsibble

我是 tsibble 包的新手。我有每月数据,我将其强制转换为 tsibble 以使用寓言包。我遇到的一些问题

  • 看来索引变量(根据我的测试)不属于类日期,即使我对其应用了 lubridate 的 ymd 函数。
  • has_gaps 函数返回 FALSE,但是当我对数据建模时,我收到错误“.data 包含隐式时间间隙”
library(dplyr)
library(fable)
library(lubridate)
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 = 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(test_ts, .full = T)

model_new <- test_ts %>% 
  model(
  snaive = SNAIVE(Claims))
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。

Rob*_*man 7

您有每日索引,但您想要每月索引。最简单的方法是使用该tsibble::yearmonth()函数,但您需要先将日期转换为字符。

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)
Run Code Online (Sandbox Code Playgroud)