-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaseforecast.R
222 lines (196 loc) · 6.84 KB
/
baseforecast.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
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
library(tidyverse)
library(magrittr)
library(future)
library(forecast)
data_label <- commandArgs(trailingOnly = TRUE) # "simulation", "corr_i", "tourism_i", or "labour_i"
# Utility function
base_forecast <- function(hts, method, h, special = NULL){
out <- list()
for(i in 1:NCOL(hts)){
series <- hts[, i]
seriesname <- colnames(hts)[i]
if (method == 'arima'){
model <- auto.arima(series)
}
if (method == 'ets'){
model <- ets(series)
}
if (method == "ar"){
if (is.null(special)){
model <- auto.arima(series, allowmean = TRUE, stationary = TRUE, stepwise = FALSE)
} else{
model <- auto.arima(series, allowmean = !(seriesname %in% special), stationary = TRUE, stepwise = FALSE)
}
}
fc <- forecast(model, h)$mean
resid <- residuals(model, type = 'response') # use regular residuals rather than innovation residuals
out[[i]] <- list(fitted = model$fitted, residuals = resid,
train = model$x, forecast = fc)
}
names(out) <- colnames(hts)
out
}
#################################################
# Import data
#################################################
#----------------------------------------------------------------------
# Simulation data
## Total/Middle/Bottom: 3 levels, n = 7
## Training set: 1978Q1-2018Q4
## Test set: 2019Q1-2022Q4
#----------------------------------------------------------------------
if (data_label == "simulation"){
# Formalize data (Index, Time, Series1, ..., Seriesn)
dat <- readr::read_csv(paste0("data/", data_label, "_data.csv")) |>
mutate(Time = tsibble::yearquarter(Quarter),
A = AA + AB,
B = BA + BB,
Total = AA + AB + BA + BB) |>
select(c(Index, Time, Total, A, B, AA, AB, BA, BB))
# Data details
freq <- 4
start_train <- c(1978, 1)
end_train <- c(2018, 4)
start_test <- c(2019, 1)
end_test <- c(2022, 4)
# Forecasting method
fmethod <- "ets"
special <- NULL
# S matrix
S <- rbind(matrix(c(1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1), 3, 4),
diag(rep(1, 4)))
}
#----------------------------------------------------------------------
# Simulation data - correlation
## Total/Middle/Bottom: 3 levels, n = 7
## Training set: 1-100
## Test set: 1
#----------------------------------------------------------------------
if (grepl("corr", data_label)){
freq <- 1
start_train <- 1
end_train <- 100
start_test <- 101
end_test <- 101
dat <- readr::read_csv(paste0("data/", data_label, "_data.csv")) |>
mutate(A = AA + AB,
B = BA + BB,
Total = AA + AB + BA + BB) |>
select(c(Index, Time, Total, A, B, AA, AB, BA, BB))
# Forecasting method
fmethod <- "ar"
special <- c("Total", "A", "BA")
# S matrix
S <- rbind(matrix(c(1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1), 3, 4),
diag(rep(1, 4)))
}
#----------------------------------------------------------------------
# Australian domestic tourism (only considering hierarchical structure)
##
## Monthly series from 1998Jan-2017Dec: 240 months (20 years) for each series
##
## Total/State/Zone/Region: 4 levels, n = 111 series in total
##
## Training set: 1998Jan-2016Dec
## Test set: 2017Jan-2017Dec
#----------------------------------------------------------------------
if (grepl("tourism", data_label)){
# Formalize data (Index, Time, Series1, ..., Seriesn)
dat <- readRDS("data/tourism_data.rds")
h <- 12
# Data details
k <- sub('.*_', '', data_label) |> as.numeric() # 1:12
freq <- 12
start_train <- c(1998, 1)
data_eg <- dat |>
filter(Index == 1) |>
select(!c(Index, Time)) |>
ts(frequency = freq, start = start_train)
length_train <- 240 - h - k + 1
end_train <- time(data_eg)[length_train]
start_test <- time(data_eg)[length_train + 1]
end_test <- time(data_eg)[length_train + h]
# Forecasting method
fmethod <- "ets"
special <- NULL
# S matrix
S <- readRDS("data/tourism_S.rds")
}
#----------------------------------------------------------------------
# ABS - Unemployed persons by Duration of job search, State and Territory
##
## 6291.0.55.001 - UM2 - Unemployed persons by Duration of job search, State and Territory, January 1991 onwards
##
## Monthly series
## Duration of job search (Duration, 6) * State and territory (STT, 8): n = 63 series in total, nb = 48 series at the bottom level
##
## Training set: 2010Jan-2022Jul
## Test set: 2022Aug-2023Jul
#----------------------------------------------------------------------
if (grepl("labour", data_label)){
# Formalize data (Index, Time, Series1, ..., Seriesn)
dat <- readRDS("data/labour_data.rds")
h <- 12
# Data details
k <- sub('.*_', '', data_label) |> as.numeric() # 1:12
freq <- 12
start_train <- c(2010, 1)
data_eg <- dat |>
filter(Index == 1) |>
select(!c(Index, Time)) |>
ts(frequency = freq, start = start_train)
length_train <- 163 - h - k + 1
end_train <- time(data_eg)[length_train]
start_test <- time(data_eg)[length_train + 1]
end_test <- time(data_eg)[length_train + h]
# Forecasting method
fmethod <- "ets"
special <- NULL
# S matrix
S <- readRDS("data/labour_S.rds")
}
#################################################
# Generate base forecasts
#################################################
indices <- dat |>
distinct(Index) |>
pull(Index) # all indices
fits <- resids <- train <- basefc <- test <- data.frame()
pb <- lazybar::lazyProgressBar(length(indices))
for (index in indices){
# Hierarchical time series
data_index <- dat |>
filter(Index == index) |>
select(!c(Index, Time)) |>
ts(frequency = freq, start = start_train)
train_index <- window(data_index, start = start_train, end = end_train)
test_index <- window(data_index, start = start_test, end = end_test)
# Base forecasts
basef <- base_forecast(train_index, method = fmethod, h = NROW(test_index), special = special)
# Results
fits <- rbind(fits,
sapply(basef, function(l) l$fitted) |>
data.frame(Index = index))
resids <- rbind(resids,
sapply(basef, function(l) l$residuals) |>
data.frame(Index = index))
train <- rbind(train,
sapply(basef, function(l) l$train) |>
data.frame(Index = index))
basefc_index <- sapply(basef, function(l) l$forecast)
if (is.vector(basefc_index)){
basefc_index <- t(basefc_index)
}
basefc <- rbind(basefc,
basefc_index |>
data.frame(Index = index))
test <- rbind(test,
test_index |> data.frame(Index = index))
pb$tick()$print()
}
#################################################
# Save base forecast results
#################################################
for (i in c("S", "fits", "resids", "train", "basefc","test")){
saveRDS(get(i), file = paste0("data/", data_label, "_", i, ".rds"))
}