【问题标题】:Error while fitting many models using List-Columns and Purrr使用 List-Columns 和 Purrr 拟合许多模型时出错
【发布时间】:2017-07-05 11:15:59
【问题描述】:

我正在尝试执行来自:http://unconj.ca/blog/forecasting-yyz-passengers-in-the-tidyverse.html的代码

在中间的某个地方.....

首先,我们创建模型及其参数的数据集。以下将生成所有可能的 ETS 模型的数据框

ets.params <- crossing(error = c("A", "M"), trend = c("N", "A", "M"),seasonal = c("N", "A", "M"), damped = c(TRUE, FALSE)) %>%
    # Drop combinations with a damped non-trend.
    mutate(drop = ifelse(trend == "N" & damped, TRUE, FALSE)) %>% 
    # Create labels for the models out of these parameters.
    mutate(kind = "ETS", desc = paste0("(", error, ",", trend,ifelse(damped, "d", ""),",", seasonal, ")"),model = paste0(error, trend, seasonal)) 
    # Drop nonsensical models (these are flagged by `ets` anyway).
ets.params1<-subset(ets.params,!(ets.params$model %in% c("MMA","AMN","AMA","AMM","ANM","AAM")))

ets.params1

# A tibble: 24 x 8
   error trend seasonal damped  drop  kind     desc model
   <chr> <chr>    <chr>  <lgl> <lgl> <chr>    <chr> <chr>
 1     A     A        A  FALSE FALSE   ETS  (A,A,A)   AAA
 2     A     A        A   TRUE FALSE   ETS (A,Ad,A)   AAA
 3     A     A        N  FALSE FALSE   ETS  (A,A,N)   AAN
 4     A     A        N   TRUE FALSE   ETS (A,Ad,N)   AAN
 5     A     N        A  FALSE FALSE   ETS  (A,N,A)   ANA
 6     A     N        A   TRUE  TRUE   ETS (A,Nd,A)   ANA
 7     A     N        N  FALSE FALSE   ETS  (A,N,N)   ANN
 8     A     N        N   TRUE  TRUE   ETS (A,Nd,N)   ANN
 9     M     A        A  FALSE FALSE   ETS  (M,A,A)   MAA
10     M     A        A   TRUE FALSE   ETS (M,Ad,A)   MAA
# ... with 14 more rows

有了这组模型参数,我们可以创建列表列,其中包含训练数据和计算模型的函数(在本例中为 ets)。

我可以执行上面的代码,但是下面的代码会报错...

ets.models <- ets.params %>%
    # Add in the training set and the modelling function.
    mutate(fn = replicate(forecast::ets, n = n()),
           train = replicate(list(train), n = n())) %>%
    # Create a "param" column to pass to `fn`.
    mutate(params = purrr::transpose(list(
        "y" = train, "model" = model, "damped" = damped
    ))) %>%
    select(kind, desc, train, fn, params)

ets.models

我收到错误: 错误:不应直接调用此函数。错误似乎来自 mutate()..

需要帮助!!!!

【问题讨论】:

    标签: r time-series tidyverse purrr


    【解决方案1】:

    看来您遇到了一个有趣的问题。如果您能提供一个可重现的示例会很好,但这是我获取源数据并将其处理到ets 模型列表中的看法

    library(tidyverse)
    library(readxl)
    
    if (!file.exists("bulletin_opendata.xlsx"))
      download.file("http://opendata.toronto.ca/it/com/bulletin_opendata.xlsx", 
                    "bulletin_opendata.xlsx")
    
    y <- read_excel("bulletin_opendata.xlsx", sheet = "Trans", 
                  col_names = FALSE, skip=256) %>% # read xlsx file
       mutate(X7=X7*1e3) %>% pull(X7) %>% ts(f=12) # extract column as ts
    
    library(forecast)
    
    ets.params <- crossing(error = c("A", "M"), trend = c("N", "A", "M"),
                           seasonal = c("N", "A", "M"), damped = c(TRUE, FALSE)) %>%
      # Drop combinations with a damped non-trend.
      mutate(drop = ifelse(trend == "N" & damped, TRUE, FALSE)) %>% 
      # Create labels for the models out of these parameters.
      mutate(kind = "ETS", 
             desc = paste0("(", error, ",", trend, ifelse(damped, "d", ""),",", seasonal, ")"), 
             model = paste0(error, trend, seasonal)) 
    # Drop nonsensical models (these are flagged by `ets` anyway).
    ets.params1<-subset(ets.params,!(ets.params$model %in% c("MMA","AMN","AMA","AMM","ANM","AAM")))
    
    illegal.models<-c("(A,Nd,A)", "(A,Nd,N)", "(M,Nd,A)", "(M,Nd,M)", "(M,Nd,N)")
    
    ets.params1 %>% mutate(y=list(y)) %>%     # add train data
      filter(!desc %in% illegal.models) %>%   # some of the model combinations are not possible with ETS
      select(y, model, damped) %>%            # prepare a list to be mapped to ets
      mutate(mod = pmap(.,ets),               # map a model
             prog = map(mod, forecast, h=12)) # and forecast 1 year ahead.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2016-11-07
      • 1970-01-01
      相关资源
      最近更新 更多