MASE returns `NAN` with accuracy function

I am trying to reproduce the following example from fpp3 book but the returned value for MASE is 'NAN'! I checked and could not find where the issue!

Library("fpp3")
google_stock <- gafa_stock %>%
  filter(Symbol == "GOOG") %>%
  mutate(day = row_number()) %>%
  update_tsibble(index = day, regular = TRUE)
google_2015 <- google_stock %>% filter(year(Date) == 2015)
google_jan_2016 <- google_stock %>%
  filter(yearmonth(Date) == yearmonth("2016 Jan"))
google_fit <- google_2015 %>%
  model(
    Mean = MEAN(Close),
    `NaΓ―ve` = NAIVE(Close),
    Drift = RW(Close ~ drift())
  )
google_fc <- google_fit %>%
  forecast(google_jan_2016)
accuracy(google_fc, google_jan_2016)

MASE requires the training data in order to compute the scaling factor in the denominator. If you don't pass that data to accuracy(), then it cannot compute the MASE. In your code, you pass the test data only. So just replace that with all the data (training and test data) and the accuracy() function will be able to do all the required calculations.

library("fpp3")
#> ── Attaching packages ───────────────────────────────────────────────────────────────── fpp3 0.1 ──
#> βœ“ tibble      2.1.3          βœ“ tsibble     0.8.6     
#> βœ“ dplyr       0.8.4          βœ“ tsibbledata 0.1.0.9000
#> βœ“ tidyr       1.0.2          βœ“ feasts      0.1.2.9000
#> βœ“ lubridate   1.7.4          βœ“ fable       0.1.2.9000
#> βœ“ ggplot2     3.3.0.9000
#> ── Conflicts ──────────────────────────────────────────────────────────────────── fpp3_conflicts ──
#> x lubridate::date()       masks base::date()
#> x dplyr::filter()         masks stats::filter()
#> x tsibble::id()           masks dplyr::id()
#> x tsibble::interval()     masks lubridate::interval()
#> x dplyr::lag()            masks stats::lag()
#> x tsibble::new_interval() masks lubridate::new_interval()
google_stock <- gafa_stock %>%
  filter(Symbol == "GOOG") %>%
  mutate(day = row_number()) %>%
  update_tsibble(index = day, regular = TRUE)
google_2015 <- google_stock %>% filter(year(Date) == 2015)
google_jan_2016 <- google_stock %>%
  filter(yearmonth(Date) == yearmonth("2016 Jan"))
google_fit <- google_2015 %>%
  model(
    Mean = MEAN(Close),
    `NaΓ―ve` = NAIVE(Close),
    Drift = RW(Close ~ drift())
  )
google_fc <- google_fit %>%
  forecast(google_jan_2016)
accuracy(google_fc, google_stock)
#> # A tibble: 3 x 10
#>   .model Symbol .type    ME  RMSE   MAE   MPE  MAPE  MASE  ACF1
#>   <chr>  <chr>  <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Drift  GOOG   Test  -49.8  53.1  49.8 -6.99  6.99  7.84 0.604
#> 2 Mean   GOOG   Test  117.  118.  117.  16.2  16.2  18.4  0.496
#> 3 NaΓ―ve  GOOG   Test  -40.4  43.4  40.4 -5.67  5.67  6.36 0.496

Created on 2020-02-09 by the reprex package (v0.3.0)

1 Like

Thank you Rob, I though fable contains training data. So, this should be changed in fpp3 right after Figure 5.12.
Instead of

accuracy(google_fc, google_jan_2016)

we should have

accuracy(google_fc, google_stock) 

Thanks. Now fixed online.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.