我想将这两个事情(与时间序列T结合在一起):
换句话说,我想使用T的经季节性调整的成分来获得预测,该成分整合了外部预测变量并“加回”了季节性。
我可以分别执行这两个操作,但不能将它们组合使用
以下是一些玩具示例:
首先,加载库和数据:
library(forecast)
library(tsibble)
library(tibble)
library(tidyverse)
library(fable)
library(feasts)
library(fabletools)
us_change <- readr::read_csv("https://otexts.com/fpp3/extrafiles/us_change.csv") %>%
mutate(Time = yearquarter(Time)) %>%
as_tsibble(index = Time)
具有T的季节性调整分量的拟合和预测示例:
model_def = decomposition_model(STL,
Consumption ~ season(window = 'periodic') + trend(window = 13),
ARIMA(season_adjust ~ PDQ(0,0,0)),
SNAIVE(season_year),
dcmp_args = list(robust=TRUE))
fit <- us_change %>% model(model_def)
report(fit)
forecast(fit, h=8) %>% autoplot(us_change)
具有ARIMA错误的回归模型示例(收入作为预测变量:]
model_def = ARIMA(Consumption ~ Income + PDQ(0,0,0))
fit <- us_change %>% model(model_def)
report(fit)
us_change_future <- new_data(us_change, 8) %>% mutate(Income = mean(us_change$Income))
forecast(fit, new_data = us_change_future) %>% autoplot(us_change)
这些示例有效,但我想做这样的事情:
model_def = decomposition_model(STL,
Consumption ~ season(window = 'periodic') + trend(window = 13),
ARIMA(season_adjust ~ Income + PDQ(0,0,0)),
SNAIVE(season_year),
dcmp_args = list(robust=TRUE))
fit <- us_change %>% model(model_def)
report(fit)
us_change_future <- new_data(us_change, 8) %>% mutate(Income = mean(us_change$Income))
forecast(fit, new_data = us_change_future) %>% autoplot(us_change)
我在控制台中获得此输出:
> fit <- us_change %>% model(model_def)
Warning message:
1 error encountered for model_def
[1] object 'Income' not found
>
> report(fit)
Series: Consumption
Model: NULL model
NULL model>
所以我尝试在分解模型中这样做:
model_def = decomposition_model(STL,
Consumption ~ season(window = 'periodic') + trend(window = 13),
ARIMA(season_adjust ~ us_change$Income + PDQ(0,0,0)),
SNAIVE(season_year),
dcmp_args = list(robust=TRUE))
拟合没有问题,但现在预测中出现错误:
> forecast(fit, new_data = us_change_future) %>% autoplot(us_change)
Error in args_recycle(.l) : all(lengths == 1L | lengths == n) is not TRUE
In addition: Warning messages:
1: In cbind(xreg, intercept = intercept) :
number of rows of result is not a multiple of vector length (arg 2)
2: In z[[1L]] + xm :
longer object length is not a multiple of shorter object length
我在做什么错?
这里的代码没问题,只是我在制作decomposition_model()
时没有想到的人会做的事情。我已经更新了分解建模方法,以包括外生回归变量,以便可以在组件模型(https://github.com/tidyverts/fabletools/commit/8dd505f6378327b8e93b8440ec17ecf9badf2561)中使用它们。如果您更新软件包,则首次建模应该可以正常进行。