Generates rolling training and testing index sets for time series resampling and forecasting evaluation.
create_time_slices(
y,
initial_window,
horizon = 1,
fixed_window = TRUE,
skip = 0
)A vector, matrix, or data frame representing the time series. Only the number of observations is used.
Integer. The number of observations used in the initial training window.
Integer. The forecasting horizon size.
Defaults to 1.
Logical. If TRUE, all training windows
have fixed size equal to initial_window. If FALSE,
the training window grows over time.
Integer. Number of resampling slices to skip between
consecutive windows. Defaults to 0.
A list with two components:
A named list of integer vectors containing training indices.
A named list of integer vectors containing testing indices.
Inspired by caret::createTimeSlices().
Training and testing slices are created sequentially in time order, making this function suitable for forecasting and time series cross-validation.
Slice names are returned in the format
"training001" and "testing001".
y <- 1:20
# Fixed rolling window
slices <- create_time_slices(
y,
initial_window = 10,
horizon = 2
)
# Expanding window
slices_expanding <- create_time_slices(
y,
initial_window = 10,
horizon = 2,
fixed_window = FALSE
)