Copyright (c) Microsoft Corporation.
Licensed under the MIT License.

In this notebook, we generate the datasets that will be used for model training and validating.

The orange juice dataset comes from the bayesm package, and gives pricing and sales figures over time for a variety of orange juice brands in several stores in Florida. Rather than installing the entire package (which is very complex), we download the dataset itself from the GitHub mirror of the CRAN repository.

# download the data from the GitHub mirror of the bayesm package source
ojfile <- tempfile(fileext=".rda")
download.file("https://github.com/cran/bayesm/raw/master/data/orangeJuice.rda", ojfile)
load(ojfile)
file.remove(ojfile)

The dataset generation parameters are obtained from the file ojdata_forecast_settings.yaml; you can modify that file to vary the experimental setup. The settings are

Parameter Description Default
N_SPLITS The number of splits to make. 10
HORIZON The forecast horizon for the test dataset for each split. 2
GAP The gap in weeks from the end of the training period to the start of the testing period; see below. 2
FIRST_WEEK The first week of data to use. 40
LAST_WEEK The last week of data to use. 156
START_DATE The actual calendar date for the start of the first week in the data. 1989-09-14

A complicating factor is that the data does not include every possible combination of store, brand and date, so we have to pad out the missing rows with complete. In addition, one store/brand combination has no data beyond week 156; we therefore end the analysis at this week. We also do not fill in the missing values in the data, as many of the modelling functions in the fable package can handle this innately.

library(tidyr)
library(dplyr)
library(tsibble)
library(feasts)
library(fable)

settings <- yaml::read_yaml(here::here("examples/grocery_sales/R/forecast_settings.yaml"))
start_date <- as.Date(settings$START_DATE)
train_periods <- seq(to=settings$LAST_WEEK - settings$HORIZON - settings$GAP + 1,
                     by=settings$HORIZON,
                     length.out=settings$N_SPLITS)

oj_data <- orangeJuice$yx %>%
    complete(store, brand, week) %>%
    mutate(week=yearweek(start_date + week*7)) %>%
    as_tsibble(index=week, key=c(store, brand))

Here are some glimpses of what the data looks like. The dependent variable is logmove, the logarithm of the total sales for a given brand and store, in a particular week.

head(oj_data)

The time series plots for a small subset of brands and stores are shown below. We can make the following observations:

library(ggplot2)

oj_data %>%
    filter(store < 25, brand < 5) %>%
    mutate(week=as.Date(week)) %>%
    ggplot(aes(x=week, y=logmove)) +
        geom_line() +
        scale_x_date(labels=NULL) +
        facet_grid(vars(store), vars(brand), labeller="label_both")

Finally, we split the dataset into separate samples for training and testing. The schema used is broadly time series cross-validation, whereby we train a model on data up to time \(t\), test it on data for times \(t+1\) to \(t+k\), then train on data up to time \(t+k\), test it on data for times \(t+k+1\) to \(t+2k\), and so on. In this specific case study, however, we introduce a small extra piece of complexity based on discussions with domain experts. We train a model on data up to week \(t\), then test it on week \(t+2\) to \(t+3\). Then we train on data up to week \(t+2\), and test it on weeks \(t+4\) to \(t+5\), and so on. There is thus always a gap of one week between the training and test samples. The reason for this is because in reality, inventory planning always takes some time; the gap allows store managers to prepare the stock based on the forecasted demand.

subset_oj_data <- function(start, end)
{
    start <- yearweek(start_date + start*7)
    end <- yearweek(start_date + end*7)
    filter(oj_data, week >= start, week <= end)
}

oj_train <- lapply(train_periods, function(i) subset_oj_data(settings$FIRST_WEEK, i))
oj_test <- lapply(train_periods, function(i) subset_oj_data(i + settings$GAP, i + settings$GAP + settings$HORIZON - 1))

save(oj_train, oj_test, file=here::here("examples/grocery_sales/R/data.Rdata"))

head(oj_train[[1]])
head(oj_test[[1]])
LS0tCnRpdGxlOiBEYXRhIHByZXBhcmF0aW9uCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KCl9Db3B5cmlnaHQgKGMpIE1pY3Jvc29mdCBDb3Jwb3JhdGlvbi5fPGJyLz4KX0xpY2Vuc2VkIHVuZGVyIHRoZSBNSVQgTGljZW5zZS5fCgpJbiB0aGlzIG5vdGVib29rLCB3ZSBnZW5lcmF0ZSB0aGUgZGF0YXNldHMgdGhhdCB3aWxsIGJlIHVzZWQgZm9yIG1vZGVsIHRyYWluaW5nIGFuZCB2YWxpZGF0aW5nLiAKClRoZSBvcmFuZ2UganVpY2UgZGF0YXNldCBjb21lcyBmcm9tIHRoZSBiYXllc20gcGFja2FnZSwgYW5kIGdpdmVzIHByaWNpbmcgYW5kIHNhbGVzIGZpZ3VyZXMgb3ZlciB0aW1lIGZvciBhIHZhcmlldHkgb2Ygb3JhbmdlIGp1a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