0

我试图查看是否可以在R 包中设置ts()函数的开始和结束参数。forecast这样做的原因是然后使用window()子集 atrain并按test日期设置。

时间范围是从2015-01-01 00:00:0012/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

我使用以下语法创建时间序列对象:

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

返回的对象是这样的:

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

看起来好像ts对象在这里不正确......

4

1 回答 1

0

据我了解,该ts对象不适用于每小时输入。建议您改为使用xtszoo打包。请参阅此 SO 帖子

尝试以下操作:

## 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])

让我知道这是否行不通。

于 2018-08-04T07:15:53.720 回答