0

I am trying to run a piece of code following strict instructions from https://otexts.com/fpp3/graphics-exercises.html

I am using the following packages

library(tsibble)
library(tidyverse)
library(tsibbledata)
library(fable)
library(fpp3)
library(forecast)
library(ggplot2)
library(ggfortify)

I ran the following lines of code in order to get a timeseries object (aus_retail)

set.seed(12345678)
myseries <- aus_retail %>%
 filter(`Series ID` == sample(aus_retail$`Series ID`,1))

As an exercise, the author suggests in the page above: "Explore your chosen retail time series using the following functions:"

autoplot(), ggseasonplot(), ggsubseriesplot(), gglagplot(), ggAcf()

So, i tried to run the following line of code

forecast::ggseasonplot(x = myseries)

Which answered me the following error:

Error in forecast::ggseasonplot(x = myseries$Turnover) : 
  autoplot.seasonplot requires a ts object, use x=object

Reading the function help, there is a Example with the AirPassengers dataset (base), which is not even a ts object

Examples

ggseasonplot(AirPassengers, year.labels=TRUE, continuous=TRUE)

which runs as below

enter image description here

The code runs without the others parameters too

 ggseasonplot(AirPassengers)

enter image description here

Why the function keeps asking me for a ts object even though i input one to it?

4

2 回答 2

0

为此表示歉意!fpp3 书仍在编写中(feasts/fable/tsibble 仍在开发中)。

您在上面找到的链接代码来自旧版本的 feasts,它不再是最新的。可以看到 Q6 中使用了正确的功能,但 Q4 中的功能错误地没有更新。

而不是ggseasonplot(),应该说gg_season()。类似的适用于其他函数名称。

相应的代码如下:

library(fpp3)
set.seed(12345678)
myseries <- aus_retail %>%
  filter(`Series ID` == sample(aus_retail$`Series ID`,1))
myseries %>% 
  autoplot(Turnover)

myseries %>% 
  gg_season(Turnover)

myseries %>% 
  gg_subseries(Turnover)

myseries %>% 
  gg_lag(Turnover)

myseries %>% 
  ACF(Turnover) %>% 
  autoplot()

reprex 包(v0.3.0)于 2020 年 1 月 23 日创建

于 2020-01-23T02:56:18.027 回答
0

在 Rstudio 社区寻找解决方案,我找到了 Rob Hyndman 对此问题的答案 https://community.rstudio.com/t/can-not-use-autoplot-with-a-tsibble/41297

因此,您必须通过 as.ts 函数将类更改为 ts。

因此,为了使用 ggseasonplot 函数,代码应如下所示:

forecast::ggseasonplot(x = as.ts(myseries))
于 2020-01-16T01:36:08.107 回答