-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForecasting-8-0-Seasonality.Rmd
154 lines (107 loc) · 4.28 KB
/
Forecasting-8-0-Seasonality.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
# Seasonality
To work with seasonal data, we need to turn our data into a ts object, which is a "time-series" object in R. This will allow us to specify the seasonality. It is important that we do not leave out any data in our time series. You data should look like so
```
Year Month metric.tons
2018 1 1
2018 2 2
2018 3 3
...
2019 1 4
2019 2 6
2019 3 NA
```
The months are in order and the years are in order.
## Chinook data
We will illustrate the analysis of seasonal catch data using a data set of monthly chinook salmon from Washington state.
### Load the chinook salmon data set {-}
This is in the **FishForecast** package.
```{r f80-load_packages}
require(FishForecast)
head(chinook.month)
```
The data are monthly and start in January 1990. To make this into a ts object do
```{r}
chinookts <- ts(chinook.month$log.metric.tons, start=c(1990,1), frequency=12)
```
`start` is the year and month and frequency is the number of months in the year. If we had quarterly data that started in 2nd quarter of 1990, our call would be
```
ts(chinook$log.metric.tons, start=c(1990,2), frequency=4)
```
If we had daily data starting on hour 5 of day 10 and each row was an hour, our call would be
```
ts(chinook$log.metric.tons, start=c(10,5), frequency=24)
```
Use `?ts` to see more examples of how to set up ts objects.
### Plot seasonal data {-}
Now that we have specified our seasonal data as a ts object, it is easy to plot because R knows what the season is.
```{r}
plot(chinookts)
```
## Seasonal Exponential Smoothing Model
Now we add a few more lines to our ETS table of models:
model | "ZZZ" | alternate function |
------------- | ------------- | --------- |
exponential smoothing no trend | "ANN" | `ses()` |
exponential smoothing with trend | "AAN" | `holt()` |
exponential smoothing with season no trend | "ANA" | NA |
exponential smoothing with season and trend | "AAA" | NA |
estimate best trend and season model | "ZZZ" | NA |
Unfortunately `ets()` will not handle missing values and will find the longest continuous piece of our data and use that.
```{r}
library(forecast)
traindat <- window(chinookts, c(1990,1), c(1999,12))
fit <- forecast::ets(traindat, model="AAA")
fr <- forecast::forecast(fit, h=24)
plot(fr)
points(window(chinookts, c(1996,1), c(1996,12)))
```
### Force seasonality to evolve more
If we plot the decomposition, we see the the seasonal component is not changing over time, unlike the actual data. The bar on the right, alerts us that the scale on the 3rd panel is much smaller.
```{r fig.height=4}
autoplot(fit)
```
Pass in a high `gamma` (the season weighting) to force the seasonality to evolve.
```{r fig.height=4}
fit <- forecast::ets(traindat, model="AAA", gamma=0.4)
autoplot(fit)
```
---
## Seasonal ARIMA model
`auto.arima()` will recognize that our data has season and fit a seasonal ARIMA model to our data by default. Let's use the data that `ets()` used. This is shorter than our training data and is Oct 1990 to Dec 1995. The data used by `ets()` is returned in `fit$x`.
We will redefine the training data to be the longest segment with no missing values.
```{r}
traindat <- window(chinookts, c(1990,10), c(1995,12))
testdat <- window(chinookts, c(1996,1), c(1996,12))
fit <- forecast::auto.arima(traindat)
fr <- forecast::forecast(fit, h=12)
plot(fr)
points(testdat)
```
## Missing values
Unlike for an exponential smoothing model, missing values are ok when fitting a seasonal ARIMA model
```{r}
fulldat <- window(chinookts, c(1990,1), c(1999,12))
fit <- forecast::auto.arima(fulldat)
fr <- forecast::forecast(fit, h=12)
plot(fr)
```
## Forecast evaluation
We can compute the forecast performance metrics as usual.
```{r}
fit.ets <- forecast::ets(traindat, model="AAA")
fr <- forecast::forecast(fit.ets, h=12)
```
Look at the forecast so you know what years and months to include in your test data. Pull those 12 months out of your data using the `window()` function.
```{r}
testdat <- window(chinookts, c(1996,1), c(1996,12))
```
Use `accuracy()` to get the forecast error metrics.
```{r}
forecast::accuracy(fr, testdat)
```
We can do the same for the ARIMA model.
```{r}
fit <- forecast::auto.arima(traindat)
fr <- forecast::forecast(fit, h=12)
forecast::accuracy(fr, testdat)
```