-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathglobal_model_experiments.R
196 lines (155 loc) · 16.8 KB
/
global_model_experiments.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
BASE_DIR <- "SETAR_Trees"
source(file.path(BASE_DIR, "configs", "configs.R", fsep = "/"))
# A wrapper function to execute global forecasting models
# Parameters
# input_file_name - .tsf file name
# lag - The number of past lags that should be considered during training
# forecast_horizon - The expected forecast horizon
# dataset_name - Name of the dataset
# method_name - Name of the global model. Current supportive global model names: pooled_regression, regression_tree, ffnn, catboost, lightgbm, xgboost, rf, cubist
# key - The name of the attribute that should be used as the key when creating the tsibble from the .tsf file. If doesn't provide, a data frame will be returned instead of a tsibble
# index - The name of the time attribute that should be used as the index when creating the tsibble from the .tsf file. If doesn't provide, it will search for a valid index. When no valid index found, a data frame will be returned instead of a tsibble
# integer_conversion - Whether the final forecasts should be rounded or not
# scale - Whether the series should be normalized before training. When TRUE, mean normalization is applied to each series
# categorical_covariates - A vector containing the names of external categorical covariates. The .tsf file should contain series corresponding with each categorical covariate
# numerical_covariates - A vector containing the names of external numerical covariates. The .tsf file should contain series corresponding with each numerical covariate
# series_prefix - The prefix used to identify original time series in the .tsf file. This is only required when the models are trained with external covariates
# splitter - The splitter used in the names of time series in the .tsf file to separate the series type and number. This is only required when the models are trained with external covariates
do_global_forecasting <- function(input_file_name, lag, forecast_horizon, dataset_name, method_name = "pooled_regression", key = "series_name", index = "start_timestamp", integer_conversion = F, scale = FALSE, categorical_covariates = NULL, numerical_covariates = NULL, series_prefix = NULL, splitter = "_"){
# Creating training and test sets
loaded_data <- create_train_test_sets(input_file_name, key, index, forecast_horizon, categorical_covariates, numerical_covariates, series_prefix, splitter)
training_set <- loaded_data[[1]]
test_set <- loaded_data[[2]]
seasonality <- loaded_data[[4]]
# Start timestamp
start_time <- Sys.time()
# Forecasting
forecasts <- start_forecasting(training_set, lag, forecast_horizon, method_name, scale, test_set, categorical_covariates, numerical_covariates)
# Finish timestamp
end_time <- Sys.time()
if(integer_conversion)
forecasts <- round(forecasts)
file_name <- paste0(dataset_name, "_", method_name)
dir.create(file.path(BASE_DIR, "results", "forecasts", "global_models", fsep = "/"), showWarnings = FALSE, recursive = TRUE)
write.table(forecasts, file.path(BASE_DIR, "results", "forecasts", "global_models", paste0(file_name, "_forecasts.txt"), fsep = "/"), row.names = FALSE, col.names = FALSE, quote=FALSE)
# Error calculations
calculate_errors(forecasts, test_set$series, training_set$series, seasonality, file_name)
# Execution time
dir.create(file.path(BASE_DIR, "results", "execution_times", fsep = "/"), showWarnings = FALSE, recursive = TRUE)
exec_time <- end_time - start_time
print(exec_time)
write(paste(exec_time, attr(exec_time, "units")), file = file.path(BASE_DIR, "results", "execution_times", paste0(file_name, ".txt"), fsep = "/"), append = FALSE)
}
# Experiments
# Pooled Regression
# Without covariates
do_global_forecasting("chaotic_logistic_dataset.tsf", 10, 8, "chaotic_logistic", method_name = "pooled_regression", index = NULL)
do_global_forecasting("mackey_glass_dataset.tsf", 10, 8, "mackey_glass", method_name = "pooled_regression", index = NULL)
do_global_forecasting("kaggle_web_traffic_dataset_1000_without_missing_values.tsf", 10, 59, "kaggle_daily", method_name = "pooled_regression", integer_conversion = T)
do_global_forecasting("tourism_quarterly_dataset.tsf", 10, 8, "tourism_quarterly", method_name = "pooled_regression")
do_global_forecasting("rossmann_dataset_without_missing_values.tsf", 10, 48, "rossmann", method_name = "pooled_regression", integer_conversion = T)
do_global_forecasting("favourita_sales_1000_dataset.tsf", 10, 16, "favourita", method_name = "pooled_regression", index = NULL)
do_global_forecasting("tourism_monthly_dataset.tsf", 15, 24, "tourism_monthly", method_name = "pooled_regression")
do_global_forecasting("m5_dataset.tsf", 10, 28, "m5", method_name = "pooled_regression")
# With covariates
do_global_forecasting("rossmann_data_with_corvariates.tsf", 10, 48, "rossmann_with_cov", method_name = "pooled_regression", integer_conversion = T, index = NULL, categorical_covariates = c("Open", "Promo", "StateHoliday", "SchoolHoliday"), numerical_covariates = "Customers", series_prefix = "T")
do_global_forecasting("kaggle_1000_with_date_corvariates.tsf", 10, 59, "kaggle_daily_with_cov", method_name = "pooled_regression", index = NULL, integer_conversion = T, categorical_covariates = "wday", series_prefix = "T")
do_global_forecasting("favourita_1000_with_date_corvariates.tsf", 10, 16, "favourita_with_cov", method_name = "pooled_regression", index = NULL, categorical_covariates = "wday", series_prefix = "T")
# Cubist
# Without covariates
do_global_forecasting("chaotic_logistic_dataset.tsf", 10, 8, "chaotic_logistic", method_name = "cubist", index = NULL)
do_global_forecasting("mackey_glass_dataset.tsf", 10, 8, "mackey_glass", method_name = "cubist", index = NULL)
do_global_forecasting("kaggle_web_traffic_dataset_1000_without_missing_values.tsf", 10, 59, "kaggle_daily", method_name = "cubist", integer_conversion = T)
do_global_forecasting("tourism_quarterly_dataset.tsf", 10, 8, "tourism_quarterly", method_name = "cubist")
do_global_forecasting("rossmann_dataset_without_missing_values.tsf", 10, 48, "rossmann", method_name = "cubist", integer_conversion = T)
do_global_forecasting("favourita_sales_1000_dataset.tsf", 10, 16, "favourita", method_name = "cubist", index = NULL)
do_global_forecasting("tourism_monthly_dataset.tsf", 15, 24, "tourism_monthly", method_name = "cubist")
do_global_forecasting("m5_dataset.tsf", 10, 28, "m5", method_name = "cubist")
# With covariates
do_global_forecasting("rossmann_data_with_corvariates.tsf", 10, 48, "rossmann_with_cov", method_name = "cubist", integer_conversion = T, index = NULL, categorical_covariates = c("Open", "Promo", "StateHoliday", "SchoolHoliday"), numerical_covariates = "Customers", series_prefix = "T")
do_global_forecasting("kaggle_1000_with_date_corvariates.tsf", 10, 59, "kaggle_daily_with_cov", method_name = "cubist", index = NULL, integer_conversion = T, categorical_covariates = "wday", series_prefix = "T")
do_global_forecasting("favourita_1000_with_date_corvariates.tsf", 10, 16, "favourita_with_cov", method_name = "cubist", index = NULL, categorical_covariates = "wday", series_prefix = "T")
# CatBoost
# Without covariates
do_global_forecasting("chaotic_logistic_dataset.tsf", 10, 8, "chaotic_logistic", index = NULL, method_name = "catboost")
do_global_forecasting("mackey_glass_dataset.tsf", 10, 8, "mackey_glass", index = NULL, method_name = "catboost")
do_global_forecasting("kaggle_web_traffic_dataset_1000_without_missing_values.tsf", 10, 59, "kaggle_daily", integer_conversion = T, method_name = "catboost")
do_global_forecasting("tourism_quarterly_dataset.tsf", 10, 8, "tourism_quarterly", method_name = "catboost")
do_global_forecasting("rossmann_dataset_without_missing_values.tsf", 10, 48, "rossmann", method_name = "catboost", integer_conversion = T)
do_global_forecasting("favourita_sales_1000_dataset.tsf", 10, 16, "favourita", method_name = "catboost", index = NULL)
do_global_forecasting("tourism_monthly_dataset.tsf", 15, 24, "tourism_monthly", method_name = "catboost")
do_global_forecasting("m5_dataset.tsf", 10, 28, "m5", method_name = "catboost")
# With covariates
do_global_forecasting("rossmann_data_with_corvariates.tsf", 10, 48, "rossmann_with_cov", method_name = "catboost", integer_conversion = T, index = NULL, categorical_covariates = c("Open", "Promo", "StateHoliday", "SchoolHoliday"), numerical_covariates = "Customers", series_prefix = "T")
do_global_forecasting("kaggle_1000_with_date_corvariates.tsf", 10, 59, "kaggle_daily_with_cov", method_name = "catboost", index = NULL, integer_conversion = T, categorical_covariates = "wday", series_prefix = "T")
do_global_forecasting("favourita_1000_with_date_corvariates.tsf", 10, 16, "favourita_with_cov", method_name = "catboost", index = NULL, categorical_covariates = "wday", series_prefix = "T")
# Regression Tree
# Without covariates
do_global_forecasting("chaotic_logistic_dataset.tsf", 10, 8, "chaotic_logistic", index = NULL, method_name = "regression_tree")
do_global_forecasting("mackey_glass_dataset.tsf", 10, 8, "mackey_glass", index = NULL, method_name = "regression_tree")
do_global_forecasting("kaggle_web_traffic_dataset_1000_without_missing_values.tsf", 10, 59, "kaggle_daily", integer_conversion = T, method_name = "regression_tree")
do_global_forecasting("tourism_quarterly_dataset.tsf", 10, 8, "tourism_quarterly", method_name = "regression_tree")
do_global_forecasting("rossmann_dataset_without_missing_values.tsf", 10, 48, "rossmann", method_name = "regression_tree", integer_conversion = T)
do_global_forecasting("favourita_sales_1000_dataset.tsf", 10, 16, "favourita", method_name = "regression_tree", index = NULL)
do_global_forecasting("tourism_monthly_dataset.tsf", 15, 24, "tourism_monthly", method_name = "regression_tree")
do_global_forecasting("m5_dataset.tsf", 10, 28, "m5", method_name = "regression_tree")
# With covariates
do_global_forecasting("rossmann_data_with_corvariates.tsf", 10, 48, "rossmann_with_cov", method_name = "regression_tree", integer_conversion = T, index = NULL, categorical_covariates = c("Open", "Promo", "StateHoliday", "SchoolHoliday"), numerical_covariates = "Customers", series_prefix = "T")
do_global_forecasting("kaggle_1000_with_date_corvariates.tsf", 10, 59, "kaggle_daily_with_cov", method_name = "regression_tree", index = NULL, integer_conversion = T, categorical_covariates = "wday", series_prefix = "T")
do_global_forecasting("favourita_1000_with_date_corvariates.tsf", 10, 16, "favourita_with_cov", method_name = "regression_tree", index = NULL, categorical_covariates = "wday", series_prefix = "T")
# LightGBM
# Without covariates
do_global_forecasting("chaotic_logistic_dataset.tsf", 10, 8, "chaotic_logistic", index = NULL, method_name = "lightgbm")
do_global_forecasting("mackey_glass_dataset.tsf", 10, 8, "mackey_glass", index = NULL, method_name = "lightgbm")
do_global_forecasting("kaggle_web_traffic_dataset_1000_without_missing_values.tsf", 10, 59, "kaggle_daily", integer_conversion = T, method_name = "lightgbm")
do_global_forecasting("tourism_quarterly_dataset.tsf", 10, 8, "tourism_quarterly", method_name = "lightgbm")
do_global_forecasting("rossmann_dataset_without_missing_values.tsf", 10, 48, "rossmann", method_name = "lightgbm", integer_conversion = T)
do_global_forecasting("favourita_sales_1000_dataset.tsf", 10, 16, "favourita", method_name = "lightgbm", index = NULL)
do_global_forecasting("tourism_monthly_dataset.tsf", 15, 24, "tourism_monthly", method_name = "lightgbm")
do_global_forecasting("m5_dataset.tsf", 10, 28, "m5", method_name = "lightgbm")
# With covariates
do_global_forecasting("rossmann_data_with_corvariates.tsf", 10, 48, "rossmann_with_cov", method_name = "lightgbm", integer_conversion = T, index = NULL, categorical_covariates = c("Open", "Promo", "StateHoliday", "SchoolHoliday"), numerical_covariates = "Customers", series_prefix = "T")
do_global_forecasting("kaggle_1000_with_date_corvariates.tsf", 10, 59, "kaggle_daily_with_cov", method_name = "lightgbm", index = NULL, integer_conversion = T, categorical_covariates = "wday", series_prefix = "T")
do_global_forecasting("favourita_1000_with_date_corvariates.tsf", 10, 16, "favourita_with_cov", method_name = "lightgbm", index = NULL, categorical_covariates = "wday", series_prefix = "T")
# XGBoost
# Without covariates
do_global_forecasting("chaotic_logistic_dataset.tsf", 10, 8, "chaotic_logistic", index = NULL, method_name = "xgboost")
do_global_forecasting("mackey_glass_dataset.tsf", 10, 8, "mackey_glass", index = NULL, method_name = "xgboost")
do_global_forecasting("kaggle_web_traffic_dataset_1000_without_missing_values.tsf", 10, 59, "kaggle_daily", integer_conversion = T, method_name = "xgboost")
do_global_forecasting("tourism_quarterly_dataset.tsf", 10, 8, "tourism_quarterly", method_name = "xgboost")
do_global_forecasting("rossmann_dataset_without_missing_values.tsf", 10, 48, "rossmann", method_name = "xgboost", integer_conversion = T)
do_global_forecasting("favourita_sales_1000_dataset.tsf", 10, 16, "favourita", method_name = "xgboost", index = NULL)
do_global_forecasting("tourism_monthly_dataset.tsf", 15, 24, "tourism_monthly", method_name = "xgboost")
do_global_forecasting("m5_dataset.tsf", 10, 28, "m5", method_name = "xgboost")
# With covariates
do_global_forecasting("rossmann_data_with_corvariates.tsf", 10, 48, "rossmann_with_cov", method_name = "xgboost", integer_conversion = T, index = NULL, categorical_covariates = c("Open", "Promo", "StateHoliday", "SchoolHoliday"), numerical_covariates = "Customers", series_prefix = "T")
do_global_forecasting("kaggle_1000_with_date_corvariates.tsf", 10, 59, "kaggle_daily_with_cov", method_name = "xgboost", index = NULL, integer_conversion = T, categorical_covariates = "wday", series_prefix = "T")
do_global_forecasting("favourita_1000_with_date_corvariates.tsf", 10, 16, "favourita_with_cov", method_name = "xgboost", index = NULL, categorical_covariates = "wday", series_prefix = "T")
# Random Forest
# Without covariates
do_global_forecasting("chaotic_logistic_dataset.tsf", 10, 8, "chaotic_logistic", index = NULL, method_name = "rf")
do_global_forecasting("mackey_glass_dataset.tsf", 10, 8, "mackey_glass", index = NULL, method_name = "rf")
do_global_forecasting("kaggle_web_traffic_dataset_1000_without_missing_values.tsf", 10, 59, "kaggle_daily", integer_conversion = T, method_name = "rf")
do_global_forecasting("tourism_quarterly_dataset.tsf", 10, 8, "tourism_quarterly", method_name = "rf")
do_global_forecasting("rossmann_dataset_without_missing_values.tsf", 10, 48, "rossmann", method_name = "rf", integer_conversion = T)
do_global_forecasting("favourita_sales_1000_dataset.tsf", 10, 16, "favourita", method_name = "rf", index = NULL)
do_global_forecasting("tourism_monthly_dataset.tsf", 15, 24, "tourism_monthly", method_name = "rf")
do_global_forecasting("m5_dataset.tsf", 10, 28, "m5", method_name = "rf")
# With covariates
do_global_forecasting("rossmann_data_with_corvariates.tsf", 10, 48, "rossmann_with_cov", method_name = "rf", integer_conversion = T, index = NULL, categorical_covariates = c("Open", "Promo", "StateHoliday", "SchoolHoliday"), numerical_covariates = "Customers", series_prefix = "T")
do_global_forecasting("kaggle_1000_with_date_corvariates.tsf", 10, 59, "kaggle_daily_with_cov", method_name = "rf", index = NULL, integer_conversion = T, categorical_covariates = "wday", series_prefix = "T")
do_global_forecasting("favourita_1000_with_date_corvariates.tsf", 10, 16, "favourita_with_cov", method_name = "rf", index = NULL, categorical_covariates = "wday", series_prefix = "T")
# FFNN
# Without covariates
do_global_forecasting("chaotic_logistic_dataset.tsf", 10, 8, "chaotic_logistic", index = NULL, method_name = "ffnn")
do_global_forecasting("mackey_glass_dataset.tsf", 10, 8, "mackey_glass", index = NULL, method_name = "ffnn")
do_global_forecasting("kaggle_web_traffic_dataset_1000_without_missing_values.tsf", 10, 59, "kaggle_daily", integer_conversion = T, method_name = "ffnn")
do_global_forecasting("tourism_quarterly_dataset.tsf", 10, 8, "tourism_quarterly", method_name = "ffnn")
do_global_forecasting("rossmann_dataset_without_missing_values.tsf", 10, 48, "rossmann", method_name = "ffnn", integer_conversion = T)
do_global_forecasting("favourita_sales_1000_dataset.tsf", 10, 16, "favourita", method_name = "ffnn", index = NULL)
do_global_forecasting("tourism_monthly_dataset.tsf", 15, 24, "tourism_monthly", method_name = "ffnn")
do_global_forecasting("m5_dataset.tsf", 10, 28, "m5", method_name = "ffnn")
# With covariates
do_global_forecasting("rossmann_data_with_corvariates.tsf", 10, 48, "rossmann_with_cov", method_name = "ffnn", integer_conversion = T, index = NULL, categorical_covariates = c("Open", "Promo", "StateHoliday", "SchoolHoliday"), numerical_covariates = "Customers", series_prefix = "T")
do_global_forecasting("kaggle_1000_with_date_corvariates.tsf", 10, 59, "kaggle_daily_with_cov", method_name = "ffnn", index = NULL, integer_conversion = T, categorical_covariates = "wday", series_prefix = "T")
do_global_forecasting("favourita_1000_with_date_corvariates.tsf", 10, 16, "favourita_with_cov", method_name = "ffnn", index = NULL, categorical_covariates = "wday", series_prefix = "T")