Introduction
In this blog post, I’ll use the data that I cleaned in a previous
blog post, which you can download
here. If you want to follow along,
download the monthly data. In my last blog post
I showed how to perform a grid search the “tidy” way. As an example, I looked for the right
hyperparameters of a SARIMA model. However, the goal of the post was not hyperparameter optimization
per se, so I did not bother with tuning the hyperparameters on a validation set, and used the test
set for both validation of the hyperparameters and testing the forecast. Of course, this is not great
because doing this might lead to overfitting the hyperparameters to the test set. So in this blog post
I split my data into trainig, validation and testing sets and use a genetic algorithm to look
for the hyperparameters. Again, this is not the most optimal way to go about this problem, since
the {forecast} package contains the very useful auto.arima() function. I just wanted to see
what kind of solution a genetic algorithm would return, and also try different cost functions.
If you’re interested, read on!
Setup
Let’s first load some libraries and define some helper functions (the helper functions were explained
in the previous blog posts):
library(tidyverse)
library(forecast)
library(rgenoud)
library(parallel)
library(lubridate)
library(furrr)
library(tsibble)
library(brotools)
ihs %
spread(key, value) %__%
rename(date = index,
upper80 = `80%`,
upper95 = `95%`)
lower %
as_tsibble() %__%
spread(key, value) %__%
rename(date = index,
lower80 = `80%`,
lower95 = `95%`)
reduce(list(point_estimate, upper, lower), full_join)
}
Now, let’s load the data:
avia_clean_monthly %
filter(year(date) __ 2013) %__%
group_by(date) %__%
summarise(total_passengers = sum(passengers)) %__%
pull(total_passengers) %__%
ts(., frequency = 12, start = c(2005, 1))
avia_clean_validation %
select(date, passengers) %__%
filter(between(year(date), 2013, 2016)) %__%
group_by(date) %__%
summarise(total_passengers = sum(passengers)) %__%
pull(total_passengers) %__%
ts(., frequency = 12, start = c(2013, 1))
avia_clean_test %
select(date, passengers) %__%
filter(year(date) __= 2016) %__%
group_by(date) %__%
summarise(total_passengers = sum(passengers)) %__%
pull(total_passengers) %__%
ts(., frequency = 12, start = c(2016, 1))
logged_test_data