简体   繁体   English

寓言:模型功能的模型列表

[英]fable: list of models to model function

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提前感谢您的帮助

One method is to use purrr::imap .一种方法是使用purrr::imap

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

This will return a list of tsibble::mable objects.这将返回一个tsibble::mable对象列表。 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::reducedplyr::left_join通过加入tsibble中的键控变量(大概是变量Country )将对象减少为单个 mable。

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.双感叹号用于取消引用 .y (否则它不会从列表中传递名称),并且在使用 tidyverse 编程时需要:=来命名变量。

For further info see What is the R assignment operator := for?有关更多信息,请参阅R 赋值运算符 := 用于什么? and ??imap in the R help pages.和 R 帮助页面中的??imap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM