【问题标题】:Hierarchical Forecasting problem generating the hts object生成 hts 对象的分层预测问题
【发布时间】:2020-01-31 22:21:21
【问题描述】:

您好,我想按照 Hyndman Forecasting 的书中第 10 章所述进行分层预测:https://otexts.com/fpp2/

我的问题是,为了生成这种类型的预测(特别是自下而上的方法),我需要生成一个矩阵的 hts 对象。例如:

如果我有这样的数据框: Image of example of dataframe prior to hts object

我需要将它转换成这样的矩阵: Image of Matrix that I need 对于这个矩阵,每一行都是一个时间单位(可能是天、月等)。

我的问题是我的数据框是这样的: Image of Problem with dataframe

一列是日期,另一列是我需要预测销售额的类别。问题是:对于supermarket=4、id_product=187 和id_label=a,系统在第21 天和第23 天记录了动作,但在第22 天没有任何反应,这意味着我需要在那天设置sales=0,或者换句话说a像这样的行: Image of Row missing

如何生成创建 hts 对象所需的矩阵,以 0 创建缺少的行? (我有数千行缺失,所以手工完成将是一场噩梦)

这是制作可重现示例的示例:

date=c("2019-03-22","2019-03-23","2019-04-24","2019-03-25")
id_supermarket=c(4,4,2,2)
id_product=c(187,187,189,190)
id_label=c("a","a","c","d")
sales=c(21,22,23,24)

df=as.data.frame(cbind(date,id_supermarket,id_product,id_label,sales))

提前致谢。

【问题讨论】:

    标签: r forecasting forecast


    【解决方案1】:

    我建议您使用fable 包而不是hts。它更新且更易于使用。这是您的数据的示例。

    图书馆(tsibble) 图书馆(寓言)

    # Create tsibble
    df <- tibble(
      date = lubridate::ymd(c("2019-03-22", "2019-03-23", "2019-03-24", "2019-03-25")),
      id_supermarket = as.character(c(4, 4, 2, 2)),
      id_product = c(187, 187, 189, 190),
      id_label = c("a", "a", "c", "d"),
      sales = c(21, 22, 23, 24)
    ) %>%
      as_tsibble(index = date, key = c(id_supermarket, id_product, id_label)) %>%
      fill_gaps(.full = TRUE)
    
    # Forecast with reconciliation
    fc <- df %>%
      aggregate_key(id_supermarket * id_label, sales = sum(sales, na.rm = TRUE)) %>%
      model(
        arima = ARIMA(sales)
      ) %>%
      reconcile(
        arima = min_trace(arima)
      ) %>%
      forecast(h = "5 days")
    
    fc
    #> # A fable: 45 x 6 [1D]
    #> # Key:     id_supermarket, id_label, .model [9]
    #>    id_supermarket id_label .model date       sales .distribution
    #>    <chr>          <chr>    <chr>  <date>     <dbl> <dist>       
    #>  1 2              c        arima  2019-03-26  5.82 N(5.8, 44)   
    #>  2 2              c        arima  2019-03-27  5.82 N(5.8, 44)   
    #>  3 2              c        arima  2019-03-28  5.82 N(5.8, 44)   
    #>  4 2              c        arima  2019-03-29  5.82 N(5.8, 44)   
    #>  5 2              c        arima  2019-03-30  5.82 N(5.8, 44)   
    #>  6 2              d        arima  2019-03-26  6.34 N(6.3, 46)   
    #>  7 2              d        arima  2019-03-27  6.34 N(6.3, 46)   
    #>  8 2              d        arima  2019-03-28  6.34 N(6.3, 46)   
    #>  9 2              d        arima  2019-03-29  6.34 N(6.3, 46)   
    #> 10 2              d        arima  2019-03-30  6.34 N(6.3, 46)   
    #> # … with 35 more rows
    

    reprex package (v0.3.0) 于 2020 年 2 月 1 日创建

    【讨论】:

    • 非常感谢 Rob,我非常欣赏您在 Forecasting 方面的工作 =)
    猜你喜欢
    • 2016-07-12
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2018-04-26
    • 1970-01-01
    • 2011-08-11
    • 2015-05-02
    相关资源
    最近更新 更多