1
library(dplyr)
library(fpp2) # for prison dataset
library(hts) # forecasting function

# prepare group time series
prison.gts <- gts(prison/1e3, characters = c(3,1,9),
                  gnames = c("State", "Gender", "Legal",
                             "State*Gender", "State*Legal",
                             "Gender*Legal"))


result_obj <- tidyr::crossing(methods = c('bu', 'comb'), 
                              fmethods = c('arima'),
                              algorithms = c("lu", "cg", "chol", "recursive", "slm")) %>%
  mutate(forecast_result = purrr::map2(methods, fmethods, algorithms,
                                              ~forecast.gts(prison.gts,
                                                            method = ..1, 
                                                            fmethod = ..2, 
                                                            algorithms = ..3)))

I'm using tidyr::crossing to create the possible combination of parameters which will then become inputs to forecast.gts().

Since I've more than 2 parameters, the parameters are mapped using the ..x notation i.e ..1, ..2, ..3 https://purrr.tidyverse.org/reference/map2.html

However, it seems the result is NULL for each of the combination.

If I were to call the function individually, it gives me results.

forecast.gts(prison.gts, method="bu", fmethod="arima", algorithms = 'lu')
Afiq Johari
  • 1,372
  • 1
  • 15
  • 28

1 Answers1

1

map2 takes only 2 parameters. For more than 2 parameters use pmap :

library(dplyr)
library(fpp2) 
library(hts)

result_obj <- tidyr::crossing(
                methods = c('bu', 'comb'), 
                fmethods = c('arima'),
                algorithms = c("lu", "cg", "chol", "recursive", "slm")) %>%
   mutate(forecast_result = purrr::pmap(list(methods, fmethods, algorithms),
                                   ~forecast.gts(prison.gts,
                                                 method = ..1, 
                                                 fmethod = ..2, 
                                                 algorithms = ..3)))

However, this returns an error message that

Error: The recursive algorithm does not support a gts object.

so you might need to remove it from algorithms vector and it works fine after that.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213