fable::model() 将外部回归量列表传递给 ARIMA() [英] fable::model() pass lists of external regressors to ARIMA()

查看:28
本文介绍了fable::model() 将外部回归量列表传递给 ARIMA()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将具有不同外部回归量的模型列表传递给fable::model() 中的一个 ARIMA 模型.最后,我想将几​​个(最多 10 个)外部变量的所有可能组合传递给 ARIMA().

I would like to pass a list of models with different external regressors to an ARIMA model within fable::model(). Ultimately, I would like to pass every possible combination of a few (up to 10) external variables to ARIMA().

以美国家庭预算数据为例

Using the household budget data for the US as an example

library(tidyverse)
library(tsibble)
library(tsibbledata)
library(fable)
library(forecast)

aus <- hh_budget %>% 
  filter(Country == "Australia") %>% 
  select(-Country)

我想在不必明确编写模型公式的情况下执行以下操作

I would like to do the following without having to explicitly write the model formula

fit1 <- aus %>% 
  model(arima = ARIMA(Debt ~ DI))

fit2 <- aus %>% 
  model(arima = ARIMA(Debt ~ DI + Expenditure))

fit3 <- aus %>% 
  model(arima = ARIMA(Debt ~ DI + Expenditure + Savings))

我无法让模型(arima = ARIMA()) 使用不断变化的公式.

简单示例

target <- "Debt"
xregs <- paste0(names(aus)[3:4], collapse = " + ") %>% noquote()

fit2 <- aus %>% 
  model(arima = ARIMA(target ~ xregs))

映射列表示例

# Build lists of external regressor combinations

subsets_list <- function(set, subset_size) {
  combn(set, subset_size) %>%
    BBmisc::convertColsToList() %>%
    unname()
}

xregs <-
  map(.x = 1:(length(aus) - 2), .f = subsets_list, 
      set = colnames(aus[3:length(aus)])) %>% 
  unlist(recursive = F)


model_arima <- function(tsibble, target, xregs){
  model(arima = ARIMA(y = tsibble[, target], 
                      xreg = tsibble[, xregs], 
                      lambda = "auto"))
}

fit <- map(.x = xregs, 
           .f = model_arima, 
           tsibble = aus, 
           target = target)

这就是我在forecast::auto.arima() 中的做法

aus_ts <- aus %>% 
  as_tibble(.) %>% 
  select(-Year) %>% 
  ts(., start = 1995, frequency = 1)

auto_arima <- function(ts, target, xregs){
auto.arima(y = ts[, target], 
           xreg = ts[, xregs], 
           lambda = "auto")
}

fit <- map(.x = xregs, .f = auto_arima, ts = aus_ts, target = target)

推荐答案

您可以通过多种方式以编程方式创建公式.最简单的是使用 as.formula():

You can create formulas programmatically in a variety of ways. The simplest is to use as.formula():

library(tidyverse)
library(fable)
aus <- tsibbledata::hh_budget %>% 
  filter(Country == "Australia") %>% 
  select(-Country)

target <- "Debt"
xregs <- paste0(names(aus)[3:4], collapse = " + ")
as.formula(paste(target, xregs, sep ="~"))
#> Debt ~ DI + Expenditure

reprex 包 (v0.3.0) 于 2020 年 7 月 3 日创建

Created on 2020-07-03 by the reprex package (v0.3.0)

使用这种方法使用解析器,它对非标准变量名有限制.要更精确地构造公式,您可以使用 rlang::new_formula().

Using this approach uses the parser, which has limitations for non-standard variable names. For more precise construction of formulas, you can use rlang::new_formula().

要估计所需的模型集,您可以使用:

To estimate your desired set of models, you can use:

# Load libraries
library(tidyverse)
library(fable)

# Prepare data
aus <- tsibbledata::hh_budget %>% 
  filter(Country == "Australia") %>% 
  select(-Country)

# Construct formulas
xregs <- c("DI", "Expenditure", "Savings")
rhs <- map_chr(seq_along(xregs), ~ paste(xregs[seq_len(.)], collapse = " + "))
lhs <- "Debt"
formulas <- map(paste(lhs, rhs, sep = " ~ "), as.formula)

# Create model specifications
model_specs <- set_names(map(formulas, ARIMA), formulas)

# Estimate models
aus %>% 
  model(!!!model_specs)
#> # A mable: 1 x 3
#>                   `Debt ~ DI`   `Debt ~ DI + Expenditure`
#>                       <model>                     <model>
#> 1 <LM w/ ARIMA(1,1,0) errors> <LM w/ ARIMA(1,1,0) errors>
#> # … with 1 more variable: `Debt ~ DI + Expenditure + Savings` <model>

reprex 包 (v0.3.0) 于 2020 年 7 月 3 日创建

Created on 2020-07-03 by the reprex package (v0.3.0)

这篇关于fable::model() 将外部回归量列表传递给 ARIMA()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆