Fellow contributors,
I have been working with a hierarchical time series, relating to a set of identical products in a number of stores. For this purpose when we aggregate the data set based on 2 attributes like "store" and "product_type" in my case, we should then aggregate the target variable which is "demand" for every individual product for every group or hierarchy.
What I would like to do is add another categorical variable to my model let's say "Dynamic Harmonic Regression" as I am using weekly time series. However, I don't know how I should include it when my external variable is a categorical variable with 4 levels. I would like to know how I could aggregate this or whether there is anything I could do about it.
Here you can find a small reproducible example:
library(tidyverse)
library(tsibble)
library(tsibbledata)
library(fable)
library(fabletools)
library(fpp3)
library(readxl)
library(fable.prophet)
library(feasts)
store <- c(rep('st1', 8), rep('st2', 8))
product_type <- c(rep('type1', 4), rep('type2', 4), rep('type1', 4), rep('type2', 4))
products <- c(rep('A', 2), rep('B', 2), rep('C', 2), rep('D', 2),
rep('A', 2), rep('B', 2), rep('C', 2), rep('D', 2))
demands <- c(round(sample(c(1:100), 16, replace = TRUE)))
external_reg <- c(sample(c('red', 'green', 'blue'), 16, replace = TRUE))
date_week <- rep(1:4, 4)
date_year <- rep(2019:2022, 4)
my_data <- tibble(date_year, date_week, store, product_type, products, demands, external_reg)
my_data %>%
mutate(Date = ymd(paste0(date_year, "-01-01")) + weeks(date_week - 1)) %>%
mutate(Week = yearweek(Date)) %>%
as_tsibble(key = c(store, product_type), index = Week) %>%
aggregate_key(store * product_type, Demand_Agg = sum(demands))
Clearly the external regeressor should be a column in my tsibble
:
# A tsibble: 36 x 4 [53W]
# Key: store, product_type [9]
Week store product_type Demand_Agg
<week> <chr*> <chr*> <dbl>
1 2019 W01 <aggregated> <aggregated> 188
2 2020 W02 <aggregated> <aggregated> 142
3 2021 W02 <aggregated> <aggregated> 259
4 2022 W03 <aggregated> <aggregated> 186
5 2019 W01 st1 <aggregated> 89
6 2019 W01 st2 <aggregated> 99
7 2020 W02 st1 <aggregated> 52
8 2020 W02 st2 <aggregated> 90
9 2021 W02 st1 <aggregated> 95
10 2021 W02 st2 <aggregated> 164
# … with 26 more rows
Thank you very much in advance.