ansett |>
filter(Airports=="MEL-SYD", Class=="Economy") |>
autoplot(Passengers)
AFRICASTTidy time series & forecasting in R
Session 2 of 10
Level
Underlying trend
Seasonal/cycle
Autocorrelation
Unpredictable patterns/Noise
Different types of events and driving factors (i.e. predictors) may affect the time series
gg_season() (ggtime)Time plot of quarterly Australian beer production since 1992.
Each observation belongs to a year. Split the line between years, half-way between Q4 and the next Q1.
Move each year back to the start in turn, stacking them. The time axis narrows to a single year.
Remove the points: the seasonal plot, with one line per year.
# A tsibble: 52,608 x 5 [30m] <Australia/Melbourne>
Time Demand Temperature Date Holiday
<dttm> <dbl> <dbl> <date> <lgl>
1 2012-01-01 00:00:00 4383. 21.4 2012-01-01 TRUE
2 2012-01-01 00:30:00 4263. 21.0 2012-01-01 TRUE
3 2012-01-01 01:00:00 4049. 20.7 2012-01-01 TRUE
4 2012-01-01 01:30:00 3878. 20.6 2012-01-01 TRUE
5 2012-01-01 02:00:00 4036. 20.4 2012-01-01 TRUE
6 2012-01-01 02:30:00 3866. 20.2 2012-01-01 TRUE
7 2012-01-01 03:00:00 3694. 20.1 2012-01-01 TRUE
8 2012-01-01 03:30:00 3562. 19.6 2012-01-01 TRUE
9 2012-01-01 04:00:00 3433. 19.1 2012-01-01 TRUE
10 2012-01-01 04:30:00 3359. 19.0 2012-01-01 TRUE
# ℹ 52,598 more rows
gg_subseries() (ggtime)Time plot of quarterly Australian beer production since 1992.
Each observation belongs to a quarter of the year.
Join the observations from the same quarter: four subseries, each a time series in its own right.
Separate the subseries and place them side by side to compare the quarters.
Add the mean of each quarter: the seasonal subseries plot.
# A tsibble: 640 x 3 [1Q]
# Key: State [8]
State Quarter Trips
<chr> <qtr> <dbl>
1 ACT 1998 Q1 196.
2 ACT 1998 Q2 127.
3 ACT 1998 Q3 111.
4 ACT 1998 Q4 170.
5 ACT 1999 Q1 108.
6 ACT 1999 Q2 125.
7 ACT 1999 Q3 178.
8 ACT 1999 Q4 218.
9 ACT 2000 Q1 158.
10 ACT 2000 Q2 155.
# ℹ 630 more rows
library(ggtime)
vic_elec |>
filter(year(Date) == 2014) |>
as_tibble() |>
ggplot(aes(x = Time, y = Demand)) +
geom_line() +
coord_calendar(rows = month(1L), cols = NULL) +
scale_x_mixtime(
time_breaks = mixtime::days(1L),
time_labels = "{cyc(day, cal_isoweek$week, label = TRUE, abbreviate = TRUE)}"
) +
theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())coord_calendar() cuts a time axis into a calendar-like grid of rows/columnsDifferences between seasonal and cyclic patterns:

Australian electricity production: seasonal, cyclic, both or neither?

Australian clay brick production: seasonal, cyclic, both or neither?

US retail employment: seasonal, cyclic, both or neither?

Amazon closing stock price: seasonal, cyclic, both or neither?

Annual Canadian lynx trappings: seasonal, cyclic, both or neither?
Differences between seasonal and cyclic patterns:
The timing of peaks and troughs is predictable with seasonal data, but unpredictable in the long term with cyclic data.
# A tsibble: 74 x 7 [1Q]
Quarter Beer Tobacco Bricks Cement Electricity Gas
<qtr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1992 Q1 443 5777 383 1289 38332 117
2 1992 Q2 410 5853 404 1501 39774 151
3 1992 Q3 420 6416 446 1539 42246 175
4 1992 Q4 532 5825 420 1568 38498 129
5 1993 Q1 433 5724 394 1450 39460 116
6 1993 Q2 421 6036 462 1668 41356 149
7 1993 Q3 410 6570 475 1648 42949 163
8 1993 Q4 512 5675 443 1863 40974 138
9 1994 Q1 449 5311 421 1468 40162 127
10 1994 Q2 381 5717 475 1755 41199 159
# ℹ 64 more rows
Time plot of quarterly Australian beer production since 1992.
Shift a copy of the series (orange) by one quarter, pairing each observation with the one before it.
Each pair is a point in the lag plot: \(y_t\) against \(y_{t-1}\). The first observation has no pair, so it is dropped.
The autocorrelation \(r_1\) measures the linear relationship in this lag plot (orange line). It is small and negative: the first ACF bar.
Shift the copy by another quarter. At lag 2, each quarter is paired with the quarter half a year earlier, so the seasonality is out of phase: peaks are paired with troughs, and \(r_2\) is strongly negative.
Shift the copy by another quarter. At lag 3, each quarter is paired with the following quarter of the previous year. Like lag 1, these are neighbouring quarters, so \(r_3\) is close to zero.
Shift the copy to lag 4. Now each quarter is paired with the same quarter a year earlier, so the seasonal peaks and troughs line up: \(r_4\) is large and positive.
Continue for lags 5, 6, … The seasonal lags 8, 12 and 16 also align the same quarters, so they are strongly positive too.
The ACF plot. Dashed blue lines indicate 95% significance thresholds to help identify white noise.
Results for first 9 lags for beer data:
Results for first 9 lags for beer data:
# A tsibble: 152 x 3 [1Q]
# Key: State [8]
State lag acf
<chr> <cf_lag> <dbl>
1 ACT 1 quarter 0.0877
2 ACT 2 quarter 0.252
3 ACT 3 quarter -0.0496
4 ACT 4 quarter 0.300
5 ACT 5 quarter -0.0741
6 ACT 6 quarter 0.269
7 ACT 7 quarter -0.00504
8 ACT 8 quarter 0.236
9 ACT 9 quarter -0.0953
10 ACT 10 quarter 0.0750
# ℹ 142 more rows
# A tsibble: 252 x 2 [!]
Date Close
<date> <dbl>
1 2015-01-02 522.
2 2015-01-05 511.
3 2015-01-06 499.
4 2015-01-07 498.
5 2015-01-08 500.
6 2015-01-09 493.
7 2015-01-12 490.
8 2015-01-13 493.
9 2015-01-14 498.
10 2015-01-15 499.
# ℹ 242 more rows

Match each time series (1-4) to its ACF (A-D)
White noise data is uncorrelated across time with zero mean and constant variance.
(Technically, we require independence as well.)
| \(r_{1}\) | \(r_{2}\) | \(r_{3}\) | \(r_{4}\) | \(r_{5}\) | \(r_{6}\) | \(r_{7}\) | \(r_{8}\) | \(r_{9}\) | \(r_{10}\) |
|---|---|---|---|---|---|---|---|---|---|
| 0.001 | -0.216 | -0.144 | 0.033 | 0.126 | -0.039 | 0.100 | -0.051 | -0.198 | -0.198 |

Is the number of pigs slaughtered a white noise series?
Monthly total number of pigs slaughtered in the state of Victoria, Australia, from January 2014 through December 2018 (Source: Australian Bureau of Statistics.)
These show the series is not a white noise series.