-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathweek5.R
95 lines (75 loc) · 2.03 KB
/
week5.R
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
library(fpp3)
# Algerian Exports
algeria_economy <- global_economy |>
filter(Country == "Algeria")
algeria_economy |>
autoplot(Exports)
fit <- algeria_economy |>
model(
ANN = ETS(Exports ~ error("A") + trend("N") + season("N")),
MNN = ETS(Exports ~ error("M") + trend("N") + season("N")),
auto = ETS(Exports)
)
fit |>
select(MNN) |>
report()
tidy(fit)
glance(fit)
accuracy(fit)
components(fit) |> autoplot()
components(fit) |>
left_join(fitted(fit), by = c("Country", ".model", "Year"))
fit |>
select(ANN) |>
gg_tsresiduals()
fit |>
augment() |>
features(.innov, ljung_box, lag = 10)
fc <- fit |>
forecast(h=5)
fc |>
filter(.model == "MNN") |>
autoplot(algeria_economy) +
ylab("Exports (% of GDP)") + xlab("Year")
# Repeat with test set
fit <- algeria_economy |>
filter(Year <= 2012) |>
model(
ANN = ETS(Exports ~ error("A") + trend("N") + season("N")),
MNN = ETS(Exports ~ error("M") + trend("N") + season("N")),
auto = ETS(Exports)
)
fc <- fit |>
forecast(h=5)
fc |>
autoplot(algeria_economy, level=NULL) +
ylab("Exports (% of GDP)") + xlab("Year")
fc |> accuracy(algeria_economy)
# Repeat with tscv
alg_exports_stretch <- algeria_economy |>
stretch_tsibble(.init = 10, .step = 1)
cv_fit <- alg_exports_stretch |>
model(
ANN = ETS(Exports ~ error("A") + trend("N") + season("N")),
MNN = ETS(Exports ~ error("M") + trend("N") + season("N")),
autoNN = ETS(Exports ~ trend("N") + season("N")),
naive = NAIVE(Exports),
drift = RW(Exports ~ drift())
)
cv_fc <- cv_fit |>
forecast(h = 12) |>
group_by(.id, .model) |>
mutate(h = row_number()) |>
ungroup() |>
as_fable(response = "Exports", distribution = Exports)
cv_fc |>
accuracy(algeria_economy, by = c("h", ".model")) |>
group_by(.model, h) |>
summarise(RMSSE = sqrt(mean(RMSSE^2))) |>
ggplot(aes(x=h, y=RMSSE, group=.model, col=.model)) +
geom_line()
cv_fc |>
accuracy(algeria_economy, by = c("h", ".model")) |>
group_by(.model) |>
summarise(RMSSE = sqrt(mean(RMSSE^2))) |>
arrange(RMSSE)