Professional Documents
Culture Documents
Outline
Time series and forecasting
in R
Exponential smoothing
ARIMA modelling
Rob J Hyndman
29 June 2008
Australian GDP
Australian GDP
> plot(ausgdp)
6500
6000
4500
5000
5500
ausgdp
7000
7500
1975
1980
1985
1990
1995
Time
Apr
144
150
154
126
127
May
155
129
137
131
151
Jun
125
131
129
125
130
Jul
153
145
128
127
119
Aug
146
137
140
143
153
Sep
138
138
143
143
Oct
190
168
151
160
Nov
192
176
177
190
Dec
192
188
184
182
beer
Mar
152
163
150
164
152
140
Feb
148
133
143
134
136
120
> beer
Jan
1991 164
1992 147
1993 139
1994 151
1995 138
160
180
> plot(beer)
1991
1992
1993
1994
1995
Time
Lag plots
Lag plots
> lag.plot(beer,lags=12)
200
100
12 47
11
24
36
48
35
23
120
39
1
15
46
5
28 7
51 3 37
34
16 27
2
13
8
19
26 44
33
45
32
25
49
9
20 21
14
18
3043
17 54
31
426
29
50
38
41
40 52
beer
23
35
22
39
1
15
46
528
3
27373416
2 13
26 45 4 33
25
2129 9 49
38 50
14
17 41 30
40 52
12
47 24
36
35
beer
10
36
48
11
7
19
18
42
44
32
20
31
43
6
15 39
46
49
46
37 27
2
13
19
26
44
25 2021
949
38
14
18
30
31
43
42 6
22
8
32
5
3
28
16
4
29
41 17
40
46
5
7
37
13
8
19
26
45
4433
329
21 25 2920
38 14
18
3041
17
31
43
642
8 19
45 33444
32
21
20
1
5
3
34
16 27
13
30 31
42 40
39
27
28 3
16
2
4
29
18
41
17
43
6
25
37
2
26
38 14
lag 10
100
39 151
46
5
34
16
8
33 45
32
2192920
31
43
140
37
13
30
6
3
2
27
26
38
14
lag 9
10 11
36
28
4
lag 11
160
12
24
36
23
1
5
73
27 34 216
8
19
33 26
45
44
32
29
20 921
38
14
4130 18
17
31 43
40
6
42
28
7
4 4419
25
41 17 18
40
42
35
22
39 15
120
4
29
18
17 30 41
6
42
22
40
11 12
24
23
beer
7 28
15
lag 8
10
36
46
5
16
33
9
40
lag 6
34
12
24
22
1
2335
35
23
22
39
15
10
47
11
12 10 47
24
36
lag 7
11
10
15 39
7 28
27
2
8 19 13
45
4426
25 32 20 21
50
14 38
31
43
34
37
10 12 11
24
36 48
47
39 15
beer
180
160
beer
140
120
46
35
23
1
347
beer
10
48
23
22
5
283 51
7
34
16
27
13
8 2
19
4
26 33
45
44
32
25
50 38499 2021 29
14
41 17 18
30 43
31
40
426
lag 5
12
22
33
45
lag 3
11
12
24
36 48
35 23
37
lag 4
11
10
47
24
48 36
3523
11
200
36
39 15
1
46
5
7
328
51
37
53
16 34
27
13 2
8
19
4
26
45
33 44
32
25
49
21 9
29
20
50
38
14
18 41
30 31 43
52 40 17
6 42
lag 2
12
47
24
180
12
180
53
160
10
24
48
23
22
160
140
34
lag 1
51
35
140
46
5
28 37
51
5337
16
227
1319
8
33 4 45
44 26
32
21
29 49
20 25 50
38
14
18
3143 52
6
55
140
1147
120
115
beer
160
beer
39
54 413017
4240
120
10
22
37
13
35
22
39
115
285
3
37 7 16 3427
2 813
26 33 444
32
29 92021 25
38 14
18
41
31 17
43 30
40
6
42
19
25
180
180
11
10
3523
160
47
22
140
180
36
120
160
12
beer
180
24
48
beer
140
beer
120
beer
100
lag 12
200
> lag.plot(beer,lags=12,do.lines=FALSE)
100
120
140
160
180
200
100
120
140
160
180
200
10
11
12
PACF
> pacf(beer)
0.4
1.0
ACF
0.2
0.0
Partial ACF
0.4
0.2
!0.4
!0.2
!0.2
0.0
ACF
0.6
0.8
> acf(beer)
0.0
0.2
0.4
0.6
0.8
1.0
1.2
1.4
0.2
0.4
0.6
Lag
0.8
1.0
1.2
1.4
Lag
13
14
Spectrum
Raw periodogram
500.0
ACF/PACF
100.0
20.0
0.2
0.5
2.0
spectrum
5.0
> spectrum(beer)
frequency
15
Spectrum
16
Spectrum
500 1000
AR(12) spectrum
> spectrum(beer,method="ar")
50
100
200
10
20
spectrum
frequency
17
Classical decomposition
STL decomposition
160
20
0
!20
152
1992
1993
1994
Time
1995
15
!5
1991
1992
1993
1994
time
1995
!15
1991
remainder
0 10 !20
146
20
trend
158
40
146 150 154
seasonal
130
120
160
data
190
plot(stl(beer,s.window="periodic"))
!20
seasonal
trend
observed
decompose(beer)
Decomposition of additive time series
random
18
19
Decomposition
21
forecast package
22
forecast package
> forecast(beer)
Point Forecast
Sep 1995
138.5042
Oct 1995
169.1987
Nov 1995
181.6725
Dec 1995
178.5394
Jan 1996
144.0816
Feb 1996
135.7967
Mar 1996
151.4813
Apr 1996
138.9345
May 1996
138.5279
Jun 1996
127.0269
Jul 1996
134.9452
Aug 1996
145.3088
Sep 1996
139.7348
Oct 1996
170.6709
Nov 1996
183.2204
Dec 1996
180.0290
Jan 1997
145.2589
Feb 1997
136.8833
Mar 1997
152.6684
Apr 1997
140.0008
May 1997
139.5691
Time series and forecasting in R
Jun 1997
127.9620
Jul 1997
135.9181
Aug 1997
146.3349
Lo 80
128.2452
156.6506
168.1640
165.2049
133.2492
125.4937
139.8517
128.1106
127.5448
116.7486
123.7716
132.9658
127.4679
155.2397
166.1298
162.6798
130.7803
122.7595
136.3514
124.4953
123.5476
112.7364
119.1567
127.6354
forecast package
Hi 80
148.7632
181.7468
195.1810
191.8738
154.9140
146.0996
163.1110
149.7584
149.5110
137.3052
146.1187
157.6518
152.0018
186.1020
200.3110
197.3783
159.7374
151.0071
168.9854
155.5064
155.5906
143.1876
152.6795
165.0344
Lo 95
122.8145
150.0081
161.0131
158.1461
127.5148
120.0396
133.6953
122.3808
121.7307
111.3076
117.8567
126.4318
120.9741
147.0709
157.0826
153.4957
123.1159
115.2828
127.7137
116.2871
115.0663
104.6764
110.2837
117.7365
Hi 95
154.1940
188.3894
202.3320
198.9327
160.6483
151.5537
169.2673
155.4882
155.3250
142.7462
152.0337
164.1858
158.4955
194.2708
209.3582
206.5624
167.4019
158.4838
177.6231
163.7145
164.0719
The forecast package
151.2476
161.5525
174.9332
23
> summary(forecast(beer))
> plot(forecast(beer))
140
160
180
200
parameters:
0.0267
0.0232
0.025
0.98
100
120
Initial states:
l = 162.5752
b = -0.1598
s = 1.1979 1.2246 1.1452 0.9354 0.9754 0.9068
0.8523 0.9296 0.9342 1.0160 0.9131 0.9696
1991
1992
1993
1994
1995
1996
sigma:
1997
0.0578
AIC
AICc
BIC
499.0295 515.1347 533.4604
24
forecast package
Exponential smoothing
Exponential smoothing
Current Reference
Hyndman, Koehler, Ord and
Snyder (2008) Forecasting with
Forecasting
with Exponential
exponential smoothing: the state
Smoothing
space approach, Springer-Verlag:
Berlin.
Hyndman Koehler Ord Snyder
Exponential smoothing
MAPE
MASE
Exponential smoothing 26
4.78826138
0.43512047
Classic Reference
Makridakis, Wheelwright and
Hyndman (1998) Forecasting:
methods and applications, 3rd ed.,
Wiley: NY.
27
Exponential smoothing
Exponential smoothing
Trend
Component
N
(None)
Seasonal Component
A
M
(Additive) (Multiplicative)
(None)
N,N
N,A
N,M
(Additive)
A,N
A,A
A,M
Ad
M
(Additive damped)
Ad ,N
(Multiplicative)
M,N
Ad ,A
M,A
Ad ,M
M,M
Md
(Multiplicative damped)
Md ,N
Md ,A
Md ,M
28
Exponential smoothing
29
Exponential smoothing
No trend or seasonality
and multiplicative errors
Example: ETS(M,N,N)
iid
yt = !t1 (1 + t )
!t = yt + (1 )!t1
= !t1 (1 + t )
01
t is white noise with mean zero.
Yt
!t
bt
st
where 0 1, 0 , 0 1
and m is the period of seasonality.
Exponential smoothing
31
Exponential smoothing
Exponential smoothing
Exponential smoothing
AIC = 2 log(Likelihood) + 2p
Exponential smoothing
33
Exponential smoothing
parameters:
0.0267
0.0232
0.025
0.98
34
Exponential smoothing
36
Smoothing parameters:
alpha = 0.247
gamma = 0.01
Initial states:
l = 168.1208
s = 1.2417 1.2148 1.1388 0.9217 0.9667 0.8934
0.8506 0.9182 0.9262 1.049 0.9047 0.9743
sigma:
0.0578
0.0604
AIC
AICc
BIC
500.0439 510.2878 528.3988
AIC
AICc
BIC
499.0295 515.1347 533.4604
Exponential smoothing
> fit2
ETS(M,N,M)
Initial states:
l = 162.5752
b = -0.1598
s = 1.1979 1.2246 1.1452 0.9354 0.9754 0.9068
0.8523 0.9296 0.9342 1.016 0.9131 0.9696
sigma:
Exponential smoothing
> fit
ETS(M,Ad,M)
Smoothing
alpha =
beta =
gamma =
phi
=
32
where p = # parameters.
Produce forecasts using best method.
Obtain prediction intervals using underlying
state space model.
Method performed very well in M3 competition.
30
Exponential smoothing
35
Exponential smoothing
Exponential smoothing
ets() function
Automatically chooses a model by default using
the AIC
Can handle any combination of trend,
seasonality and damping
Produces prediction intervals for every model
Ensures the parameters are admissible
(equivalent to invertible)
Produces an object of class ets.
ets objects
Methods: coef(), plot(),
summary(), residuals(), fitted(),
simulate() and forecast()
plot() function shows time plots of the
original time series along with the
extracted components (level, growth and
seasonal).
Exponential smoothing
37
Exponential smoothing
Exponential smoothing
38
Goodness-of-fit
160
155
120
> accuracy(fit)
ME
RMSE
0.0774 8.4156
0.0
MAE
MPE
7.0331 -0.2915
MAPE
4.7883
MASE
0.4351
> accuracy(fit2)
ME
RMSE
MAE
MPE
-1.3884 9.0015 7.3303 -1.1945
MAPE
5.0237
MASE
0.4535
1.1
0.9
season
!1.0
slope
0.5
145
level
observed
plot(fit)
Decomposition by ETS(M,Ad,M) method
1991
1992
1993
1994
1995
Time
Exponential smoothing
39
Forecast intervals
Exponential smoothing
40
Exponential smoothing
200
160
> accuracy(test)
ME
RMSE
MAE
MPE
-4.3057 58.1668 43.5241 -0.1023
100
120
140
180
1992
1993
1994
1995
1996
MASE
0.5206
> plot(forecast(fit,level=c(50,80,95)))
1991
MAPE
1.1758
1997
180
200
Exponential smoothing
41
42
100
120
140
forecast() function
Takes either a time series as its main
argument, or a time series model.
> Methods
plot(forecast(fit,fan=TRUE))
for objects of class ts, ets,
1991
1992
1993
1994
1995
1996
1997
arima, HoltWinters, StructTS, ar
and others.
If argument is ts, it uses ets model.
Calls predict() when appropriate.
Output as class forecast.
Exponential smoothing
forecast package
160
forecast package
ARIMA modelling
ARIMA modelling
The arima() function in the stats package
provides seasonal and non-seasonal ARIMA
model estimation including covariates.
However, it does not allow a constant unless
the model is stationary
It does not return everything required for
forecast()
It does not allow re-fitting a model to new data.
So I prefer the Arima() function in the
forecast package which acts as a wrapper to
arima().
Even better, the auto.arima() function in the
forecast package.
44
ARIMA modelling
ARIMA modelling
> fit <- auto.arima(beer)
> fit
Series: beer
ARIMA(0,0,0)(1,0,0)[12] with non-zero mean
Coefficients:
sar1 intercept
0.8431
152.1132
s.e. 0.0590
5.1921
sigma^2 estimated as 122.1: log likelihood = -221.44
AIC = 448.88
AICc = 449.34
BIC = 454.95
45
ARIMA modelling
46
ARIMA modelling
47
ARIMA modelling
48
ARIMA modelling
ARIMA modelling
49
ARIMA vs ETS
100
120
140
160
180
200
> plot(forecast(fit))
1991
1992
1993
1994
1995
1996
1997
180
200
> plot(forecast(beer))
51
52
160
120
140
100
tsdisplay
53
seasonplot
> tsdisplay(beer)
> seasonplot(beer)
180
!
!
!
!
140
!
!
!
!
!
!
!
!
!
120
!
!
!
!
!
!
1991
1992
1993
1994
1995
!
!
!
!
!
10
Lag
15
!
!
!
!
!
!
!
!
Feb
Mar
Apr
May
Jun
Jul
Month
5
120
!0.4
15
!
!
!
0.4
PACF
0.0
!0.4
10
Jan
Lag
!
!
!
!
!
0.0
0.4
140
!
!
!
!
180
160
160
ACF
!
!
Aug
Sep
Oct
Nov
Dec
54
56
58
60
59
61
ltsa
dlm
timsac
fArma
fGarch
BootPR
57
Basic facilities
62
63
64
65
66
67
Functional data
68
Miscellaneous
hydrosanity Graphical user interface for exploring
hydrological time series
pastecs Regulation, decomposition and
analysis of space-time series.
RSEIS Seismic time series analysis tools
paleoTS Modeling evolution in
paleontological time-series
GeneTS Microarray Time Series and Network
Analysis
fractal Fractal Time Series Modeling and
Analysis
69