-
Notifications
You must be signed in to change notification settings - Fork 2
/
Forecasting-4-1---Exp-Smoothing.html
681 lines (483 loc) · 18.5 KB
/
Forecasting-4-1---Exp-Smoothing.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
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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>Forecasting-4-1---Exp-Smoothing.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
# Forecasting Time Series
## Simple Exponential Smoothing
.futnote[Eli Holmes, UW SAFS]
.citation[[email protected]]
---
## Naive forecast
For a naive forecast of the anchovy catch in 1988, we just use the 1987 catch.
`$$\hat{x}_{1988} = x_{1987}$$`
Which is the same as saying that we put 100% of the 'weight' on the most recent value and no weight on any value prior to that.
`$$\hat{x}_{1988} = 1 \times x_{1987} + 0 \times x_{1986} + 0 \times x_{1985} + \dots$$`
---
Past values in the time series have information about the current state, but only the most recent past value.
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-1-1.png" style="display: block; margin: auto;" />
---
That's a bit extreme. Often the values prior to the last value also have some information about future states. But the 'information content' should decrease the farther in the past that we go.
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-2-1.png" style="display: block; margin: auto;" />
---
Simple exponential smoothing uses this type of weighting that falls off exponentially and the objective to estimate the best weighting ( `\(\alpha\)` ):
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" />
---
## Fitting exponential smoothing models
The forecast package will fit a wide variety of exponential smoothing models. The main fitting function is `ets()`:
```
ets(y, model = "ZZZ", < + many other arguments >)
```
* y : your data. A time series of responses.
* model: what type of exponential smoothing model.
---
We are going to `ets()` to fit three simple types of exponential smoothing models:
model | "ZZZ" | alternate function |
------------- | ------------- | --------- |
exponential smoothing no trend | "ANN" | `ses()` |
exponential smoothing with trend | "AAN" | `holt()` |
exponential smoothing choose trend | "AZN" | NA |
The alternate function does exactly the same fitting. It is just a 'shortcut'.
---
## Fit exponential smoothing with no trend
This is like the naive model that just uses the last value to make the forecast, but instead of only using the last value it will use values farther in the past also. The weighting fall off exponentially.
Load the data and **forecast** package.
```r
data(greeklandings, package="FishForecast")
anchovy <- subset(greeklandings,
Species=="Anchovy" & Year<=1987)$log.metric.tons
anchovy <- ts(anchovy, start=1964)
library(forecast)
```
```r
fit <- ets(anchovy, model="ANN")
fr <- forecast(fit, h=5)
```
---
```r
plot(fr)
```
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" />
---
## Look at the estimates
```r
fit
```
```
## ETS(A,N,N)
##
## Call:
## ets(y = anchovy, model = "ANN")
##
## Smoothing parameters:
## alpha = 0.7065
##
## Initial states:
## l = 8.5553
##
## sigma: 0.2166
##
## AIC AICc BIC
## 6.764613 7.964613 10.298775
```
---
## The weighting function
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/ann.weighting-1.png" style="display: block; margin: auto;" />
---
## Produce forecast with ets from a previous fit
Say you want to estimate a forecasting model from one dataset and use that model to forecast another dataset or another area. Here is how to do that.
This is the fit to the 1964-1987 data:
```r
fit1 <- ets(anchovy, model="ANN")
```
Use that model with the 2000-2007 data and produce a forecast:
```r
dat <- subset(landings, Species=="Anchovy" & Year>=2000 & Year<=2007)
dat <- ts(dat$log.metric.tons, start=2000)
fit2 <- ets(dat, model=fit1)
```
```
## Model is being refit with current smoothing parameters but initial states are being re-estimated.
## Set 'use.initial.values=TRUE' if you want to re-use existing initial values.
```
```r
fr2 <- forecast(fit2, h=5)
```
---
```r
plot(fr2)
```
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" />
---
## Naive model with drift
Fit a model that uses the last observation as the forecast but includes a trend estimated from ALL the data. This is what the naive model with drift does.
```r
fit.rwf <- Arima(anchovy, order=c(0,1,0), include.drift=TRUE)
fr.rwf <- forecast(fit.rwf, h=5)
plot(fr.rwf)
```
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" />
---
The trend seen in the blue line is estimated from the overall trend in ALL the data.
```r
coef(fit.rwf)
```
```
## drift
## 0.06577281
```
The trend from all the data is (last-first)/(number of steps).
```r
mean(diff(anchovy))
```
```
## [1] 0.06577281
```
So we only use the latest data to choose the level for our forecast but use all the data to choose the trend? It would make more sense to weight the more recent trends more heavily.
---
## Exponential smoothing model with trend
The exponential smoothing model with trend does this. The one-year trend is
`$$x_t - x_{t-1}$$`
That is how much the data increased or decreased.
---
```r
plot(diff(anchovy),ylim=c(-0.3,.3))
abline(h=0, col="blue")
abline(h=mean(diff(anchovy)),col="red")
title("0 means no change")
```
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" />
---
If we take the average of all `\(x_t - x_{t-1}\)` we are using the average trend like the naive model with drift. We put an equal weighting on all trends.
But we could use a weighting that falls off exponentially so that we more recent trends affect the forecast more than trends in the distant past. That is what an exponential smoothing model with trend does.
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" />
---
## Naive model with trend
If your training data are length `\(T\)`, then a forecast for `\(T+h\)` is
`$$\hat{x}_{T+h} = l_T + h \bar{b}$$`
where `\(\hat{b}\)` is the mean of the the yearly changes in `\(x\)`, so the mean of `\(x_t - x_{t-1}\)`.
`$$\hat{b} = \sum_{t=2}^T (x_t - x_{t-1})$$`
## Exponential smoothing model with trend
`$$\hat{x}_{T+h} = l_T + h b_T$$`
where `\(b_T\)` is a weighted average with the more recent trends given more weight.
`$$b_t = \sum_{t=2}^T \beta (1-\beta)^{t-2}(x_t - x_{t-1})$$`
---
## Fit exponential smoothing with trend
```r
fit <- ets(anchovy, model="AAN")
fr <- forecast(fit, h=5)
plot(fr)
```
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" />
---
## Decomposing your model fit
Sometimes you would like to see the smoothed level and smoothed trend that the model estimated. You can see that with `plot(fit)` or `autoplot(fit)`.
```r
autoplot(fit)
```
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" />
---
## Forecast performance
We can evaluate the forecast performance with forecasts of our test data or we can use all the data and use time-series cross-validation.
Let's start with the former.
---
## Test forecast performance against a test data set
We will fit an an exponential smoothing model with trend to the training data and make a forecast for the years that we 'held out'.
```r
fit1 <- ets(traindat, model="AAN")
h=length(testdat)
fr <- forecast(fit1, h=h)
plot(fr)
points(end(traindat)[1]+1:h, testdat, pch=2, col="red")
legend("topleft", c("forecast","actual"), pch=c(20,2), col=c("blue","red"))
```
<img src="Forecasting-4-1---Exp-Smoothing_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto;" />
---
We can calculate a variety of forecast error metrics with
```r
accuracy(fr, testdat)
```
```
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0155561 0.1788989 0.1442712 0.1272938 1.600532 0.7720807
## Test set -0.5001701 0.5384355 0.5001701 -5.1678506 5.167851 2.6767060
## ACF1 Theil's U
## Training set -0.008371542 NA
## Test set -0.500000000 2.690911
```
We would now repeat this for all the models in our candidate set and choose the model with the best forecast performance.
---
## Forecast performance with time-series cross-validation
Another approach is to use all the data and test a series of forecasts made by fitting the model to different lengths of the data.
In this approach, we don't have test data. Instead we will use all the data for fitting and for forecast testing.
We will redefine `traindat` as all our Anchovy data.
---
## tsCV() function
We will use the `tsCV()` function. We need to define a function that returns a forecast.
```r
far2 <- function(x, h, model){
fit <- ets(x, model=model)
forecast(fit, h=h)
}
```
---
Now we can use `tsCV()` to run our `far2()` function to a series of training data sets. We will specify that a NEW ets model be estimated for each training set. We are not using the weighting estimated for the whole data set but estimating the weighting new for each set.
The `e` are our forecast errors for all the forecasts that we did with the data.
```r
e <- tsCV(traindat, far2, h=1, model="AAN")
e
```
```
## Time Series:
## Start = 1
## End = 26
## Frequency = 1
## [1] -0.245378390 0.366852341 0.419678595 -0.414861770 -0.152727933
## [6] -0.183775208 -0.013799590 0.308433377 -0.017680471 -0.329690537
## [11] -0.353441463 0.266143346 -0.110848616 -0.005227309 0.157821831
## [16] 0.196184446 0.008135667 0.326024067 0.085160559 0.312668447
## [21] 0.246437781 0.117274740 0.292601670 -0.300814605 -0.406118961
## [26] NA
```
---
Let's look at the first few `e` so we see exactly with `tsCV()` is doing.
```r
e[2]
```
```
## [1] 0.3668523
```
This uses training data from `\(t=1\)` to `\(t=2\)` so fits an ets to the first two data points alone. Then it creates a forecast for `\(t=3\)` and compares that forecast to the actual value observed for `\(t=3\)`.
```r
TT <- 2 # end of the temp training data
temp <- traindat[1:TT]
fit.temp <- ets(temp, model="AAN")
fr.temp <- forecast(fit.temp, h=1)
traindat[TT+1] - fr.temp$mean
```
```
## Time Series:
## Start = 3
## End = 3
## Frequency = 1
## [1] 0.3668523
```
---
```r
e[3]
```
```
## [1] 0.4196786
```
This uses training data from `\(t=1\)` to `\(t=2\)` so fits an ets to the first two data points alone. Then it creates a forecast for `\(t=3\)` and compares that forecast to the actual value observed for `\(t=3\)`.
```r
TT <- 3 # end of the temp training data
temp <- traindat[1:TT]
fit.temp <- ets(temp, model="AAN")
fr.temp <- forecast(fit.temp, h=1)
traindat[TT+1] - fr.temp$mean
```
```
## Time Series:
## Start = 4
## End = 4
## Frequency = 1
## [1] 0.4196786
```
---
## Testing a specific ets model
By specifying `model="AAN"`, we estimated a new ets model (meaning new weighting) for each training set used. We might want to specify that we use only the weighting we estimated for the full data set.
We do this by passing in a fit to `model`.
The `e` are our forecast errors for all the forecasts that we did with the data. `fit1` below is the ets estimated from all the data 1964 to 1989. Note, the code will produce a warning that it is estimating the initial value and just using the weighting. That is what we want.
```r
fit1 <- ets(traindat, model="AAN")
e <- tsCV(traindat, far2, h=1, model=fit1)
e
```
```
## Time Series:
## Start = 1
## End = 26
## Frequency = 1
## [1] NA 0.576663901 1.031385937 0.897828249 1.033164616
## [6] 0.935274283 0.958914499 1.265427119 -0.017241938 -0.332751184
## [11] -0.330473144 0.255886314 -0.103926617 0.031206730 0.154727479
## [16] 0.198328366 -0.020605522 0.297475742 0.005297401 0.264939892
## [21] 0.196256334 0.129798648 0.335887872 -0.074017535 -0.373267163
## [26] NA
```
---
## Forecast accuracy metrics
Now we can compute forecast accuracy metrics from the forecast errors (`e`).
RMSE: root mean squared error
```r
rmse <- sqrt(mean(e^2, na.rm=TRUE))
```
MAE: mean absolute error
```r
mae <- mean(abs(e), na.rm=TRUE)
```
We would repeat this process for all models in our candidate set and compare forecast fits to choose our forecast model.
---
## Next steps
Now you are ready to compare forecasts from a variety of models and choose a forecast model. Note when you compare models, you can use both 'training data/test data' and use time-series cross-validation, but report the metrics in separate columns. Example, 'RMSE from tsCV' and 'RMSE from test data'.
---
## Example candidate set for anchovy
* Exponential smoothing model with trend
```
fit <- ets(traindat, model="AAN")
fr <- forecast(fit, h=1)
```
* Exponential smoothing model no trend
```
fit <- ets(traindat, model="ANN")
fr <- forecast(fit, h=1)
```
* ARIMA(0,1,1) with drift (best)
```
fit <- Arima(traindat, order(0,1,1), include.drift=TRUE)
fr <- forecast(fit, h=1)
```
* ARIMA(2,1,0) with drift (within 2 AIC of best)
```
fit <- Arima(traindat, order(2,1,0), include.drift=TRUE)
fr <- forecast(fr)
```
---
## Candidata models continued
* Time-varying regression with linear time
```
traindat$t <- 1:24
fit <- lm(log.metric.tons ~ t, data=traindat)
fr <- forecast(fit, newdata=data.frame(t=25))
```
* Naive no trend
```
fit <- Arima(traindat, order(0,1,0))
fr <- forecast(fit, h=1)
# or simply
fr <- rwf(traindat)
```
* Naive with trend
```
fit <- Arima(traindat, order(0,1,0), include.drift=TRUE)
fr <- forecast(fit)
# or simply
fr <- rwf(traindat, drift=TRUE)
```
* Average or mean
```
fit <- Arima(traindat, order(0,0,0))
fr <- forecast(fit)
```
</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>