forked from robjhyndman/linear-hierarchical-forecasting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ETS-ARIMA.R
164 lines (131 loc) · 3.67 KB
/
ETS-ARIMA.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
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
library(fable)
library(fabletools)
library(tidyverse)
library(tsibble)
library(readr)
aus <- read.csv('TourismData_v3.csv', header = TRUE)[,-c(1,2)]
aus <- tibble(aus)
aus$Date <- rep(yearmonth("1998 Jan") + 0:227)
aus <- aus %>%
pivot_longer(-Date, names_to = "group", values_to = "value") %>%
mutate(
State = stringr::str_sub(group, 1, 1),
Zone = stringr::str_sub(group, 1, 2),
Region = stringr::str_sub(group, 1, 3),
Purpose = stringr::str_sub(group, 4, 6),
) %>%
select(-group) %>%
as_tsibble(index = Date, key=c(State, Zone, Region, Purpose))
ausgts <- aus %>%
aggregate_key(Purpose * (State/ Zone/ Region), value = sum(value))
new_data <- ausgts %>%
dplyr::filter(Date > yearmonth ("2014 Dec")) %>%
rename(actual = value)
#########
## Fixed origin
########
#ETS
## computation time
start_time <- Sys.time()
fc.ets <- ausgts %>%
filter(Date <= yearmonth ("2014 Dec")) %>%
model(ets = ETS(value ))
end_time <- Sys.time()
end_time - start_time
fc.ets <- fc.ets %>%
reconcile(ets_adjusted = min_trace(ets, method="wls_struct"))%>%
forecast(h = "2 years")
fc.ets.error <- fc.ets %>%
left_join(new_data) %>%
mutate(error = actual - .mean)
fc.ets <- fc.ets.error %>%
hilo(level=95) %>%
unpack_hilo("95%")
#ARIMA
## computation time
start_time <- Sys.time()
fc.arima <- ausgts %>%
filter(Date <= yearmonth ("2014 Dec")) %>%
model(arima = ARIMA(value ))
end_time <- Sys.time()
end_time - start_time
fc.arima <- fc.arima %>%
reconcile(arima_adjusted = min_trace(arima, method="wls_struct"))%>%
forecast(h = "2 years")
fc.arima.error <- fc.arima %>%
left_join(new_data) %>%
mutate(error = actual - .mean)
fc.arima <- fc.arima.error %>%
hilo(level=95) %>%
unpack_hilo("95%")
## saving ets and arima results
fc.fix <- bind_rows (fc.arima, fc.ets)
fc.fix <- fc.fix %>%
distinct(across(-value))
write_csv(fc.fix, 'fc.fix.tourism.ets.arima.csv')
#########
## Rolling origin
########
## rolling window
gts.rolling <- ausgts %>%
filter(Date < yearmonth ("2016 Dec")) %>%
stretch_tsibble(.init = 204 , .step = 1)
new_data <- ausgts %>%
dplyr::filter(Date > yearmonth ("2014 Dec")) %>%
rename(actual = value)%>%
arrange(`Date`) %>%
mutate(new_index = dense_rank(Date))
# ETS
## computation time
start_time <- Sys.time()
fc.ets <- gts.rolling %>%
model(ets = ETS(value))
end_time <- Sys.time()
end_time - start_time
m <- c(1:24)
fc.ets.rec <- data.frame(a=c(), b=c())
for(i in m){
result <- fc.ets %>%
filter(.id == i) %>%
reconcile(ets_adjusted = min_trace(ets, method="wls_struct")) %>%
forecast(h = 1) %>%
hilo(level=95)%>%
unpack_hilo("95%")
result <- result %>%
distinct(across(-value)) %>%
mutate('h' = i)
new_data2 <- new_data %>%
filter(new_index == i)
fc.rec <- result %>%
left_join(new_data2) %>%
mutate(error = actual - .mean)
fc.ets.rec <- bind_rows(fc.ets.rec, fc.rec)
}
# ARIMA
## computation time
start_time <- Sys.time()
fc.arima <- gts.rolling %>%
model(arima = ARIMA(value))
end_time <- Sys.time()
end_time - start_time
m <- c(1:24)
fc.arima.rec <- data.frame(a=c(), b=c())
for(i in m){
result <- fc.arima %>%
filter(.id == i) %>%
reconcile(arima_adjusted = min_trace(arima, method="wls_struct")) %>%
forecast(h = 1) %>%
hilo(level=95)%>%
unpack_hilo("95%")
result <- result %>%
distinct(across(-value)) %>%
mutate('h' = i)
new_data2 <- new_data %>%
filter(new_index == i)
fc.rec <- result %>%
left_join(new_data2) %>%
mutate(error = actual - .mean)
fc.arima.rec <- bind_rows(fc.arima.rec, fc.rec)
}
fc.rolling <- bind_rows (fc.arima.rec, fc.ets.rec)
write_csv(fc.rolling, 'fc.rolling.tourism.ets.arima.csv')