Mohit Pandey
Mohit Pandey

Reputation: 1

Split time series data in Train and test set with year selection in R

At present I am using this but it is not giving write figure after for test set

p <- read.csv("steel.csv")
p$Date= parse_date_time(p$Date, "mdy")

train=ts(p$data,c(2014,1),c(2019,06),12)
test=ts(p$data,c(2019,07),c(2020,06),12)

any solution please. also can be used in shiny

Upvotes: 0

Views: 362

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76641

To subset an object of class "ts", use window.

## Using January 2014 as start date:
set.seed(2022)

x <- cumsum(1 + round(rnorm(78), 2))
x <- ts(x, start = c(2014, 1), frequency = 12)

train <- window(x, start = c(2014, 1), end = c(2019, 6))
test <- window(x, start = c(2019, 07), end = c(2020, 6))

Upvotes: 1

Related Questions