我在运行时间序列模型时遇到问题,预测结果(C.I。95%)为负值。它没有任何意义。我不确定哪个部分犯了错误。
week total_amount_by_week
52 10000
52 12000
52 12300
52 9800
52 23400
51
51...
.
.
.
00 10000
00 12930
数据集如下所示: 因为plot.ts向后看,所以我对数据集做了相反的反应。
order_ts1 <- ts(order_sum$Total_Amount_by_week,start = c(00),end =c(52))
order_ts1[] <-rev(order_ts1)
plot.ts(order_ts1,col ='blue')
然后我计算了差异,ACF和PACF。
order_tsdiff3 <-diff(order_ts1,differences=3)
plot.ts(order_tsdiff3) # d=3
# calcualate ACF
acf(order_tsdiff3,lag.max=53)
acf(order_tsdiff3,lag.max=53,plot=FALSE)
# calculate PACF
pacf(order_tsdiff3,lag.max=53)
pacf(order_tsdiff3,lag.max=53,plot=FALSE)
fit_ma <- arima(order_ts1, order = c(1, 3, 1))
fit_ma
order_arimaforecast1 <- forecast.Arima(fit_ma,h=3,level=c(99.5))
order_arimaforecast1
所以我得到了一些估计值,但我认为这是错误的。为什么它会出现负值?
Call:
arima(x = order_sumts1, order = c(1, 3, 1))
Coefficients:
ar1 ma1
-0.6673 -1.0000
s.e. 0.1135 0.0539
sigma^2 estimated as 84368661: log likelihood = -529.98, aic = 1065.96
Point Forecast Lo 99.5 Hi 99.5
53 -1420.589 -27459.41 24618.23
54 -7983.391 -51772.69 35805.91
55 -21921.514 -93114.57 49271.54
所有估计金额应为正值。
答案 0 :(得分:0)
尝试使用转换来帮助确保始终获得正值
## some sample data
a<-ts(c, frequency = 5)
## use ar() model with BoxCox() transform on the data & setting lambda = 0 (equivalent to taking the log)
fit <- ar(BoxCox(a,lambda=0))
## compute forecast and set lambda to 0 so that a back-transform is done (since we transformed the original data set we now want to back transform it to the original scale)
forecast(fit,h=3, lambda=0)
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
3.0 2.015135 1.1019547 3.685060 0.8005621 5.072398
3.2 1.385040 0.6984705 2.746480 0.4861378 3.946073
3.4 1.692792 0.8355244 3.429637 0.5749517 4.983975
如果你在没有变换的情况下完成这些步骤,即只预测“a”,你也会得到负值,但上述情况下的变换有助于此。
a<-ts(c, frequency = 5)
## lambda = 1 means no transformation
fit <- ar(BoxCox(a,lambda=1))
forecast(fit,h=3, lambda=1)
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
3.0 1.8 0.4764192 3.123581 -0.2242421 3.824242
3.2 1.8 0.4764192 3.123581 -0.2242421 3.824242
3.4 1.8 0.4764192 3.123581 -0.2242421 3.824242
您可以根据具体情况调整此代码:
arima(x = BoxCox(order_sumts1, lambda=0), order = c(1, 3, 1))
希望它有所帮助!