首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跨越两个以上的参数(..1,..2,..3)

跨越两个以上的参数(..1,..2,..3)
EN

Stack Overflow用户
提问于 2020-04-15 08:30:13
回答 1查看 51关注 0票数 0
代码语言:javascript
复制
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)))

我使用tidyr::inter越来创建参数的可能组合,然后这些参数将成为forecast.gts()的输入。

由于我有两个以上的参数,所以参数使用..x表示法进行映射,即..1、..2、..3 https://purrr.tidyverse.org/reference/map2.html

但是,似乎每个组合的结果都是空的。

如果我单独调用这个函数,它会给出结果。

代码语言:javascript
复制
forecast.gts(prison.gts, method="bu", fmethod="arima", algorithms = 'lu')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-15 08:38:23

map2只接受两个参数。对于超过2个参数,请使用pmap

代码语言:javascript
复制
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)))

但是,这将返回以下错误消息:

错误:递归算法不支持gts对象。

因此,您可能需要从algorithms向量中删除它,然后它就可以正常工作了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61224456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档