0
ausbeer %>% as_tsibble() %>% 
  filter(year(index) >= 1992) %>% 
  mutate(Year = year(index), Quarter = quarter(index)) %>% 
  as_tibble() %>% 
  select(Year, Quarter, value) %>% 
  pivot_wider(names_from = Quarter, values_from = value) %>% 
  as_tsibble(index = Year) %>% 
  gg_lag(,2:5)

I'm trying to make a lag plot of 'ausbeer' dataset, and doing this using verbs from 'fpp3' package. Of course, the easy way is to use the former version of 'gglagplot()' but, I want to keep using verbs from fpp3 package.

When I run the above code, it shows 5 seasons(0~5), instead of 4 (q1~q4). Can anyone fix this problem?

1 Answers1

0

A lag plot of fpp2::ausbeer from 1992 onwards can be produced with:

library(fpp3)
#> ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
#> ✓ tibble      3.1.2          ✓ tsibble     1.0.1     
#> ✓ dplyr       1.0.6          ✓ tsibbledata 0.3.0     
#> ✓ tidyr       1.1.3          ✓ feasts      0.2.1.9000
#> ✓ lubridate   1.7.10         ✓ fable       0.3.1     
#> ✓ ggplot2     3.3.3.9000
#> ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
#> x lubridate::date()    masks base::date()
#> x dplyr::filter()      masks stats::filter()
#> x tsibble::intersect() masks base::intersect()
#> x tsibble::interval()  masks lubridate::interval()
#> x dplyr::lag()         masks stats::lag()
#> x tsibble::setdiff()   masks base::setdiff()
#> x tsibble::union()     masks base::union()
as_tsibble(fpp2::ausbeer) %>% 
  filter(year(index) >= 1992) %>% 
  gg_lag(value)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

This gives the same output as was given from the gglagplot() function.

library(fpp2)
#> ── Attaching packages ────────────────────────────────────────────── fpp2 2.4 ──
#> ✓ forecast  8.14     ✓ expsmooth 2.3 
#> ✓ fma       2.4
#> 
#> 
#> Attaching package: 'fpp2'
#> The following object is masked from 'package:fpp3':
#> 
#>     insurance
gglagplot(window(ausbeer, start = 1992))

There is no need to pivot the quarters into columns of the data. The tsibble format has each column being a different variable (in this case the amount of beer produced in Australia).

The y argument is used to specify which column to plot, and the separation of seasonal periods is controlled using the period argument. The default here will choose a common seasonal window, which in this case is period = "1 year" to show the quarters in a year separately.

Created on 2021-06-15 by the reprex package (v2.0.0)