我怎样才能用完一个函数的所有参数组合并将它们的结果集中在一起table?

How can I exhaust all parameter combinations of a function and gather their results in one tidy table?

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"))

# run forecasts
forecast.gts(prison.gts, method="bu", fmethod="arima")
forecast.gts(prison.gts, method="comb", fmethod="arima")
forecast.gts(prison.gts, method="bu", fmethod="ets")
forecast.gts(prison.gts, method="comb", fmethod="ets")
forecast.gts(prison.gts, method="bu", fmethod="rw")
forecast.gts(prison.gts, method="comb", fmethod="rw")

如您所见,我正在尝试这 2 个参数 'method' 和 'fmethod' 可用的所有不同值来比较预测结果。

有没有更简单的方法可以以 tidyverse 格式执行此操作? 基本上我想创建一个所有参数组合的列表,将它们的结果收集在一个 table.

使用 tidyverse 方法,您可以使用 crossing 来创建 methodsfmethod 的所有组合。我们可以使用 map2forecast.gts 函数应用于每个组合。

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

result_obj <- tidyr::crossing(methods = c('bu', 'comb'), 
                              fmethod = c('arima', 'ets', 'rw')) %>%
                  mutate(forecast_result = purrr::map2(methods, fmethod, 
                      ~forecast.gts(prison.gts,method = .x, fmethod = .y)))