我正在尝试以编程方式生成用于测试 ARIMA 模型中各种滞后回归量的公式,使用fable
. 本质上,我希望从 fpp3 进行滞后预测器分析,但公式是由 R 基于指定数量的 xreg 滞后生成的。我尝试按照https://stackoverflow.com/a/62708245/11071807中的说明进行操作,但出现错误:
library(magrittr)
cast_lags_formula <- function(x, lags, names = TRUE) {
if (names) names(lags) <- paste0("L", lags)
lapply(lags, function(l, x) {
paste0("lag(",x,", ",l,")", collapse = "+")
}, x = x)
}
lags <- 0:3
formula_lags <- cast_lags_formula("Income", lags)
arima_rhs <- purrr::accumulate(formula_lags, paste, sep = " + ")
arima_formulas <- lapply(arima_rhs,
function(x) as.formula(paste("Consumption", x, sep = " ~ ")))
model_formulas <- purrr::set_names(purrr::map(arima_formulas, fable::ARIMA), paste0("ARIMAL", lags))
model_formulas
#> $ARIMAL0
#> <ARIMA model definition>
#>
#> $ARIMAL1
#> <ARIMA model definition>
#>
#> $ARIMAL2
#> <ARIMA model definition>
#>
#> $ARIMAL3
#> <ARIMA model definition>
print(model_formulas$ARIMAL0$formula)
#> <quosure>
#> expr: ^<formula>
#> env: empty
print(model_formulas$ARIMAL1$formula)
#> <quosure>
#> expr: ^<formula>
#> env: empty
print(model_formulas$ARIMAL2$formula)
#> <quosure>
#> expr: ^<formula>
#> env: empty
print(model_formulas$ARIMAL3$formula)
#> <quosure>
#> expr: ^<formula>
#> env: empty
train_df <- fpp3::us_change
train_stretched <- train_df %>%
tsibble::stretch_tsibble(.init = 190)
train_stretched
#> # A tsibble: 1,746 x 7 [1Q]
#> # Key: .id [9]
#> Quarter Consumption Income Production Savings Unemployment .id
#> <qtr> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
#> 1 1970 Q1 0.619 1.04 -2.45 5.30 0.9 1
#> 2 1970 Q2 0.452 1.23 -0.551 7.79 0.5 1
#> 3 1970 Q3 0.873 1.59 -0.359 7.40 0.5 1
#> 4 1970 Q4 -0.272 -0.240 -2.19 1.17 0.700 1
#> 5 1971 Q1 1.90 1.98 1.91 3.54 -0.100 1
#> 6 1971 Q2 0.915 1.45 0.902 5.87 -0.100 1
#> 7 1971 Q3 0.794 0.521 0.308 -0.406 0.100 1
#> 8 1971 Q4 1.65 1.16 2.29 -1.49 0 1
#> 9 1972 Q1 1.31 0.457 4.15 -4.29 -0.200 1
#> 10 1972 Q2 1.89 1.03 1.89 -4.69 -0.100 1
#> # … with 1,736 more rows
fits <- train_stretched %>%
fabletools::model(!!!model_formulas)
fits
#> # A mable: 9 x 5
#> # Key: .id [9]
#> .id ARIMAL0 ARIMAL1
#> <int> <model> <model>
#> 1 1 <LM w/ ARIMA(1,0,2) errors> <LM w/ ARIMA(3,0,0)(2,0,0)[4] errors>
#> 2 2 <LM w/ ARIMA(1,0,2) errors> <LM w/ ARIMA(3,0,0)(2,0,0)[4] errors>
#> 3 3 <LM w/ ARIMA(1,0,2) errors> <LM w/ ARIMA(3,0,0)(2,0,0)[4] errors>
#> 4 4 <LM w/ ARIMA(1,0,2) errors> <LM w/ ARIMA(3,0,0)(2,0,0)[4] errors>
#> 5 5 <LM w/ ARIMA(1,0,2) errors> <LM w/ ARIMA(3,0,0)(2,0,0)[4] errors>
#> 6 6 <LM w/ ARIMA(1,0,2) errors> <LM w/ ARIMA(3,0,0)(2,0,0)[4] errors>
#> 7 7 <LM w/ ARIMA(1,0,2) errors> <LM w/ ARIMA(3,0,0)(2,0,0)[4] errors>
#> 8 8 <LM w/ ARIMA(1,0,2) errors> <LM w/ ARIMA(3,0,0)(2,0,0)[4] errors>
#> 9 9 <LM w/ ARIMA(1,0,2) errors> <LM w/ ARIMA(3,0,0)(2,0,0)[4] errors>
#> # … with 2 more variables: ARIMAL2 <model>, ARIMAL3 <model>
test_stretched <- tsibble::new_data(train_stretched, n = 4) %>%
dplyr::left_join(train_df, by = c("Quarter"))
test_stretched
#> # A tsibble: 36 x 7 [1Q]
#> # Key: .id [9]
#> Quarter .id Consumption Income Production Savings Unemployment
#> <qtr> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2017 Q3 1 0.585 0.580 -0.201 0.478 -0.100
#> 2 2017 Q4 1 1.13 0.907 1.81 -1.32 -0.100
#> 3 2018 Q1 1 0.417 1.67 0.564 17.4 -0.100
#> 4 2018 Q2 1 0.983 0.662 1.12 -2.72 0
#> 5 2017 Q4 2 1.13 0.907 1.81 -1.32 -0.100
#> 6 2018 Q1 2 0.417 1.67 0.564 17.4 -0.100
#> 7 2018 Q2 2 0.983 0.662 1.12 -2.72 0
#> 8 2018 Q3 2 0.853 0.806 1.26 -0.0857 -0.300
#> 9 2018 Q1 3 0.417 1.67 0.564 17.4 -0.100
#> 10 2018 Q2 3 0.983 0.662 1.12 -2.72 0
#> # … with 26 more rows
fc <- fabletools::forecast(fits, new_data = tidyr::drop_na(test_stretched))
#> Error: Problem with `mutate()` column `ARIMAL0`.
#> ℹ `ARIMAL0 = (function (object, ...) ...`.
#> x object 'Income' not found
#> Unable to compute required variables from provided `new_data`.
#> Does your model require extra variables to produce forecasts?
由reprex 包于 2021-08-13 创建 (v2.0.0 )
我不确定为什么会收到错误消息,因为Income
在test_stretched
数据集中清晰可见。我唯一能想到的是,它与使用 创建的公式的环境有关as.formula()
,但我不知道如果model
过程正常工作,这只是forecast
引发的步骤错误。