如何在时间序列的季节性调整分量(R)上拟合具有ARIMA误差的回归模型?
我想用时间序列T做这两件事(结合起来):如何在时间序列的季节性调整分量(R)上拟合具有ARIMA误差的回归模型?,r,time-series,forecasting,arima,fable-r,R,Time Series,Forecasting,Arima,Fable R,我想用时间序列T做这两件事(结合起来): 预测T的季节性调整成分(用于分解的STL)并“加回”季节性(我假设季节性成分是不变的,所以我对季节性成分使用天真的方法) 拟合具有ARIMA误差的回归模型(公式中包含外生回归系数) 换言之,我希望使用T的季节性调整分量,将外部预测值和季节性“相加”得到预测 我可以分别做这两个操作,但我不能让它们同时工作 以下是一些玩具示例: 首先,加载库和数据: library(forecast) library(tsibble) library(tibble) lib
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)
拟合没有问题,但现在预测中出现错误:
> 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
我做错了什么?这里的代码没有问题,只是我没有考虑过人们在进行
分解\u model()
时会做的事情。我已经更新了分解建模方法,以包括外部回归,以便可以在组件模型()中使用它们。如果您更新了软件包,您的第一次建模尝试应该可以正常工作
至于第二次尝试失败的原因,预测方法是找到us_change$收入,并将其用作未来预测的外生回归。此值的长度为us\u change
,与us\u change\u future
的长度不匹配,导致(混淆)错误
雷普雷克斯:
库(tidyverse)
图书馆(TSIBLE)
图书馆(寓言)
图书馆(宴会)
美元兑换率%
突变(时间=年季度(时间))%>%
as_tsible(索引=时间)
模型定义=分解模型(STL,
消费季节(窗口=‘周期’)+趋势(窗口=13),
ARIMA(季节调整收入+PDQ(0,0,0)),
斯奈夫(季节和年份),
dcmp_args=list(稳健=TRUE))
适合%型号(型号定义)
报告(fit)
#>系列:消费
#>模型:STL分解模型
#>组合:季节调整+季节年
#>
#> ========================================
#>
#>系列:季节调整
#>模型:LM w/ARIMA(1,0,2)误差
#>
#>系数:
#>ar1 ma1 ma2收入截距
#> 0.6922 -0.5777 0.1975 0.2035 0.5993
#>s.e.0.1163 0.1305 0.0755 0.0462 0.0883
#>
#>西格玛^2估计为0.3234:对数似然=-157.39
#>AIC=326.77 AICc=327.24 BIC=346.16
#>
#>系列:第二季
#>型号:斯奈夫
#>
#>西格玛^2:0
美元变动未来百分比变动(收入=平均值(美元变动$收入))
预测(拟合、新数据=美国变化\未来)%>%自动批次(美国变化)
由(v0.2.1)于2019-10-09创建
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