使用傅立叶对 xreg 和 newxreg 进行分层时间序列预测
Hierarchical time series prediction using Fourier for xreg and newxreg
我想在分层时间序列中使用 xreg = Fourier(ts, k,f) 作为回归变量。
我可以让回归器为单个系列工作。
我有一个分层时间序列 hts,它由类似于第一个序列的序列组成(相同数量的时间步长)。
hts 参考:https://robjhyndman.com/publications/hierarchical-tourism/
我使用相同的回归量从 hts 得到的预测有误。有什么问题?
train_ts:整个 hts,形式为 visnights。
top_ts : 只是顶级时间序列
完成单个系列后,top_ts,使用 xreg 和 newxreg 有效。
KK <- 12
REG= fourier(top_ts,KK,52)
model <- auto.arima(top_ts, xreg=fourier(top_ts, K=KK))
fcstResult <- forecast(model, h=1, xreg= REG, newxreg=REG)
作品
当我做类似visnights的hts预测时,train_ts,
train_ts <- window(vis_ts, c(2,1), end= c(3, 52))
fcstResult <- forecast(train_ts, h=1, fmethod="arima", method = "bu")
作品
当我在 hts 上使用相同的回归器 XREG 时,出现错误:
fcstResult <- forecast(
train_ts, h = 1, method = "bu", fmethod = "arima", xreg=REG, newxreg=REG)
OR
fcstResult <- forecast(
train_ts, h = 1, method = "bu", fmethod = "arima", xreg=REG, newxreg=REG, lambda=0)
Error in model.frame.default(formula = x ~ xregg, drop.unused.levels = TRUE) :
variable lengths differ (found for 'xregg')
(我使所有列都非零并添加了一个小的随机噪声,所以这不是问题)
在您的第一个代码块中,newxreg
参数被忽略,h
参数也是如此。 REG
的值被认为是未来时间段所需的回归变量。如果你的训练数据的长度等于 52 的倍数,由于傅立叶项的周期性,这恰好是正确的,但最好像这样明确:
library(hts)
vis_ts <- hts(fpp2::visnights, characters = c(3, 5))
train_ts <- window(vis_ts, end=c(2010,4))
test_ts <- window(vis_ts, start=c(2011,1))
train_top <- aggts(train_ts, level=0)
test_top <- aggts(test_ts, level=0)
train_reg <- fourier(train_top, K=2)
test_reg <- fourier(test_top, K=2)
model <- auto.arima(train_top, xreg=train_reg)
fcstResult <- forecast(model, xreg=test_reg)
当使用层次化方法时,xreg
和 newxreg
参数都需要传递对应于训练和测试周期。在此函数中,检查 newxreg
的行以确保它们与 h
的值匹配。这会导致错误。
下面的代码可以工作
fcast_hts <- forecast(train_ts, method='bu', fmethod='arima',
xreg=train_reg, newxreg=test_reg)
我想在分层时间序列中使用 xreg = Fourier(ts, k,f) 作为回归变量。
我可以让回归器为单个系列工作。
我有一个分层时间序列 hts,它由类似于第一个序列的序列组成(相同数量的时间步长)。
hts 参考:https://robjhyndman.com/publications/hierarchical-tourism/
我使用相同的回归量从 hts 得到的预测有误。有什么问题?
train_ts:整个 hts,形式为 visnights。
top_ts : 只是顶级时间序列
完成单个系列后,top_ts,使用 xreg 和 newxreg 有效。
KK <- 12
REG= fourier(top_ts,KK,52)
model <- auto.arima(top_ts, xreg=fourier(top_ts, K=KK))
fcstResult <- forecast(model, h=1, xreg= REG, newxreg=REG)
作品
当我做类似visnights的hts预测时,train_ts,
train_ts <- window(vis_ts, c(2,1), end= c(3, 52))
fcstResult <- forecast(train_ts, h=1, fmethod="arima", method = "bu")
作品
当我在 hts 上使用相同的回归器 XREG 时,出现错误:
fcstResult <- forecast(
train_ts, h = 1, method = "bu", fmethod = "arima", xreg=REG, newxreg=REG)
OR
fcstResult <- forecast(
train_ts, h = 1, method = "bu", fmethod = "arima", xreg=REG, newxreg=REG, lambda=0)
Error in model.frame.default(formula = x ~ xregg, drop.unused.levels = TRUE) :
variable lengths differ (found for 'xregg')
(我使所有列都非零并添加了一个小的随机噪声,所以这不是问题)
在您的第一个代码块中,newxreg
参数被忽略,h
参数也是如此。 REG
的值被认为是未来时间段所需的回归变量。如果你的训练数据的长度等于 52 的倍数,由于傅立叶项的周期性,这恰好是正确的,但最好像这样明确:
library(hts)
vis_ts <- hts(fpp2::visnights, characters = c(3, 5))
train_ts <- window(vis_ts, end=c(2010,4))
test_ts <- window(vis_ts, start=c(2011,1))
train_top <- aggts(train_ts, level=0)
test_top <- aggts(test_ts, level=0)
train_reg <- fourier(train_top, K=2)
test_reg <- fourier(test_top, K=2)
model <- auto.arima(train_top, xreg=train_reg)
fcstResult <- forecast(model, xreg=test_reg)
当使用层次化方法时,xreg
和 newxreg
参数都需要传递对应于训练和测试周期。在此函数中,检查 newxreg
的行以确保它们与 h
的值匹配。这会导致错误。
下面的代码可以工作
fcast_hts <- forecast(train_ts, method='bu', fmethod='arima',
xreg=train_reg, newxreg=test_reg)