0

I was trying to see if it is possible to set the start and end parameters of the ts() function in the forecast R package. The reason for this is to then use window() to subset a train and test set by date.

The time frame is from 2015-01-01 00:00:00 to 12/31/2017 23:00

                 index esti
2015-01-01 00:00:00    1
2015-01-01 01:00:00    2
2015-01-01 02:00:00    3
2015-01-01 03:00:00    2
2015-01-01 04:00:00    5
2015-01-01 05:00:00    2
...
2017-12-31 18:00:00    0
2017-12-31 19:00:00    1
2017-12-31 20:00:00    0
2017-12-31 21:00:00    2
2017-12-31 22:00:00    0
2017-12-31 23:00:00    4

I used the following syntax to create the time series object:

tmp <- ts(dat, start = c(2015,1), frequency=24)

The returned object is this:

Time Series:
Start = c(2015, 1) 
End = c(2015, 6) 
Frequency = 24 

It looks as if the ts object isn't correct here...

yokota
  • 1,007
  • 12
  • 23

1 Answers1

0

As far as I understand, the ts object does not work well with hourly input. It is recommended that you work with xts or zoo package instead. See this SO post.

Try the following:

## Creating an entire hourly dataframe similar to the example dat
x <- 
  lubridate::parse_date_time(
    c("2015-01-01 00:00:00", "2017-12-31 23:00:00"), 
    orders = "ymdHMS"
  )
y <- seq(x[1], x[2], by = "hour")
dat <- data.frame(
  index = y, esti = sample(seq(0, 10), size = length(y),
 replace = TRUE)
)

## xts package
library(xts)
tmp <- xts(dat, order.by = dat$index)

## Example window-ing
window(tmp, end = y[100])

Let me know if this does not work out.

Kim
  • 4,080
  • 2
  • 30
  • 51