Skip to contents

k_fold() shuffles n rows with seed and cuts them into k folds; group_k_fold() keeps each group's rows in one fold; time_ordered() tests on each of the last n_test periods after training on the earlier ones (with calendar diagonals as periods, the triangle backtest). time_splits() generalises it: each split tests on horizon consecutive periods, the last ending at the latest period and each earlier one step periods before the next, and trains on the periods before its test window less the gap just before it, all of them or the latest window (scikit-learn's TimeSeriesSplit with horizon = step = test_size). permutation() shuffles 1:n with seed on stream stream; bootstrap_rows() draws n of 1:n with replacement, the resample booster_fit() refits on (the same as Python's bootstrap_rows, plus one).

Usage

k_fold(n, k, seed)

group_k_fold(groups, k, seed)

time_ordered(periods, n_test)

time_splits(periods, n_test, window = NULL, horizon = 1, gap = 0, step = 1)

permutation(n, seed, stream = 0)

bootstrap_rows(n, seed, stream = 0)

Arguments

n

Number of rows.

k

Number of folds.

seed

Generator seed.

groups

Group label per row.

periods

Period per row (whole numbers).

n_test

Number of final periods to test on.

window

A rolling window of that many periods, or NULL for an expanding one.

horizon, gap, step

Periods tested, left out before each test window, and moved from one split to the next.

stream

For permutation() and bootstrap_rows(), the stream of seed to draw from.

Value

A list of splits, each list(train, test) of row numbers; permutation(), the row numbers 1:n shuffled, the shuffle the explanations apply (the same as Python's permutation, plus one); bootstrap_rows(), n row numbers drawn from 1:n.

Examples

length(k_fold(10, 5, seed = 1))
#> [1] 5
time_ordered(c(0, 1, 2, 1, 2, 2), 1)
#> [[1]]
#> [[1]]$train
#> [1] 1 2 4
#> 
#> [[1]]$test
#> [1] 3 5 6
#> 
#> 
time_splits(0:9, 2, window = 3, horizon = 2, gap = 1, step = 2)
#> [[1]]
#> [[1]]$train
#> [1] 3 4 5
#> 
#> [[1]]$test
#> [1] 7 8
#> 
#> 
#> [[2]]
#> [[2]]$train
#> [1] 5 6 7
#> 
#> [[2]]$test
#> [1]  9 10
#> 
#>