Example usage for org.apache.commons.math3.stat.regression OLSMultipleLinearRegression estimateResiduals

List of usage examples for org.apache.commons.math3.stat.regression OLSMultipleLinearRegression estimateResiduals

Introduction

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

Prototype

public double[] estimateResiduals() 

Source Link

Usage

From source file:dase.timeseries.analysis.GrangerTest.java

/**
 * Returns p-value for Granger causality test.
 *
 * @param y/*ww  w.j a v a2 s  . co  m*/
 *            - predictable variable
 * @param x
 *            - predictor
 * @param L
 *            - lag, should be 1 or greater.
 * @return p-value of Granger causality
 */
public static double granger(double[] y, double[] x, int L) {
    OLSMultipleLinearRegression h0 = new OLSMultipleLinearRegression();
    OLSMultipleLinearRegression h1 = new OLSMultipleLinearRegression();

    double[][] laggedY = createLaggedSide(L, y);

    double[][] laggedXY = createLaggedSide(L, x, y);

    int n = laggedY.length;

    h0.newSampleData(strip(L, y), laggedY);
    h1.newSampleData(strip(L, y), laggedXY);

    double rs0[] = h0.estimateResiduals();
    double rs1[] = h1.estimateResiduals();

    double RSS0 = sqrSum(rs0);
    double RSS1 = sqrSum(rs1);

    double ftest = ((RSS0 - RSS1) / L) / (RSS1 / (n - 2 * L - 1));

    System.out.println(RSS0 + " " + RSS1);
    System.out.println("F-test " + ftest);

    FDistribution fDist = new FDistribution(L, n - 2 * L - 1);

    double pValue = 1.0 - fDist.cumulativeProbability(ftest);
    System.out.println("P-value " + pValue);
    return pValue;
}

From source file:modelcreation.ModelCreation.java

public static void printRegressionStatistics(OLSMultipleLinearRegression regression) {
    System.out.println("Adjusted R^2 = " + regression.calculateAdjustedRSquared());
    System.out.println("R^2 = " + regression.calculateRSquared());
    System.out.println("Residual Sum Of Squares = " + regression.calculateResidualSumOfSquares());
    System.out.println("Total Sum of Squares = " + regression.calculateTotalSumOfSquares());

    double[] standardErrors = regression.estimateRegressionParametersStandardErrors();
    double[] residuals = regression.estimateResiduals();
    double[] parameters = regression.estimateRegressionParameters();

    int residualdf = residuals.length - parameters.length;
    for (int i = 0; i < parameters.length; i++) {
        double coeff = parameters[i];
        double tstat = parameters[i] / regression.estimateRegressionParametersStandardErrors()[i];
        double pvalue = new TDistribution(residualdf).cumulativeProbability(-FastMath.abs(tstat)) * 2;

        System.out.println("Coefficient(" + i + ") : " + coeff);
        System.out.println("Standard Error(" + i + ") : " + standardErrors[i]);
        System.out.println("t-stats(" + i + ") : " + tstat);
        System.out.println("p-value(" + i + ") : " + pvalue);
    }/*from  w ww .  j  a  v a 2 s  .  c  o m*/
}

From source file:edu.cmu.tetrad.search.Lofs2.java

private double resolveOneEdgeMaxR3(double[] x, double[] y) {
    System.out.println("Resolving " + x + " === " + y);

    TetradLogger.getInstance().log("info", "\nEDGE " + x + " --- " + y);

    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    double[][] _x = new double[1][];
    _x[0] = x;//from   www. java  2  s . co m
    double[][] _y = new double[1][];
    _y[0] = y;

    regression.newSampleData(x, transpose(_y));
    double[] rXY = regression.estimateResiduals();

    regression.newSampleData(y, transpose(_x));
    double[] rYX = regression.estimateResiduals();

    double xPlus = new AndersonDarlingTest(rXY).getASquared();
    double xMinus = new AndersonDarlingTest(x).getASquared();
    double yPlus = new AndersonDarlingTest(rYX).getASquared();
    double yMinus = new AndersonDarlingTest(y).getASquared();

    double deltaX = xPlus - xMinus;
    double deltaY = yPlus - yMinus;

    return deltaX - deltaY;
}

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

private void correctCovariatesForQtls(ExpressionDataset datasetCovariates, ExpressionDataset datasetGenotypes,
        HashMultimap<String, String> qtlProbeSnpMultiMap) throws Exception {

    System.out.println("Correcting covariate data for cis-eQTL effects:");

    OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();

    HashMap<String, Integer> snpMap = new HashMap<String, Integer>(datasetGenotypes.nrProbes);
    for (Map.Entry<String, Integer> snpEntry : datasetGenotypes.hashProbes.entrySet()) {
        try {//from w ww .ja  va  2  s  .  co m
            snpMap.put(snpEntry.getKey().substring(0, snpEntry.getKey().indexOf('_')), snpEntry.getValue());
        } catch (Exception e) {
            System.out.println(snpEntry.getKey());
            throw e;
        }
    }

    for (int p = 0; p < datasetCovariates.nrProbes; p++) {

        String probe = datasetCovariates.probeNames[p];
        Set<String> probeQtls = qtlProbeSnpMultiMap.get(probe);

        //         System.out.println("");
        //         System.out.println("-------------------------------------");
        //         System.out.println("");
        //         System.out.println(probe + " with " + probeQtls.size() + " SNPs");
        //         System.out.println("");

        if (!probeQtls.isEmpty()) {

            int snpsInData = 0;
            for (String snp : probeQtls) {

                Integer s = snpMap.get(snp);
                if (s != null) {
                    ++snpsInData;
                }

            }

            double[][] x = new double[datasetCovariates.nrSamples][snpsInData];

            int k = 0;
            for (String snp : probeQtls) {

                Integer s = snpMap.get(snp);
                if (s == null) {
                    continue;
                    //throw new Exception("Snp " + snp + " not found");
                }
                double[] snpData = datasetGenotypes.rawData[s];
                for (int i = 0; i < datasetGenotypes.nrSamples; ++i) {
                    x[i][k] = snpData[i];
                }

                k++;
            }

            //            PearsonsCorrelation cor = new PearsonsCorrelation();

            //            System.out.println("Before");
            //            for(String snp : probeQtls){
            //               Integer s = snpMap.get(snp);
            //               System.out.println(snp + " - " + cor.correlation(datasetCovariates.rawData[p], datasetGenotypes.rawData[s]));   
            //            }

            ols.newSampleData(datasetCovariates.rawData[p], x);
            datasetCovariates.rawData[p] = ols.estimateResiduals();

            //            System.out.println("After");
            //            for(String snp : probeQtls){
            //               Integer s = snpMap.get(snp);
            //               System.out.println(snp + " - " + cor.correlation(datasetCovariates.rawData[p], datasetGenotypes.rawData[s]));   
            //            }

        }

    }

    //      for (int p = 0; p < datasetCovariates.nrProbes; p++) {
    //         if (datasetExpression.hashProbes.containsKey(datasetCovariates.probeNames[p])) {
    //            int index = ((Integer) datasetExpression.hashProbes.get(datasetCovariates.probeNames[p])).intValue();
    //            double[] rc = getLinearRegressionCoefficients(datasetGenotypes.rawData[index], datasetCovariates.rawData[p]);
    //            for (int s = 0; s < datasetGenotypes.nrSamples; s++) {
    //               datasetCovariates.rawData[p][s] -= rc[0] * datasetGenotypes.rawData[index][s];
    //            }
    //         }
    //      }

}

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

private void correctExpressionDataForInteractions(String[] covsToCorrect, ExpressionDataset datasetCovariates,
        ExpressionDataset datasetGenotypes, int nrSamples, ExpressionDataset datasetExpression,
        OLSMultipleLinearRegression regression, HashMultimap<String, String> qtlProbeSnpMultiMap)
        throws MathIllegalArgumentException, Exception {

    System.out.println("Correcting expression data for predefined gene environment interaction effects: "
            + Arrays.toString(covsToCorrect));
    int[] covsToCorrectIndex = new int[covsToCorrect.length];
    for (int c = 0; c < covsToCorrect.length; c++) {
        covsToCorrectIndex[c] = ((Integer) datasetCovariates.hashProbes.get(covsToCorrect[c])).intValue();

    }//from  w ww . ja va 2 s . c o m

    HashMap<String, Integer> snpMap = new HashMap<String, Integer>(datasetGenotypes.nrProbes);
    for (Map.Entry<String, Integer> snpEntry : datasetGenotypes.hashProbes.entrySet()) {
        try {
            snpMap.put(snpEntry.getKey().substring(0, snpEntry.getKey().indexOf('_')), snpEntry.getValue());
        } catch (Exception e) {
            System.out.println(snpEntry.getKey());
            throw e;
        }
    }

    Variance v = new Variance();

    for (int p = 0; p < datasetExpression.nrProbes; p++) {

        String probe = datasetExpression.probeNames[p].substring(0,
                datasetExpression.probeNames[p].lastIndexOf('_'));
        Set<String> probeQtls = qtlProbeSnpMultiMap.get(probe);

        if (probeQtls.isEmpty()) {
            throw new Exception("No eQTLs found for: " + probe);
        }

        int snpsInData = 0;
        HashSet<String> excludedSnps = new HashSet<String>();
        for (String snp : probeQtls) {

            Integer s = snpMap.get(snp);
            if (s != null) {

                if (v.evaluate(datasetGenotypes.rawData[s]) > 0) {
                    ++snpsInData;
                } else {
                    excludedSnps.add(snp);
                }

            }

        }

        //boolean foundPisS = false;
        double[][] valsX = new double[nrSamples][snpsInData + covsToCorrect.length * 2]; //store genotypes, covariates, interactions
        int k = 0;
        for (String snp : probeQtls) {

            if (excludedSnps.contains(snp)) {
                continue;
            }

            Integer s = snpMap.get(snp);
            if (s == null) {
                //throw new Exception("Snp " + snp + " not found");
                continue;
            }
            //            if(s.intValue() == p){
            //               foundPisS = true;
            //            }
            double[] snpData = datasetGenotypes.rawData[s];
            for (int i = 0; i < datasetGenotypes.nrSamples; ++i) {
                valsX[i][k] = snpData[i];
            }

            k++;
        }
        //         if(!foundPisS){
        //            
        //            System.out.println("Expected snp: " + datasetGenotypes.probeNames[p] + " at index: " + p);
        //            
        //            for(String qtlSnp : probeQtls = qtlProbeSnpMultiMap.get(probe)){
        //               System.out.println("QTL snp: " + qtlSnp + " found at index: " + snpMap.get(qtlSnp));
        //            }
        //            
        //            throw new Exception("Error 2");
        //         }
        for (int c = 0; c < covsToCorrect.length; c++) {
            double[] covData = datasetCovariates.rawData[covsToCorrectIndex[c]];
            double[] snpData = datasetGenotypes.rawData[p];

            for (int s = 0; s < nrSamples; s++) {
                valsX[s][c * 2 + snpsInData] = covData[s]; //covariate
                valsX[s][c * 2 + snpsInData + 1] = snpData[s] * covData[s]; //interction
            }
        }
        double[] valsY = datasetExpression.rawData[p];
        regression.newSampleData(valsY, valsX);
        try {
            datasetExpression.rawData[p] = regression.estimateResiduals();
        } catch (Exception up) {
            System.err.println(
                    "Error correcting for interactions: " + probe + " - " + datasetGenotypes.probeNames[p]);
        }
    }
}

From source file:org.opentestsystem.airose.regression.ols.OLSRegressionModeller.java

protected AbstractModel customProcessData(double[] y, double[][] x) {
    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    regression.newSampleData(y, x);/*from   ww w.  j  av  a2s  . c om*/

    double[] beta = regression.estimateRegressionParameters();
    double[] residuals = regression.estimateResiduals();
    double[][] parametersVariance = regression.estimateRegressionParametersVariance();
    double regressandVariance = regression.estimateRegressandVariance();
    double rSquared = regression.calculateRSquared();
    double sigma = regression.estimateRegressionStandardError();

    OLSModel olsModel = new OLSModel(beta, residuals, parametersVariance, regressandVariance, rSquared, sigma,
            y, x);
    return olsModel;
}