【问题标题】:Forecasting in R(fable package): accuracy function in fable cannot find the y variableR(寓言包)中的预测:寓言中的准确度函数找不到 y 变量
【发布时间】:2020-06-28 13:20:54
【问题描述】:

我正在尝试从 fable 包中精确功能。它有时会出现这样的意外错误

"Error: Could not find response variable(s) in the fable: Trips"

(这是来自https://otexts.com/fpp3/toolbox-exercises.html 的示例 12)

有人遇到过这个问题吗? 这是我正在使用的代码:

# reprex is one of the (many) packages installed when you install tidyverse
#install.packages("tidyverse")
#install.packages("shiny")
#install.packages("htmltools")
#library(shiny)
#library(miniUI)

# install reprex by itself
library(reprex)
library(fpp3)
#> ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────── fpp3 0.3 ──
#> ✓ tibble      3.0.1     ✓ tsibble     0.9.0
#> ✓ dplyr       1.0.0     ✓ tsibbledata 0.2.0
#> ✓ tidyr       1.1.0     ✓ feasts      0.1.3
#> ✓ lubridate   1.7.9     ✓ fable       0.2.0
#> ✓ ggplot2     3.3.1
#> ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────── fpp3_conflicts ──
#> x lubridate::date()   masks base::date()
#> x dplyr::filter()     masks stats::filter()
#> x tsibble::interval() masks lubridate::interval()
#> x dplyr::lag()        masks stats::lag()
gc_tourism <- tourism %>% filter(Region=='Gold Coast') %>%
  group_by(Purpose) %>%
  summarise(Trips=sum(Trips))
gc_train_1 <- gc_tourism %>% 
  group_by(Purpose) %>%
  slice(1:(n()-4))
fit1 <- gc_train_1 %>%
  model(SNAIVE=SNAIVE(Trips))
gc_fc_1 <- fit1 %>% forecast(h=4)
gc_fc_1 %>% accuracy(gc_tourism)
#> Error: Could not find response variable(s) in the fable: Trips

reprex package (v0.3.0) 于 2020 年 6 月 28 日创建

【问题讨论】:

    标签: r fable-r


    【解决方案1】:

    在将 tsibble 和 fable 软件包升级到最新版本(分别为 0.9.1 和 0.2.1)后,我能够解决此问题。

    rm(list=ls())
    library(fpp3)
    #> ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────── fpp3 0.3 ──
    #> ✓ tibble      3.0.1     ✓ tsibble     0.9.1
    #> ✓ dplyr       1.0.0     ✓ tsibbledata 0.2.0
    #> ✓ tidyr       1.1.0     ✓ feasts      0.1.4
    #> ✓ lubridate   1.7.9     ✓ fable       0.2.1
    #> ✓ ggplot2     3.3.2
    #> ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────── fpp3_conflicts ──
    #> x lubridate::date()   masks base::date()
    #> x dplyr::filter()     masks stats::filter()
    #> x tsibble::interval() masks lubridate::interval()
    #> x dplyr::lag()        masks stats::lag()
    library(reprex)
    
    gc_tourism <- tourism %>% dplyr::filter(Region=='Gold Coast') %>%
      dplyr::group_by(Purpose) %>%
      dplyr::summarise(Trips=sum(Trips))
    gc_train_1 <- gc_tourism %>% 
      dplyr::group_by(Purpose) %>%
      dplyr::slice(1:(n()-4))
    fit1 <- gc_train_1 %>%
      model(SNAIVE=SNAIVE(Trips))
    gc_fc_1 <- fit1 %>% forecast(h=4)
    gc_fc_1 %>% accuracy(gc_tourism)
    #> # A tibble: 4 x 10
    #>   .model Purpose  .type    ME  RMSE   MAE   MPE  MAPE  MASE    ACF1
    #>   <chr>  <chr>    <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>
    #> 1 SNAIVE Business Test  17.4  41.3  38.6   9.63  35.4 1.81  -0.276 
    #> 2 SNAIVE Holiday  Test  38.9  73.9  70.7   6.42  13.0 1.42  -0.213 
    #> 3 SNAIVE Other    Test  -1.50  6.27  5.61 -9.01  17.3 0.493 -0.0655
    #> 4 SNAIVE Visiting Test  20.4  64.2  59.0   5.41  17.8 1.39  -0.574
    
    
    gc_tourism <- tourism %>% dplyr::filter(Region=='Gold Coast') %>%
      group_by(Purpose) %>%
      summarise(Trips=sum(Trips))
    
    gc_train_1 <- gc_tourism %>% 
      group_by(Purpose) %>%
      slice(1:(n()-4))
    fit1 <- gc_train_1 %>%
      model(SNAIVE=SNAIVE(Trips))
    gc_fc_1 <- fit1 %>% forecast(h=4)
    gc_fc_1 %>% accuracy(gc_tourism)
    #> # A tibble: 4 x 10
    #>   .model Purpose  .type    ME  RMSE   MAE   MPE  MAPE  MASE    ACF1
    #>   <chr>  <chr>    <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>
    #> 1 SNAIVE Business Test  17.4  41.3  38.6   9.63  35.4 1.81  -0.276 
    #> 2 SNAIVE Holiday  Test  38.9  73.9  70.7   6.42  13.0 1.42  -0.213 
    #> 3 SNAIVE Other    Test  -1.50  6.27  5.61 -9.01  17.3 0.493 -0.0655
    #> 4 SNAIVE Visiting Test  20.4  64.2  59.0   5.41  17.8 1.39  -0.574
    

    reprex package (v0.3.0) 于 2020-07-01 创建

    【讨论】:

      【解决方案2】:

      当您致电library(fpp3) 时,您会收到一条警告(冲突),说明与dpylr::filter 存在冲突。这意味着您必须使用dyplr::filter 调用filter,如此reprex 所示。

      library(fpp3)
      #> ── Attaching packages ─────────────── fpp3 0.3 ──
      #> ✓ tibble      3.0.1     ✓ tsibble     0.9.1
      #> ✓ dplyr       1.0.0     ✓ tsibbledata 0.2.0
      #> ✓ tidyr       1.1.0     ✓ feasts      0.1.4
      #> ✓ lubridate   1.7.4     ✓ fable       0.2.1
      #> ✓ ggplot2     3.3.2
      #> ── Conflicts ────────────────── fpp3_conflicts ──
      #> x lubridate::date()       masks base::date()
      #> x dplyr::filter()         masks stats::filter()
      #> x tsibble::interval()     masks lubridate::interval()
      #> x dplyr::lag()            masks stats::lag()
      #> x tsibble::new_interval() masks lubridate::new_interval()
      
      gc_tourism <- tourism %>% dplyr::filter(Region=='Gold Coast') %>%
        group_by(Purpose) %>%
        summarise(Trips=sum(Trips))
      
      gc_train_1 <- gc_tourism %>% 
        group_by(Purpose) %>%
        slice(1:(n()-4))
      fit1 <- gc_train_1 %>%
        model(SNAIVE=SNAIVE(Trips))
      gc_fc_1 <- fit1 %>% forecast(h=4)
      gc_fc_1 %>% accuracy(gc_tourism)
      #> # A tibble: 4 x 10
      #>   .model Purpose  .type    ME  RMSE   MAE   MPE  MAPE  MASE    ACF1
      #>   <chr>  <chr>    <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>
      #> 1 SNAIVE Business Test  17.4  41.3  38.6   9.63  35.4 1.81  -0.276 
      #> 2 SNAIVE Holiday  Test  38.9  73.9  70.7   6.42  13.0 1.42  -0.213 
      #> 3 SNAIVE Other    Test  -1.50  6.27  5.61 -9.01  17.3 0.493 -0.0655
      #> 4 SNAIVE Visiting Test  20.4  64.2  59.0   5.41  17.8 1.39  -0.574
      

      【讨论】:

      • 谢谢你 MarBlo。我使用了你的代码,它仍然给我同样的错误。
      • 进一步,dplyr::lag() 掩盖了 stats::filter(dplyr 是 fpp3 的一部分)。所以,我不确定 dplyr 是否是问题所在。
      • 这很奇怪。我再次使用了reprex,它起作用了。我做了rm(list=ls()) 并在没有dplyr::filter 的情况下再次使用了代码并得到了错误。 fpp3的包版本只有细微差别。
      • 感谢您的帮助 MarBlo。我升级了 fable 和 tsibble 包。之后它就可以工作了。
      • @Lokesh 如果您投了反对票,请告诉我原因。我指出了您的问题的可能原因以及在我的情况下没有显示错误消息的包版本不同的事实。
      猜你喜欢
      • 2020-07-19
      • 2022-01-15
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多