0

有没有办法使用 tidyverts 包按键创建时间序列交叉验证集?我似乎无法正确处理。以下是我尝试的代表。

该示例涉及为预测创建时间序列交叉验证(提前 1 步的切片)。键变量有 2 个不同的值,我希望有一个包含两个键的时间序列切片的 tsibble。当我尝试对两个 tsibble 进行行绑定时,出现错误。

library(dplyr)
library(tibble)
library(tsibble)

# helper function
create_cv_slices <- function(data, forecast_horizon) {
  data %>%
    dplyr::slice(1:(nrow(data) - forecast_horizon)) %>%
    tsibble::stretch_tsibble(.init = nrow(data) - 2 * forecast_horizon, .step = 1)
}

# get data
raw_tsbl <- tibble::tribble(
  ~index,      ~key,    ~Revenue,     ~Claims,
  20160101, "series1",  11011836.1, 5386836.696,
  20160201, "series1", 11042641.16, 9967325.715,
  20160301, "series1", 11445687.52, 10947197.89,
  20160401, "series1", 11252943.11, 6980431.415,
  20160101, "series2",    12236155,    12526224,
  20160201, "series2",     8675364,     9812904,
  20160301, "series2",    10081130,     8423497,
  20160401, "series2",    14840111,     8079813
) %>%
  dplyr::mutate(index = tsibble::yearmonth(as.character(index))) %>%
  tsibble::as_tsibble(index = index, key = key)

keys <- unique(raw_tsbl$key)

# split & combine
tbl1 = raw_tsbl %>%
  dplyr::filter(key == keys[1]) %>%
  create_cv_slices(., forecast_horizon = 1) %>%
  tibble::as_tibble()

tbl2 = raw_tsbl %>%
  dplyr::filter(key == keys[2]) %>%
  create_cv_slices(., forecast_horizon = 1) %>%
  tibble::as_tibble()

dplyr::bind_rows(tbl1, tbl2) %>%
  tsibble::as_tsibble(index = index, key = key)
#> Error: A valid tsibble must have distinct rows identified by key and index.
#> Please use `duplicates()` to check the duplicated rows.

谢谢你。

4

2 回答 2

0

似乎使用 bind_rows 来组合 tsibbles 是行不通的。使用 bind_rows 并validate = FALSEas_tsibble函数中设置,可以创建一个 tsibble,但它会将 tsibble 显示为每日系列而不是每月(应该是这样)。但是,使用具有相同参数设置的 rbind 会创建所需的 tsibble。

rbind(tbl1, tbl2) %>%
  tsibble::as_tsibble(index = index, key = c(key, .id), validate = F)

谢谢。

于 2020-05-06T02:03:43.973 回答
0

您可以在 tsibble 组上计算切片,而不是通过键手动拆分数据。group_by_key()是一个方便函数(具有更好的性能),它等价于group_by(key). 该n()函数是一个组感知 dplyr 函数,它给出当前组的观察次数。

library(dplyr)
library(tibble)
library(tsibble)

# get data
raw_tsbl <- tibble::tribble(
  ~index,      ~key,    ~Revenue,     ~Claims,
  20160101, "series1",  11011836.1, 5386836.696,
  20160201, "series1", 11042641.16, 9967325.715,
  20160301, "series1", 11445687.52, 10947197.89,
  20160401, "series1", 11252943.11, 6980431.415,
  20160101, "series2",    12236155,    12526224,
  20160201, "series2",     8675364,     9812904,
  20160301, "series2",    10081130,     8423497,
  20160401, "series2",    14840111,     8079813
) %>%
  dplyr::mutate(index = tsibble::yearmonth(as.character(index))) %>%
  tsibble::as_tsibble(index = index, key = key)

forecast_horizon <- 1

raw_tsbl %>% 
  group_by_key() %>% 
  slice(1:(n() - forecast_horizon)) %>% 
  ungroup() %>% 
  stretch_tsibble(.init = 2, .step = 1)
#> # A tsibble: 10 x 5 [1M]
#> # Key:       .id, key [4]
#>       index key       Revenue    Claims   .id
#>       <mth> <chr>       <dbl>     <dbl> <int>
#>  1 2016 Jan series1 11011836.  5386837.     1
#>  2 2016 Feb series1 11042641.  9967326.     1
#>  3 2016 Jan series2 12236155  12526224      1
#>  4 2016 Feb series2  8675364   9812904      1
#>  5 2016 Jan series1 11011836.  5386837.     2
#>  6 2016 Feb series1 11042641.  9967326.     2
#>  7 2016 Mar series1 11445688. 10947198.     2
#>  8 2016 Jan series2 12236155  12526224      2
#>  9 2016 Feb series2  8675364   9812904      2
#> 10 2016 Mar series2 10081130   8423497      2

reprex 包(v0.3.0)于 2020-05-08 创建

此代码中的细微差别.init是设置为 2,而不是nrow(data)-2*forecast_horizon. 对于此数据,它给出了相同的结果,但是每个键的观察次数不会有所不同。一旦 dplyr v1.0.0 发布,将更容易使用类似的工具group_map()bind_rows()使用拆分-应用-组合方法为每个键指定不同的窗口参数。

于 2020-05-08T07:27:33.240 回答