http://stackoverflow.com/questions/8960517/how-to-calling-auto-arima-function-of-r-in-java-and-store-the-result-of-forecast
打开R,安装一个叫做"Forecast"的包。每次开启R后,使用之前用
library('forecast')
加载该包。
这里我用传说中的Airline Model数据。载入数据,转换成TS格式
airdata<-read.table('airline.dat')
airts<-ts(airdata,start=1949,frequency=12)
然后用forecast包中的auto.arima自动拟合Arima模型。
arima1<-auto.arima(airts,trace=T)
显示的结果如下:
ARIMA(2,0,2)(1,1,1)[12] with drift : 974.1468
ARIMA(0,0,0)(0,1,0)[12] with drift : 1077.823
ARIMA(1,0,0)(1,1,0)[12] with drift : 974.92
ARIMA(0,0,1)(0,1,1)[12] with drift : 1022.198
ARIMA(2,0,2)(0,1,1)[12] with drift : 967.1033
ARIMA(2,0,2)(0,1,0)[12] with drift : 966.755
ARIMA(1,0,2)(0,1,0)[12] with drift : 964.3004
ARIMA(1,0,1)(0,1,0)[12] with drift : 963.9208
ARIMA(1,0,1)(0,1,0)[12] : 971.225
ARIMA(1,0,1)(1,1,0)[12] with drift : 972.4003
ARIMA(1,0,1)(0,1,1)[12] with drift : 963.9781
ARIMA(1,0,1)(1,1,1)[12] with drift : 971.7862
ARIMA(0,0,1)(0,1,0)[12] with drift : 1022.291
ARIMA(2,0,1)(0,1,0)[12] with drift : 965.183
ARIMA(1,0,0)(0,1,0)[12] with drift : 966.9728
Best model: ARIMA(1,0,1)(0,1,0)[12] with drift
结果是一个AR(1),MA(1)和季节差分一次的Arima模型。Arima模型自动拟合的关键就是定阶,以前用的办法是EACF(extended (sample) autocorrelation function)来定阶,不过现在一般用AIC,AICc,BIC等统计量来定阶。例如上面的974.1468 等就是该模型的AIC
然后可以预测了。
airfore<-forecast(arima1,h=30,fan=T)
预测了30个月,另外计算了置信区间50到99的预测值。
plot(airfore)
最后绘图。如果需要获取其中的预测数据,可以用: airefore$mean 获取。
http://blog.macro2.org/?p=609