split_index: Create indices for train and test splits

View source: R/make_split.R

split_indexR Documentation

Create indices for train and test splits

Description

Create train and test indices for time series cross-validation.

Usage

split_index(
  n_total,
  n_init,
  n_ahead,
  n_skip = 0,
  n_lag = 0,
  mode = "slide",
  exceed = FALSE
)

Arguments

n_total

Integer. The total number of observations in the time series.

n_init

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

n_ahead

Integer. The forecast horizon, i.e. the number of observations in each test window.

n_skip

Integer. The number of observations to skip between split origins. The default is 0.

n_lag

Integer. The number of lagged observations to include before the test window. The default is 0.

mode

Character value. Either "slide" for a fixed-window approach or "stretch" for an expanding-window approach.

exceed

Logical value. If TRUE, test indices may exceed the original sample size.

Details

split_index() creates integer index vectors for rolling-origin resampling. The function can create either fixed-window or expanding-window splits:

  • mode = "slide" creates a fixed training window that moves forward over time.

  • mode = "stretch" creates an expanding training window that always starts at the first observation.

The first training window contains n_init observations. Each test window contains n_ahead observations. The argument n_skip controls how many observations are skipped between consecutive split origins. For example, with n_ahead = 18 and n_skip = 17, consecutive test windows are non-overlapping.

If n_lag > 0, the test indices include lagged observations before the forecast horizon. This is useful when lagged predictors are needed for constructing features during testing.

If exceed = TRUE, additional out-of-sample test indices are allowed to exceed the original sample size.

Value

A list with two elements:

  • train: a list of integer vectors with training indices.

  • test: a list of integer vectors with test indices.

See Also

Other time series cross-validation: make_future(), make_split(), make_tsibble(), slice_test(), slice_train()

Examples

# Fixed-window splits
fixed_index <- split_index(
  n_total = 180,
  n_init = 120,
  n_ahead = 18,
  n_skip = 17,
  n_lag = 0,
  mode = "slide",
  exceed = FALSE
)

fixed_index

# Expanding-window splits
expanding_index <- split_index(
  n_total = 180,
  n_init = 120,
  n_ahead = 18,
  n_skip = 17,
  n_lag = 0,
  mode = "stretch",
  exceed = FALSE
)

expanding_index

tscv documentation built on May 13, 2026, 9:07 a.m.