1

I am using RServe() and trying to pass an arraylist from java to R. I am getting an error, both program code and error I have given.

String vector = "c(1,2,3,4)";
connR.eval("meanVal=mean(" + vector + ")");
double mean = connR.eval("meanVal").asDouble();
System.out.println("The mean of given vector is=" + mean);
ArrayList fDataset = (ArrayList)f.FDataset;
connR.eval("library(forecast)");
connR.eval("library(tseries)");
System.out.println(fDataset.toString());
connR.assign("myData",fDataset.toString());
System.out.println("*************myData ******************");   
System.out.println(connR.eval("myData"));
connR.eval("timeSeries <- ts(myData,start=1,frequency=7)");
System.out.println("this is time series object : " + connR.eval("timeSeries"));
connR.eval("fitModel <- auto.arima(timeSeries)");
REXP fc = connR.eval("forecast(fitModel, n=3)");
System.out.println("this is the forecast output values: " + fc);

I am getting the output as

The mean of given vector is=2.5

[0, 0, 2, 1, 2, 10, 21, 0, 0, 3, 6, 5, 11, 51, 0, 11, 8, 6, 24, 25, 104, 0, 0, 6, 4, 5, 25, 71]
*************myData ******************

org.rosuda.REngine.REXPString@3c873c87[1]
this is time series object : org.rosuda.REngine.REXPString@4e774e77+[1]
org.rosuda.REngine.Rserve.RserveException: eval failed, request status: 
error code: 127
at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:234)
at forecast.ForecastAction.calculateForecast1(ForecastAction.java:323)
at forecast.ForecastAction.action(ForecastAction.java:140)
at forecast.ForecastAction.runApp(ForecastAction.java:83)
at forecast.ForecastAction.main(ForecastAction.java:36)

The error is at line

connR.eval("fitModel <- auto.arima(timeSeries)");
  • Maybe nulls in the data: https://stackoverflow.com/questions/21907545/how-to-resolve-exception-eval-failed-request-status-error-code-127-in-r-and – AidanGawronski Feb 04 '18 at 02:37
  • I have printed the data also, there is no null. There shouldn't be. My concern is whether I am using this step correctly connR.assign("myData",fDataset.toString()) – Biswajit Jana Feb 04 '18 at 02:43

1 Answers1

0

You have an error with auto.arima because of the error on the other line. Particularly:

connR.assign("myData",fDataset.toString());

You assign the myData variable with a string value. Thus, everything after assign is pointless and timeSeries just create it with one character point.

Make a proper assignment with .assign(Symbol symbol, double[] d) and it will work.

I've changed the code, now it will work. I'm not sure what's the f variable you have, you just need to convert your ArrayList into double[] before assigning it with RServe.

RConnection c = new RConnection();

double[] list = {0, 0, 2, 1, 2, 10, 21, 0, 0, 3, 6, 5, 11, 51, 0, 11, 8, 6, 24, 25, 104, 0, 0, 6, 4, 5, 25, 71};

c.eval("library(forecast)");
c.eval("library(tseries)");

c.assign("myData", list);

c.eval("timeSeries <- ts(myData,start=1,frequency=7)");
c.eval("fitModel <- auto.arima(timeSeries)");
REXP fc = c.eval("forecast(fitModel, n=3)");

double[] res = fc.asList().at("mean").asDoubles();
System.out.println("this is the forecast output values: " + Arrays.toString(res));

c.close();

// this is the forecast output values: [0.0, 0.0, 6.0, 4.0, 5.0, 25.0, 71.0, 0.0, 0.0, 6.0, 4.0, 5.0, 25.0, 71.0]
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
  • Thanks a ton. m0nhawk. I get it now clearly. I have one naive question though. I ran the same list in R and getting some different result. Wondering why so > mydata = c(0.0, 0.0, 2.0, 1.0, 2.0, 10.0, 21.0, 0.0, 0.0, 3.0, 6.0, 5.0, 11.0, 51.0, 0.0, 11.0, 8.0, 6.0, 24.0, 25.0, 104.0, 0.0, 0.0, 6.0, 4.0, 5.0, 25.0, 71.0) > timeSeries <- ts(myData,start=1,frequency=7) > fitModel <- auto.arima(timeSeries) > forecast(fitModel, n=3) Point Forecast 5.000000 5.142857 5.285714 5.428571 5.571429 5.714286 5.857143 6.000000 6.142857 – Biswajit Jana Feb 05 '18 at 02:18
  • but again, your answer will guide people on how to handle arrays to R and from R. Thanks. – Biswajit Jana Feb 05 '18 at 02:21
  • @BiswajitJana What I see is that you have `mydata` and `myData` variables. And you calling `ts` on the second one. I think you have `myData` with different values and thus it's produces a different forecast. – m0nhawk Feb 05 '18 at 11:46
  • my stupidity. I extracted wrong list from forecast object in R..... The program is perfectly okay. Please ignore. Note for other developers:-, Forecast object contains several list. fc.asList().at("mean").asDoubles() provides the list for point forecast. – Biswajit Jana Feb 05 '18 at 16:16