-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForecasting-3-6---ARMA-Performance.html
474 lines (368 loc) · 13.1 KB
/
Forecasting-3-6---ARMA-Performance.html
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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>Forecasting-3-6---ARMA-Performance.utf8</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="my-theme.css" type="text/css" />
</head>
<body>
<textarea id="source">
layout: true
.hheader[<a href="index.html"><svg style="height:0.8em;top:.04em;position:relative;fill:steelblue;" viewBox="0 0 576 512"><path d="M280.37 148.26L96 300.11V464a16 16 0 0 0 16 16l112.06-.29a16 16 0 0 0 15.92-16V368a16 16 0 0 1 16-16h64a16 16 0 0 1 16 16v95.64a16 16 0 0 0 16 16.05L464 480a16 16 0 0 0 16-16V300L295.67 148.26a12.19 12.19 0 0 0-15.3 0zM571.6 251.47L488 182.56V44.05a12 12 0 0 0-12-12h-56a12 12 0 0 0-12 12v72.61L318.47 43a48 48 0 0 0-61 0L4.34 251.47a12 12 0 0 0-1.6 16.9l25.5 31A12 12 0 0 0 45.15 301l235.22-193.74a12.19 12.19 0 0 1 15.3 0L530.9 301a12 12 0 0 0 16.9-1.6l25.5-31a12 12 0 0 0-1.7-16.93z"/></svg></a>]
---
class: center, middle, inverse
# ARIMA Models
## Cross Validation
.futnote[Eli Holmes, UW SAFS]
.citation[[email protected]]
---
## Measures of forecast fit
To measure the forecast fit, we fit a model to training data and test a forecast against data in a test set. We 'held out' the test data and did not use it at all in our fitting.
<img src="Forecasting-3-6---ARMA-Performance_files/figure-html/unnamed-chunk-1-1.png" style="display: block; margin: auto;" />
---
We will fit to the training data and make a forecast.
```r
fit1 <- auto.arima(traindat)
fr <- forecast(fit1, h=2)
fr
```
```
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 25 10.03216 9.789577 10.27475 9.661160 10.40317
## 26 10.09625 9.832489 10.36001 9.692861 10.49964
```
<img src="Forecasting-3-6---ARMA-Performance_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" />
---
How to we quantify the difference between the forecast and the actual values?
```r
fr.err <- testdat - fr$mean
fr.err
```
```
## Time Series:
## Start = 25
## End = 26
## Frequency = 1
## [1] -0.1704302 -0.4944778
```
There are many metrics. The `accuracy()` function in forecast provides many different metrics: mean error, root mean square error, mean absolute error, mean percentage error, mean absolute percentage error.
---
### ME Mean err
```r
me <- mean(fr.err)
me
```
```
## [1] -0.332454
```
### RMSE Root mean squared error
```r
rmse <- sqrt(mean(fr.err^2))
rmse
```
```
## [1] 0.3698342
```
### MAE Mean absolute error
```r
mae <- mean(abs(fr.err))
mae
```
```
## [1] 0.332454
```
---
### MPE Mean percentage error
```r
fr.pe <- 100*fr.err/testdat
mpe <- mean(fr.pe)
mpe
```
```
## [1] -3.439028
```
### MAPE Mean absolute percentage error
```r
mape <- mean(abs(fr.pe))
mape
```
```
## [1] 3.439028
```
---
```r
accuracy(fr, testdat)[,1:5]
```
```
## ME RMSE MAE MPE MAPE
## Training set -0.00473511 0.1770653 0.1438523 -0.1102259 1.588409
## Test set -0.33245398 0.3698342 0.3324540 -3.4390277 3.439028
```
```r
c(me, rmse, mae, mpe, mape)
```
```
## [1] -0.3324540 0.3698342 0.3324540 -3.4390277 3.4390277
```
---
## Test all the models in your candidate model
Now that you have some metrics for forecast accuracy, you can compute these for all the models in your candidate set.
```r
# The model picked by auto.arima
fit1 <- Arima(traindat, order=c(0,1,1))
fr1 <- forecast(fit1, h=2)
test1 <- accuracy(fr1, testdat)[2,1:5]
# AR-1
fit2 <- Arima(traindat, order=c(1,1,0))
fr2 <- forecast(fit2, h=2)
test2 <- accuracy(fr2, testdat)[2,1:5]
# Naive model with drift
fit3 <- rwf(traindat, drift=TRUE)
fr3 <- forecast(fit3, h=2)
test3 <- accuracy(fr3, testdat)[2,1:5]
```
---
## Show a summary
<table>
<thead>
<tr>
<th style="text-align:left;"> </th>
<th style="text-align:left;"> ME </th>
<th style="text-align:left;"> RMSE </th>
<th style="text-align:left;"> MAE </th>
<th style="text-align:left;"> MPE </th>
<th style="text-align:left;"> MAPE </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> (0,1,1) </td>
<td style="text-align:left;"> -0.293 </td>
<td style="text-align:left;"> 0.320 </td>
<td style="text-align:left;"> 0.293 </td>
<td style="text-align:left;"> -3.024 </td>
<td style="text-align:left;"> 3.024 </td>
</tr>
<tr>
<td style="text-align:left;"> (1,1,0) </td>
<td style="text-align:left;"> -0.309 </td>
<td style="text-align:left;"> 0.341 </td>
<td style="text-align:left;"> 0.309 </td>
<td style="text-align:left;"> -3.200 </td>
<td style="text-align:left;"> 3.200 </td>
</tr>
<tr>
<td style="text-align:left;"> Naive </td>
<td style="text-align:left;"> -0.483 </td>
<td style="text-align:left;"> 0.510 </td>
<td style="text-align:left;"> 0.483 </td>
<td style="text-align:left;"> -4.985 </td>
<td style="text-align:left;"> 4.985 </td>
</tr>
</tbody>
</table>
---
## Cross-Validation
An alternate approach to testing a model's forecast accuracy is to use cross-validation. This approach uses windows or shorter segments of the whole time series to make a series of single forecasts. We can use either a sliding or a fixed window. For example for the Anchovy time series, we could fit the model 1964-1973 and forecast 1974, then 1964-1974 and forecast 1975, then 1964-1975 and forecast 1976, and continue up to 1964-1988 and forecast 1989. This would create 16 forecasts to test. The window is 'sliding' because the length of the time series used for fitting the model, keeps increasing by 1.
---
<img src="Forecasting-3-6---ARMA-Performance_files/figure-html/cv.sliding-1.png" style="display: block; margin: auto;" />
---
Another approach uses a fixed window. For example, a 10-year window.
<img src="Forecasting-3-6---ARMA-Performance_files/figure-html/cv.fixed-1.png" style="display: block; margin: auto;" />
---
## Time-series cross-validation with the forecast package
```r
far2 <- function(x, h, order){
forecast(Arima(x, order=order), h=h)
}
e <- tsCV(traindat, far2, h=1, order=c(0,1,1))
tscv1 <- c(ME=mean(e, na.rm=TRUE), RMSE=sqrt(mean(e^2, na.rm=TRUE)), MAE=mean(abs(e), na.rm=TRUE))
tscv1
```
```
## ME RMSE MAE
## 0.1128788 0.2261706 0.1880392
```
Compare to RMSE from just the 2 test data points.
```r
test1[c("ME","RMSE","MAE")]
```
```
## ME RMSE MAE
## -0.2925326 0.3201093 0.2925326
```
---
## Cross-validation farther in future
<img src="Forecasting-3-6---ARMA-Performance_files/figure-html/cv.sliding.4plot-1.png" style="display: block; margin: auto;" />
---
Compare accuracy of forecasts 1 year out versus 4 years out. If `h` is greater than 1, then the errors are returned as a matrix with each `h` in a column. Column 4 is the forecast, 4 years out.
```r
e <- tsCV(traindat, far2, h=4, order=c(0,1,1))[,4]
#RMSE
tscv4 <- c(ME=mean(e, na.rm=TRUE), RMSE=sqrt(mean(e^2, na.rm=TRUE)), MAE=mean(abs(e), na.rm=TRUE))
rbind(tscv1, tscv4)
```
```
## ME RMSE MAE
## tscv1 0.1128788 0.2261706 0.1880392
## tscv4 0.2839064 0.3812815 0.3359689
```
---
Compare accuracy of forecasts with a fixed 10-year window and 1-year out forecasts.
```r
e <- tsCV(traindat, far2, h=1, order=c(0,1,1), window=10)
#RMSE
tscvf1 <- c(ME=mean(e, na.rm=TRUE), RMSE=sqrt(mean(e^2, na.rm=TRUE)), MAE=mean(abs(e), na.rm=TRUE))
tscvf1
```
```
## ME RMSE MAE
## 0.1387670 0.2286572 0.1942840
```
---
```r
comp.tab <- rbind(test1=test1[c("ME","RMSE","MAE")],
slide1=tscv1,
slide4=tscv4,
fixed1=tscvf1)
knitr::kable(comp.tab, format="html")
```
<table>
<thead>
<tr>
<th style="text-align:left;"> </th>
<th style="text-align:right;"> ME </th>
<th style="text-align:right;"> RMSE </th>
<th style="text-align:right;"> MAE </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> test1 </td>
<td style="text-align:right;"> -0.2925326 </td>
<td style="text-align:right;"> 0.3201093 </td>
<td style="text-align:right;"> 0.2925326 </td>
</tr>
<tr>
<td style="text-align:left;"> slide1 </td>
<td style="text-align:right;"> 0.1128788 </td>
<td style="text-align:right;"> 0.2261706 </td>
<td style="text-align:right;"> 0.1880392 </td>
</tr>
<tr>
<td style="text-align:left;"> slide4 </td>
<td style="text-align:right;"> 0.2839064 </td>
<td style="text-align:right;"> 0.3812815 </td>
<td style="text-align:right;"> 0.3359689 </td>
</tr>
<tr>
<td style="text-align:left;"> fixed1 </td>
<td style="text-align:right;"> 0.1387670 </td>
<td style="text-align:right;"> 0.2286572 </td>
<td style="text-align:right;"> 0.1942840 </td>
</tr>
</tbody>
</table>
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();
// adds .remark-code-has-line-highlighted class to <pre> parent elements
// of code chunks containing highlighted lines with class .remark-code-line-highlighted
(function(d) {
const hlines = d.querySelectorAll('.remark-code-line-highlighted');
const preParents = [];
const findPreParent = function(line, p = 0) {
if (p > 1) return null; // traverse up no further than grandparent
const el = line.parentElement;
return el.tagName === "PRE" ? el : findPreParent(el, ++p);
};
for (let line of hlines) {
let pre = findPreParent(line);
if (pre && !preParents.includes(pre)) preParents.push(pre);
}
preParents.forEach(p => p.classList.add("remark-code-has-line-highlighted"));
})(document);</script>
<script>
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
</script>
<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>