Example usage for org.apache.commons.math.stat.correlation PearsonsCorrelation PearsonsCorrelation

List of usage examples for org.apache.commons.math.stat.correlation PearsonsCorrelation PearsonsCorrelation

Introduction

In this page you can find the example usage for org.apache.commons.math.stat.correlation PearsonsCorrelation PearsonsCorrelation.

Prototype

public PearsonsCorrelation(Covariance covariance) 

Source Link

Document

Create a PearsonsCorrelation from a Covariance .

Usage

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  ww w.  java  2s  . c  o  m*/

        //            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);//from w w  w .ja va2 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);
    }
}