4

With fable I can do:

require(fable)   
it <-  tsibbledata::global_economy %>%    filter(Country == "Italy")
fm <-  model(.data = it, ARIMA(log(GDP) ~ Population), ETS(log(GDP)))

I would like to be able to build the model list prior passing it to model(), something like:

mod_list <- list2(m1 = ARIMA(log(GDP) ~ Population), m2 = ETS(log(GDP)))
fm <- model(.data = it, mod_list)        

I could not find a way of achieving this. Any suggestions?

Something like:

map(mod_list, model, .data = it)

could be a starting point but not exactly what I would like to achieve

Thanks in advance for any help

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
Andrea
  • 593
  • 2
  • 8
  • 1
    If there were some data to work with and some `library` calls to load all needed packages then tested code might be forthcoming. – IRTFM May 20 '19 at 01:44

1 Answers1

1

One method is to use purrr::imap.

purrr::imap(mod_list, ~fabletools(it, !!.y:= .x))

This will return a list of tsibble::mable objects. You can then use purrr::reduce and dplyr::left_join to reduce the the objects into a single mable by joining on the keyed variable in your tsibble which is presumably the variable Country.

purrr::imap(mod_list, ~fabletools(it, !!.y:= .x)) %>%
    purrr::reduce(dplyr::left_join, by = "Country")

The double exclamation mark is used to unquote .y (otherwise it would not pass the name from the list) and the := is required to name the variable when programming with tidyverse.

For further info see What is the R assignment operator := for? and ??imap in the R help pages.

Luke Heley
  • 31
  • 2
  • More efficient code: it %>% fable tools::model(!!!purrr:map(.x = mod_list, .f = function(x) x)) https://community.rstudio.com/t/list-of-models-to-model-function/63191/4 – Luke Heley Jun 03 '22 at 15:54