Middle out fabletools reconciliation approach give problem with forecast function

179 views Asked by At

I have a grouped time series with items and their category and I would like to make 6months sales forecasting. I would like to o use intermediate level (category) to make base forecasting because the stagionality and trends maybe are better valued. So i grouped my data for key, and i would like to use middle_out approch, the total sales use bottom up and single item are forected useing top down approach I'm using fabletools middle_out function, but when i try to make forecast it doesn't work this is my code:

library(reshape)
library(tidyverse)
library(tsibble)
library(dplyr)
library(fable)
library(fpp2)
library(forecast)

#read data from csv
#example dataset
set.seed(42)  ## for sake of reproducibility
n <- 6
data_example <- data.frame(Date=seq.Date(as.Date("2020-12-01"), as.Date("2021-05-01"), "month"),
                  No_=sample(1800:1830, n, replace=TRUE),
                  Category=rep(LETTERS[1:3], n),
                  Quantity=sample(18:24, n, replace=TRUE))


sell_full <- data_example %>% mutate(Month=yearmonth(Date)) %>% group_by(No_,Category, Month) %>% summarise(Quant = sum(Quantity), .groups = 'keep')
sell_full <- na.omit(sell_full)

#data


#conversion to tsibble for forecastings

sell_full <- as_tsibble(sell_full, key=c(No_, Category), index=Month)
sell_full <- sell_full %>% aggregate_key((Category/No_), Quant= sum(Quant)) 
#sell_full<- filter(sell_full, !is.na(sell_full$Quant))

sell_full <- sell_full %>% fill_gaps(Quant=0, .full=TRUE)

fit <- sell_full %>%model(ets = ETS(Quant~ error("A") + trend("A") + season("A")))%>% middle_out(split=1)

fc <- forecast(fit, h = "6 months", level=1,lambda="auto")

if I put method="mo" in forecast method as documentation says it return this error

Error in meanf(object, h = h, level = level, fan = fan, lambda = lambda,  : 
  unused argument (method = "mo")

if i doesn't put method info in forecast it return this error:

<error/vctrs_error_ptype2>
Error in `vec_compare()`:
! Can't combine `..1` <agg_vec> and `..2` <double>.
---
Backtrace:
  1. generics::forecast(fit, h = "6 months", level = 1, lambda = "auto")
  2. forecast:::forecast.default(fit, h = "6 months", level = 1, lambda = "auto")
  3. forecast:::forecast.ts(object, ...)
  4. forecast::meanf(...)
  5. forecast::BoxCox(x, lambda)
  6. forecast::BoxCox.lambda(x, lower = -0.9)
  7. fabletools:::Ops.lst_mdl(x, 0)
 11. fabletools:::map2(e1, e2, .Generic)
 12. base::mapply(.f, .x, .y, MoreArgs = list(...), SIMPLIFY = FALSE)
 13. vctrs:::`<=.vctrs_vctr`(dots[[1L]][[1L]], dots[[2L]][[1L]])
 14. vctrs::vec_compare(e1, e2)

The Documentions about it is very bad, someone can help me?

UPDATE: As someone suggest to me, I tried to remove some package, now my library are:

library(tsibble)
library(dplyr)
library(fable)
library(fpp3)
library(conflicted)

Now the error is changed. when I try to make forecast function I have this error:

Error in build_key_data_smat(key_data) : 
  argument "key_data" is missing, with no default

and if I put key_data = "Category" (Category is the split layer) the error is:

fc <- forecast(fit, h = "6 months",level=1,lambda="auto", key_data= "Category")
Error in -ncol(x) : invalid argument to unary operator
1

There are 1 answers

0
Isaiah On
library(conflicted)
library(fpp3)
library(tidyverse)
n <- 6
data_example <- data.frame(Date = seq.Date(as.Date("2020-12-01"), as.Date("2021-05-01"), "month"),
                           No_ = sample(1800:1830, n, replace = TRUE),
                           Category = rep(LETTERS[1:3], n),
                           Quantity = sample(18:24, n, replace = TRUE))

sell_full <- data_example |> mutate(Month = yearmonth(Date)) |> group_by(No_,Category, Month) |> summarise(Quant = sum(Quantity), .groups = 'keep')
sell_full <- ungroup(sell_full)
sell_full <- as_tsibble(sell_full, key = c(No_, Category), index = Month)
sell_full <- sell_full %>% aggregate_key((Category/No_), Quant = sum(Quant))
sell_full <- sell_full %>% fill_gaps(Quant = 0, .full = TRUE)
fit <- sell_full %>% model(ets = ETS(Quant~ error("A") + trend("A")))

fc <- fabletools::forecast(fit, h = "6 months", lambda = "auto")

Thought I'd have a look at the code to generate sell_full.

Added an ungroup, took out the seasonal, and took out the middle_out. Runs now, and no longer asks for key_value. The ungroup, as it seemed that you were finished with the grouping. The seasonal as it was not supported by the data. The middle out as it would cause the prompt for key_value. Spent a bit of time on the middle_out leading to forecast asking for key_value, though, hence comment above.

This led me to try another way to do middle_out:

fit <- sell_full %>% model(ets = ETS(Quant~ error("A") + trend("A"))) |> reconcile(mo = middle_out(ets))

This runs fine. This idea came from fpp3 Hoping that this helps! :-)