0

I am trying to accuracy function from fable package. It sometimes gives unexpected errors like this

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

(This is Example 12 from https://otexts.com/fpp3/toolbox-exercises.html)

Has anyone come across this issue? Here's the code that I am using:

# 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

Created on 2020-06-28 by the reprex package (v0.3.0)

MarBlo
  • 4,195
  • 1
  • 13
  • 27
Lokesh
  • 21
  • 3

2 Answers2

2

I was able to resolve this after upgrading tsibble and fable packages to their latest versions(0.9.1 and 0.2.1 respectively).

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

Created on 2020-07-01 by the reprex package (v0.3.0)

Lokesh
  • 21
  • 3
-1

when you call library(fpp3) you get a warning (Conflicts) that says, there is a conflict with dpylr::filter. That means you have to call filter with dyplr::filter as shown in this 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
  • 4,195
  • 1
  • 13
  • 27
  • Thank you MarBlo. I used your code and it still gives me the same error. – Lokesh Jun 30 '20 at 03:59
  • Further, dplyr::lag() masks stats::filter(dplyr is a part of fpp3). So, I am not sure if dplyr is the problem. – Lokesh Jun 30 '20 at 04:54
  • That is strange. I used the reprex again, and it worked. I did `rm(list=ls())` and used the code again without `dplyr::filter` and got the error. There is only a slight difference in the package version of fpp3. – MarBlo Jun 30 '20 at 04:55
  • Thanks for your help MarBlo. I upgraded fable and tsibble packages.It works after that. – Lokesh Jul 01 '20 at 09:16
  • @Lokesh If you did the downvote, please let me know why. I pointed out to possible reasons for your problem and the fact that in my case which does not show the error message the package versions were different. – MarBlo Jul 03 '20 at 03:45
  • I did not downvote. In fact, I am thankful to you as you helped me in reviewing & editing my first question as well as spent time on answering the question. – Lokesh Jul 04 '20 at 05:22
  • @Lokesh Thank you. I am glad I could help and the answer was useful – MarBlo Jul 04 '20 at 05:34