【问题标题】:ggplot & holt winters predictionsggplot & holt Winters 预测
【发布时间】:2014-05-30 18:29:16
【问题描述】:

使用数据 UKDriverDeaths

尝试使用 Holt-Winters 预测函数和 ggplot()。

基本复现ggplot中的数据。

data('UKDriverDeaths')    
past <- window(UKDriverDeaths, end = c(1982, 12))
hw <- HoltWinters(past)
pred <- predict(hw, n.ahead = 10)
plot(hw, pred, ylim = range(UKDriverDeaths))
lines(UKDriverDeaths)

我想根据实际数据展示 holt winters 从 1983 年开始的预测。 两个问题是:

1) ggplot 不理解 ts 数据。

2) 使用 HoltWinters() 使用 ts 数据,而不是 zoo(日期或 xts)。我需要在切割点显示预测和实际数据(通常 + geom_line(aes()) 这样做)

如果置信区间是可能的,那就太好了。

非常感谢,完全卡住了

【问题讨论】:

    标签: r ggplot2 prediction holtwinters


    【解决方案1】:

    我正在使用xts 自动合并数据。

    library(xts)
    ts_pred <- ts(c(hw$fitted[, 1], pred), start = 1970, frequency = 12)
    df <- merge(as.xts(ts_pred), as.xts(UKDriverDeaths))
    names(df) <- c("predicted", "actual")
    ggplot(df, aes(x=as.POSIXct(index(df)))) + 
      geom_line(aes(y=predicted), col='red') + 
      geom_line(aes(y=actual), col='black') + 
      theme_bw() +
      geom_vline(xintercept=as.numeric(as.POSIXct("1982-12-01")), linetype="dashed") + 
      labs(title="Holt-Winters filtering\n", x="Time", y="Observed / Fitted") + 
      theme(plot.title = element_text(size=18, face="bold"))
    

    【讨论】:

    • 如果添加置信区间,我只需要: +geom_ribbon(aes(ymin=as.POSIXct(lower), ymax=as.POSIXct(upper)) 我添加 2 列(上, 较低) 到 df, 然后转换回 ts. 但是错误 Error: ggplot2 doesn't know how to deal with data of class mtstsmatrix 出现. 我确实将新矩阵转换为具有 CI 限制的 XT 系列. 我错过了吗什么?非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 2014-11-04
    • 2018-11-19
    • 1970-01-01
    相关资源
    最近更新 更多