/** * Method checks if Column is a existing column in R. * * @param col the column to check * @return true if column exists else false */ protected Boolean checkRColumn(String col) { REXP rCols = eval("colnames(" + RVAR + ")", false); String[] cols = rCols.asStringArray(); List<String> collist = Arrays.asList(cols); if (collist.contains(col)) { return true; } return false; }
/** * Sends a single command to R. * * @param sz The R command * @param szDescription The Description showing up before the output */ private String[] sendCommand2R(String sz) { // set command if (sz.length() == 0) { return null; } // parse long lParsed = re.rniParse(sz, 1); // eval long lEvaluated = re.rniEval(lParsed, 0); // generate REXP if (lEvaluated > 0) { REXP exp = new REXP(re, lEvaluated); String[] szArr = exp.asStringArray(); if (szArr != null) { return szArr; } double[] dArr = exp.asDoubleArray(); if (dArr != null) { String[] szDArr = new String[dArr.length]; for (int cnt = 0; cnt < dArr.length; ++cnt) { szDArr[cnt] = String.valueOf(dArr[cnt]); } return szDArr; } int[] iArr = exp.asIntArray(); if (iArr != null) { String[] szIArr = new String[iArr.length]; for (int cnt = 0; cnt < iArr.length; ++cnt) { szIArr[cnt] = String.valueOf(iArr[cnt]); } return szIArr; } } return null; }
@Override public String[] asStrings() { return r.asStringArray(); }
// Run prediction method on data based on file name public StockInfo[] runPrediction(Map<String, StockInfo[]> dataToProcess) { double[] resultArrayOpen = null; double[] resultArrayHigh = null; double[] resultArrayLow = null; double[] resultArrayClose = null; double[] resultArrayVolume = null; StockInfo[] predictedDataArray = null; try { logger.log("prediction will start"); // Flat the data to perform prediction on it StockInfo[] flatStockInfo = this.flatMapOfData(dataToProcess); Collections.reverse(Arrays.asList(flatStockInfo)); REXP x; RVector v; // Check for installed packages x = re.eval("installed.packages()"); v = x.asVector(); String[] packages = x.asStringArray(); boolean isForecastInstalled = false; logger.log("<R> getting installed packages"); for (int index = 0; index < packages.length && isForecastInstalled == false; index++) { logger.log("<R> has installed " + packages[index]); if (packages[index] != null && packages[index].compareTo("forecast") == 0) { isForecastInstalled = true; } } // If forecast needs to be installed if (isForecastInstalled == false) { logger.log("<R> will set repos"); // Set CRAN re.eval("r <- getOption(\"repos\")"); re.eval("r[\"CRAN\"] <- \"http://cran.us.r-project.org\""); re.eval("options(repos = r)"); re.eval("rm(r)"); // Install forecast re.eval("install.packages(\"forecast\")"); logger.log("<R> will install forecast package"); } // Load forecast library re.eval("library(\"forecast\")"); logger.log("<R> loaded forecast"); // Make prediction for Open value ----------------------- // Load data into R logger.log("<R> loading data into R"); StringBuilder builder = new StringBuilder("inputData <- c("); for (int index = 0; index < flatStockInfo.length; index++) { builder.append(flatStockInfo[index].open); if (index != flatStockInfo.length - 1) { builder.append(","); } else { builder.append(")"); } } String stringFromBuilder = builder.toString(); re.eval(stringFromBuilder); // Create time series from data logger.log("<R> forecasting open values BestFit"); re.eval("temporalData <- ts(inputData, frequency=365)"); // Forecast data re.eval("forecastData <- forecast(temporalData, h=30)"); // re.eval("arimaModel <- auto.arima(temporalData, max.p=5, max.q=5, max.P=5, max.Q=5)"); // re.eval("forecastData <- forecast(arimaModel, h=30)"); x = re.eval("forecastData"); v = x.asVector(); x = (REXP) v.elementAt(1); // instead of 3 resultArrayOpen = x.asDoubleArray(); // Make prediction for High value ------------------------ builder = new StringBuilder("inputData <- c("); for (int index = 0; index < flatStockInfo.length; index++) { builder.append(flatStockInfo[index].high); if (index != flatStockInfo.length - 1) { builder.append(","); } else { builder.append(")"); } } stringFromBuilder = builder.toString(); re.eval(stringFromBuilder); // Create time series from data logger.log("<R> forecasting high values BestFit"); re.eval("temporalData <- ts(inputData, frequency=365)"); // Forecast data re.eval("forecastData <- forecast(temporalData, h=30)"); // re.eval("arimaModel <- auto.arima(temporalData, max.p=5, max.q=5, max.P=5, max.Q=5)"); // re.eval("forecastData <- forecast(arimaModel, h=30)"); x = re.eval("forecastData"); v = x.asVector(); x = (REXP) v.elementAt(1); resultArrayHigh = x.asDoubleArray(); // Make prediction for Low value ------------------------ builder = new StringBuilder("inputData <- c("); for (int index = 0; index < flatStockInfo.length; index++) { builder.append(flatStockInfo[index].low); if (index != flatStockInfo.length - 1) { builder.append(","); } else { builder.append(")"); } } stringFromBuilder = builder.toString(); re.eval(stringFromBuilder); // Create time series from data logger.log("<R> forecasting low values BestFit"); re.eval("temporalData <- ts(inputData, frequency=365)"); // Forecast data re.eval("forecastData <- forecast(temporalData, h=30)"); // re.eval("arimaModel <- auto.arima(temporalData, max.p=5, max.q=5, max.P=5, max.Q=5)"); // re.eval("forecastData <- forecast(arimaModel, h=30)"); x = re.eval("forecastData"); v = x.asVector(); x = (REXP) v.elementAt(1); resultArrayLow = x.asDoubleArray(); // Make prediction for Close value ------------------------ builder = new StringBuilder("inputData <- c("); for (int index = 0; index < flatStockInfo.length; index++) { builder.append(flatStockInfo[index].close); if (index != flatStockInfo.length - 1) { builder.append(","); } else { builder.append(")"); } } stringFromBuilder = builder.toString(); re.eval(stringFromBuilder); // Create time series from data logger.log("<R> forecasting close values BestFit"); re.eval("temporalData <- ts(inputData, frequency=365)"); // Forecast data re.eval("forecastData <- forecast(temporalData, h=30)"); // re.eval("arimaModel <- auto.arima(temporalData, max.p=5, max.q=5, max.P=5, max.Q=5)"); // re.eval("forecastData <- forecast(arimaModel, h=30)"); x = re.eval("forecastData"); v = x.asVector(); x = (REXP) v.elementAt(1); resultArrayClose = x.asDoubleArray(); // Make prediction for Close value ------------------------ builder = new StringBuilder("inputData <- c("); for (int index = 0; index < flatStockInfo.length; index++) { builder.append(flatStockInfo[index].volume); if (index != flatStockInfo.length - 1) { builder.append(","); } else { builder.append(")"); } } stringFromBuilder = builder.toString(); re.eval(stringFromBuilder); // Create time series from data re.eval("temporalData <- ts(inputData, frequency=365)"); // Forecast data re.eval("forecastData <- forecast(temporalData, h=30)"); // re.eval("arimaModel <- auto.arima(temporalData, max.p=5, max.q=5, max.P=5, max.Q=5)"); // re.eval("forecastData <- forecast(arimaModel, h=30)"); x = re.eval("forecastData"); v = x.asVector(); x = (REXP) v.elementAt(1); resultArrayVolume = x.asDoubleArray(); // Create a single StockInfo[] for all data StockInfo predictedData; predictedDataArray = new StockInfo[30]; Date lastDate = flatStockInfo[flatStockInfo.length - 1].date; Calendar c = Calendar.getInstance(); c.setTime(lastDate); c.add(Calendar.DATE, 1); logger.log("<R> values for forecasted data"); SimpleDateFormat dateFormat = new SimpleDateFormat(); dateFormat.applyPattern("dd/MM/YYYY"); // For all days that were predicted for (int index = 0; index < 30; index++) { predictedData = new StockInfo(); predictedData.open = (float) resultArrayOpen[index]; float maxHigh = (float) StrictMath.max( resultArrayClose[index], StrictMath.max(resultArrayHigh[index], resultArrayOpen[index])); predictedData.high = maxHigh; float minLow = (float) StrictMath.min( resultArrayClose[index], StrictMath.min(resultArrayLow[index], resultArrayOpen[index])); predictedData.low = minLow; predictedData.close = (float) resultArrayClose[index]; predictedData.volume = (int) resultArrayVolume[index]; while (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { c.add(Calendar.DATE, 1); } predictedData.date = c.getTime(); predictedDataArray[index] = predictedData; logger.log( "<R> stock prediction " + dateFormat.format(predictedData.date.getTime()) + " open: " + predictedData.open + " high: " + predictedData.high + " low: " + predictedData.low + " close: " + predictedData.close); c.add(Calendar.DATE, 1); } } catch (Exception e) { logger.logException(e); } return predictedDataArray; }
public void retrieveNextDesign() { REXP expr = runCommand("colnames(inputConfig$alg.currentDesign)", true); paramIds = expr.asStringArray(); }