在Java中为ARIMA调用R.

时间:2018-06-05 17:16:47

标签: java r arima jri

我尝试在JAVA中调用R来获取ARIMA功能。第一个例子运行arima()。效果很好。

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 prediction values for the next 7 numbers。在JAVA中运行的arima()没有任何问题。第二个示例运行auto.arima()。 但是,它不起作用。有谁知道为什么它不起作用?

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*  ");

错误消息是&#34;线程中的异常&#34; main&#34;显示java.lang.NullPointerException&#34;对于forecast[i] = rengine.eval("f$pred[" + j + "]").asDouble();

PS。 arima()和auto.arima()的两个示例都可以正确地在R中运行。 auto.arima()的相应R代码可以正确运行在R中,如auto.arima R example所示。

1 个答案:

答案 0 :(得分:0)

在这里:

rengine.eval("f$pred[" + j + "]").asDouble();

rengine.eval("f$pred[" + j + "]")这个是空的。

根据:http://www.rforge.net/org/doc/org/rosuda/JRI/Rengine.html#eval(java.lang.String)

  

EVAL

     

public REXP eval(java.lang.String s)

Parses and evaluates an R expression and returns the result. Has the same effect as calling eval(s, true).

Parameters:

    s - expression (as string) to parse and evaluate
Returns:
    resulting expression or null if something wnet wrong

看起来,就像R方面出了问题一样。

好吧,也许你在图书馆等方面存在某些问题。

如果我安装预测

install.packages('forecast')
在R里面,我调用这样的代码:

javac -cp /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/jri/JRI.jar SimpleR.java
java -Djava.library.path=/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/jri -cp .:/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/jri/JRI.jar SimpleR
  *BEGIN*
Forecast: 0
Result: 14436.670778092368
Forecast: 1
Result: 14104.789779948998
Forecast: 2
Result: 14104.789779948998
Forecast: 3
Result: 14104.789779948998
Forecast: 4
Result: 14104.789779948998
  *DONE*

我得到了正确的输出。我的SimpleR.java看起来像:

import org.rosuda.JRI.*;

public class SimpleR {

  public static void main(String [] arg) {
    Rengine rengine = new Rengine(new String[]{"--vanilla"},false,null);
    System.out.println("  *BEGIN*  ");
    // enable R log
    rengine.eval("log<-file('/tmp/file.log')");
    rengine.eval("sink(log, append=TRUE)");
    rengine.eval("sink(log, append=TRUE, type='message')");
    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("ar<-auto.arima(traindatats)");
    REXP result = rengine.eval("f<-predict(ar,n.ahead=" + 5 + ")");
    if(result == null) {
      System.out.println("Error");
    }
    double forecast[] = new double[5];
    for (int i = 0;i < 5;i++) {
      System.out.println("Forecast: " + i);
      forecast[i] = rengine.eval("f$pred[" + (i+1) + "]").asDouble();
      System.out.println("Result: " + forecast[i]);
    }
    System.out.println("  *DONE*  ");
  }
}

似乎工作正常。

通过记录我指的是代码:

rengine.eval("log<-file('/tmp/file.log')");
rengine.eval("sink(log, append=TRUE)");
rengine.eval("sink(log, append=TRUE, type='message')");