Example usage for org.apache.commons.math3.stat.regression SimpleRegression regress

List of usage examples for org.apache.commons.math3.stat.regression SimpleRegression regress

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.regression SimpleRegression regress.

Prototype

public RegressionResults regress() throws ModelSpecificationException, NoDataException 

Source Link

Document

Performs a regression on data present in buffers and outputs a RegressionResults object.

Usage

From source file:IBDMUT.Tools.java

public static double[] ApacheRegression(double[][] x, double[] y) {
    if (x.length < 2) {
        //            Tools.warning("******************************************************");
        //            Tools.warning("******************************************************");
        //            Tools.warning("******************************************************");
        //            Tools.warning("Trying to run regression with " + x.length + " points.");
        //            Tools.warning("******************************************************");
        //            Tools.warning("******************************************************");
        //            Tools.warning("******************************************************");
        exit("Trying to run regression with " + x.length + " points.");
    }/*from w w w  .  j  a  v a 2  s .co  m*/
    if (Tools.loggerIsOn()) {
        Tools.writeLog("Regression");
        Tools.writeLog("\tx\ty");
        for (int i = 0; i < x.length; i++) {
            Tools.writeLog("\t" + x[i][0] + "\t" + y[i]);
        }
    }
    SimpleRegression reg = new SimpleRegression(true);
    reg.addObservations(x, y);
    RegressionResults regRes = reg.regress();
    double[] regResValues = regRes.getParameterEstimates();
    double intercept = regResValues[0];
    double slope = regResValues[1];
    return new double[] { intercept, slope, regRes.getRSquared() };
}

From source file:jp.ac.tohoku.ecei.sb.metabolomeqc.basiccorrector.helper.RegressionTest.java

@Test
public void testRegression() {
    SimpleRegression simpleRegression = new SimpleRegression();
    simpleRegression.addData(0.0, 1);//from   w w w.  ja v a 2s. c o m
    simpleRegression.addData(0.5, 2);
    simpleRegression.addData(1.0, 3);
    simpleRegression.addData(1.5, 4);
    RegressionResults result = simpleRegression.regress();
    Assert.assertArrayEquals(new double[] { 1, 2 }, result.getParameterEstimates(), 0);
}

From source file:com.act.analysis.surfactant.SurfactantAnalysis.java

/**
 * Perform linear regression over atoms' projection onto `lv` using their logP contributions as y-axis values.
 *
 * @return The slope of the regression line computed over the `lv`-projection.
 *//*from   w  ww.ja  v  a 2 s .c  o  m*/
public Double performRegressionOverLVProjectionOfLogP() {
    SimpleRegression regression = new SimpleRegression();
    for (int i = 0; i < mol.getAtomCount(); i++) {
        Double x = distancesAlongLongestVector.get(i);
        Double y = plugin.getAtomlogPIncrement(i);
        regression.addData(x, y);
    }
    regression.regress();
    return regression.getSlope();
}

From source file:com.act.analysis.surfactant.SurfactantAnalysis.java

/**
 * Perform linear regression over a list of X/Y coordinates
 * @param coords A set of coordinates over which to perform linear regression.
 * @return The slope and intercept of the regression line.
 *///w ww . ja  v  a 2s . com
public Pair<Double, Double> performRegressionOverXYPairs(List<Pair<Double, Double>> coords) {
    SimpleRegression regression = new SimpleRegression(true);
    for (Pair<Double, Double> c : coords) {
        regression.addData(c.getLeft(), c.getRight());
    }
    // Note: the regress() call can raise an exception for small molecules.  We should probably handle that gracefully.
    RegressionResults result = regression.regress();
    return Pair.of(regression.getSlope(), regression.getIntercept());
}

From source file:jp.ac.tohoku.ecei.sb.metabolomeqc.basiccorrector.RegressionIntensityCorrector.java

@Override
public IntensityMatrix doCorrection(IntensityMatrix original) {
    updateBadInjections(original);/* w w  w.j a  va  2  s. c o m*/
    // Make corrected intensity matrix
    List<Injection> correctedSamples = original.getNormalInjections();

    IntensityMatrix corrected = new IntensityMatrixImpl(original.getSize()[0], correctedSamples.size());
    corrected.setRowKeys(original.getRowKeys());
    corrected.setColumnKeys(correctedSamples);

    List<Sample> globalQCIndexes = original.getGlobalQCSamples();
    if (globalQCIndexes.size() == 0)
        throw new UnsupportedOperationException("No global QC");
    log.info("Global QC {}", globalQCIndexes);
    log.info("Bad injections {}", badInjections);

    // median of SQCs for compounds
    Map<Compound, Double> medianForCompounds = GlobalQCMedianCalculator.calcGlobalQCMedian(original,
            badInjections);

    // do correction
    Map<Plate, Map<Sample.SampleType, List<Injection>>> map = original.getInjectionsByPlateAndType();
    for (Map.Entry<Plate, Map<Sample.SampleType, List<Injection>>> oneRun : map.entrySet()) {
        Stream<CorrectionResult[]> oneresult = corrected.getRowKeys().parallelStream().map(oneCompound -> {
            SimpleRegression simpleRegression = new SimpleRegression();
            for (Injection oneSqc : oneRun.getValue().get(Sample.SampleType.QC)) {
                if (!oneSqc.getSample().equals(globalQCIndexes.get(0)))
                    continue; // skip non global QC
                if (badInjections.indexOf(oneSqc) != -1)
                    continue; // skip bad sample
                if (oneSqc.isIgnored())
                    continue; // skip ignored QCs
                simpleRegression.addData(oneSqc.getRunIndex(), original.get(oneCompound, oneSqc));
            }

            CorrectionResult[] resultArray = new CorrectionResult[oneRun.getValue()
                    .get(Sample.SampleType.NORMAL).size()];

            log.info("Simple Regression N : {}", simpleRegression.getN());

            if (simpleRegression.getN() < 3) {
                // Failed to correct
                int i = 0;
                for (Injection oneNormal : oneRun.getValue().get(Sample.SampleType.NORMAL)) {
                    //corrected.put(oneCompound, oneNormal, Double.NaN);
                    resultArray[i++] = new CorrectionResult(oneNormal, oneCompound, Double.NaN);
                }
            } else {
                RegressionResults result = simpleRegression.regress();
                double[] coefficients = result.getParameterEstimates();

                int i = 0;
                // correct
                for (Injection oneNormal : oneRun.getValue().get(Sample.SampleType.NORMAL)) {
                    double offset = coefficients[0] + coefficients[1] * oneNormal.getRunIndex()
                            - medianForCompounds.get(oneCompound);
                    //corrected.put(oneCompound, oneNormal, original.get(oneCompound, oneNormal) - offset);
                    resultArray[i++] = new CorrectionResult(oneNormal, oneCompound,
                            original.get(oneCompound, oneNormal) - offset);
                }
            }

            //log.info("resultArray: {} {}", oneRun, resultArray);

            return resultArray;
        });

        oneresult.forEachOrdered(correctionResultArray -> {
            for (CorrectionResult oneResult : correctionResultArray) {
                corrected.put(oneResult.getCompound(), oneResult.getSample(), oneResult.getValue());
            }
        });
    }

    return corrected;
}