forked from robjhyndman/ETC3550Slides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
8-arima.Rmd
1649 lines (1179 loc) · 45.1 KB
/
8-arima.Rmd
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "ETC3550: Applied forecasting for business and economics"
author: "Ch8. ARIMA models"
date: "OTexts.org/fpp2/"
fontsize: 14pt
output:
beamer_presentation:
fig_width: 7
fig_height: 3.5
highlight: tango
theme: metropolis
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, cache=TRUE, warning=FALSE, message=FALSE)
library(fpp2)
```
# Stationarity and differencing
## Stationarity
\begin{block}{Definition}
If $\{y_t\}$ is a stationary time series, then for all $s$, the distribution of $(y_t,\dots,y_{t+s})$ does not depend on $t$.
\end{block}\pause
A \textbf{stationary series} is:
* roughly horizontal
* constant variance
* no patterns predictable in the long-term
## Stationary?
```{r}
autoplot(dj) + ylab("Dow Jones Index") + xlab("Day")
```
## Stationary?
```{r}
autoplot(diff(dj)) + ylab("Change in Dow Jones Index") + xlab("Day")
```
## Stationary?
```{r}
autoplot(strikes) + ylab("Number of strikes") + xlab("Year")
```
## Stationary?
```{r}
autoplot(hsales) + xlab("Year") + ylab("Total sales") +
ggtitle("Sales of new one-family houses, USA")
```
## Stationary?
```{r}
autoplot(eggs) + xlab("Year") + ylab("$") +
ggtitle("Price of a dozen eggs in 1993 dollars")
```
## Stationary?
```{r}
autoplot(window(pigs/1e3, start=1990)) + xlab("Year") + ylab("thousands") +
ggtitle("Number of pigs slaughtered in Victoria")
```
## Stationary?
```{r}
autoplot(lynx) + xlab("Year") + ylab("Number trapped") +
ggtitle("Annual Canadian Lynx Trappings")
```
## Stationary?
```{r}
autoplot(window(ausbeer, start=1992)) + xlab("Year") + ylab("megalitres") +
ggtitle("Australian quarterly beer production")
```
## Stationarity
\begin{block}{Definition}
If $\{y_t\}$ is a stationary time series, then for all $s$, the distribution of $(y_t,\dots,y_{t+s})$ does not depend on $t$.
\end{block}\pause\vspace*{0.4cm}
Transformations help to \textbf{stabilize the variance}.
For ARIMA modelling, we also need to \textbf{stabilize the mean}.
## Non-stationarity in the mean
\structure{Identifying non-stationary series}
* time plot.
* The ACF of stationary data drops to zero relatively quickly
* The ACF of non-stationary data decreases slowly.
* For non-stationary data, the value of $r_1$ is often
large and positive.
## Example: Dow-Jones index
```{r}
autoplot(dj) + ylab("Dow Jones Index") + xlab("Day")
```
## Example: Dow-Jones index
```{r}
ggAcf(dj)
```
## Example: Dow-Jones index
```{r}
autoplot(diff(dj)) + ylab("Change in Dow Jones Index") + xlab("Day")
```
## Example: Dow-Jones index
```{r}
ggAcf(diff(dj))
```
## Differencing
* Differencing helps to \textbf{stabilize the mean}.
* The differenced series is the \emph{change} between each observation
in the original series: ${y'_t = y_t - y_{t-1}}$.
* The differenced series will have only $T-1$ values since it is not possible to calculate a difference $y_1'$ for the first observation.
## Second-order differencing
Occasionally the differenced data will not appear stationary and it
may be necessary to difference the data a second time:\pause
\begin{align*}
y''_{t} &= y'_{t} - y'_{t - 1} \\
&= (y_t - y_{t-1}) - (y_{t-1}-y_{t-2})\\
&= y_t - 2y_{t-1} +y_{t-2}.
\end{align*}\pause
* $y_t''$ will have $T-2$ values.
* In practice, it is almost never necessary to go beyond second-order
differences.
## Seasonal differencing
A seasonal difference is the difference between an observation and the corresponding observation from the previous year.\pause
$${y'_t = y_t - y_{t-m}}$$
where $m=$ number of seasons.\pause
* For monthly data $m=12$.
* For quarterly data $m=4$.
## Electricity production
```{r, echo=TRUE, fig.height=4}
usmelec %>% autoplot()
```
## Electricity production
```{r, echo=TRUE, fig.height=4}
usmelec %>% log() %>% autoplot()
```
## Electricity production
```{r, echo=TRUE, fig.height=3.5}
usmelec %>% log() %>% diff(lag=12) %>%
autoplot()
```
## Electricity production
```{r, echo=TRUE, fig.height=3.5}
usmelec %>% log() %>% diff(lag=12) %>%
diff(lag=1) %>% autoplot()
```
## Electricity production
* Seasonally differenced series is closer to being stationary.
* Remaining non-stationarity can be removed with further first difference.
If $y'_t = y_t - y_{t-12}$ denotes seasonally differenced series, then twice-differenced series i
\begin{block}{}
\begin{align*}
y^*_t &= y'_t - y'_{t-1} \\
&= (y_t - y_{t-12}) - (y_{t-1} - y_{t-13}) \\
&= y_t - y_{t-1} - y_{t-12} + y_{t-13}\: .
\end{align*}
\end{block}\vspace*{10cm}
## Seasonal differencing
When both seasonal and first differences are applied\dots\pause
* it makes no difference
which is done first---the result will be the same.
* If seasonality is strong, we recommend that seasonal differencing be done first because sometimes the resulting series will be stationary and there will be no need for further first difference.
\pause
It is important that if differencing is used, the differences are
interpretable.
## Interpretation of differencing
* first differences are the change between \textbf{one observation and the
next};
* seasonal differences are the change between \textbf{one year to the
next}.
\pause
But taking lag 3 differences for yearly data, for example, results in a model which cannot be sensibly interpreted.
## Unit root tests
\structure{Statistical tests to determine the required order of differencing.}
1. Augmented Dickey Fuller test: null hypothesis is that the data are non-stationary and non-seasonal.
2. Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test: null hypothesis is that the data are stationary and non-seasonal.
3. Other tests available for seasonal data.
## KPSS test
\fontsize{10}{11}\sf
```{r, echo=TRUE}
library(urca)
summary(ur.kpss(goog))
```
\pause
```{r, echo=TRUE}
ndiffs(goog)
```
## Automatically selecting differences
STL decomposition: $y_t = T_t+S_t+R_t$
Seasonal strength $F_s = \max\big(0, 1-\frac{\text{Var}(R_t)}{\text{Var}(S_t+R_t)}\big)$
If $F_s > 0.64$, do one seasonal difference.
\pause\fontsize{12}{15}\sf\vspace*{1cm}
```{r, echo=TRUE}
usmelec %>% log() %>% nsdiffs()
usmelec %>% log() %>% diff(lag=12) %>% ndiffs()
```
## Your turn
For the `visitors` series, find an appropriate differencing (after transformation if necessary) to obtain stationary data.
## Backshift notation
A very useful notational device is the backward shift operator, $B$, which is used as follows:
$$
{B y_{t} = y_{t - 1}} \: .
$$\pause
In other words, $B$, operating on $y_{t}$, has the
effect of \textbf{shifting the data back one period}. \pause
Two applications of $B$ to $y_{t}$ \textbf{shifts the data back two
periods}:
$$
B(By_{t}) = B^{2}y_{t} = y_{t-2}\: .
$$\pause
For monthly data, if we wish to shift attention to ``the
same month last year,'' then $B^{12}$
is used, and the
notation is $B^{12}y_{t}$ = $y_{t-12}$.
## Backshift notation
The backward shift operator is convenient for describing the
process of \textit{differencing}. \pause
A first difference can be written as
$$
y'_{t} = y_{t} - y_{t-1} = y_t - By_{t} = (1 - B)y_{t}\: .
$$\pause
Note that a first difference is represented by $(1 - B)$.
\pause
Similarly, if second-order differences (i.e., first
differences of first differences) have to be computed,
then:
\[
y''_{t} = y_{t} - 2y_{t - 1} + y_{t - 2} = (1 - B)^{2} y_{t}\: .
\]
## Backshift notation
* Second-order difference is denoted $(1- B)^{2}$.
* \textit{Second-order difference} is not the same as a \textit{second difference}, which would be denoted $1- B^{2}$;
* In general, a $d$th-order difference can be written as
$$(1 - B)^{d} y_{t}.$$
* A seasonal difference followed by a first difference can be written as
$$ (1-B)(1-B^m)y_t\: .$$
## Backshift notation
The ``backshift'' notation is convenient because the terms can be multiplied
together to see the combined effect.
\begin{align*}
(1-B)(1-B^m)y_t &= (1 - B - B^m + B^{m+1})y_t \\
&= y_t-y_{t-1}-y_{t-m}+y_{t-m-1}.
\end{align*}\pause
For monthly data, $m=12$ and we obtain the same result as earlier.
# Non-seasonal ARIMA models
## Autoregressive models
\begin{block}{Autoregressive (AR) models:}
$$
y_{t} = c + \phi_{1}y_{t - 1} + \phi_{2}y_{t - 2} + \cdots + \phi_{p}y_{t - p} + \varepsilon_{t},
$$
where $\varepsilon_t$ is white noise. This is a multiple regression with \textbf{lagged values} of $y_t$ as predictors.
\end{block}
```{r arp, echo=FALSE, fig.height=3}
set.seed(1)
p1 <- autoplot(10 + arima.sim(list(ar = -0.8), n = 100)) +
ylab("") + ggtitle("AR(1)")
p2 <- autoplot(20 + arima.sim(list(ar = c(1.3, -0.7)), n = 100)) +
ylab("") + ggtitle("AR(2)")
gridExtra::grid.arrange(p1,p2,nrow=1)
```
## AR(1) model
\begin{block}{}
\centerline{$y_{t} = 2 -0.8 y_{t - 1} + \varepsilon_{t}$}
\end{block}
\rightline{$\varepsilon_t\sim N(0,1)$,\quad $T=100$.}
```{r, echo=FALSE, out.width="50%", fig.height=2.2, fig.width=2.2}
p1
```
## AR(1) model
\begin{block}{}
\centerline{$y_{t} = c + \phi_1 y_{t - 1} + \varepsilon_{t}$}
\end{block}
* When $\phi_1=0$, $y_t$ is **equivalent to WN**
* When $\phi_1=1$ and $c=0$, $y_t$ is **equivalent to a RW**
* When $\phi_1=1$ and $c\ne0$, $y_t$ is **equivalent to a RW with drift**
* When $\phi_1<0$, $y_t$ tends to **oscillate between positive and negative values**.
## AR(2) model
\begin{block}{}
\centerline{$y_t = 8 + 1.3y_{t-1} - 0.7 y_{t-2} + \varepsilon_t$}
\end{block}
\rightline{$\varepsilon_t\sim N(0,1)$, \qquad $T=100$.}
```{r, fig.height=2.2, fig.width=2.2, out.width="50%"}
p2
```
## Stationarity conditions
We normally restrict autoregressive models to stationary data, and then some constraints on the values of the parameters are required.
\begin{block}{General condition for stationarity}
Complex roots of $1-\phi_1 z - \phi_2 z^2 - \dots - \phi_pz^p$ lie outside the unit circle on the complex plane.
\end{block}\pause
* For $p=1$: $-1<\phi_1<1$.
* For $p=2$:\newline $-1<\phi_2<1\qquad \phi_2+\phi_1 < 1 \qquad \phi_2 -\phi_1 < 1$.
* More complicated conditions hold for $p\ge3$.
* Estimation software takes care of this.
## Moving Average (MA) models
\begin{block}{Moving Average (MA) models:}
$$
y_{t} = c + \varepsilon_t + \theta_{1}\varepsilon_{t - 1} + \theta_{2}\varepsilon_{t - 2} + \cdots + \theta_{q}\varepsilon_{t - q},
$$
where $\varepsilon_t$ is white noise.
This is a multiple regression with \textbf{past \emph{errors}}
as predictors. \emph{Don't confuse this with moving average smoothing!}
\end{block}
```{r maq, fig.height=2.5}
set.seed(2)
p1 <- autoplot(20 + arima.sim(list(ma = 0.8), n = 100)) +
ylab("") + ggtitle("MA(1)")
p2 <- autoplot(arima.sim(list(ma = c(-1, +0.8)), n = 100)) +
ylab("") + ggtitle("MA(2)")
gridExtra::grid.arrange(p1,p2,nrow=1)
```
## MA(1) model
\begin{block}{}
\centerline{$y_t = 20 + \varepsilon_t + 0.8 \varepsilon_{t-1}$}
\end{block}
\rightline{$\varepsilon_t\sim N(0,1)$,\quad $T=100$.}
```{r, fig.height=2.2, fig.width=2.2, out.width="50%"}
p1
```
## MA(2) model
\begin{block}{}
\centerline{$y_t = \varepsilon_t -\varepsilon_{t-1} + 0.8 \varepsilon_{t-2}$}
\end{block}
\rightline{$\varepsilon_t\sim N(0,1)$,\quad $T=100$.}
```{r, fig.height=2.2, fig.width=2.2, out.width="50%"}
p2
```
## MA($\infty$) models
It is possible to write any stationary AR($p$) process as an MA($\infty$) process.
**Example: AR(1)**
\begin{align*}
y_t &= \phi_1y_{t-1} + \varepsilon_t\\
&= \phi_1(\phi_1y_{t-2} + \varepsilon_{t-1}) + \varepsilon_t\\
&= \phi_1^2y_{t-2} + \phi_1 \varepsilon_{t-1} + \varepsilon_t\\
&= \phi_1^3y_{t-3} + \phi_1^2\varepsilon_{t-2} + \phi_1 \varepsilon_{t-1} + \varepsilon_t\\
&\dots
\end{align*}\pause
Provided $-1 < \phi_1 < 1$:
\[ y_t = \varepsilon_t + \phi_1 \varepsilon_{t-1} + \phi_1^2 \varepsilon_{t-2} + \phi_1^3 \varepsilon_{t-3} + \cdots
\]
## Invertibility
* Any MA($q$) process can be written as an AR($\infty$) process if we impose some constraints on the MA parameters.
* Then the MA model is called "invertible".
* Invertible models have some mathematical properties that make them easier to use in practice.
* Invertibility of an ARIMA model is equivalent to forecastability of an ETS model.
## Invertibility
\begin{block}{General condition for invertibility}
Complex roots of $1+\theta_1 z + \theta_2 z^2 + \dots + \theta_qz^q$ lie outside the unit circle on the complex plane.
\end{block}\pause
* For $q=1$: $-1<\theta_1<1$.
* For $q=2$:\newline $-1<\theta_2<1\qquad \theta_2+\theta_1 >-1 \qquad \theta_1 -\theta_2 < 1$.
* More complicated conditions hold for $q\ge3$.
* Estimation software takes care of this.
## ARIMA models
\begin{block}{Autoregressive Moving Average models:}
\begin{align*}
y_{t} &= c + \phi_{1}y_{t - 1} + \cdots + \phi_{p}y_{t - p} \\
& \hspace*{2.4cm}\text{} + \theta_{1}\varepsilon_{t - 1} + \cdots + \theta_{q}\varepsilon_{t - q} + \varepsilon_{t}.
\end{align*}
\end{block}\pause
* Predictors include both **lagged values of $y_t$ and lagged errors.**
* Conditions on coefficients ensure stationarity.
* Conditions on coefficients ensure invertibility.
\pause
### Autoregressive Integrated Moving Average models
* Combine ARMA model with **differencing**.
* $(1-B)^d y_t$ follows an ARMA model.
## ARIMA models
\structure{Autoregressive Integrated Moving Average models}
\begin{block}{ARIMA($p, d, q$) model}
\begin{tabular}{rl}
AR:& $p =$ order of the autoregressive part\\
I: & $d =$ degree of first differencing involved\\
MA:& $q =$ order of the moving average part.
\end{tabular}
\end{block}
* White noise model: ARIMA(0,0,0)
* Random walk: ARIMA(0,1,0) with no constant
* Random walk with drift: ARIMA(0,1,0) with \rlap{const.}
* AR($p$): ARIMA($p$,0,0)
* MA($q$): ARIMA(0,0,$q$)
## Backshift notation for ARIMA
* ARMA model:\vspace*{-1cm}\newline
\parbox{12cm}{\small\begin{align*}
\hspace*{-1cm}
y_{t} &= c + \phi_{1}By_{t} + \cdots + \phi_pB^py_{t}
+ \varepsilon_{t} + \theta_{1}B\varepsilon_{t} + \cdots + \theta_qB^q\varepsilon_{t} \\
\hspace*{-1cm}
\text{or}\quad & (1-\phi_1B - \cdots - \phi_p B^p) y_t = c + (1 + \theta_1 B + \cdots + \theta_q B^q)\varepsilon_t
\end{align*}}
* ARIMA(1,1,1) model:
\[
\begin{array}{c c c c}
(1 - \phi_{1} B) & (1 - B) y_{t} &= &c + (1 + \theta_{1} B) \varepsilon_{t}\\
{\uparrow} & {\uparrow} & &{\uparrow}\\
{\text{AR(1)}} & {\text{First}} & &{\text{MA(1)}}\\
& {\hbox to 0cm{\hss\text{difference}\hss}}\\
\end{array}
\]\pause
Written out:
$$y_t = c + y_{t-1} + \phi_1 y_{t-1}- \phi_1 y_{t-2} + \theta_1\varepsilon_{t-1} + \varepsilon_t $$
## R model
\fontsize{13}{16}\sf
\begin{block}{Intercept form}
\centerline{$(1-\phi_1B - \cdots - \phi_p B^p) y_t' = c + (1 + \theta_1 B + \cdots + \theta_q B^q)\varepsilon_t$}
\end{block}
\begin{block}{Mean form}
\centerline{$(1-\phi_1B - \cdots - \phi_p B^p)(y_t' - \mu) = (1 + \theta_1 B + \cdots + \theta_q B^q)\varepsilon_t$}
\end{block}
* $y_t' = (1-B)^d y_t$
* $\mu$ is the mean of $y_t'$.
* $c = \mu(1-\phi_1 - \cdots - \phi_p )$.
## US personal consumption
```{r}
autoplot(uschange[,"Consumption"]) +
xlab("Year") +
ylab("Quarterly percentage change") +
ggtitle("US consumption")
```
## US personal consumption
\fontsize{10}{11}\sf
```{r, echo=TRUE}
(fit <- auto.arima(uschange[,"Consumption"]))
```
\pause\vfill
```
```{r usconsumptioncoefs, echo=FALSE}
coef <- coefficients(fit)
intercept <- coef['intercept'] * (1-coef['ar1'] - coef['ar2'])
```
```{r, include=FALSE}
if(!identical(arimaorder(fit),c(p=2L,d=0L,q=2L)))
stop("Different model from expected")
```
### ARIMA(2,0,2) model:
\centerline{$
y_t = c + `r format(coef['ar1'], nsmall=3, digits=3)`y_{t-1}
`r format(coef['ar2'], nsmall=3, digits=3)` y_{t-2}
`r format(coef['ma1'], nsmall=3, digits=3)` \varepsilon_{t-1}
+ `r format(coef['ma2'], nsmall=3, digits=3)` \varepsilon_{t-2}
+ \varepsilon_{t},
$}
where $c= `r format(coef['intercept'], nsmall=3, digits=3)` \times (1 - `r format(coef['ar1'], nsmall=3, digits=3)` + `r format(-coef['ar2'], nsmall=3, digits=3)`) = `r format(intercept, nsmall=3, digits=3)`$
and $\varepsilon_t$ is white noise with a standard deviation of $`r format(sqrt(fit$sigma2), nsmall=3, digits=3)` = \sqrt{`r format(fit$sigma2, nsmall=3, digits=3)`}$.
## US personal consumption
\fontsize{12}{15}\sf
```{r, echo=TRUE, fig.height=4}
fit %>% forecast(h=10) %>% autoplot(include=80)
```
## Understanding ARIMA models
\fontsize{14}{16}\sf
* If $c=0$ and $d=0$, the long-term forecasts will go to zero.
* If $c=0$ and $d=1$, the long-term forecasts will go to a non-zero constant.
* If $c=0$ and $d=2$, the long-term forecasts will follow a straight line.
* If $c\ne0$ and $d=0$, the long-term forecasts will go to the mean of the data.
* If $c\ne0$ and $d=1$, the long-term forecasts will follow a straight line.
* If $c\ne0$ and $d=2$, the long-term forecasts will follow a quadratic trend.
## Understanding ARIMA models
\fontsize{14}{15.5}\sf
### Forecast variance and $d$
* The higher the value of $d$, the more rapidly the prediction intervals increase in size.
* For $d=0$, the long-term forecast standard deviation will go to the standard deviation of the historical data.
### Cyclic behaviour
* For cyclic forecasts, $p\ge2$ and some restrictions on coefficients are required.
* If $p=2$, we need $\phi_1^2+4\phi_2<0$. Then average cycle of length
\[
(2\pi)/\left[\text{arc cos}(-\phi_1(1-\phi_2)/(4\phi_2))\right].
\]
# Estimation and order selection
## Maximum likelihood estimation
Having identified the model order, we need to estimate the
parameters $c$, $\phi_1,\dots,\phi_p$,
$\theta_1,\dots,\theta_q$.\pause
* MLE is very similar to least squares estimation obtained by minimizing
$$\sum_{t-1}^T e_t^2.$$
* The `Arima()` command allows CLS or MLE estimation.
* Non-linear optimization must be used in either case.
* Different software will give different estimates.
## Partial autocorrelations
\structure{Partial autocorrelations} measure relationship\newline
between $y_{t}$ and $y_{t - k}$, when
the effects of other time lags --- $1,
2, 3, \dots, k - 1$ --- are removed.\pause
\begin{block}{}
\begin{align*}
\alpha_k&= \text{$k$th partial autocorrelation coefficient}\\
&= \text{equal to the estimate of $b_k$ in regression:}\\
& \hspace*{0.8cm} y_t = c + \phi_1 y_{t-1} + \phi_2 y_{t-2} + \dots + \phi_k y_{t-k}.
\end{align*}
\end{block}\pause
* Varying number of terms on RHS gives $\alpha_k$ for different values of $k$.
* There are more efficient ways of calculating $\alpha_k$.
* $\alpha_1=\rho_1$
* same critical values of $\pm 1.96/\sqrt{T}$ as for ACF.
## Example: US consumption
```{r}
uschange[,"Consumption"] %>% diff() %>%
autoplot() +
xlab("Year") +
ylab("Quarterly percentage change") +
ggtitle("US consumption")
```
## Example: US consumption
```{r usconsumptionacf}
p1 <- ggAcf(uschange[,"Consumption"],main="")
p2 <- ggPacf(uschange[,"Consumption"],main="")
gridExtra::grid.arrange(p1,p2,nrow=1)
```
## ACF and PACF interpretation
**AR(1)**
\begin{align*}
\hspace*{1cm}\rho_k &= \phi_1^k\qquad\text{for $k=1,2,\dots$};\\
\alpha_1 &= \phi_1 \qquad\alpha_k = 0\qquad\text{for $k=2,3,\dots$}.
\end{align*}
So we have an AR(1) model when
* autocorrelations exponentially decay
* there is a single significant partial autocorrelation.
## ACF and PACF interpretation
**AR($p$)**
* ACF dies out in an exponential or damped sine-wave manner
* PACF has all zero spikes beyond the $p$th spike
So we have an AR($p$) model when
* the ACF is exponentially decaying or sinusoidal
* there is a significant spike at lag $p$ in PACF, but none beyond $p$
## ACF and PACF interpretation
**MA(1)**
\begin{align*}
\hspace*{1cm}\rho_1 &= \theta_1\qquad \rho_k = 0\qquad\text{for $k=2,3,\dots$};\\
\alpha_k &= -(-\theta_1)^k
\end{align*}
So we have an MA(1) model when
* the PACF is exponentially decaying and
* there is a single significant spike in ACF
## ACF and PACF interpretation
**MA($q$)**
* PACF dies out in an exponential or damped sine-wave manner
* ACF has all zero spikes beyond the $q$th spike
So we have an MA($q$) model when
* the PACF is exponentially decaying or sinusoidal
* there is a significant spike at lag $q$ in ACF, but none beyond $q$
## Example: Mink trapping
```{r}
autoplot(mink) +
xlab("Year") +
ylab("Minks trapped (thousands)") +
ggtitle("Annual number of minks trapped")
```
## Example: Mink trapping
```{r}
p1 <- ggAcf(mink,main="")
p2 <- ggPacf(mink,main="")
gridExtra::grid.arrange(p1,p2,nrow=1)
```
## Information criteria
\structure{Akaike's Information Criterion (AIC):}
\centerline{$\text{AIC} = -2 \log(L) + 2(p+q+k+1),$}
where $L$ is the likelihood of the data,\newline
$k=1$ if $c\ne0$ and $k=0$ if $c=0$.\pause\vspace*{0.2cm}
\structure{Corrected AIC:}
\centerline{$\text{AICc} = \text{AIC} + \frac{2(p+q+k+1)(p+q+k+2)}{T-p-q-k-2}.$}\pause\vspace*{0.2cm}
\structure{Bayesian Information Criterion:}
\centerline{$\text{BIC} = \text{AIC} + [\log(T)-2](p+q+k-1).$}
\pause\vspace*{-0.2cm}
\begin{block}{}Good models are obtained by minimizing either the AIC, \text{AICc}\ or BIC\@. Our preference is to use the \text{AICc}.\end{block}
# ARIMA modelling in R
## How does auto.arima() work?
\begin{block}{A non-seasonal ARIMA process}
\[
\phi(B)(1-B)^dy_{t} = c + \theta(B)\varepsilon_t
\]
Need to select appropriate orders: \alert{$p,q, d$}
\end{block}
\structure{Hyndman and Khandakar (JSS, 2008) algorithm:}
* Select no.\ differences \alert{$d$} and \alert{$D$} via KPSS test and seasonal strength measure.
* Select \alert{$p,q$} by minimising AICc.
* Use stepwise search to traverse model space.
## How does auto.arima() work?
\fontsize{12}{13}\sf
\begin{block}{}
\centerline{$\text{AICc} = -2 \log(L) + 2(p+q+k+1)\left[1 +
\frac{(p+q+k+2)}{T-p-q-k-2}\right].$}
where $L$ is the maximised likelihood fitted to the \textit{differenced} data,
$k=1$ if $c\neq 0$ and $k=0$ otherwise.
\end{block}\pause
Step1:
: Select current model (with smallest AICc) from:\newline
ARIMA$(2,d,2)$\newline
ARIMA$(0,d,0)$\newline
ARIMA$(1,d,0)$\newline
ARIMA$(0,d,1)$
\pause\vspace*{-0.1cm}
Step 2:
: Consider variations of current model:
* vary one of $p,q,$ from current model by $\pm1$;
* $p,q$ both vary from current model by $\pm1$;
* Include/exclude $c$ from current model.
Model with lowest AICc becomes current model.
\structure{Repeat Step 2 until no lower AICc can be found.}
## Choosing your own model
```{r, echo=TRUE, fig.height=4}
ggtsdisplay(internet)
```
## Choosing your own model
```{r, echo=TRUE, fig.height=4}
ggtsdisplay(diff(internet))
```
## Choosing your own model
\fontsize{13}{14}\sf
```{r, echo=TRUE, fig.height=4}
(fit <- Arima(internet,order=c(3,1,0)))
```
## Choosing your own model
\fontsize{13}{14}\sf
```{r, echo=TRUE, fig.height=4}
auto.arima(internet)
```
## Choosing your own model
\fontsize{13}{14}\sf
```{r internettryharder, echo=TRUE, fig.height=4}
auto.arima(internet, stepwise=FALSE,
approximation=FALSE)
```
## Choosing your own model
```r
checkresiduals(fit)
```
```{r, echo=FALSE, fig.height=4}
checkresiduals(fit, test=FALSE)
```
## Choosing your own model
```{r, echo=FALSE}
checkresiduals(fit, plot=FALSE)
```
## Choosing your own model
```{r, echo=TRUE, fig.height=4}
fit %>% forecast %>% autoplot
```
## Modelling procedure with `Arima`
\fontsize{12}{13}\sf
1. Plot the data. Identify any unusual observations.
2. If necessary, transform the data (using a Box-Cox transformation) to stabilize the variance.
3. If the data are non-stationary: take first differences of the data until the data are stationary.
4. Examine the ACF/PACF: Is an AR($p$) or MA($q$) model appropriate?
5. Try your chosen model(s), and use the \text{AICc} to search for a better model.
6. Check the residuals from your chosen model by plotting the ACF of the residuals, and doing a portmanteau test of the residuals. If they do not look like white noise, try a modified model.
7. Once the residuals look like white noise, calculate forecasts.
## Modelling procedure with `auto.arima`
\fontsize{12}{13}\sf
1. Plot the data. Identify any unusual observations.
2. If necessary, transform the data (using a Box-Cox transformation) to stabilize the variance.
\vspace*{1.15cm}
3. Use `auto.arima` to select a model.
\vspace*{1.15cm}
6. Check the residuals from your chosen model by plotting the ACF of the residuals, and doing a portmanteau test of the residuals. If they do not look like white noise, try a modified model.
7. Once the residuals look like white noise, calculate forecasts.
## Modelling procedure
\centerline{\includegraphics[height=8.cm]{Figure-8-10}}
## \large Seasonally adjusted electrical equipment
\fontsize{11.5}{15}\sf
```{r ee1, fig.height=3.3, echo=TRUE}
eeadj <- seasadj(stl(elecequip, s.window="periodic"))
autoplot(eeadj) + xlab("Year") +
ylab("Seasonally adjusted new orders index")
```
## \large Seasonally adjusted electrical equipment
1. Time plot shows sudden changes, particularly big drop in 2008/2009 due to global economic environment. Otherwise nothing unusual and no need for data adjustments.
2. No evidence of changing variance, so no Box-Cox transformation.
3. Data are clearly non-stationary, so we take first differences.
## \large Seasonally adjusted electrical equipment
```{r ee2, echo=TRUE, fig.height=4}
ggtsdisplay(diff(eeadj))
```
## \large Seasonally adjusted electrical equipment
4. PACF is suggestive of AR(3). So initial candidate model is ARIMA(3,1,0). No other obvious candidates.
5. Fit ARIMA(3,1,0) model along with variations: ARIMA(4,1,0), ARIMA(2,1,0), ARIMA(3,1,1), etc. ARIMA(3,1,1) has smallest \text{AICc} value.
## \large Seasonally adjusted electrical equipment
\fontsize{10}{10}\sf
```{r, echo=TRUE}
(fit <- Arima(eeadj, order=c(3,1,1)))
```
## \large Seasonally adjusted electrical equipment
6. ACF plot of residuals from ARIMA(3,1,1) model look like white noise.
\fontsize{11}{11}\sf
```r
checkresiduals(fit)
```
```{r, echo=FALSE, fig.height=3.4}
checkresiduals(fit)
```
## \large Seasonally adjusted electrical equipment
```{r, echo=FALSE}
checkresiduals(fit, plot=FALSE)
```
## \large Seasonally adjusted electrical equipment
```{r, echo=TRUE}
fit %>% forecast %>% autoplot
```
# Forecasting
## Point forecasts
1. Rearrange ARIMA equation so $y_t$ is on LHS.
2. Rewrite equation by replacing $t$ by $T+h$.
3. On RHS, replace future observations by their forecasts, future errors by zero, and past errors by corresponding residuals.
Start with $h=1$. Repeat for $h=2,3,\dots$.
## Point forecasts
\fontsize{14}{14}\sf
\structure{ARIMA(3,1,1) forecasts: Step 1}
\begin{block}{}
\centerline{$(1-\phi_1B -\phi_2B^2-\phi_3B^3)(1-B) y_t = (1+\theta_1B)\varepsilon_{t},$}
\end{block}
\pause\vspace*{-0.4cm}
\begin{align*}
\left[1-(1+\phi_1)B +(\phi_1-\phi_2)B^2 + (\phi_2-\phi_3)B^3 +\phi_3B^4\right] y_t\\ = (1+\theta_1B)\varepsilon_{t},
\end{align*}\pause\vspace*{-0.4cm}
\begin{align*}
y_t - (1+\phi_1)y_{t-1} +(\phi_1-\phi_2)y_{t-2} + (\phi_2-\phi_3)y_{t-3}\\ \mbox{}+\phi_3y_{t-4} = \varepsilon_t+\theta_1\varepsilon_{t-1}.
\end{align*}\pause\vspace*{-0.4cm}
\begin{align*}
y_t = (1+\phi_1)y_{t-1} -(\phi_1-\phi_2)y_{t-2} - (\phi_2-\phi_3)y_{t-3}\\\mbox{} -\phi_3y_{t-4} + \varepsilon_t+\theta_1\varepsilon_{t-1}.
\end{align*}
## Point forecasts (h=1)
\fontsize{14}{14}\sf