在ARIMA或ETS预测之后,将季节性和非平稳性添加到最终结果中

时间:2018-10-29 05:49:10

标签: r forecasting arima

TS<-ts(Product_Dist_Training_Trans[,17], frequency = 7)

plot(TS)
library(tseries)
library(forecast)

TSETS<- TS

adf<- adf.test(TS)

Order<- ndiffs(TS)

if (adf$p.value>.05)
{
for(i in  1:Order){
TSDiff<- diff(TS)  
TS<- TSDiff}} else
{
TS
}

decomp<- decompose(TS)

TSadj<- TS-decomp$seasonal

Arima<- auto.arima(TSadj)
AM<- forecast(Arima, h = 29)
AM$mean
plot(AM)

ETS<- ets(TSETS)
fc <- forecast(ETS, h = 29)
fc$mean
plot(fc)

我正尝试使用118天的训练数据来预测29天的每日销售量,同时使用ARIMA和ETS预测。我在ARIMA的最终结果中得到负值(因为我使ARIMA处于固定和非季节性的序列)以及ETS,如何在最终结果中获得正确的预测值?另外,我应该如何比较这两种方法以获得更准确的结果?

1 个答案:

答案 0 :(得分:0)

首先,您定义了频率为7的时间序列对象,这意味着您正在定义数据的每周季节性

要使用Arima预测时间序列,请使用以下代码:

TS<-ts(Product_Dist_Training_Trans[,17], frequency = 7)
plot(TS)
library(tseries)
library(forecast)

decomp<-stl(TS , s.window = "periodic") 
plot(decomp)
decomp_components<-as.data.frame(decomp$time.series)

seasonal_component<-ts(decomp_components$seasonal, frequency = 7)

relation1<-auto.arima(seasonal_component,seasonal = T ,max.p = 10, max.q = 10, max.d = 5, max.P = 5, max.Q = 5, max.D = 2, max.order = 30, start.p = 1, start.q = 1)
result1<-forecast(relation1, model = "Arima", h = 29)

plot(result1)
deseasonal_cnt<-seasadj(decomp)
relation2 <- auto.arima(deseasonal_cnt,seasonal = F ,max.p = 10, max.q = 10, max.d = 5,max.P = 5, max.Q = 5, max.D = 2, max.order = 30, start.p = 1, start.q = 1)

result2<-forecast(relation2, model = "Arima",h=29)
plot(result2)
result<-result1$mean+result2$mean

如果这仍然为您提供负值,则表示趋势分量正在减少,为避免出现负值,请改为零

result[result<0]<- 0

您的代码存在的问题是,您仅针对季节性部分进行了预测,还必须针对非季节性部分进行预测,然后合并结果