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

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

Introduction

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

Prototype

public long getN() 

Source Link

Document

Returns the number of observations that have been added to the model.

Usage

From source file:de.qaware.chronix.timeseries.RegressionTest.java

@Test
public void testRegression() {

    SimpleRegression regression = new SimpleRegression();
    regression.addData(0.0, 1.0);/*w  w w. j av a  2s  . c  om*/
    regression.addData(1.0, 2.5);
    regression.addData(2.0, 3.0);

    double slope = regression.getSlope();
    double intercept = regression.getIntercept();
    long n = regression.getN();
    double err = regression.getMeanSquareError();
}

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

@Override
public IntensityMatrix doCorrection(IntensityMatrix original) {
    updateBadInjections(original);/*  www .j  a v  a 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;
}

From source file:mase.mason.generic.systematic.SystematicEvaluator.java

@Override
protected void postSimulation(MasonSimState sim) {
    if (timeMode == TimeMode.mean) {
        // Make averages
        double[] res = new double[size + 1];
        for (int i = 0; i < size; i++) {
            List<Double> featureSample = features.get(i);
            double sum = 0;
            int count = 0;
            for (Double d : featureSample) {
                if (!Double.isNaN(d)) {
                    sum += d;/*www  .j a  va2s  . c o  m*/
                    count++;
                }
            }
            res[i] = (double) (count == 0 ? 0 : sum / count);
        }
        res[size] = sim.schedule.getSteps();
        vbr = new SystematicResult(res);
    } else if (timeMode == TimeMode.last) {
        double[] res = new double[size + 1];
        for (int i = 0; i < size; i++) {
            List<Double> featureSample = features.get(i);
            double last = 0;
            for (Double d : featureSample) {
                if (!Double.isNaN(d)) {
                    last = d;
                }
            }
            res[i] = (double) last;
        }
        res[size] = sim.schedule.getSteps();
        vbr = new SystematicResult(res);
    } else if (timeMode == TimeMode.meanlast) {
        double[] res = new double[size * 2 + 1];
        for (int i = 0; i < size; i++) {
            List<Double> featureSample = features.get(i);
            double last = 0;
            double sum = 0;
            int count = 0;
            for (Double d : featureSample) {
                if (!Double.isNaN(d)) {
                    sum += d;
                    count++;
                    last = d;
                }
            }
            res[i * 2] = (double) (count == 0 ? 0 : sum / count);
            res[i * 2 + 1] = (double) last;
        }
        res[size * 2] = sim.schedule.getSteps();
        vbr = new SystematicResult(res);
    } else if (timeMode == TimeMode.simplereg || timeMode == TimeMode.meanslope) {
        SimpleRegression reg = new SimpleRegression(true);
        double[] res = new double[size * 2 + 1];
        for (int i = 0; i < size; i++) {
            reg.clear();
            double sum = 0;
            List<Double> featureSample = features.get(i);
            for (int j = 0; j < featureSample.size(); j++) {
                double d = featureSample.get(j);
                if (!Double.isNaN(d)) {
                    reg.addData(j, d);
                    sum += d;
                }
            }

            if (timeMode == TimeMode.simplereg) {
                res[i * 2] = (double) (reg.getN() >= 2 ? reg.getIntercept() : 0);
            } else if (timeMode == TimeMode.meanslope) {
                res[i * 2] = (double) (reg.getN() > 0 ? sum / reg.getN() : 0);
            }
            res[i * 2 + 1] = (double) (reg.getN() >= 2 ? reg.getSlope() : 0);
        }
        res[size * 2] = sim.schedule.getSteps();
        vbr = new SystematicResult(res);
    } else if (timeMode == TimeMode.frames) {
        int len = features.get(0).size();
        int frameLen = (int) Math.ceil((double) len / timeFrames);

        // DEBUG
        for (int i = 0; i < size; i++) {
            if (features.get(i).size() != len) {
                System.out.println(
                        "DIFFERENT FEATURE LENGTHS. Expected: " + len + " Got: " + features.get(i).size());
                for (List<Double> sample : features) {
                    System.out.print(sample.size() + " ");
                }
                System.out.println();
            }
        }

        double[] res = new double[size * timeFrames + 1];
        for (int t = 0; t < timeFrames; t++) {
            for (int i = 0; i < size; i++) {
                double sum = 0;
                int count = 0;
                for (int x = t * frameLen; x < Math.min(len, (t + 1) * frameLen); x++) {
                    double d = features.get(i).get(x);
                    if (!Double.isNaN(d)) {
                        sum += d;
                        count++;
                    }
                }
                res[t * size + i] = (double) (count == 0 ? 0 : sum / count);
            }
        }
        res[size * timeFrames] = features.get(0).size();
        vbr = new SystematicResult(res);
    }
}

From source file:edu.ucsc.barrel.cdf_gen.ExtractTiming.java

public void fillModels() {
    int last_rec = 0, frame_i = 0, size_1Hz;
    long last_frame;
    SimpleRegression fit = null, new_fit = null;

    size_1Hz = data.getSize("1Hz");
    //create a model for each batch of time records
    for (int first_rec = 0; first_rec < time_rec_cnt; first_rec = last_rec) {
        //incriment the last_rec by the max, or however many recs are left
        last_rec += Math.min(MAX_RECS, (time_rec_cnt - first_rec));

        //try to generate a model
        new_fit = genModel(first_rec, last_rec);

        //Need to add better criteria than this for accepting a new model
        if (new_fit != null) {
            fit = new_fit;

            models[model_cnt] = new LinModel();
            models[model_cnt].setSlope(fit.getSlope());
            models[model_cnt].setIntercept(fit.getIntercept());
            models[model_cnt].setFirst(time_recs[first_rec].getFrame());
            models[model_cnt].setLast(time_recs[last_rec - 1].getFrame());
            model_cnt++;/*from   w  w  w.  jav a2 s  .  c o m*/

            System.out.println(
                    "Frames " + time_recs[first_rec].getFrame() + " - " + time_recs[last_rec - 1].getFrame());
            System.out.println("\tm = " + fit.getSlope() + ", b = " + fit.getIntercept() + " slope error = "
                    + fit.getSlopeStdErr() + " n = " + fit.getN());
        } else {
            System.out.println("Failed to get model using " + (last_rec - first_rec) + " records.");
        }
    }

    if (fit == null) {
        //no timing model was ever created. 
        //Use slope=1000 and intercept=0 to use frame number epoch.
        //this will clearly not give a good result for time, but will
        //allow the data to be plotted as a time series.
        //This will be a place to add a quality flag
        models[model_cnt] = new LinModel();
        models[model_cnt].setSlope(1000);
        models[model_cnt].setIntercept(0);
        models[model_cnt].setFirst(0);
        models[model_cnt].setLast(data.frame_1Hz[size_1Hz]);
        model_cnt = 1;
    }
}

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

@Override
public Object doWork(Object first, Object second) throws IOException {
    if (null == first) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }// w  w w  . j  av a2  s.  com
    if (null == second) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));
    }
    if (!(first instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the first value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }
    if (!(second instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the second value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    List<?> l1 = (List<?>) first;
    List<?> l2 = (List<?>) second;

    if (l2.size() < l1.size()) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - first list (%d) has more values than the second list (%d)",
                toExpression(constructingFactory), l1.size(), l2.size()));
    }

    SimpleRegression regression = new SimpleRegression();
    for (int idx = 0; idx < l1.size(); ++idx) {
        regression.addData(((BigDecimal) l1.get(idx)).doubleValue(), ((BigDecimal) l2.get(idx)).doubleValue());
    }

    Map<String, Number> map = new HashMap<>();
    map.put("slope", regression.getSlope());
    map.put("intercept", regression.getIntercept());
    map.put("R", regression.getR());
    map.put("N", regression.getN());
    map.put("RSquare", regression.getRSquare());
    map.put("regressionSumSquares", regression.getRegressionSumSquares());
    map.put("slopeConfidenceInterval", regression.getSlopeConfidenceInterval());
    map.put("interceptStdErr", regression.getInterceptStdErr());
    map.put("totalSumSquares", regression.getTotalSumSquares());
    map.put("significance", regression.getSignificance());
    map.put("meanSquareError", regression.getMeanSquareError());

    return new RegressionTuple(regression, map);
}

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

public Tuple evaluate(Tuple tuple) throws IOException {

    if (subEvaluators.size() != 2) {
        throw new IOException("Regress expects 2 columns as parameters");
    }//from w ww. j  a  v  a  2s  .  c o  m

    StreamEvaluator colEval1 = subEvaluators.get(0);
    StreamEvaluator colEval2 = subEvaluators.get(1);

    List<Number> numbers1 = (List<Number>) colEval1.evaluate(tuple);
    List<Number> numbers2 = (List<Number>) colEval2.evaluate(tuple);
    double[] column1 = new double[numbers1.size()];
    double[] column2 = new double[numbers2.size()];

    for (int i = 0; i < numbers1.size(); i++) {
        column1[i] = numbers1.get(i).doubleValue();
    }

    for (int i = 0; i < numbers2.size(); i++) {
        column2[i] = numbers2.get(i).doubleValue();
    }

    SimpleRegression regression = new SimpleRegression();
    for (int i = 0; i < column1.length; i++) {
        regression.addData(column1[i], column2[i]);
    }

    Map map = new HashMap();
    map.put("slope", regression.getSlope());
    map.put("intercept", regression.getIntercept());
    map.put("R", regression.getR());
    map.put("N", regression.getN());
    map.put("regressionSumSquares", regression.getRegressionSumSquares());
    map.put("slopeConfidenceInterval", regression.getSlopeConfidenceInterval());
    map.put("interceptStdErr", regression.getInterceptStdErr());
    map.put("totalSumSquares", regression.getTotalSumSquares());
    map.put("significance", regression.getSignificance());
    map.put("meanSquareError", regression.getMeanSquareError());
    return new RegressionTuple(regression, map);
}