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
)

Arguments

y

A vector, matrix, or data frame representing the time series. Only the number of observations is used.

initial_window

Integer. The number of observations used in the initial training window.

horizon

Integer. The forecasting horizon size. Defaults to 1.

fixed_window

Logical. If TRUE, all training windows have fixed size equal to initial_window. If FALSE, the training window grows over time.

skip

Integer. Number of resampling slices to skip between consecutive windows. Defaults to 0.

Value

A list with two components:

train

A named list of integer vectors containing training indices.

test

A named list of integer vectors containing testing indices.

Details

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".

See also

Examples

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
)