0

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

以美国的家庭预算数据为例

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

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

我想执行以下操作,而不必明确编写模型公式

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

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

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

我无法让 model(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)
4

1 回答 1

0

您可以通过多种方式以编程方式创建公式。最简单的是使用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-07-03 创建

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

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

# 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-07-03 创建

于 2020-07-03T03:53:12.657 回答