List of usage examples for org.apache.commons.math.stat.correlation SpearmansCorrelation SpearmansCorrelation
public SpearmansCorrelation(final RealMatrix dataMatrix)
From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.svmlib.regression.SVMLibRegressionExperimentRunner.java
@Override public void runCrossValidation(File dataDir) throws IOException { Set<SinglePrediction> allPredictions = new HashSet<SinglePrediction>(); List<File> files = new ArrayList<File>(FileUtils.listFiles(dataDir, new String[] { "libsvm.txt" }, false)); for (File testFile : files) { // create training files from the rest Set<File> trainingFiles = new HashSet<File>(files); trainingFiles.remove(testFile);//from w w w . j av a 2s.c om // System.out.println("Training files: " + trainingFiles); System.out.println("Training files size: " + trainingFiles.size()); System.out.println("Test file: " + testFile); Set<SinglePrediction> predictions = runSingleFold(testFile, new ArrayList<File>(trainingFiles)); allPredictions.addAll(predictions); } List<SinglePrediction> predictions = new ArrayList<SinglePrediction>(allPredictions); double[][] matrix = new double[predictions.size()][]; for (int i = 0; i < predictions.size(); i++) { SinglePrediction prediction = predictions.get(i); matrix[i] = new double[2]; matrix[i][0] = Double.valueOf(prediction.getGold()); matrix[i][1] = Double.valueOf(prediction.getPrediction()); } PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation(matrix); try { double pValue = pearsonsCorrelation.getCorrelationPValues().getEntry(0, 1); double correlation = pearsonsCorrelation.getCorrelationMatrix().getEntry(0, 1); System.out.println("Correlation: " + correlation); System.out.println("p-Value: " + pValue); System.out.println("Samples: " + predictions.size()); SpearmansCorrelation sc = new SpearmansCorrelation(new Array2DRowRealMatrix(matrix)); double pValSC = sc.getRankCorrelation().getCorrelationPValues().getEntry(0, 1); double corrSC = sc.getCorrelationMatrix().getEntry(0, 1); System.out.println("Spearman: " + corrSC + ", p-Val " + pValSC); } catch (MathException e) { throw new IOException(e); } }
From source file:guineu.modules.dataanalysis.correlations.CorrelationTask.java
@Override public void run() { setStatus(TaskStatus.PROCESSING);// w ww . ja v a 2 s . c o m try { Dataset data1 = dataset[0]; Dataset data2 = null; if (dataset.length == 1) { data2 = dataset[0]; } else if (dataset.length > 1) { data2 = dataset[1]; } Dataset newDataset = new SimpleBasicDataset("Correlation matrix"); newDataset.addColumnName("ID"); // Column names from the first dataset for (PeakListRow row : data1.getRows()) { String name = row.getID() + " - " + row.getName(); newDataset.addColumnName(name); if (this.showPvalue) { newDataset.addColumnName(name + " - pValue"); } } // Row names from the second dataset for (int i = 0; i < data2.getNumberRows(); i++) { PeakListRow row = data2.getRow(i); PeakListRow newRow = new SimplePeakListRowOther(); newRow.setPeak("ID", row.getID() + " - " + row.getName()); newDataset.addRow(newRow); } for (PeakListRow row : data1.getRows()) { String name = row.getID() + " - " + row.getName(); String pValueName = name + " - pValue"; for (int i = 0; i < data2.getNumberRows(); i++) { RealMatrix matrix = getCorrelation(row, data2.getRow(i), data1.getAllColumnNames()); PearsonsCorrelation correlation = null; if (this.correlationType.contains("Pearsons")) { correlation = new PearsonsCorrelation(matrix); } else if (this.correlationType.contains("Spearmans")) { SpearmansCorrelation scorrelation = new SpearmansCorrelation(matrix); correlation = scorrelation.getRankCorrelation(); } if (correlation != null) { double c = correlation.getCorrelationMatrix().getEntry(1, 0); double p = correlation.getCorrelationPValues().getEntry(1, 0); String cp = String.valueOf(c); Color cell = Color.WHITE; if (p < cutoff) { if (c < 0) { cell = Color.green; } else { cell = Color.red; } } int pColumnIndex = 0; if (this.showPvalue) { newDataset.getRow(i).setPeak(pValueName, String.valueOf(p)); pColumnIndex = newDataset.getAllColumnNames().indexOf(pValueName) + 2; newDataset.setCellColor(cell, i, pColumnIndex); } pColumnIndex = newDataset.getAllColumnNames().indexOf(name) + 2; newDataset.getRow(i).setPeak(name, cp); newDataset.setCellColor(cell, i, pColumnIndex); } if (this.correlationType.contains("Covariance")) { Covariance covariance = new Covariance(matrix); double c = covariance.getCovarianceMatrix().getEntry(1, 0); newDataset.getRow(i).setPeak(name, String.valueOf(c)); } } } GUIUtils.showNewTable(newDataset, true); setStatus(TaskStatus.FINISHED); } catch (Exception ex) { Logger.getLogger(CorrelationTask.class.getName()).log(Level.SEVERE, null, ex); setStatus(TaskStatus.ERROR); } }
From source file:guineu.modules.filter.Alignment.SerumHuNormalization.SerumHuNormalizationTask.java
private void searchBestFunction(Dataset data, PeakListRow comparedRow, int numBatches) { double bestValue = 0; int bestID = -1; List<String> names = data.getAllColumnNames(); for (int i = 0; i < data.getNumberRows(); i++) { PeakListRow row = data.getRow(i); RealMatrix m = this.getMatrix(comparedRow, row, names); if (m != null && m.getData().length > 3) { SpearmansCorrelation scorrelation = new SpearmansCorrelation(m); PearsonsCorrelation correlation = scorrelation.getRankCorrelation(); double value = correlation.getCorrelationMatrix().getEntry(1, 0); if (value > bestValue && functions.get(row.getID()) != null && row.getID() != comparedRow.getID()) { List<PolynomialSplineFunction> functionList = functions.get(row.getID()); if (functionList.size() == numBatches) { bestValue = value;//from www.ja va2s. co m bestID = row.getID(); } } } } if (bestID > -1) { List<PolynomialSplineFunction> functionList = functions.get(bestID); functions.put(comparedRow.getID(), functionList); } this.correlated.put(comparedRow.getID(), bestID); }