【问题标题】:splitting data for time series prediction拆分数据以进行时间序列预测
【发布时间】:2021-04-07 03:40:56
【问题描述】:

我正在寻找一个 R 包,它允许我进行 n 倍 CV 类型的超参数优化(例如 n = 10)。假设这是我可以用来调整超参数的数据(我倾向于使用 rBayesianOptimization,所以让我们抽象一下):

dates <- seq(as.Date('2017-01-01'), as.Date('2019-12-31'), by = 'days')

df <- data.frame(date = dates)
df$y <- 42

这里的因变量 y 显然是众所周知的常数,它只是被添加到这里而没有被利用。

我遇到了插入符号函数createTimeSlices,这可能是拆分数据的一种方法:

slices <- createTimeSlices(df$date, initialWindow = 365 * 2.5, horizon = 30, fixedWindow = TRUE)

我最终得到一个这样的列表:

List of 2
 $ train:List of 153
  ..$ Training0912.5: int [1:912] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ Training0913.5: int [1:912] 2 3 4 5 6 7 8 9 10 11 ...
...   
  ..$ Training1010.5: int [1:912] 99 100 101 102 103 104 105 106 107 108 ...
  .. [list output truncated]
 $ test :List of 153
  ..$ Testing0912.5: num [1:30] 914 914 916 916 918 ...
  ..$ Testing0913.5: num [1:30] 914 916 916 918 918 ...

有人可以指出如何使用这个或将我推荐给另一个包吗?就我个人而言,我对仅移动 1 天(?)的训练数据索引有点困惑。我原以为它会改变 30 天(见地平线)。

谢谢。

【问题讨论】:

  • 您的具体目标是什么?你提到了10倍CV、参数调优、数据分割,还有一个关于如何使用createTimeSlices函数的问题。如果您能提供准确的预期输出,并阐明您的问题是什么,那将很有帮助。
  • 在这个 casa imho 中提供一个例子有点困难。在 10 折 CV 中,您使用 9 折用于训练和 1 折用于验证。您将获得 10 个绩效指标,例如曲线下面积。然后,您可以计算超参数设置的平均值。我想为时间序列做类似的事情。显然,时间序列是顺序的,但原理是一样的。
  • 您可能想查看软件包 fable & fabletools 和 modeltime。他们处理时间序列和预测。
  • 谢谢看看。
  • @cs0815,must read book 用于寓言和寓言工具以及一般预测。对于模型时间:business-science.io/code-tools/2020/06/29/…modeltime.ensemble:business-science.io/code-tools/2020/10/13/…modeltime.resample:cran.r-project.org/web/packages/modeltime.resample/vignettes/…

标签: r time-series r-caret


【解决方案1】:

我找到了一种使用受Shambho's SO answer 启发的 createTimeSlices 的方法。

library(caret)

dates <- seq(as.Date('2017-01-01'), as.Date('2019-12-31'), by = 'days')

df <- data.frame(date = dates)
df$x <- 1
df$y <- 42

timeSlices <- createTimeSlices(1:nrow(df), initialWindow = 365 * 2, horizon = 30, fixedWindow = TRUE, skip = 30)

#str(timeSlices, max.level = 1)

trainSlices <- timeSlices[[1]]
testSlices <- timeSlices[[2]]

for (i in 1:length(trainSlices)) {

    train <- df[trainSlices[[i]],]
    test <- df[testSlices[[i]],]

    # fit and calculate performance on test to ultimately get average etc.

    print(paste0(min(train$date), " - ", max(train$date)))
    print(paste0(min(test$date), " - ", max(test$date)))
    print("")
}

对我来说关键是要指定跳过,否则窗口只会移动 1 天,最终会出现很多“折叠”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2021-12-21
    • 2015-04-24
    • 2020-08-05
    • 2020-05-25
    • 2017-11-22
    相关资源
    最近更新 更多