1

包寓言的预测功能似乎对日期(索引)有奇怪的影响

# 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

适合任何型号

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

并预测它

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

如您所见,索引变量具有不同的格式

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

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

知道为什么会发生所有这些以及如何避免它吗?

在此先感谢您的任何建议...

4

1 回答 1

0

随着所有软件包的更新版本,问题似乎解决了。

这是我的最后一个脚本

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
于 2019-06-18T10:52:04.657 回答