R Holt-Winters 预测火车/测试



我正在寻找一个简单的示例(或链接到一个示例(,该示例使用基本Holt-Winters或R中预测包中的一个来绘制与预测相同范围内的实际值。

我见过许多显示实际值/拟合在一起的例子,或者实际值"紧跟"预测,但我找不到实际值/预测值一起运行的代码示例。

我这样做的原因是使用实际值(训练(的子集创建预测,然后将整个数据集放入其中,以便我可以显示预测的实际效果。希望这是有道理的,并提前感谢任何帮助!

使用dygraph包找到了一个非常简单的解决方案。这可能是一件简单的事情,但是将我的问题/答案留在这里,以防其他新手遇到并尝试做同样的事情:

我从这个链接(第二张图(中获取了代码: https://rstudio.github.io/dygraphs/gallery-upper-lower-bars.html

并修改为:

library(dygraphs)
library(magrittr)
#hw <- HoltWinters(ldeaths)
hw <- HoltWinters(window(ldeaths, 1974, c(1976,12)))        #subset timeseries to first 3 years in ldeath (train data)
p <- predict(hw, n.ahead = 36, prediction.interval = TRUE)  #predict for the last 3 years in ldeath (test data)
all <- cbind(ldeaths, p) #we add full ldeaths into graph to see test data (1977,78,79) against the prediction
dygraph(all, "Deaths from Lung Disease (UK)") %>%
dySeries("ldeaths", label = "Actual") %>%
dySeries(c("p.lwr", "p.fit", "p.upr"), label = "Predicted")

最新更新