ts_sample: Time Series Sample

View source: R/ts_sample.R

ts_sampleR Documentation

Time Series Sample

Description

Split a ts_data into train and test sets.

Extracts test_size rows from the end (minus an optional offset) as the test set. The remaining initial rows form the training set. The offset is useful to reproduce experiments with different forecast origins.

Usage

ts_sample(ts, test_size = 1, offset = 0)

Arguments

ts

A ts_data matrix.

test_size

Integer. Number of rows in the test split (default = 1).

offset

Integer. Offset from the end before the test split (default = 0).

Value

A list with ⁠$train⁠ and ⁠$test⁠ (both ts_data).

Examples

# Setting up a ts_data and making a temporal split
# Load example dataset and build windows
data(tsd)
ts <- ts_data(tsd$y, 10)

# Separating into train and test
test_size <- 3
samp <- ts_sample(ts, test_size)

# First five rows from training data
ts_head(samp$train, 5)

# Last five rows from training data
ts_head(samp$train[-c(1:(nrow(samp$train)-5)),])

# Testing data
ts_head(samp$test)

tspredit documentation built on Feb. 11, 2026, 9:08 a.m.