How to calling auto.Arima function of R in java and store the result of forecast?

2.6k views Asked by At

I'm new to R in Java. I use auto.Arima function of R in Java to forecast my data for 12 periods. but the period of forecast result is ten period. How can I do for 12 periods of forecast? I also want to store result of forecast at an array. This is my code, it can't stop running and I am getting error messages.

What can I do to store it's result?

package Arima;

import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;

public class Arima {

    public static void main(String[] args) {
        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("data<-scan('D:/timeseries.txt',skip=1);");
        re.eval("datats<-data;");
        // I use auto.arima function to forecast my data for 12 periods. 
        // but the period of forecast result is ten period.
        // How can I do for 12 periods of forecast ?  

        re.eval("arima<-auto.arima(datats);");
        re.eval("fcast<-forecast(arima);");
        REXP fs = re.eval("summary(fcast);");
        // I want to get result of forecast and returned it at an array
        double[] forecast = fs.asDoubleArray();
        for(int i=0; i<forecast.length; i++)
            System.out.println(forecast[i]);
        re.end();
    }
}
1

There are 1 answers

0
Rob Hyndman On

The forecast function has an argument h that specifies the forecast horizon. By default h=10. So just change your call to forecast(arima,h=12) and it should be fine.