1

我正在尝试使用 R 包fabletsibble. 如果我不在模型中包含外部变量,我可以成功地做到这一点。可以在此处找到示例:https ://otexts.com/fpp3/simple-methods.html 。


library(tsibbledata)
library(tsibble)
library(fpp3)
#> -- Attaching packages --------------------------------------------------------------- fpp3 0.3 --
#> v tibble    3.0.3     v ggplot2   3.3.2
#> v dplyr     1.0.1     v feasts    0.1.4
#> v tidyr     1.1.1     v fable     0.2.1
#> v lubridate 1.7.9
#> -- Conflicts ------------------------------------------------------------------ fpp3_conflicts --
#> x lubridate::date()     masks base::date()
#> x dplyr::filter()       masks stats::filter()
#> x lubridate::interval() masks tsibble::interval()
#> x dplyr::lag()          masks stats::lag()

data(gafa_stock)

google_stock <- gafa_stock %>%
  filter(Symbol == "GOOG") %>%
  mutate(day = row_number()) %>%
  update_tsibble(index = day, regular = TRUE)

# Without external variables
google_2015 <- google_stock %>% filter(year(Date) == 2015) 

google_2015_tr <- google_2015 %>%
  slice(1:(n()-1)) %>%
  stretch_tsibble(.init = 3, .step = 1)

google_2015_tr %>%
  model(Mean = MEAN(Close)) %>%
  forecast(h=1) %>% 
  head()
#> # A fable: 6 x 6 [1]
#> # Key:     .id, Symbol, .model [6]
#>     .id Symbol .model   day       Close .mean
#>   <int> <chr>  <chr>  <dbl>      <dist> <dbl>
#> 1     1 GOOG   Mean     256 N(511, 172)  511.
#> 2     2 GOOG   Mean     257 N(508, 156)  508.
#> 3     3 GOOG   Mean     258 N(506, 126)  506.
#> 4     4 GOOG   Mean     259 N(504, 129)  504.
#> 5     5 GOOG   Mean     260 N(502, 138)  502.
#> 6     6 GOOG   Mean     261 N(501, 127)  501.

# Using external variables
google_2015_tr %>%
  model(TSLM(Close ~ Volume)) %>% 
  forecast(
    new_data = google_2015_tr %>% 
      group_by(.id) %>% 
      slice(n()) %>%
      ungroup() %>% 
      mutate(.id = .id - 1) 
  ) %>%
  head()
#> Error: Provided data contains a different key structure to the models.

google_2015_tr %>%
  model(TSLM(Close ~ Volume)) %>% 
  key_vars()
#> [1] ".id"    "Symbol"

google_2015_tr %>% 
  group_by(.id) %>% 
  slice(n()) %>%
  ungroup() %>% 
  mutate(.id = .id - 1) %>% 
  key_vars()
#> [1] "Symbol" ".id"

reprex 包(v0.3.0)于 2020-08-07 创建

该错误表明密钥结构不同,但密钥变量顺序只是颠倒了。如何以这种方式生成滚动预测?

4

1 回答 1

0

我只需要用以下命令反转密钥结构update_tsibble()

google_2015_tr %>%
  model(TSLM(Close ~ Volume)) %>% 
  forecast(
    new_data = google_2015_tr %>% 
      group_by(.id) %>% 
      slice(n()) %>%
      ungroup() %>% 
      mutate(.id = .id - 1) %>% 
      update_tsibble(key = c(.id, Symbol))
  ) 
于 2020-08-07T19:11:08.920 回答