I try to call R in JAVA for ARIMA function. The first example runs arima(). It works well.
Rengine rengine = new Rengine(new String[]{"--vanilla"},false,null);
System.out.println(" *BEGIN* ");
rengine.eval("library(forecast)");
double[] trainingData = = {10930,10318,10595,10972,7706,6756,9092,10551,9722,10913,11151,8186,6422,
+ 6337,11649,11652,10310,12043,7937,6476,9662,9570,9981,9331,9449,6773,6304,9355,10477,
+ 10148,10395,11261,8713,7299,10424,10795,11069,11602,11427,9095,7707,10767,12136,12812,
+ 12006,12528,10329,7818,11719,11683,12603,11495,13670,11337,10232,13261,13230,15535,
+ 16837,19598,14823,11622,19391,18177,19994,14723,15694,13248,9543,12872,13101,15053,
+ 12619,13749,10228,9725,14729,12518,14564,15085,14722,11999,9390,13481,14795,15845,
+ 15271,14686,11054,10395,14775,14618,16029,15231,14246,12095,10473,15323,15381,14947};
rengine.assign("value", trainingData);
rengine.eval("traindatats<-ts(value)");
rengine.eval("loopMinLag<-0");
rengine.eval("loopMaxLag<-8");
rengine.eval("best.order <- c(loopMinLag, 1, loopMinLag)");
rengine.eval("best.aic = Inf");
rengine.eval("for(P in loopMinLag:loopMaxLag) { for(Q in loopMinLag:loopMaxLag) { myModel <- arima(traindatats, order=c(P,1,Q), method='ML'); fit.aic <- myModel$aic; if(fit.aic < best.aic) { best.order <- c(P, 1, Q); best.aic <- fit.aic; } } }");
rengine.eval("model <- list()");
rengine.eval("model$P <- best.order[1]");
rengine.eval("model$D <- 1");
rengine.eval("model$Q <- best.order[3]");
rengine.eval("ar<-arima(traindatats,order=c(model$P,model$D,model$Q))");
rengine.eval("f<-predict(ar,n.ahead=7)");
System.out.println(" *RESULT* ");
double forecast[] = new double[7];
for (int i = 0,j=1; i < 7; i++,j++) {
forecast[i] = rengine.eval("f$pred[" + j + "]").asDouble();
System.out.print(" "+forecast[i]);
}
System.out.println("\r\n *DONE* ");
The result is the prediction values for the next 7 numbers. There is no any problem for arima() running in JAVA. The second example runs auto.arima(). However, it doesn't work. Does anyone know why it can't work?
Rengine rengine = new Rengine(new String[]{"--vanilla"},false,null);
System.out.println(" *BEGIN* ");
rengine.eval("library(forecast)");
double[] trainingData = {10930,10318,10595,10972,7706,6756,9092,10551,9722,10913,11151,8186,6422,
+ 6337,11649,11652,10310,12043,7937,6476,9662,9570,9981,9331,9449,6773,6304,9355,10477,
+ 10148,10395,11261,8713,7299,10424,10795,11069,11602,11427,9095,7707,10767,12136,12812,
+ 12006,12528,10329,7818,11719,11683,12603,11495,13670,11337,10232,13261,13230,15535,
+ 16837,19598,14823,11622,19391,18177,19994,14723,15694,13248,9543,12872,13101,15053,
+ 12619,13749,10228,9725,14729,12518,14564,15085,14722,11999,9390,13481,14795,15845,
+ 15271,14686,11054,10395,14775,14618,16029,15231,14246,12095,10473,15323,15381,14947};
Rengine rengine = new Rengine(new String[]{"--vanilla"},false,null);
System.out.println(" *BEGIN* ");
rengine.assign("value", trainingData);
rengine.eval("traindatats<-ts(value)");
rengine.eval("ar<-auto.arima(traindatats)");
rengine.eval("f<-predict(ar,n.ahead=" + 7 + ")");
double forecast[] = new double[7];
for (int i = 0,j=1; i < forcastSize; i++,j++) {
forecast[i] = rengine.eval("f$pred[" + j + "]").asDouble();
System.out.print(" "+forecast[i]);
}
System.out.println(" *DONE* ");
The error message is "Exception in thread "main" java.lang.NullPointerException" for the line forecast[i] = rengine.eval("f$pred[" + j + "]").asDouble();
PS. Both of examples for arima() and auto.arima() can run in R correctly. The corresponding R code for auto.arima() can run in R correctly, as shown in auto.arima R example.
In here:
rengine.eval("f$pred[" + j + "]")this one is null.According to: http://www.rforge.net/org/doc/org/rosuda/JRI/Rengine.html#eval(java.lang.String)
It looks, like something goes wrong on R side.
Well, maybe you have some sort of issues with libraries, etc.
If I install forecast
inside R, and I call the code like this:
I am getting proper output. My SimpleR.java looks like:
It seems to be working fine.
By logging I am referring to code: