Example usage for org.apache.commons.math3.stat.regression RegressionResults getParameterEstimates

List of usage examples for org.apache.commons.math3.stat.regression RegressionResults getParameterEstimates

Introduction

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

Prototype

public double[] getParameterEstimates() 

Source Link

Document

Returns a copy of the regression parameters estimates.

The parameter estimates are returned in the natural order of the data.

A redundant regressor will have its redundancy flag set, as will a parameter estimate equal to Double.NaN .

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  va  2  s. c o  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  .  j  ava 2 s.  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:jp.ac.tohoku.ecei.sb.metabolomeqc.basiccorrector.RegressionIntensityCorrector.java

@Override
public IntensityMatrix doCorrection(IntensityMatrix original) {
    updateBadInjections(original);//w ww  .j  av a  2s  . 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;
}