簡體   English   中英

寓言中的索引類::預測

[英]Class of index in fable::forecast

包裝寓言的預測功能似乎對日期有一個奇怪的影響(索引)

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

知道為什么會發生這種情況以及如何避免它嗎?

在此先感謝您的任何建議...

使用所有包的更新版本,問題似乎已解決。

這是我的最后一個腳本

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM