在 R 中使用外部变量生成滚动预测

Generating rolling forecasts with external variables in R

我正在尝试使用 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 package (v0.3.0)

于 2020-08-07 创建

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

我只需要用 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))
  )