我想将具有不同外部回归器的模型列表传递给 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)