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?

2 Answers2

0

Looking for the solution on Rstudio community i found the answer from Rob Hyndman to this issue https://community.rstudio.com/t/can-not-use-autoplot-with-a-tsibble/41297

So, you have to change the class to ts by as.ts function.

So, in order to work with ggseasonplot function, the code should be as the following:

forecast::ggseasonplot(x = as.ts(myseries))
0

Sorry about this! The fpp3 book is still being written (and feasts/fable/tsibble are still being developed).

The code you've found linked above is from an older version of feasts which is no longer current. You can see the correct functions have been used in Q6, but the ones in Q4 had mistakenly not been updated.

Instead of ggseasonplot(), it should say gg_season(). Similar applies to the other function names.

The appropriate code is as follows:

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()

Created on 2020-01-23 by the reprex package (v0.3.0)