首页 > 解决方案 > 从预测寓言中提取置信区间

问题描述

我遇到了一个问题,试图从一个预测模型中提取 90/95% 置信区间,该预测模型是由一个关键变量构建的,该变量在总共 4 个预测模型中包含 5 个组。主要问题是我不熟悉 R 如何处理和使用 dist 和 hilo 对象类型。

原始 tsibble 的结构为 5 组中的每组 60 个月(300 个观察值)

    >groups
    # A tsibble: 300 x 3 [1M]
    # Key:       Group [5]
          Month Group         Measure
          <mth> <chr>       <dbl>
     1 2016 May Group1      8.75
     2 2016 Jun Group1      8.5 
     3 2016 Jul Group1      7   
     4 2016 Aug Group1      10   
     5 2016 Sep Group1      2   
     6 2016 Oct Group1      6   
     7 2016 Nov Group1      8   
     8 2016 Dec Group1      0   
     9 2017 Jan Group1      16   
    10 2017 Feb Group1      9  
 ... with 290 more rows

我用不同的预测方法形成了一个模型,以及一个组合模型:

    groups%>%model(ets=ETS(Measure),
    mean=MEAN(Measure),
    snaive=SNAIVE(Measure))%>%mutate(combination=(ets+mean+snaive)/3)->groups_avg

这导致结构的mable

    >groups_avg
    # A mable: 5 x 5
    # Key:     Group [5]
      Group              ets   mean   snaive   combination
      <chr>         <model> <mode>  <model>       <model>
    1 Group1 <ETS(A,N,N)> <MEAN> <SNAIVE> <COMBINATION>
    2 Group2 <ETS(A,N,N)> <MEAN> <SNAIVE> <COMBINATION>
    3 Group3 <ETS(M,N,N)> <MEAN> <SNAIVE> <COMBINATION>
    4 Group4 <ETS(A,N,N)> <MEAN> <SNAIVE> <COMBINATION>
    5 Group5 <ETS(A,N,N)> <MEAN> <SNAIVE> <COMBINATION>

然后我预测了 6 个月

    groups_avg%>%forecast(h=6,level=c(90,95))->groups_fc

在产生我对输出 tsibble 应该是什么的想法之前:

>firm_fc%>%hilo(level=c(90,95))->firm_hilo
    > groups_hilo
    # A tsibble: 120 x 7 [1M]
    # Key:       Group, .model [20]
       Group        .model    Month      Measure    .mean            `90%`                   `95%`
       <chr>       <chr>     <mth>    <dist> <dbl>                  <hilo>                  <hilo>
     1 CapstoneLaw ets    2021 May    N(12, 21)     11.6  [4.1332418, 19.04858]90 [ 2.704550, 20.47727]95
     2 CapstoneLaw ets    2021 Jun    N(12, 21)     11.6  [4.0438878, 19.13793]90 [ 2.598079, 20.58374]95
     3 CapstoneLaw ets    2021 Jul    N(12, 22)     11.6  [3.9555794, 19.22624]90 [ 2.492853, 20.68897]95
     4 CapstoneLaw ets    2021 Aug    N(12, 22)     11.6  [3.8682807, 19.31354]90 [ 2.388830, 20.79299]95
     5 CapstoneLaw ets    2021 Sep    N(12, 23)     11.6  [3.7819580, 19.39986]90 [ 2.285970, 20.89585]95
     6 CapstoneLaw ets    2021 Oct    N(12, 23)     11.6  [3.6965790, 19.48524]90 [ 2.184235, 20.99758]95
     7 CapstoneLaw mean   2021 May    N(8, 21)      7.97 [0.3744124, 15.56725]90 [-1.080860, 17.02253]95
     8 CapstoneLaw mean   2021 Jun    N(8, 21)      7.97 [0.3744124, 15.56725]90 [-1.080860, 17.02253]95
     9 CapstoneLaw mean   2021 Jul    N(8, 21)      7.97 [0.3744124, 15.56725]90 [-1.080860, 17.02253]95
    10 CapstoneLaw mean   2021 Aug    N(8, 21)      7.97 [0.3744124, 15.56725]90 [-1.080860, 17.02253]95
    # ... with 110 more rows

正如我对更简单的结构化预测所做的那样,我尝试将这些预测结果写入 csv。

> write.csv(firm_hilo,dir)
Error: Can't convert <hilo> to <character>.
Run `rlang::last_error()` to see where the error occurred.

但我对如何将生成的 90/95% 置信区间强制转换为我可以导出的格式非常迷茫。有没有人遇到过这个问题?如果我应该包含更多信息,请告诉我!

标签: rtime-seriesdistributionconfidence-intervalfable-r

解决方案


推荐阅读