簡體   English   中英

`fabletools::autoplot()` 中的錯誤:! 提供的數據包含與預測不同的關鍵結構

[英]Error in `fabletools::autoplot()`: ! Provided data contains a different key structure to the forecasts

我剛剛開始研究交叉驗證預測。 我似乎無法正確使用 autoplot() 。 我試圖只預測過去 4 年的數據,並在整個數據集中預測 plot。 當我不指定 autoplot() 時,它是 plot 下圖。 我試過 filter_index(. ~ "2014"), filter(Country=="United States") 嵌套在 autoplot() 中,但沒有用,所以我把它從下面的代碼中去掉了。 我正在使用 tsibble 庫中的 global_economy 數據集嘗試預測美國 GDP。 如果我將 autoplot() 留空,它會繪制整個時間段的預測。

econ <- global_economy
model <- econ %>%
  filter(Country=="United States") %>%
  stretch_tsibble(.init=15, .step=1) %>%
  model(NAIVE(GDP),NNETAR(GDP)) %>%
  fabletools::forecast(h="1 year")


model  %>%
  group_by(.id,.model) %>%
  mutate(h = row_number()) %>%
  ungroup() %>%
  as_fable(response = "GDP", distribution = GDP) %>%
  filter(h==1) %>%
  as_fable(key=c(.model)) %>%
fabletools::autoplot(econ)

在此處輸入圖像描述

econ數據集將Country作為關鍵變量, <fable>中需要它來匹配適當的繪圖系列。 此外,如果您只想通過交叉驗證預測過去 4 年,您可以將交叉驗證拉伸折疊的.init增加到 54。

library(fpp3)
#> ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
#> ✔ tibble      3.1.8          ✔ tsibble     1.1.3     
#> ✔ dplyr       1.0.10         ✔ tsibbledata 0.4.1     
#> ✔ tidyr       1.2.1          ✔ feasts      0.3.0     
#> ✔ lubridate   1.9.0          ✔ fable       0.3.2.9000
#> ✔ ggplot2     3.4.0
#> ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
#> ✖ lubridate::date()    masks base::date()
#> ✖ dplyr::filter()      masks stats::filter()
#> ✖ tsibble::intersect() masks base::intersect()
#> ✖ tsibble::interval()  masks lubridate::interval()
#> ✖ dplyr::lag()         masks stats::lag()
#> ✖ tsibble::setdiff()   masks base::setdiff()
#> ✖ tsibble::union()     masks base::union()
econ <- global_economy
model <- econ %>%
  filter(Country=="United States") %>%
  stretch_tsibble(.init=54, .step=1) %>%
  model(NAIVE(GDP),NNETAR(GDP)) %>%
  fabletools::forecast(h="1 year")

model  %>%
  group_by(.id,.model) %>%
  mutate(h = row_number()) %>%
  filter(h==1) %>%
  ungroup() %>% 
  update_tsibble(key=c(.model, Country)) %>% 
  as_fable(response = "GDP", distribution = GDP) %>%
  fabletools::autoplot(econ)

創建於 2023-01-11,使用reprex v2.0.2

暫無
暫無

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

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