3

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();
    }
}
Falko
  • 17,076
  • 13
  • 60
  • 105
Nurul
  • 61
  • 1
  • 2
  • 4
  • Hard to answer without information about the exact error message(s) you are getting. – tmr Feb 15 '12 at 22:42

1 Answers1

3

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.

Rob Hyndman
  • 30,301
  • 7
  • 73
  • 85