我是 R 语言的初学者,我有一个按产品接收的请求数量的月度数据列表,我如何使用 ARIMA 模型(最佳模型)对任何类型的数据进行预测。我使用了下面的代码,但我不知道结果是否正确可靠,或者我必须更改另一个模型或对此代码进行简单更改。
脚本 :
#Step 1: Plot Qty data as time series
data <- structure(c(3108L, 2508L, 3516L, 3828L, 3755L, 6612L, 6708L,
3624L, 4032L, 4104L, 3000L, 3204L, 2640L, 2124L, 1884L, 12382L,
1488L, 1356L, 2028L, 1764L, 1524L, 7248L, 1248L, 816L, 804L,
708L, 756L, 972L, 4104L, 1296L, 2268L, 588L, 768L, 792L, 744L,
1680L, 684L, 2052L, 672L, 492L, 744L, 768L, 828L, 936L, 840L,
5364L, 408L, 528L, 60L, 612L, 684L, 852L, 756L, 972L),
.Tsp = c(2013, 2017.41666666667, 12), class = "ts")
plot(data, xlab='Years', ylab = ' Qty ')
# Step 2: Difference data to make data stationary on mean (remove trend)
plot(diff(data),ylab='Differenced Qty')
#Step 3: log transform data to make data stationary on variance
plot(log10(data),ylab='Log (Qty)')
#Step 4: Difference log transform data to make data stationary on both mean and variance
plot(diff(log10(data)),ylab='Differenced Log (Qty)')
# Step 5: Plot ACF and PACF to identify potential AR and MA model
par(mfrow = c(1,2))
acf(ts(diff(log10(data))),main='ACF Qty')
pacf(ts(diff(log10(data))),main='PACF Qty ')
# Step 6: Identification of best fit ARIMA model
require(forecast)
ARIMAfit = auto.arima(log10(data), approximation=FALSE,trace=FALSE)
summary(ARIMAfit)
# Step 6: Forecast sales using the best fit ARIMA model
par(mfrow = c(1,1))
pred = predict(ARIMAfit, n.ahead = 36)
pred
plot(data,type='l',xlim=c(2004,2018),ylim=c(1,1600),xlab = 'Year',ylab = ' Qty ')
lines(10^(pred$pred),col='blue')
lines(10^(pred$pred+2*pred$se),col='orange')
lines(10^(pred$pred-2*pred$se),col='orange')
# Step 7: Plot ACF and PACF for residuals of ARIMA model to ensure no more information is left for extraction
par(mfrow=c(1,2))
acf(ts(ARIMAfit$residuals),main='ACF Residual')