1

forecast function of package fable seems to have a strange effect on dates (index)

# build a simple tible
> df <- tibble(
  d = seq.Date(ymd('2018-02-12'), by =  7 , length = n ),
  x = seq_len(10))

# convert dates to yearweek objects
> df <- df %>% 
  mutate(d = yearweek(d))

# build the tsibble
> ts <- as_tsibble(df, index = d)

> ts

# A tsibble: 10 x 2 [1W]
          d     x
     <week> <int>
 1 2018 W07     1
 2 2018 W08     2
 3 2018 W09     3
 4 2018 W10     4
 5 2018 W11     5
 6 2018 W12     6
 7 2018 W13     7
 8 2018 W14     8
 9 2018 W15     9
10 2018 W16    10

Fit any model

> fm <- model(ts, ETS(x))

And forecast it

> fore <-  forecast(fm , h = 4)
> fore

# A fable: 4 x 4 [1W]
# Key:     .model [1]
  .model d              x .distribution 
  <chr>  <date>     <dbl> <dist>        
1 ETS(x) 2018-04-23  11.0 N(11, 3.7e-05)
2 ETS(x) 2018-04-30  12.0 N(12, 1.5e-04)
3 ETS(x) 2018-05-07  13.0 N(13, 3.9e-04)
4 ETS(x) 2018-05-14  14.0 N(14, 8.2e-04)

As you can see, the index variable has a different format

> class(ts$d) 
[1] "yearweek" "Date"    

> class(fore$d)
[1] "Date"

Any idea why all of this happens and how to avoid it?

Thanks in advance for any suggestions...

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
Andrea
  • 593
  • 2
  • 8
  • I can see the dates in `fore$d` are weekly, so it should be easy to have them in week format with `yearweek`, just like you did when creating the tsibble – PavoDive Jun 16 '19 at 10:59
  • I think it happens because ts$d has both classes, so the function takes the most common (Date) and that is what it returns. – PavoDive Jun 16 '19 at 11:03
  • it does give the weekly forecasts, but the internal implementation clearly drops the year week class. It’s a bug. – Earo Wang Jun 16 '19 at 12:55
  • I'm unable to reproduce this issue. Could you try installing the latest versions of fablelite and fable? – Mitchell O'Hara-Wild Jun 16 '19 at 12:56

1 Answers1

0

with an updated version of all packages the problem seems solved.

Here is my last script

R> require(dplyr)
R> require(tsibble)
R> require(lubridate)
R> require(fable)
R> 
R> # build a simple tible
R> df <- tibble(
+   d = seq.Date(ymd('2018-02-12'), by =  7 , length = 10 ),
+   x = seq_len(10))
R> 
R> # convert dates to yearweek objects
R> df <- df %>% 
+   mutate(d = yearweek(d))
R> 
R> # build the tsibble
R> ts <- as_tsibble(df, index = d)
R> 
R> ts
# A tsibble: 10 x 2 [1W]
          d     x
     <week> <int>
 1 2018 W07     1
 2 2018 W08     2
 3 2018 W09     3
 4 2018 W10     4
 5 2018 W11     5
 6 2018 W12     6
 7 2018 W13     7
 8 2018 W14     8
 9 2018 W15     9
10 2018 W16    10
R> 
R> # fit any model 
R> fm <- model(ts, ETS(x))
R> 
R> fore <-  forecast(fm , h = 4)
R> fore
# A fable: 4 x 4 [1W]
# Key:     .model [1]
  .model        d     x .distribution 
  <chr>    <week> <dbl> <dist>        
1 ETS(x) 2018 W17  11.0 N(11, 3.7e-05)
2 ETS(x) 2018 W18  12.0 N(12, 1.5e-04)
3 ETS(x) 2018 W19  13.0 N(13, 3.9e-04)
4 ETS(x) 2018 W20  14.0 N(14, 8.2e-04)
R> 
R> # check class
R> class(ts$d) 
[1] "yearweek" "Date"    
R> class(fore$d)
[1] "yearweek" "Date"    
R> 
R> 
R> # package version 
R> sapply(c('lubridate','tibble', 'tsibble', 'fable', 'fablelite'  ), packageVersion)
$lubridate
[1] 1 7 4

$tibble
[1] 2 1 3

$tsibble
[1]    0    8    2 9000

$fable
[1]    0    0    0 9100

$fablelite
[1]    0    0    0 9100
Andrea
  • 593
  • 2
  • 8