我是R語言的初學者,我有一個按產品收到的請求數量的月度數據列表,我如何使用ARIMA模型(最佳模型)對任何類型的數據進行預測。 我使用了下面的代碼,但我不知道結果是否正確和可靠,或者我必須更改此代碼中的其他模型或簡單更改。用Arima模型在R中預測數據
腳本:
#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')
做'plot(decomposed(data)'。有一個強大的季節性組件到你的數據中,考慮用這個做一些事情 – AkselA
請閱讀[在什麼情況下我可以在我的問題中加入「urgent」或其他類似的短語,爲了獲得更快的答案?](// meta.stackoverflow.com/q/326569) - 總結是,這不是解決志願者的理想方式,並且可能對獲得答案起反作用。請不要將此添加到您的 – halfer
對不起,但這是我第一次使用Stackoverflow,好的,謝謝您的建議 –