Example usage for org.apache.commons.math3.stat.ranking NaturalRanking NaturalRanking

List of usage examples for org.apache.commons.math3.stat.ranking NaturalRanking NaturalRanking

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.ranking NaturalRanking NaturalRanking.

Prototype

public NaturalRanking() 

Source Link

Document

Create a NaturalRanking with default strategies for handling ties and NaNs.

Usage

From source file:nl.systemsgenetics.eqtlinteractionanalyser.eqtlinteractionanalyser.TestEQTLDatasetForInteractions.java

private ExpressionDataset correctCovariateDataPCA(String[] covsToCorrect2, String[] covsToCorrect,
        ExpressionDataset datasetGenotypes, ExpressionDataset datasetCovariatesPCAForceNormal)
        throws Exception {

    int nrCompsToCorrectFor = 25;

    System.out.println("Preparing data for testing eQTL effects of SNPs on covariate data:");
    System.out.println("Correcting covariate data for cohort specific effects:");

    ExpressionDataset datasetCovariatesToCorrectFor = new ExpressionDataset(
            covsToCorrect2.length + covsToCorrect.length + nrCompsToCorrectFor, datasetGenotypes.nrSamples);
    datasetCovariatesToCorrectFor.sampleNames = datasetGenotypes.sampleNames;

    // add covariates from the first list
    HashMap hashCovsToCorrect = new HashMap();

    // add covariates from the second list
    for (int i = 0; i < covsToCorrect2.length; ++i) {
        String cov = covsToCorrect2[i];
        hashCovsToCorrect.put(cov, null);
        Integer c = datasetCovariatesPCAForceNormal.hashProbes.get(cov);
        if (c == null) {
            throw new Exception("Covariate not found: " + cov);
        }/*from  w  ww .j a  v a  2  s  .  c o m*/
        for (int s = 0; s < datasetGenotypes.nrSamples; s++) {
            datasetCovariatesToCorrectFor.rawData[i][s] = datasetCovariatesPCAForceNormal.rawData[c][s];
        }
    }

    int[] covsToCorrectIndex = new int[covsToCorrect.length];
    for (int c = 0; c < covsToCorrect.length; c++) {
        hashCovsToCorrect.put(covsToCorrect[c], null);
        covsToCorrectIndex[c] = ((Integer) datasetCovariatesPCAForceNormal.hashProbes.get(covsToCorrect[c]))
                .intValue();
        for (int s = 0; s < datasetGenotypes.nrSamples; s++) {
            datasetCovariatesToCorrectFor.rawData[covsToCorrect2.length
                    + c][s] = datasetCovariatesPCAForceNormal.rawData[covsToCorrectIndex[c]][s];
        }
    }

    // add PCs
    if (nrCompsToCorrectFor > 0) {
        for (int comp = 0; comp < nrCompsToCorrectFor; comp++) {
            for (int s = 0; s < datasetGenotypes.nrSamples; s++) {
                datasetCovariatesToCorrectFor.rawData[covsToCorrect2.length + covsToCorrect.length
                        + comp][s] = datasetCovariatesPCAForceNormal.rawData[datasetCovariatesPCAForceNormal.nrProbes
                                - 51 + comp][s];
            }
        }
    }

    datasetCovariatesToCorrectFor.transposeDataset();

    datasetCovariatesToCorrectFor.save(inputDir + "/CovariatesToCorrectFor.txt");
    orthogonalizeDataset(inputDir + "/CovariatesToCorrectFor.txt");
    datasetCovariatesToCorrectFor = new ExpressionDataset(
            inputDir + "/CovariatesToCorrectFor.txt.PrincipalComponents.txt");
    datasetCovariatesToCorrectFor.transposeDataset();
    ExpressionDataset datasetCovariatesToCorrectForEigenvalues = new ExpressionDataset(
            inputDir + "/CovariatesToCorrectFor.txt.Eigenvalues.txt");

    for (int p = 0; p < datasetCovariatesPCAForceNormal.nrProbes; p++) {
        if (!hashCovsToCorrect.containsKey(datasetCovariatesPCAForceNormal.probeNames[p])) {
            for (int cov = 0; cov < datasetCovariatesToCorrectFor.nrProbes; cov++) {
                if (datasetCovariatesToCorrectForEigenvalues.rawData[cov][0] > 1E-5) {
                    double[] rc = getLinearRegressionCoefficients(datasetCovariatesToCorrectFor.rawData[cov],
                            datasetCovariatesPCAForceNormal.rawData[p]);
                    for (int s = 0; s < datasetGenotypes.nrSamples; s++) {
                        datasetCovariatesPCAForceNormal.rawData[p][s] -= rc[0]
                                * datasetCovariatesToCorrectFor.rawData[cov][s];
                    }
                }
            }
            /*double stdev = JSci.maths.ArrayMath.standardDeviation(datasetCovariates.rawData[p]);
             double mean = JSci.maths.ArrayMath.mean(datasetCovariates.rawData[p]);
             if (stdev < 1E-5) {
             for (int s = 0; s < datasetGenotypes.nrSamples; s++) {
             datasetCovariatesPCAForceNormal.rawData[p][s] = mean;
             }
             }*/
        }
    }

    System.out.println("Enforcing normal distribution on covariates");

    NaturalRanking ranker = new NaturalRanking();

    for (int p = 0; p < datasetCovariatesPCAForceNormal.nrProbes; p++) {
        //Rank order the expression values:
        double[] values = new double[datasetCovariatesPCAForceNormal.nrSamples];
        for (int s = 0; s < datasetGenotypes.nrSamples; s++) {
            values[s] = datasetCovariatesPCAForceNormal.rawData[p][s];
        }
        double[] rankedValues = ranker.rank(values);
        //Replace the original expression value with the standard distribution enforce:
        for (int s = 0; s < datasetGenotypes.nrSamples; s++) {
            //Convert the rank to a proportion, with range <0, 1>
            double pValue = (0.5d + rankedValues[s] - 1d) / (double) (rankedValues.length);
            //Convert the pValue to a Z-Score:
            double zScore = cern.jet.stat.tdouble.Probability.normalInverse(pValue);
            datasetCovariatesPCAForceNormal.rawData[p][s] = zScore; //Replace original expression value with the Z-Score
        }
    }
    return datasetCovariatesPCAForceNormal;
}

From source file:nl.systemsgenetics.eqtlinteractionanalyser.eqtlinteractionanalyser.TestEQTLDatasetForInteractions.java

private void forceNormalCovariates(ExpressionDataset datasetCovariates, ExpressionDataset datasetGenotypes)
        throws ArithmeticException {
    System.out.println("Enforcing normal distribution on covariates");

    NaturalRanking ranker = new NaturalRanking();

    for (int p = 0; p < datasetCovariates.nrProbes; p++) {
        //Rank order the expression values:
        double[] values = new double[datasetCovariates.nrSamples];
        for (int s = 0; s < datasetGenotypes.nrSamples; s++) {
            values[s] = datasetCovariates.rawData[p][s];
        }/*from w  w w.jav a2  s .  c  o  m*/
        double[] rankedValues = ranker.rank(values);
        //Replace the original expression value with the standard distribution enforce:
        for (int s = 0; s < datasetGenotypes.nrSamples; s++) {
            //Convert the rank to a proportion, with range <0, 1>
            double pValue = (0.5d + rankedValues[s] - 1d) / (double) (rankedValues.length);
            //Convert the pValue to a Z-Score:
            double zScore = cern.jet.stat.tdouble.Probability.normalInverse(pValue);
            datasetCovariates.rawData[p][s] = zScore; //Replace original expression value with the Z-Score
        }
    }
}

From source file:nl.systemsgenetics.eqtlinteractionanalyser.eqtlinteractionanalyser.TestEQTLDatasetForInteractions.java

private void forceNormalExpressionData(ExpressionDataset datasetExpression) throws ArithmeticException {
    System.out.println("Enforcing normal distribution on expression data:");

    NaturalRanking ranker = new NaturalRanking();

    for (int p = 0; p < datasetExpression.nrProbes; p++) {
        //Rank order the expression values:
        double[] values = new double[datasetExpression.nrSamples];
        for (int s = 0; s < datasetExpression.nrSamples; s++) {
            values[s] = datasetExpression.rawData[p][s];
        }/*w  w  w.  j  a v  a 2 s.  com*/

        double[] rankedValues = ranker.rank(values);
        //Replace the original expression value with the standard distribution enforce:
        for (int s = 0; s < datasetExpression.nrSamples; s++) {
            //Convert the rank to a proportion, with range <0, 1>
            double pValue = (0.5d + rankedValues[s] - 1d) / (double) (rankedValues.length);
            //Convert the pValue to a Z-Score:
            double zScore = cern.jet.stat.tdouble.Probability.normalInverse(pValue);
            datasetExpression.rawData[p][s] = zScore; //Replace original expression value with the Z-Score
        }
    }

    System.out.println("Expression data now force normal");
}

From source file:org.apache.solr.client.solrj.io.eval.RankEvaluator.java

@Override
public Object doWork(Object value) {
    if (null == value) {
        return null;
    } else if (value instanceof List) {
        NaturalRanking rank = new NaturalRanking();
        return Arrays
                .stream(rank.rank(((List<?>) value).stream()
                        .mapToDouble(innerValue -> ((Number) innerValue).doubleValue()).toArray()))
                .mapToObj(Double::new).collect(Collectors.toList());
    } else {/*from   w ww .  ja  va 2s  .c o  m*/
        return doWork(Arrays.asList((BigDecimal) value));
    }
}

From source file:org.apache.solr.client.solrj.io.stream.RankEvaluator.java

public List<Number> evaluate(Tuple tuple) throws IOException {
    StreamEvaluator colEval = subEvaluators.get(0);

    List<Number> numbers = (List<Number>) colEval.evaluate(tuple);
    double[] values = new double[numbers.size()];
    for (int i = 0; i < numbers.size(); i++) {
        values[i] = numbers.get(i).doubleValue();
    }/*from   ww  w  . j a v  a 2  s.c o  m*/

    NaturalRanking rank = new NaturalRanking();
    double[] ranked = rank.rank(values);
    List<Number> rankedList = new ArrayList();
    for (int i = 0; i < numbers.size(); i++) {
        rankedList.add(ranked[i]);
    }

    return rankedList;
}

From source file:sanger.team16.common.stats.SpearmansRank.java

/**
 * rankX represents sequence variations and rankY expression/methylation levels
 *//*from   www.j a va  2s .  c  o  m*/
public SpearmansRank(double[] x, double[] y) {
    n = x.length;
    rankX = new NaturalRanking().rank(x);
    rankY = new NaturalRanking().rank(y);
}

From source file:stats.SpearmansCorrelation.java

/**
 * Create a SpearmansCorrelation without data.
 */
public SpearmansCorrelation() {
    this(new NaturalRanking());
}

From source file:stats.SpearmansCorrelation.java

/**
 * Create a SpearmansCorrelation from the given data matrix.
 *
 * @param dataMatrix/*w w  w.  j a va2  s .co m*/
 *          matrix of data with columns representing variables to correlate
 */
public SpearmansCorrelation(final RealMatrix dataMatrix) {
    this(dataMatrix, new NaturalRanking());
}