用Arima模型预测R
我是R语言的初学者,我有一个每月收到的产品数量请求的数据列表,我如何使用ARIMA模型(最佳模型)预测任何类型的数据。 我使用了下面的代码,但我不知道结果是否正确可靠,或者我必须更改另一个模型,或者对代码进行简单更改 脚本:用Arima模型预测R,r,forecasting,predict,arima,R,Forecasting,Predict,Arima,我是R语言的初学者,我有一个每月收到的产品数量请求的数据列表,我如何使用ARIMA模型(最佳模型)预测任何类型的数据。 我使用了下面的代码,但我不知道结果是否正确可靠,或者我必须更改另一个模型,或者对代码进行简单更改 脚本: #Step 1: Plot Qty data as time series data <- structure(c(3108L, 2508L, 3516L, 3828L, 3755L, 6612L, 6708L, 3624L, 4032L, 4104L, 30
#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')
#步骤1:将数量数据绘制为时间序列
数据你让它变得比需要的复杂了一点<代码>自动。arima
将确定自动使用的AR、MA和差分术语的数量
最终,如果您只是想要适合的代码,尽管您可以这样做
library(forecast)
data = ts(data[,2],start = c(2013,1),frequency = 12)
model = auto.arima(data)
我们可以查看模型的摘要来确定它适合哪个模型
> summary(model)
Series: dat
ARIMA(0,1,1)
Coefficients:
ma1
-0.8501
s.e. 0.0591
sigma^2 estimated as 4166267: log likelihood=-479.27
AIC=962.53 AICc=962.77 BIC=966.47
我们有ARIMA(0,1,1)。在自动拾取的模型中有一个差异和1 MA项
如果我们预测模型,我们可以这样绘制它
plot(forecast(model,h=12),include=24)
我们没有像之前的数据中那样看到峰值的原因是模型没有包括任何季节性参数。从情节上看,高峰似乎不是在同一个时间范围内发生的。如果峰值与某个事件相关,则需要将其包括在模型中,这可以使用xreg参数完成,该参数具有该事件的二进制指示符。请查看我的第二个示例post@TarhouniBilel这不是一个错误,只是没有那么合适。感谢freind,我将尝试在modelDo
plot(分解)中添加xreg(数据)你的数据有很强的季节性成分,请考虑用它来做些什么。请阅读摘要:这不是一个理想的方式来解决志愿者的问题,并且可能会对获得答案产生反作用。请不要把这个添加到你的问题中。抱歉,这是我第一次使用StAccess,谢谢。你的建议