0

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

我已经看到了许多显示实际值/拟合在一起的示例,或者实际值“跟随”预测,但我找不到与实际值/预测值一起运行的代码示例。

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

4

1 回答 1

0

使用 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")
于 2017-08-05T02:11:06.583 回答