【问题标题】:Error in Autoplot function for time series时间序列的自动绘图功能错误
【发布时间】:2019-04-15 21:01:08
【问题描述】:

Autoplot 函数在绘制预测中的时间序列示例时出现以下错误:原理与实践 - Hyndman 和 Athanasopoulos:

library(forecast)
library(fpp2)
data(ausbeer)
beer2 <- window(ausbeer, start=1992)
fit.beer <- tslm(beer2 ~ trend + season)
summary(fit.beer)
autoplot(beer2, series="Data") +
  autolayer(fitted(fit.beer), series="Fitted") +
  xlab("Year") + ylab("Megalitres") +
  ggtitle("Quarterly Beer Production")

# the error is:
# Error: Invalid input: date_trans works with objects of class Date only

它似乎来自 autolayer 行。

我不知道如何将 x 值格式化为日期。

【问题讨论】:

  • 没有错误。按我的意愿工作。
  • 请包含任何其他软件包:data set ‘ausbeer’ not found
  • 不要使用自动绘图 - 但我的猜测是 as.Date(your x) ??
  • ausbeer 数据采用时间序列格式,这让我不清楚如何将 x 值重新格式化为日期。

标签: r ggplot2 time-series forecasting


【解决方案1】:

试试这个(我没有尝试过你的代码,但我之前遇到过类似的问题):

library(scales)
.....
autoplot(beer2, series="Data") + + scale_x_yearmon() + 
autolayer(fitted(fit.beer), series="Fitted") +
xlab("Year") + ylab("Megalitres") +
ggtitle("Quarterly Beer Production")

【讨论】:

    猜你喜欢
    • 2017-05-09
    • 2012-10-13
    • 2017-08-22
    • 2020-02-14
    • 2020-10-10
    • 1970-01-01
    • 2020-02-28
    • 2020-01-01
    • 1970-01-01
    相关资源
    最近更新 更多