2

I am trying to import the R package 'forecast; in netbeans to use its functions. I have managed to make the JRI connection and also to import the javaGD library and experimented with it with a certain success. The problem about the forecasting package is that I cannot find the corresponding JAR files so to include them as a library in my project. I am loading it normally : re.eval(library(forecast)), but when I implement one of the library's function, a null value is returned. Although I am quite sure that the code is correct I am posting it just in case.

tnx in advance

      Rengine re = new Rengine(Rargs, false, null);
    System.out.println("rengine created, waiting for R!");
    if(!re.waitForR())
    {
        System.out.println("cannot load R");
        return;
    }
    re.eval("library(forecast)");
    re.eval("library(tseries)");

    re.eval("myData <- read.csv('C:/.../I-35E-NB_1.csv', header=F, dec='.', sep=',')");
    System.out.println(re.eval("myData"));

    re.eval("timeSeries <- ts(myData,start=1,frequency=24)");
    System.out.println("this is time series object : " + re.eval("timeSeries"));

    re.eval("fitModel <- auto.arima(timeSeries)");
    REXP fc = re.eval("forecast(fitModel, n=20)");
    System.out.println("this is the forecast output values: " + fc);
  • and this is the output I get: rengine created, waiting for R! [VECTOR ([REAL* (8.81, 8.805, ... (140 more values follow))])] this is time series object : [REAL* (8.81, 8.805, 8.77, 8.78, 8.78, , 9.375, 9.525, 9.15, 9.19, 9.12, 9.05, 9.02, 9.075, 9.08, 9.145, ... (140 more values follow))] this is the forecast output values: null ???? HERE IS THE PROBLEM – user1333584 Apr 14 '12 at 18:27
  • Could it be that the argument to forecast should be h=20 not n=20? – Rob Hyndman Apr 15 '12 at 06:04

1 Answers1

1

You did not convert values from R into java, you should first create a numerical vector of auto.arima output in R, and then use the method .asDoubleArray() to read it into java.

I gave a complete example in [here] How I can load add-on R libraries into JRI and execute from Java? , that shows exactly How to use the auto.arima function in Java using JRI.

Community
  • 1
  • 1
Yehoshaphat Schellekens
  • 2,305
  • 2
  • 22
  • 49