3

I'm encountering an issue attempting to extract the 90/95% confidence intervals resulting from a forecast model built from a key variable holding 5 groups across a total of 4 forecasting models. The primary problem is that I'm not familiar with how R treats and works with dist and hilo object types.

The original tsibble has a structure of 60 months for each of the 5 groups (300 observations)

    >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

I form a model with different forecast methods, as well as a combination model:

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

This results in a mable of the structure

    >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>

Which I then forecast out 6 months

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

Before generating my idea of what the output tsibble should be:

>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

As I've done with more simply structured forecasts, I tried to write these forecast results to a csv.

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

But I am quite lost on how to coerce the generated 90/95% confidence intervals into a format that I can export. Has anyone encountered this issue? Please let me know if I should include any more information!

Aster
  • 31
  • 2
  • See https://fabletools.tidyverts.org/reference/unpack_hilo.html – Rob Hyndman Jul 01 '21 at 23:36
  • I've opened an issue for allowing `` objects to be converted to character for writing to csv. Note that you may lose precision if you do this, and so it is a better idea to store their numerical values. You can also extract the numerical values using `$upper`, `$lower`, and `$lelvel` as needed, or using the helper `fabletools::unpack_hilo()` as Rob suggested. – Mitchell O'Hara-Wild Jul 02 '21 at 00:19
  • It is now possible to save datasets containing CSVs to a file using `write.csv()` as you have tried above. For now you will need the development version of the {distributional} package, but the changes will be sent to CRAN shortly (likely under version 0.3.0 of the package). – Mitchell O'Hara-Wild Jul 06 '21 at 09:15
  • I am having significant trouble with this also. Would it be possible to add an example to the fabletools::unpack_hilo() documentation? The example could address the google_2015 forecast in section 5.5 of the text (i.e. Forecasting: Principles and Practice). – dkent Dec 08 '21 at 13:19

0 Answers0