-
Notifications
You must be signed in to change notification settings - Fork 0
/
88-exercises.Rmd
175 lines (114 loc) · 4.51 KB
/
88-exercises.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
---
title: "Forecasting: Principles and Practice Chapter 8 Exercises"
author: "Neil Martin"
date: "2024-10-02"
output: html_document
---
```{r setup, include=FALSE}
load(".RData")
library(fpp3)
library(knitr)
library(ggplot2)
library(readxl)
library(dplyr)
library(readr)
library(seasonal)
library(latex2exp)
library(tsibble)
library(feasts)
library(tidyr)
```
#### Consider the the number of pigs slaughtered in Victoria, available in the aus_livestock dataset.
#### Use the ETS() function to estimate the equivalent model for simple exponential smoothing. Find the optimal values of α and ℓ0, and generate forecasts for the next four months.
```{r q1}
vic_pigs <- aus_livestock |> filter(Animal == "Pigs", State == "Victoria")
vic_pigs |> autoplot()
vicpig_mod <- vic_pigs |>
model(ETS(Count ~ error("A") + trend("N") + season("N")))
report(vicpig_mod)
vicpig_mod
vicpig_fc <- vicpig_mod |>
forecast(h = 4) |>
autoplot(vic_pigs)
vicpig_fc
```
#### Data set global_economy contains the annual Exports from many countries. Select one country to analyse.
```{r q5}
india <- global_economy |>
filter(Country == "India")
india |> autoplot(Exports)
```
<p>From this plot we can see that the German exports have been following a significant linear trend from the early 1990s. There is a slight dip around the 2008 financial crash but there seems to be an uptick in exports from Germany after this. </p>
#### Use an ETS(A,N,N) model to forecast the series, and plot the forecasts.
```{r q5b}
india_mod <- india |>
model(ETS(Exports ~ error("A") + trend("N") + season("N")))
report(india_mod)
india_mod
india_fc <- india_mod |>
forecast(h = 4) |>
autoplot(india)
india_fc
fitted_india <- fitted(india_mod)
residuals <- india$Exports - fitted_india$.fitted
rmse <- sqrt(mean(residuals^2))
print(rmse)
```
#### Compare the results to those from an ETS(A,A,N) model. (Remember that the trended model is using one more parameter than the simpler model.) Discuss the merits of the two forecasting methods for this data set.
#### Compare the forecasts from both methods. Which do you think is best?
```{r q5cd}
india_mod <- india |>
model(ETS(Exports ~ error("A") + trend("A") + season("N")))
report(india_mod)
india_mod
india_fc <- india_mod |>
forecast(h = 4) |>
autoplot(india)
india_fc
fitted_india <- fitted(india_mod)
residuals <- india$Exports - fitted_india$.fitted
rmse <- sqrt(mean(residuals^2))
print(rmse)
```
<p> The RMSE shows that the AAN model is roughly 0.02 better than the ANN model.</p>
#### Forecast the Chinese GDP from the global_economy data set using an ETS model. Experiment with the various options in the ETS() function to see how much the forecasts change with damped trend, or with a Box-Cox transformation. Try to develop an intuition of what each is doing to the forecasts.
```{r q6}
ch_gdp <- global_economy |>
filter(Country == "China") |>
mutate(GDP_Billions = GDP / 1e9)
ch_gdp |> autoplot(GDP_Billions)
lambda <- ch_gdp |>
features(GDP_Billions, features = guerrero) |>
pull(lambda_guerrero)
ch_gdp_mod <- ch_gdp |>
model(`Simple` = ETS(GDP ~ error("A") + trend("N") + season("N")),
`Holt's method` = ETS(GDP ~ error("A") + trend("A") + season("N")),
`Damped Holt's method` = ETS(GDP ~ error("A") + trend("Ad", phi = 0.8) + season("N")),
`Box-Cox` = ETS(box_cox(GDP,lambda) ~ error("A") + trend("A") + season("N")),
`Box-Cox Damped` = ETS(box_cox(GDP,lambda) ~ error("A") + trend("Ad", phi = 0.8) + season("N")),
`Log` = ETS(log(GDP) ~ error("A") + trend("A") + season("N")),
`Log Damped` = ETS(log(GDP) ~ error("A") + trend("Ad", phi = 0.8) + season("N"))
)
ch_gdp_mod
ch_gdp_fc <- ch_gdp_mod |>
forecast(h = 20) |>
autoplot(ch_gdp) + labs(title = "Chinese 2040 GDP Forecast")
ch_gdp_fc
```
#### Find an ETS model for the Gas data from aus_production and forecast the next few years. Why is multiplicative seasonality necessary here? Experiment with making the trend damped. Does it improve the forecasts?
```{r q7}
aus_gas <- aus_production |>
select(Gas)
aus_gas |> autoplot()
aus_gas_mod <- aus_gas|>
model(additive = ETS(Gas ~ error("A") + trend("A") + season("A")),
multiplicative = ETS(Gas ~ error("M") + trend("A") + season("M")),
`damped multiplicative` = ETS(Gas ~ error("M") + trend("Ad", phi = 0.9) + season("M")))
aus_gas_mod
report(aus_gas_mod)
aus_gas_fc <- aus_gas_mod |>
forecast(h = 10) |>
autoplot(aus_gas) + labs(title = "Australian Gas Production")
aus_gas_fc
```
#### Moving to Chapter 9