简体   繁体   English

Middle out fabletools 协调方法给出预测问题 function

[英]Middle out fabletools reconciliation approach give problem with forecast function

I have a grouped time series with items and their category and I would like to make 6months sales forecasting.我有一个包含项目及其类别的分组时间序列,我想进行 6 个月的销售预测。 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:所以我将我的数据分组为关键,我想使用 middle_out approch,总销售额使用自下而上和单个项目使用自上而下的方法来预测我正在使用 fabletools middle_out function,但是当我尝试进行预测时它没有'不工作这是我的代码:

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如果我将 method="mo" 放入 forecast 方法中,如文档所述,它会返回此错误

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:更新:正如有人向我建议的那样,我试图删除一些 package,现在我的图书馆是:

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:当我尝试预测 function 时出现此错误:

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:如果我把 key_data = "Category"(类别是分割层)错误是:

fc <- forecast(fit, h = "6 months",level=1,lambda="auto", key_data= "Category")
Error in -ncol(x) : invalid argument to unary operator
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.以为我会看一下生成 sell_full 的代码。

Added an ungroup, took out the seasonal, and took out the middle_out .添加了一个ungroup,取出了seasonal,取出了middle_out Runs now, and no longer asks for key_value .现在运行,不再要求key_value The ungroup , as it seemed that you were finished with the grouping. ungroup分组,因为您似乎已经完成了分组。 The seasonal as it was not supported by the data.季节性,因为它不受数据支持。 The middle out as it would cause the prompt for key_value . middle out ,因为它会导致提示key_value Spent a bit of time on the middle_out leading to forecast asking for key_value , though, hence comment above.不过,在middle_out上花了一些时间导致 forecast 询问key_value ,因此上面有评论。

This led me to try another way to do middle_out:这让我尝试了另一种方法来做 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: :-)这个想法来自fpp3希望这会有所帮助::-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM