rfable-rfable

Forecasting using fable, how to use specials?


I'm trying to use specials when forecasting using the fable package but I can't figure out the syntax of how to use them and I haven't been able to find an example that I can use as a guide.

I've put a simple example below of trying to use a window with the MEAN forecasting model but it is giving me an error.

Any help is much appreciated!

# Load libraries
library(fable)
#> Warning: package 'fable' was built under R version 3.6.3
#> Loading required package: fabletools
#> Warning: package 'fabletools' was built under R version 3.6.3
library(tsibble)
#> Warning: package 'tsibble' was built under R version 3.6.3
library(tsibbledata)
#> Warning: package 'tsibbledata' was built under R version 3.6.3
library(lubridate)
#> Warning: package 'lubridate' was built under R version 3.6.3
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:tsibble':
#> 
#>     interval
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(dplyr)
#> Warning: package 'dplyr' was built under R version 3.6.3
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:lubridate':
#> 
#>     intersect, setdiff, union
#> The following object is masked from 'package:tsibble':
#> 
#>     id
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# Run example
aus_retail %>%
  filter(
    State %in% c("New South Wales", "Victoria"),
    Industry == "Department stores"
  ) %>% 
  model(
    snaive = SNAIVE(Turnover),
    mean_f = MEAN(Turnover, window(size = 12))
  )
#> Error in hasTsp(x): argument "x" is missing, with no default

Created on 2020-04-23 by the reprex package (v0.3.0)

sessionInfo()
#> R version 3.6.2 (2019-12-12)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows Server x64 (build 17763)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_Ireland.1252  LC_CTYPE=English_Ireland.1252   
#> [3] LC_MONETARY=English_Ireland.1252 LC_NUMERIC=C                    
#> [5] LC_TIME=English_Ireland.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] dplyr_0.8.5       lubridate_1.7.8   tsibbledata_0.1.0 tsibble_0.8.6    
#> [5] fable_0.1.2       fabletools_0.1.3 
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.3       compiler_3.6.2   pillar_1.4.3     highr_0.8       
#>  [5] tools_3.6.2      digest_0.6.23    evaluate_0.14    lifecycle_0.2.0 
#>  [9] tibble_3.0.1     gtable_0.3.0     anytime_0.3.7    pkgconfig_2.0.3 
#> [13] rlang_0.4.5      yaml_2.2.0       xfun_0.12        stringr_1.4.0   
#> [17] knitr_1.27       generics_0.0.2   vctrs_0.2.4      grid_3.6.2      
#> [21] tidyselect_1.0.0 glue_1.4.0       R6_2.4.1         rmarkdown_2.1   
#> [25] purrr_0.3.4      ggplot2_3.3.0    tidyr_1.0.2      magrittr_1.5    
#> [29] scales_1.1.0     ellipsis_0.3.0   htmltools_0.4.0  assertthat_0.2.1
#> [33] colorspace_1.4-1 stringi_1.4.5    munsell_0.5.0    crayon_1.3.4

Solution

  • The fable package uses a formula style interface for model specification, it is very similar to the lm() function. The specials are included in the right hand side of the model formula, and if multiple specials are supported they can be included additively. Instead of MEAN(Turnover, window(size = 12)), it should be MEAN(Turnover ~ window(size = 12). For ARIMA() models which can have many specials, you might use ARIMA(Turnover ~ pdq(0,1,4) + PDQ(3,1,0)).

    # Load libraries
    library(fable)
    library(tsibble)
    library(tsibbledata)
    library(lubridate)
    library(dplyr)
    
    # Run example
    aus_retail %>%
      filter(
        State %in% c("New South Wales", "Victoria"),
        Industry == "Department stores"
      ) %>% 
      model(
        snaive = SNAIVE(Turnover),
        mean_f = MEAN(Turnover ~ window(size = 12))
      )
    #> # A mable: 2 x 4
    #> # Key:     State, Industry [2]
    #>   State           Industry          snaive   mean_f 
    #>   <chr>           <chr>             <model>  <model>
    #> 1 New South Wales Department stores <SNAIVE> <MEAN> 
    #> 2 Victoria        Department stores <SNAIVE> <MEAN>
    

    Created on 2020-05-08 by the reprex package (v0.3.0)