简体   繁体   中英

get output of arima forecast from R and capture as an array in Java

I am trying to forecast using R's arima from java using Eclipse. I am using Rserve. I need to output the forecast and the intervals in array format. I can print out the forecast in the Eclipse console as an output. How do I retrieve the point forecast and the confidence interval as an array. Here is my code.

RConnection c = null;

int[] kings = { 60, 43, 67, 50, 56, 42, 50, 65, 68, 43, 65, 34, 47, 34,
        49, 41, 13, 35, 53, 56, 16, 43, 69, 59, 48, 59, 86, 55, 68, 51,
        33, 49, 67, 77, 81, 67, 71, 81, 68, 70, 77, 56 };
try {
    c = new RConnection();
    System.out.println("INFO : The Server version is :-- " + c.getServerVersion());
    c.eval("library(\"forecast\")");
    c.assign("kings", kings);
    c.eval("datats<-data;");
    c.eval("kingsts<-ts(kings);");
    c.eval("arima<-auto.arima(kingsts);");
    c.eval("fcast<-forecast(arima, h=12);");
    String f = c.eval("paste(capture.output(print(fcast)),collapse='\\n')").asString();
    System.out.println(f);

//Codes online suggest I do the following but this does not work REXP fs = re.eval("summary(fcast);"); double[] forecast = fs.asDoubleArray(); for(int i=0; i

I figured out how to retrieve the values. I converted this into the data frame. c.eval("ds=as.data.frame(fcast);"); //get the forecast values

            c.eval("names(ds)[1]<-paste(\"actual\");");
        REXP actual=c.eval("(ds$\"actual\")");  
        double[] forecast = actual.asDoubles();
        for(int i=0; i<forecast.length; i++)
        System.out.println("forecast values are: "+forecast[i]);

The still need to figure out how to attach the time stamp to the dataframe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM