Java Code Examples for org.apache.commons.math3.stat.regression.SimpleRegression#getIntercept()

The following examples show how to use org.apache.commons.math3.stat.regression.SimpleRegression#getIntercept() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Example1.java    From Java-Data-Analysis with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    SimpleRegression sr = getData("data/Data1.dat");
    double m = sr.getSlope();
    double b = sr.getIntercept();
    double r = sr.getR();  // correlation coefficient
    double r2 = sr.getRSquare();
    double sse = sr.getSumSquaredErrors();
    double tss = sr.getTotalSumSquares();

    System.out.printf("y = %.6fx + %.4f%n", m, b);
    System.out.printf("r = %.6f%n", r);
    System.out.printf("r2 = %.6f%n", r2);
    System.out.printf("EV = %.5f%n", tss - sse);
    System.out.printf("UV = %.4f%n", sse);
    System.out.printf("TV = %.3f%n", tss);
}
 
Example 2
Source File: Calibration.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
public double getSlope(BufferedImage bi1, BufferedImage bi2, int u, int v,  int s, int n) throws IOException {
   
    Raster r1 = bi1.getRaster().createTranslatedChild(0,0);
    Raster r2 = bi2.getRaster().createTranslatedChild(0,0);
    if (r1.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    if (r2.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    SimpleRegression reg = new SimpleRegression(true);
    int minX = u<0?u*-1:0;
    int minY = v<0?v*-1:0;
    int maxX = u>0?bi1.getWidth()-u: bi1.getWidth();
    int maxY = v>0?bi1.getHeight()-v: bi1.getHeight();
    for (int x=minX; x<maxX; x++) {
         for (int y=minY; y<maxY; y++) {
             double d1 = r1.getSampleDouble(x+u,y+v,0);
             if (d1> intensityThreshold) {
                 double d2 = r2.getSampleDouble(x, y, 0);
                 reg.addData(d2, d1);
             }
         }
     }

    double slope = reg.getSlope();
    double intercept = reg.getIntercept();
    logger.info("i,j: "+s+","+n+": "+ "slope: "+slope+" ; intercept: "+intercept);
    return slope;
}
 
Example 3
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 4
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 5
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 6
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 7
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 8
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 9
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 10
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 11
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 12
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 13
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 14
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 15
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 16
Source File: OLSTests.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the Morpheus OLS model yields the same results as Apache Math
 * @param frame      the data for regression
 * @param actual    the Morpheus results
 * @param expected  the Apache results
 */
private <R> void assertResultsMatch(DataFrame<String,String> frame, DataFrameLeastSquares<String,String> actual, SimpleRegression expected) {

    final double tss1 = actual.getTotalSumOfSquares();
    final double tss2 = expected.getTotalSumSquares();
    final double threshold = ((tss1 + tss2) / 2d) * 0.00001d;

    Assert.assertEquals(actual.getTotalSumOfSquares(), expected.getTotalSumSquares(), threshold, "Total sum of squares matches");
    Assert.assertEquals(actual.getRSquared(), expected.getRSquare(), 0.0000001, "R^2 values match");

    final double beta1 = actual.getBetaValue("X", Field.PARAMETER);
    final double beta2 = expected.getSlope();
    Assert.assertEquals(beta1, beta2, 0.000001, "Beta parameters match");

    final double intercept = expected.getIntercept();
    final double interceptStdError = expected.getInterceptStdErr();
    Assert.assertEquals(actual.getInterceptValue(Field.PARAMETER), intercept,  0.0000001, "The intercepts match");
    Assert.assertEquals(actual.getInterceptValue(Field.STD_ERROR), interceptStdError, 0.000000001, "The intercept std errors match");

    final double betaStdErr1 = actual.getBetaValue("X", Field.STD_ERROR);
    final double betaStdErr2 = expected.getSlopeStdErr();
    Assert.assertEquals(betaStdErr1, betaStdErr2, 0.00000001, "Beta Standard errors match");

    final DataFrame<String,String> residuals = actual.getResiduals();
    Assert.assertEquals(residuals.rows().count(), frame.rows().count(), "There are expected number of residuals");
    residuals.rows().forEach(row -> {
        final double x = frame.data().getDouble(row.ordinal(), "X");
        final double y = frame.data().getDouble(row.ordinal(), "Y");
        final double residual = row.getDouble(0);
        final double expect = y - expected.predict(x);
        Assert.assertEquals(residual, expect, 0.0000001, "Residual matches for x=" + x + " at row index " + row.ordinal());
    });
}
 
Example 17
Source File: TestDoubleRegrInterceptAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
private void testNonTrivialAggregation(Double[] y, Double[] x)
{
    SimpleRegression regression = new SimpleRegression();
    for (int i = 0; i < x.length; i++) {
        regression.addData(x[i], y[i]);
    }
    double expected = regression.getIntercept();
    checkArgument(Double.isFinite(expected) && expected != 0., "Expected result is trivial");
    testAggregation(expected, createDoublesBlock(y), createDoublesBlock(x));
}
 
Example 18
Source File: TestDoubleRegrInterceptAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Object getExpectedValue(int start, int length)
{
    if (length <= 1) {
        return null;
    }
    SimpleRegression regression = new SimpleRegression();
    for (int i = start; i < start + length; i++) {
        regression.addData(i + 2, i);
    }
    return regression.getIntercept();
}
 
Example 19
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 20
Source File: CTSlinearRegression.java    From systemsgenetics with GNU General Public License v3.0 4 votes vote down vote up
public CTSlinearRegression(ArrayList<IndividualSnpData> all_individuals) {
   
    //basic information, get the zero instance.
    snpName = all_individuals.get(0).getSnpName();
    chromosome = all_individuals.get(0).getChromosome();
    position = all_individuals.get(0).getPosition();
    
    //isolate heterozygotes
    ArrayList<IndividualSnpData> het_individuals = UtilityMethods.isolateValidHeterozygotesFromIndividualSnpData(all_individuals);
    numberOfHets = het_individuals.size();
    
            hetSampleNames = new ArrayList<String>();
    asRef = new ArrayList<Integer>(); 
    asAlt = new ArrayList<Integer>(); 
    asNo  = new ArrayList<Integer>(); 
    
  
    cellProp   = new ArrayList<Double>();
    int total_overlap = 0;
    
    //Get the basic data without doing any tests.
    for (IndividualSnpData temp_het : het_individuals) {
        //Do nothing if there is no data in het_individuals

        hetSampleNames.add(temp_het.getSampleName());
        
        asRef.add(temp_het.getRefNum());
        asAlt.add(temp_het.getAltNum());
        asNo.add(temp_het.getNoNum());
        cellProp.add(temp_het.getCellTypeProp());
        
        //this is used to check if we will continue with calculations.
        //BASED on the minHets and MinReads
        total_overlap += temp_het.getRefNum() + temp_het.getAltNum();
    }
    
    
    //Check if we do a test.
    if((total_overlap >= GlobalVariables.minReads) && (numberOfHets >= GlobalVariables.minHets) && (numberOfHets >= 3)){
        
        ASScatterPlot plotThis = null;
        
        if(!GlobalVariables.plotDir.equals("")){
            plotThis = new ASScatterPlot(400);
        }
        
        
        SimpleRegression thisRegression = new SimpleRegression();
        for(int i=0; i< asRef.size(); i++ ){
            Double asRatio;
            //do this check, otherwise the denominator will be zero.
            
            if(asRef.get(i) != 0){
                asRatio = ((double)asRef.get(i)) / ((double)(asRef.get(i) + asAlt.get(i)));
            }else{
                asRatio = 0.0;
            }
            
            Double phenoRatio =  cellProp.get(i);
            thisRegression.addData(phenoRatio, asRatio);
            if(!GlobalVariables.plotDir.equals("")){
                plotThis.plot(asRatio, phenoRatio);
            }
        }
        
        if(!GlobalVariables.plotDir.equals("")){
            plotThis.draw(GlobalVariables.plotDir + "/" + snpName +  "_ASratio_Pheno_Plot.png");
        } 
        
        slope = thisRegression.getSlope();
        intercept  = thisRegression.getIntercept();
        Rsquared = thisRegression.getRSquare();
        stdErrorIntercept = thisRegression.getInterceptStdErr();
        stdErrorSlope = thisRegression.getSlopeStdErr();
        
        pValue = thisRegression.getSignificance();
        
        
        if(GlobalVariables.verbosity >= 10){
                System.out.println("\n--- Starting cell type specific linear regression ---");
                System.out.println("\tSlope:                   " + Double.toString(slope));
                System.out.println("\tStdError of Slope:       " + Double.toString(stdErrorSlope) + "\n");
                System.out.println("\tIntercept:               " + Double.toString(intercept));
                System.out.println("\tStdError of Intercept:   " + Double.toString(stdErrorIntercept) + "\n");   
                System.out.println("\tP value:                 " + Double.toString(pValue));
                System.out.println("--------------------------------------------------------------");
                
        }
        
        
        testPerformed = true;
    }
    
}