如何在时间序列的季节性调整部分(在 R 中)拟合具有 ARIMA 误差的回归模型?
How to fit a regression model with ARIMA errors on the seasonally adjusted component of a time series (in R)?
我想用时间序列 T 做这两件事(组合):
- 预测 T 的季节性调整分量(STL 用于分解)和"add back" 季节性(我假设季节性分量不变,所以我对季节性分量使用朴素方法)
- 拟合具有 ARIMA 误差的回归模型(公式中包含外生回归变量)
换句话说,我想使用 T 的季节性调整分量集成外部预测变量和 "adding back" 季节性来获得预测。
我可以单独进行这两个操作,但我不能让它们结合使用
这里有一些玩具示例:
首先,加载库和数据:
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>
所以我尝试在 decomposition_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)。如果您更新包,您的第一次建模尝试应该可以正常工作。
至于第二次尝试失败的原因,预测方法是找到 us_change$Income 并将其用作未来预测的外生回归量。此值的长度为 us_change
,与 us_change_future
的长度不匹配,导致(混淆)错误。
代表:
library(tidyverse)
library(tsibble)
library(fable)
library(feasts)
us_change <- readr::read_csv("https://otexts.com/fpp3/extrafiles/us_change.csv") %>%
mutate(Time = yearquarter(Time)) %>%
as_tsibble(index = Time)
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)
#> Series: Consumption
#> Model: STL decomposition model
#> Combination: season_adjust + season_year
#>
#> ========================================
#>
#> Series: season_adjust
#> Model: LM w/ ARIMA(1,0,2) errors
#>
#> Coefficients:
#> ar1 ma1 ma2 Income intercept
#> 0.6922 -0.5777 0.1975 0.2035 0.5993
#> s.e. 0.1163 0.1305 0.0755 0.0462 0.0883
#>
#> sigma^2 estimated as 0.3234: log likelihood=-157.39
#> AIC=326.77 AICc=327.24 BIC=346.16
#>
#> Series: season_year
#> Model: SNAIVE
#>
#> sigma^2: 0
us_change_future <- new_data(us_change, 8) %>% mutate(Income = mean(us_change$Income))
forecast(fit, new_data = us_change_future) %>% autoplot(us_change)
由 reprex package (v0.2.1)
于 2019-10-09 创建
我想用时间序列 T 做这两件事(组合):
- 预测 T 的季节性调整分量(STL 用于分解)和"add back" 季节性(我假设季节性分量不变,所以我对季节性分量使用朴素方法)
- 拟合具有 ARIMA 误差的回归模型(公式中包含外生回归变量)
换句话说,我想使用 T 的季节性调整分量集成外部预测变量和 "adding back" 季节性来获得预测。
我可以单独进行这两个操作,但我不能让它们结合使用
这里有一些玩具示例:
首先,加载库和数据:
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>
所以我尝试在 decomposition_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)。如果您更新包,您的第一次建模尝试应该可以正常工作。
至于第二次尝试失败的原因,预测方法是找到 us_change$Income 并将其用作未来预测的外生回归量。此值的长度为 us_change
,与 us_change_future
的长度不匹配,导致(混淆)错误。
代表:
library(tidyverse)
library(tsibble)
library(fable)
library(feasts)
us_change <- readr::read_csv("https://otexts.com/fpp3/extrafiles/us_change.csv") %>%
mutate(Time = yearquarter(Time)) %>%
as_tsibble(index = Time)
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)
#> Series: Consumption
#> Model: STL decomposition model
#> Combination: season_adjust + season_year
#>
#> ========================================
#>
#> Series: season_adjust
#> Model: LM w/ ARIMA(1,0,2) errors
#>
#> Coefficients:
#> ar1 ma1 ma2 Income intercept
#> 0.6922 -0.5777 0.1975 0.2035 0.5993
#> s.e. 0.1163 0.1305 0.0755 0.0462 0.0883
#>
#> sigma^2 estimated as 0.3234: log likelihood=-157.39
#> AIC=326.77 AICc=327.24 BIC=346.16
#>
#> Series: season_year
#> Model: SNAIVE
#>
#> sigma^2: 0
us_change_future <- new_data(us_change, 8) %>% mutate(Income = mean(us_change$Income))
forecast(fit, new_data = us_change_future) %>% autoplot(us_change)
由 reprex package (v0.2.1)
于 2019-10-09 创建