forked from robjhyndman/ETC3550Slides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-tsgraphics.qmd
163 lines (132 loc) · 4.57 KB
/
2-tsgraphics.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
title: "ETC3550/ETC5550 Applied forecasting"
author: "Ch2. Time series graphics"
institute: "OTexts.org/fpp3/"
pdf-engine: pdflatex
fig-width: 7.5
fig-height: 3.5
format:
beamer:
theme: monash
aspectratio: 169
fontsize: 14pt
section-titles: false
knitr:
opts_chunk:
dev: "cairo_pdf"
include-in-header: header.tex
execute:
echo: false
message: false
warning: false
---
```{r setup, include=FALSE}
source("setup.R")
library(patchwork)
global_economy <- global_economy |>
select(Year, Country, GDP, Imports, Exports, Population)
tourism <- tourism |>
mutate(
State = recode(State,
"Australian Capital Territory" = "ACT",
"New South Wales" = "NSW",
"Northern Territory" = "NT",
"Queensland" = "QLD",
"South Australia" = "SA",
"Tasmania" = "TAS",
"Victoria" = "VIC",
"Western Australia" = "WA"
)
)
```
## `tsibble` objects
\fontsize{10}{10.8}\sf
```{r, echo = TRUE}
global_economy
```
\only<2->{\begin{textblock}{.75}(1.65,3.4)
\begin{alertblock}{}\fontsize{9}{9}\sf Index\phantom{dg}\end{alertblock}
\end{textblock}}
\only<3->{\begin{textblock}{1.6}(2.78,3.4)
\begin{alertblock}{}\fontsize{9}{9}\sf Key\phantom{dg}\end{alertblock}
\end{textblock}}
\only<4>{\begin{textblock}{6.7}(4.8,3.4)
\begin{alertblock}{}\fontsize{9}{9}\sf Measured variables\phantom{dg}\end{alertblock}
\end{textblock}}
## `tsibble` objects
\fontsize{10}{10.8}\sf
```{r, echo = TRUE}
tourism
```
\only<3->{\begin{textblock}{.98}(1.65,3.37)
\begin{alertblock}{}\fontsize{9}{9}\sf Index\phantom{dg}\end{alertblock}
\end{textblock}}
\only<4->{\begin{textblock}{3.9}(3,3.37)
\begin{alertblock}{}\fontsize{9}{9}\sf Keys\phantom{dg}\end{alertblock}
\end{textblock}}
\only<5>{\begin{textblock}{1.5}(7.4,3.37)
\begin{alertblock}{}\fontsize{9}{9}\sf Measure\phantom{dg}\end{alertblock}
\end{textblock}}
\only<2->{\begin{textblock}{3}(9,5)
\begin{block}{}\fontsize{10}{10}\sf Domestic visitor nights in thousands by state/region and purpose.\phantom{dg}\end{block}
\end{textblock}}
## `tsibble` objects
* A `tsibble` allows storage and manipulation of multiple time series in R.
* It contains:
+ An index: time information about the observation
+ Measured variable(s): numbers of interest
+ Key variable(s): optional unique identifiers for each series
* It works with tidyverse functions.
## The `tsibble` index
Time index variables can be created with these functions:
###
\vspace*{-0.2cm}
```{r tstable2, echo=FALSE}
tribble(
~`Frequency`, ~Function,
"Annual", "`start:end`",
"Quarterly", "`yearquarter()`",
"Monthly", "`yearmonth()`",
"Weekly", "`yearweek()`",
"Daily", "`as_date()`, `ymd()`",
"Sub-daily", "`as_datetime()`"
) |>
knitr::kable(booktabs = TRUE)
```
## Seasonal or cyclic?
\alert{Differences between seasonal and cyclic patterns:}
* seasonal pattern constant length; cyclic pattern variable length
* average length of cycle longer than length of seasonal pattern
* magnitude of cycle more variable than magnitude of seasonal pattern
\pause
\begin{alertblock}{}
The timing of peaks and troughs is predictable with seasonal data, but unpredictable in the long term with cyclic data.
\end{alertblock}
## Trend and seasonality in ACF plots
- When data have a trend, the autocorrelations for small lags tend to be large and positive.
- When data are seasonal, the autocorrelations will be larger at the seasonal lags (i.e., at multiples of the seasonal frequency)
- When data are trended and seasonal, you see a combination of these effects.
## Which is which?
```{r, fig.height=6, fig.width=12, echo=FALSE, warning=FALSE, out.width="15cm"}
cowtemp <- as_tsibble(fma::cowtemp)
USAccDeaths <- as_tsibble(USAccDeaths)
AirPassengers <- as_tsibble(AirPassengers)
mink <- as_tsibble(fma::mink)
tp1 <- autoplot(cowtemp, value) +
labs(x = "", y = "chirps per minute", title = "1. Daily temperature of cow")
tp2 <- autoplot(USAccDeaths, value) +
labs(x = "", y = "thousands", title = "2. Monthly accidental deaths")
tp3 <- autoplot(AirPassengers, value) +
labs(x = "", y = "thousands", title = "3. Monthly air passengers")
tp4 <- autoplot(mink, value) +
labs(x = "", y = "thousands", title = "4. Annual mink trappings")
acfb <- ACF(cowtemp, value) |> autoplot() +
labs(x = "", title = "B") + ylim(-0.4, 1)
acfa <- ACF(USAccDeaths, value) |> autoplot() +
labs(x = "", title = "A") + ylim(-0.4, 1)
acfd <- ACF(AirPassengers, value) |> autoplot() +
labs(x = "", title = "D") + ylim(-0.4, 1)
acfc <- ACF(mink, value) |> autoplot() +
labs(x = "", title = "C") + ylim(-0.4, 1)
(tp1 | tp2 | tp3 | tp4) / (acfa | acfb | acfc | acfd)
```