R/helpers.R
select_dates_by_offset.Rd
Helper function to create a tibble of training/prediction start and end dates based on a rolling window with an offset
select_dates_by_offset(dates, window_size, step_size, offset, rolling = TRUE)
A vector of unique dates in increasing order
A string that contains the window size, e.g. "3 days", "1 week", "2 months", "1 year"
A string that contains the step size, e.g. "3 days", "1 week", "2 months", "1 year"
A string that contains the offset, e.g. "3 days", "1 week", "2 months", "1 year"
A boolean that indicates whether the window should be rolling or expanding (FALSE)
A tibble with columns training_start, training_end, prediction_start, prediction_end, and prediction_phase
dates <- seq(as.Date("2020-01-01"), as.Date("2023-01-01"), by = "month")
window_size <- "1 year"
step_size <- "3 months"
offset <- "1 month"
date_intervals <- select_dates_by_offset(dates, window_size, step_size, offset)
print(date_intervals)
#> # A tibble: 8 × 5
#> training_start training_end prediction_start prediction_end prediction_phase
#> <date> <date> <date> <date> <chr>
#> 1 2020-01-01 2021-01-01 2021-02-01 2021-05-01 OOS
#> 2 2020-04-01 2021-04-01 2021-05-01 2021-08-01 OOS
#> 3 2020-07-01 2021-07-01 2021-08-01 2021-11-01 OOS
#> 4 2020-10-01 2021-10-01 2021-11-01 2022-02-01 OOS
#> 5 2021-01-01 2022-01-01 2022-02-01 2022-05-01 OOS
#> 6 2021-04-01 2022-04-01 2022-05-01 2022-08-01 OOS
#> 7 2021-07-01 2022-07-01 2022-08-01 2022-11-01 OOS
#> 8 2021-10-01 2022-10-01 2022-11-01 2023-04-01 OOS
date_intervals_exp <- select_dates_by_offset(dates, window_size, step_size, offset, rolling=FALSE)
print(date_intervals_exp)
#> # A tibble: 8 × 5
#> training_start training_end prediction_start prediction_end prediction_phase
#> <date> <date> <date> <date> <chr>
#> 1 2020-01-01 2021-01-01 2021-02-01 2021-05-01 OOS
#> 2 2020-01-01 2021-04-01 2021-05-01 2021-08-01 OOS
#> 3 2020-01-01 2021-07-01 2021-08-01 2021-11-01 OOS
#> 4 2020-01-01 2021-10-01 2021-11-01 2022-02-01 OOS
#> 5 2020-01-01 2022-01-01 2022-02-01 2022-05-01 OOS
#> 6 2020-01-01 2022-04-01 2022-05-01 2022-08-01 OOS
#> 7 2020-01-01 2022-07-01 2022-08-01 2022-11-01 OOS
#> 8 2020-01-01 2022-10-01 2022-11-01 2023-04-01 OOS