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

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

Introduction

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

Prototype

public Covariance(RealMatrix matrix) 

Source Link

Document

Create a covariance matrix from a matrix whose columns represent covariates.

Usage

From source file:guineu.modules.dataanalysis.correlations.CorrelationTask.java

@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);/*w w  w  .j  av 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);
    }
}