Learn R Programming

tspredit (version 1.2.767)

ts_sample: 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)

Value

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

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

Examples

Run this code
# 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)

Run the code above in your browser using DataLab