在R,arima中使用用户定义的函数使用傅立叶进行分层预测

时间:2019-02-11 12:52:15

标签: r forecasting arima

我正在尝试一种自上而下的方法来预测零售商店中产品的需求。

fourier_forecasts = forecast(sales_weekly_hts, h=12,method="tdfp", FUN=function(x) auto.arima(x, xreg=fourier(x, K=12), seasonal=FALSE))

sales_weekly_hts 是一个 hts 对象,包含2.5年的每周销售数据。

它给我错误:-

预测中的错误。Arima(模型,h = h):未提供回归变量

我猜这个错误是因为它无法获得样本预测之外的傅立叶项,但我不知道如何解决这个问题。 是否不知道可以预测多少个时期?

最小的可复制示例:-

library(dplyr)
library(hts)

# creating a time series matrix containing 4 series and 133 weeks random data 
min_rep_eg = matrix(data = rnorm(n = 133*4 ,mean = 2), nrow = 133, ncol = 4) %>% ts(frequency = 365.25/7)

# giving names to the 5 time series. These names are used to create the hierarchy.
colnames(min_rep_eg) = c("10011001","10011003","10031021","10031031")

# creating the hts.
min_rep_eg_hts = hts(min_rep_eg, characters = c(4, 4))

min_rep_eg_hts_fc = forecast(min_rep_eg_hts, h=2,method="tdfp", FUN=function(x) auto.arima(x, xreg=fourier(x, K=12), seasonal=FALSE))

1 个答案:

答案 0 :(得分:1)

我刚刚看到了你的帖子。当您使用auto.arima函数时,解决问题的最简单方法是通过fmethod参数使用arima模型。然后,通过xregnewxreg设置外部回归变量的值。

以可复制的示例为例:

library(dplyr)
library(hts)

# creating a time series matrix containing 4 series and 133 weeks random data 
min_rep_eg = matrix(data = rnorm(n = 133*4 ,mean = 2), nrow = 133, ncol = 4) %>% ts(frequency = 365.25/7)

# giving names to the 5 time series. These names are used to create the hierarchy.
colnames(min_rep_eg) = c("10011001","10011003","10031021","10031031")



# creating the hts.
min_rep_eg_hts = hts(min_rep_eg, characters = c(4, 4))
x=allts(min_rep_eg_hts)[,1]



min_rep_eg_hts_fc = forecast(min_rep_eg_hts, h=1,method="tdfp", fmethod ="arima",xreg=fourier(x, K=12),newxreg=tail(fourier(x, K=12),1))

输出:

enter image description here