Is there a way to create time series cross validation sets by key using the tidyverts package? I can't seem to get it right. Below is a reprex of my attempt.
The example involves creating time series cross-validation (slices with 1 step ahead) for forecasting. The key variable has 2 distinct values and I will like to have one tsibble containing the time series slices for both keys. When I try to row-bind both tsibbles, I get an error.
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.
Thank you.
It appears that using bind_rows to combine the tsibbles is what doesn't work. Using bind_rows and setting
validate = FALSE
in theas_tsibble
function, creates a tsibble alright but it displays the tsibble as a daily series instead of monthly (which is what it should be). However, using rbind with the same argument setting, creates the desired tsibble.Thanks.