在 R 中创建一天前滚预测
Creating a One-Day-Ahead Roll-Forward Forecast in R
我正在尝试计算前滚一天前预测的预测措施并绘制结果。
我对下面的示例代码有一些疑问:
- 我该怎么做才能解决下面的 "replacement has length zero" 错误?
- 如何输出预测值数组各自的日期?
- 如何在一张图表上绘制数据和预测?
这是执行脚本时收到的错误:
Error in error[j - fixed.nTrain + 1] <- valid.ts - naive.pred$mean[stepsAhead] :
replacement has length zero
可重现的例子如下。
d <- structure(list(Date = structure(c(17349, 17350, 17351, 17352,
17353, 17354, 17355, 17356, 17357, 17358, 17359, 17360, 17361,
17362, 17363, 17364, 17365, 17366, 17367, 17368, 17369, 17370,
17371, 17372, 17373, 17374, 17375, 17376, 17377, 17378, 17379,
17380, 17381, 17382, 17383), class = "Date"), Ratio = c(67, 50,
67, 50, 100, 50, 33, 67, 0, 0, 0, 0, 100, 75, 0, 0, 75, 100,
67, 33, 33, 33, 50, 50, 67, 100, 67, 50, 25, 25, 33, 33, 100,
33, 0)), .Names = c("Date", "Ratio"), row.names = 183:217, class = "data.frame")
library(xts)
dates = as.Date(d$Date,"%Y-%m-%d")
xs = xts(d$Ratio,dates)
library("forecast")
fixed.nValid <- 6
fixed.nTrain <- length(xs) - fixed.nValid
stepsAhead <- 2
error <- rep(0, fixed.nValid - stepsAhead + 1)
percent.error <- rep(0, fixed.nValid - stepsAhead + 1)
predictions <-rep(0, fixed.nValid - stepsAhead + 1)
for (j in fixed.nTrain:(fixed.nTrain + fixed.nValid - stepsAhead)) {
train.ts <- window(xs, start = as.Date("2017-07-02"), end = as.Date("2017-07-02") + j)
valid.ts <- window(xs, start = as.Date("2017-07-02") + j + stepsAhead, end = as.Date("2017-07-02") + j + stepsAhead)
naive.pred <- naive(train.ts, h = stepsAhead)
error[j - fixed.nTrain + 1] <- valid.ts - naive.pred$mean[stepsAhead]
percent.error[j - fixed.nTrain + 1] <- error[j - fixed.nTrain + 1] / valid.ts
}
mean(abs(error))
sqrt(mean(error^2))
mean(abs(percent.error))
这是上面脚本的输出:
谢谢!
问题在于,当您的 for 循环中的 j = 33 时,
的值
as.Date("2017-07-02") + j + stepsAhead
是"2017-08-06"
,晚于xs
中的最新日期。这导致 valid.ts
的长度为零,这会导致您看到的错误。
我正在尝试计算前滚一天前预测的预测措施并绘制结果。
我对下面的示例代码有一些疑问:
- 我该怎么做才能解决下面的 "replacement has length zero" 错误?
- 如何输出预测值数组各自的日期?
- 如何在一张图表上绘制数据和预测?
这是执行脚本时收到的错误:
Error in error[j - fixed.nTrain + 1] <- valid.ts - naive.pred$mean[stepsAhead] : replacement has length zero
可重现的例子如下。
d <- structure(list(Date = structure(c(17349, 17350, 17351, 17352,
17353, 17354, 17355, 17356, 17357, 17358, 17359, 17360, 17361,
17362, 17363, 17364, 17365, 17366, 17367, 17368, 17369, 17370,
17371, 17372, 17373, 17374, 17375, 17376, 17377, 17378, 17379,
17380, 17381, 17382, 17383), class = "Date"), Ratio = c(67, 50,
67, 50, 100, 50, 33, 67, 0, 0, 0, 0, 100, 75, 0, 0, 75, 100,
67, 33, 33, 33, 50, 50, 67, 100, 67, 50, 25, 25, 33, 33, 100,
33, 0)), .Names = c("Date", "Ratio"), row.names = 183:217, class = "data.frame")
library(xts)
dates = as.Date(d$Date,"%Y-%m-%d")
xs = xts(d$Ratio,dates)
library("forecast")
fixed.nValid <- 6
fixed.nTrain <- length(xs) - fixed.nValid
stepsAhead <- 2
error <- rep(0, fixed.nValid - stepsAhead + 1)
percent.error <- rep(0, fixed.nValid - stepsAhead + 1)
predictions <-rep(0, fixed.nValid - stepsAhead + 1)
for (j in fixed.nTrain:(fixed.nTrain + fixed.nValid - stepsAhead)) {
train.ts <- window(xs, start = as.Date("2017-07-02"), end = as.Date("2017-07-02") + j)
valid.ts <- window(xs, start = as.Date("2017-07-02") + j + stepsAhead, end = as.Date("2017-07-02") + j + stepsAhead)
naive.pred <- naive(train.ts, h = stepsAhead)
error[j - fixed.nTrain + 1] <- valid.ts - naive.pred$mean[stepsAhead]
percent.error[j - fixed.nTrain + 1] <- error[j - fixed.nTrain + 1] / valid.ts
}
mean(abs(error))
sqrt(mean(error^2))
mean(abs(percent.error))
这是上面脚本的输出:
谢谢!
问题在于,当您的 for 循环中的 j = 33 时,
的值as.Date("2017-07-02") + j + stepsAhead
是"2017-08-06"
,晚于xs
中的最新日期。这导致 valid.ts
的长度为零,这会导致您看到的错误。