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

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

Introduction

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

Prototype

public double[] estimateRegressionParameters() 

Source Link

Usage

From source file:modelcreation.ModelCreation.java

public static double evaluateModel(OLSMultipleLinearRegression regression, double[][] subXTest,
        double[] subYTest) {
    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[] parameters = regression.estimateRegressionParameters();
    double[] predictions = new double[subYTest.length];

    for (int i = 0; i < subYTest.length; i++) {
        double prediction = parameters[0] + (parameters[1] * subXTest[i][0]) + (parameters[2] * subXTest[i][1]);
        predictions[i] = prediction;//from   ww w. j a v a 2s  .  c o m
    }

    double meanSquaredError = calculateMeanSquaredError(subYTest, predictions);
    System.out.println("Mean Squared Error = " + meanSquaredError);
    return meanSquaredError;
}

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);
    }//  w  ww  .java  2s.  c o m
}

From source file:net.gtl.movieanalytics.model.LinearRegression.java

private double[] estimateParameter(double[][] x, double[] y) {
    //printTestData(x, y);

    OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
    ols.newSampleData(y, x);/*w w w .ja va  2 s.co  m*/
    return ols.estimateRegressionParameters();
}

From source file:com.insightml.models.regression.OLS.java

@Override
public IModel<Sample, Double> train(final double[][] features, final double[] expected,
        final String[] featureNames) {
    final OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    regression.newSampleData(expected, features);
    return new LinearRegressionModel(regression.estimateRegressionParameters(), featureNames);
}

From source file:lu.lippmann.cdb.lab.regression.Regression.java

/**
 * Constructor.// w w  w . j av a 2 s . c  om
 */
public Regression(final Instances ds, final int idx) throws Exception {
    this.newds = WekaDataProcessingUtil.buildDataSetSortedByAttribute(ds, idx);

    //System.out.println("Regression -> "+newds.toSummaryString());

    final int N = this.newds.numInstances();
    final int M = this.newds.numAttributes();

    final double[][] x = new double[N][M - 1];
    final double[] y = new double[N];
    for (int i = 0; i < N; i++) {
        y[i] = this.newds.instance(i).value(0);
    }
    for (int i = 0; i < N; i++) {
        for (int j = 1; j < M; j++) {
            x[i][j - 1] = this.newds.instance(i).value(j);
        }
    }

    final OLSMultipleLinearRegression reg = new OLSMultipleLinearRegression();
    //reg.setNoIntercept(true);
    reg.newSampleData(y, x);

    this.r2 = reg.calculateRSquared();
    //this.r2=-1d;

    this.coe = reg.estimateRegressionParameters();

    this.estims = calculateEstimations(x, y, coe);
}

From source file:com.davidbracewell.ml.regression.LeastSquaresLearner.java

@Override
protected void trainAll(List<Instance> trainingData) {
    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    double[] y = new double[trainingData.size()];
    double[][] x = new double[trainingData.size()][];
    int i = 0;//  w  ww .j av a 2s. c o m
    for (Instance datum : trainingData) {
        y[i] = datum.getTargetValue();
        x[i] = datum.toArray();
        i++;
    }
    regression.newSampleData(y, x);
    double[] params = regression.estimateRegressionParameters();
    model.bias = params[0];
    double[] weights = new double[params.length - 1];
    System.arraycopy(params, 1, weights, 0, params.length - 1);
    model.weights = new DenseVector(weights);
}

From source file:model.Modelo.java

public Equacao ajustarModelo(Local local, ArrayList<ArvoreAjuste> arvoresAjuste) throws Exception {

    DecimalFormat df4casas = new DecimalFormat("##,###,###,##0.0000");

    JEP myParser = new JEP(); //http://www.singularsys.com/jep/doc/html/index.html

    myParser.addStandardFunctions();//from  w w w. j  a  va2  s  .c  om
    myParser.addStandardConstants();

    double resultadoTermo = 0.0;
    int qtdeVariaveis = 0;
    int idMetodoCalculo = 1; //Equao

    ArrayList<Termo> termos = getTermos();

    //      ArrayList<ArvoreAjuste> arvoresAjuste = new ArrayList<ArvoreAjuste>();
    //      arvoresAjuste = getArvoresAjuste();

    double[] qtdeObs = new double[arvoresAjuste.size()];
    double[] qtdeEst = new double[arvoresAjuste.size()];
    int iArvoreAjuste = 0;

    double[][] valorEntrada = new double[arvoresAjuste.size()][termos.size()];
    int iTermo = 0;

    for (ArvoreAjuste arvoreAjuste : arvoresAjuste) {

        for (Termo termo : termos) {

            for (VariavelArvoreAjuste variavelArvoreAjuste : arvoreAjuste.variaveisArvoreAjuste) {
                String sigla = variavelArvoreAjuste.getVariavel().getSigla();
                Double valor = variavelArvoreAjuste.getValor();
                myParser.addVariable(sigla, valor);
            }
            myParser.parseExpression(termo.getExpressao());
            resultadoTermo = myParser.getValue();
            valorEntrada[iArvoreAjuste][iTermo] = resultadoTermo;

            iTermo++;

        }

        qtdeObs[iArvoreAjuste] = arvoreAjuste.getQtdeObs(idVariavelInteresse);

        iArvoreAjuste++;
        iTermo = 0;
    }

    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    regression.newSampleData(qtdeObs, valorEntrada);
    double[] valorCoeficiente = regression.estimateRegressionParameters();

    //Monta equacao a partir dos coeficientes calculados
    String valorCoeficienteFormatado = df4casas.format(valorCoeficiente[0]);

    String expressaoEquacao = Double.toString(valorCoeficiente[0]);
    String expressaoEquacaoFormatada = valorCoeficienteFormatado;
    iTermo = 1;
    for (Termo termo : termos) {
        valorCoeficienteFormatado = df4casas.format(valorCoeficiente[iTermo]);
        if (valorCoeficiente[iTermo] < 0) {
            expressaoEquacao = expressaoEquacao + valorCoeficiente[iTermo] + "*" + termo.getExpressao();
            expressaoEquacaoFormatada = expressaoEquacaoFormatada + valorCoeficienteFormatado + "*"
                    + termo.getExpressao();
        } else {
            expressaoEquacao = expressaoEquacao + "+" + valorCoeficiente[iTermo] + "*" + termo.getExpressao();
            expressaoEquacaoFormatada = expressaoEquacaoFormatada + "+" + valorCoeficienteFormatado + "*"
                    + termo.getExpressao();
        }
        iTermo++;
    }
    //Cadastra equao ajustada
    Equacao equacao = new Equacao();
    equacao.setIdModelo(this.id);
    equacao.setExpressao(expressaoEquacao);
    equacao.setExpressaoFormatada(expressaoEquacaoFormatada);
    equacao.setIdVariavelInteresse(idVariavelInteresse);

    ArrayList<Variavel> variaveisEquacao = new ArrayList<Variavel>();
    variaveisEquacao = equacao.extraiVariaveis();
    equacao.setVariaveisEquacao(variaveisEquacao);
    EquacaoDao equacaoDao = new EquacaoDao();
    int idEquacao = equacaoDao.cadastrar(equacao);
    equacao.setId(idEquacao);

    EquacaoLocalDao equacaoLocalDao = new EquacaoLocalDao();
    EquacaoLocal equacaoLocal = new EquacaoLocal();
    equacaoLocal.setIdLocal(local.getId());
    equacaoLocal.setIdEquacao(idEquacao);
    equacaoLocal.setIdVariavelInteresse(idVariavelInteresse);
    equacaoLocalDao.cadastrar(equacaoLocal);

    //Aplica equacaoModelo em todas as ArvoresAjuste para calcular valorEstimado        
    myParser.parseExpression(expressaoEquacao);

    iArvoreAjuste = 0;
    for (ArvoreAjuste arvoreAjuste : arvoresAjuste) {

        arvoreAjuste.variaveisArvoreAjuste = arvoreAjuste.getVariaveisArvoreAjuste();
        qtdeVariaveis = arvoreAjuste.variaveisArvoreAjuste.size();

        for (VariavelArvoreAjuste variavelArvoreAjuste : arvoreAjuste.variaveisArvoreAjuste) {
            String sigla = variavelArvoreAjuste.getVariavel().getSigla();
            Double valor = variavelArvoreAjuste.getValor();
            myParser.addVariable(sigla, valor);
        }

        ArvoreAjusteDao arvoreAjusteDao = new ArvoreAjusteDao();
        arvoreAjuste.setQtdeEst(myParser.getValue(), idVariavelInteresse, 1); //Equacao
        arvoreAjusteDao.updateQtdeEst(arvoreAjuste, idVariavelInteresse, 1); //Equacao

        qtdeEst[iArvoreAjuste] = arvoreAjuste.getQtdeEst(idVariavelInteresse, 1); //Equacao);
        iArvoreAjuste++;
    }

    EstatisticaAjuste estatisticaAjuste = new EstatisticaAjuste();
    estatisticaAjuste.setIdModelo(id);
    estatisticaAjuste.setIdLocal(local.getId());
    estatisticaAjuste.setIdVariavelInteresse(idVariavelInteresse);
    estatisticaAjuste.setIdMetodoCalculo(1); //Equao

    estatisticaAjuste.calcularEstatisticasAjuste(qtdeObs, qtdeEst, qtdeVariaveis);

    return equacao;
}

From source file:KitchenManagement.FoodDemandForecastingAndPlanning.FoodDemandForecastingAndPlanningBean.java

@Override
public SaleForecastEntity getSalesForecastMultipleLinearRegression(Long storeId, Long menuItemId,
        Long scheduleId) {/* w  w w  .j  a va 2s  .c o  m*/
    System.out.println("debug......" + "getSalesForecastMultipleLinearRegression is called.");
    try {
        Query q = em.createQuery(
                "select sf from SaleForecastEntity sf where sf.menuItem.id = ?1 AND sf.store.id = ?2 AND sf.schedule.id = ?3")
                .setParameter(1, menuItemId).setParameter(2, storeId).setParameter(3, scheduleId);

        if (!q.getResultList().isEmpty()) {
            em.remove(q.getResultList().get(0));
            em.flush();
        }

        // if not exist, then create it
        MonthScheduleEntity schedule = em.find(MonthScheduleEntity.class, scheduleId);
        StoreEntity store = em.find(StoreEntity.class, storeId);
        MenuItemEntity menuItem = em.find(MenuItemEntity.class, menuItemId);

        try {

            List<SalesFigureEntity> salesFigureList = new ArrayList<>();

            MonthScheduleEntity lastSchedule = schedule;

            Query q1 = em.createQuery(
                    "select sf from SalesFigureEntity sf where sf.menuItem.id = ?1 AND sf.store.id = ?2 AND sf.schedule.id = ?3")
                    .setParameter(1, menuItemId).setParameter(2, storeId).setParameter(3, lastSchedule.getId());

            if (!q1.getResultList().isEmpty()) {
                SalesFigureEntity salesFigureEntity = (SalesFigureEntity) q1.getResultList().get(0);
                salesFigureList.add(salesFigureEntity);
            }

            while (this.getTheBeforeOne(lastSchedule) != null) {
                lastSchedule = this.getTheBeforeOne(lastSchedule);

                Query q2 = em.createQuery(
                        "select sf from SalesFigureEntity sf where sf.menuItem.id = ?1 AND sf.store.id = ?2 AND sf.schedule.id = ?3")
                        .setParameter(1, menuItemId).setParameter(2, storeId)
                        .setParameter(3, lastSchedule.getId());

                if (!q2.getResultList().isEmpty()) {
                    SalesFigureEntity salesFigureEntity = (SalesFigureEntity) q2.getResultList().get(0);
                    salesFigureList.add(salesFigureEntity);
                }
            }

            OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();

            double[] y = new double[salesFigureList.size()];
            double[][] x = new double[salesFigureList.size()][];
            for (int i = 0; i < salesFigureList.size(); i++) {
                y[i] = salesFigureList.get(i).getQuantity();

                switch (salesFigureList.get(i).getSchedule().getMonth()) {
                case 1:
                    x[i] = new double[] { i, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                    break;
                case 2:
                    x[i] = new double[] { i, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                    break;
                case 3:
                    x[i] = new double[] { i, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                    break;
                case 4:
                    x[i] = new double[] { i, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
                    break;
                case 5:
                    x[i] = new double[] { i, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
                    break;
                case 6:
                    x[i] = new double[] { i, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 };
                    break;
                case 7:
                    x[i] = new double[] { i, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 };
                    break;
                case 8:
                    x[i] = new double[] { i, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 };
                    break;
                case 9:
                    x[i] = new double[] { i, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 };
                    break;
                case 10:
                    x[i] = new double[] { i, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 };
                    break;
                case 11:
                    x[i] = new double[] { i, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
                    break;
                case 12:
                    x[i] = new double[] { i, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
                    break;
                }
            }
            regression.newSampleData(y, x);
            double[] coefficient = regression.estimateRegressionParameters();
            System.out.println("coefficient.length: " + coefficient.length);
            for (int i = 0; i < coefficient.length; i++) {
                System.out.println("coefficient[i]: " + coefficient[i]);
            }
            double forecastQuantity = coefficient[0] - coefficient[1] + coefficient[schedule.getMonth() + 1];

            SaleForecastEntity saleForecast = new SaleForecastEntity(store, menuItem, schedule,
                    Math.round((float) forecastQuantity));
            saleForecast.setMethod("M");
            em.persist(saleForecast);

            System.out.println("schedule.getId(): " + schedule.getId());

            return saleForecast;

        } catch (Exception ex) {
            ex.printStackTrace();
            SaleForecastEntity saleForecast = new SaleForecastEntity(store, menuItem, schedule, 0);
            System.out.println("debug......" + " exception is catched");
            return saleForecast;
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:eqtlmappingpipeline.interactionanalysis.InteractionPlotter.java

public InteractionPlotter(String interactionFile, String genotypeDir, String expressionDataFile,
        String covariateDataFile, String gteFile, String outdir) throws IOException {
    outdir = Gpio.formatAsDirectory(outdir);
    Gpio.createDir(outdir);/*w w  w  . jav  a  2  s .  com*/
    Map<String, String> gte = null;
    if (gteFile != null) {
        TextFile tf = new TextFile(gteFile, TextFile.R);
        gte = tf.readAsHashMap(0, 1);
        tf.close();
    }

    HashSet<String> expressionProbes = new HashSet<String>();
    TextFile tf = new TextFile(interactionFile, TextFile.R);
    String[] elems = tf.readLineElems(TextFile.tab);
    ArrayList<Triple<String, String, String>> triples = new ArrayList<Triple<String, String, String>>();
    while (elems != null) {
        if (elems.length == 2) {
            String snp = elems[0];
            String probe = elems[1];
            expressionProbes.add(probe);
            triples.add(new Triple<String, String, String>(snp, null, probe));
        } else if (elems.length == 3) {
            String snp = elems[0];
            String covariate = elems[1];
            String probe = elems[2];
            expressionProbes.add(probe);
            triples.add(new Triple<String, String, String>(snp, covariate, probe));
        }
        elems = tf.readLineElems(TextFile.tab);
    }
    tf.close();
    System.out.println(triples.size() + " SNP - covariate - probe combinations read from: " + interactionFile);

    DoubleMatrixDataset<String, String> expressionData = new DoubleMatrixDataset<String, String>(
            expressionDataFile, expressionProbes);
    DoubleMatrixDataset<String, String> covariateData = new DoubleMatrixDataset<String, String>(
            covariateDataFile);

    int samplesHaveCovariatesOnCols = 0;
    int samplesHaveCovariatesOnRows = 0;
    for (int i = 0; i < expressionData.colObjects.size(); i++) {
        String expSample = expressionData.colObjects.get(i);
        Integer id1 = covariateData.hashCols.get(expSample);
        Integer id2 = covariateData.hashRows.get(expSample);
        if (id1 != null) {
            samplesHaveCovariatesOnCols++;
        }
        if (id2 != null) {
            samplesHaveCovariatesOnRows++;
        }
    }
    if (samplesHaveCovariatesOnRows > samplesHaveCovariatesOnCols) {
        System.out.println("Rows contain covariate samples in covariate file. Transposing covariates.");
        covariateData.transposeDataset();
    }

    TriTyperGenotypeData geno = new TriTyperGenotypeData(genotypeDir);
    SNPLoader loader = geno.createSNPLoader();

    int[] genotypeToCovariate = new int[geno.getIndividuals().length];
    int[] genotypeToExpression = new int[geno.getIndividuals().length];
    String[] genoIndividuals = geno.getIndividuals();
    for (int i = 0; i < genotypeToCovariate.length; i++) {
        String genoSample = genoIndividuals[i];
        if (geno.getIsIncluded()[i] != null && geno.getIsIncluded()[i]) {
            if (gte != null) {
                genoSample = gte.get(genoSample);
            }

            Integer covariateSample = covariateData.hashCols.get(genoSample);
            Integer expressionSample = expressionData.hashCols.get(genoSample);
            if (genoSample != null && covariateSample != null && expressionSample != null) {
                genotypeToCovariate[i] = covariateSample;
                genotypeToExpression[i] = expressionSample;
            } else {
                genotypeToCovariate[i] = -9;
                genotypeToExpression[i] = -9;

            }
        } else {
            genotypeToCovariate[i] = -9;
            genotypeToExpression[i] = -9;
        }
    }

    OLSMultipleLinearRegression regressionFullWithInteraction = new OLSMultipleLinearRegression();
    cern.jet.random.tdouble.StudentT tDistColt = null;
    org.apache.commons.math3.distribution.FDistribution fDist = null;
    cern.jet.random.tdouble.engine.DoubleRandomEngine randomEngine = null;

    Color[] colorarray = new Color[3];
    colorarray[0] = new Color(171, 178, 114);
    colorarray[1] = new Color(98, 175, 255);
    colorarray[2] = new Color(204, 86, 78);

    DecimalFormat decFormat = new DecimalFormat("#.###");
    DecimalFormat decFormatSmall = new DecimalFormat("0.#E0");
    for (Triple<String, String, String> triple : triples) {

        String snp = triple.getLeft();
        String covariate = triple.getMiddle();
        String probe = triple.getRight();

        Integer snpId = geno.getSnpToSNPId().get(snp);

        Integer probeId = expressionData.hashRows.get(probe);

        int startCovariate = -1;
        int endCovariate = -1;

        if (covariate == null) {
            startCovariate = 0;
            endCovariate = covariateData.nrRows;
        } else {
            Integer covariateId = covariateData.hashRows.get(covariate);
            if (covariateId != null) {
                startCovariate = covariateId;
                endCovariate = covariateId + 1;
            }
        }

        if (snpId >= 0 && probeId != null && startCovariate >= 0) {

            SNP snpObj = geno.getSNPObject(snpId);
            loader.loadGenotypes(snpObj);
            if (loader.hasDosageInformation()) {
                loader.loadDosage(snpObj);
            }

            double signInteractionEffectDirection = 1;
            String[] genotypeDescriptions = new String[3];
            if (snpObj.getAlleles()[1] == snpObj.getMinorAllele()) {
                signInteractionEffectDirection = -1;
                genotypeDescriptions[2] = BaseAnnot.toString(snpObj.getAlleles()[0]) + ""
                        + BaseAnnot.toString(snpObj.getAlleles()[0]);
                genotypeDescriptions[1] = BaseAnnot.toString(snpObj.getAlleles()[0]) + ""
                        + BaseAnnot.toString(snpObj.getAlleles()[1]);
                genotypeDescriptions[0] = BaseAnnot.toString(snpObj.getAlleles()[1]) + ""
                        + BaseAnnot.toString(snpObj.getAlleles()[1]);
            } else {
                genotypeDescriptions[0] = BaseAnnot.toString(snpObj.getAlleles()[0]) + ""
                        + BaseAnnot.toString(snpObj.getAlleles()[0]);
                genotypeDescriptions[1] = BaseAnnot.toString(snpObj.getAlleles()[0]) + ""
                        + BaseAnnot.toString(snpObj.getAlleles()[1]);
                genotypeDescriptions[2] = BaseAnnot.toString(snpObj.getAlleles()[1]) + ""
                        + BaseAnnot.toString(snpObj.getAlleles()[1]);

            }

            for (int q = startCovariate; q < endCovariate; q++) {
                System.out.println("Plotting: " + snp + "\t" + covariateData.rowObjects.get(q) + "\t" + probe);
                System.out.println(
                        "Individual\tAllele1\tAllele2\tGenotype\tGenotypeFlipped\tCovariate\tExpression");
                byte[] alleles1 = snpObj.getAllele1();
                byte[] alleles2 = snpObj.getAllele2();
                byte[] genotypes = snpObj.getGenotypes();
                ArrayList<Byte> genotypeArr = new ArrayList<Byte>();
                ArrayList<Double> covariateArr = new ArrayList<Double>();
                ArrayList<Double> expressionArr = new ArrayList<Double>();

                int nrCalled = 0;

                for (int i = 0; i < genoIndividuals.length; i++) {
                    if (genotypes[i] != -1 && genotypeToCovariate[i] != -9 && genotypeToExpression[i] != -9) {

                        if (!Double.isNaN(covariateData.rawData[q][genotypeToCovariate[i]])) {

                            int genotypeflipped = genotypes[i];
                            if (signInteractionEffectDirection == -1) {
                                genotypeflipped = 2 - genotypeflipped;
                            }

                            String output = genoIndividuals[i] + "\t" + BaseAnnot.toString(alleles1[i]) + "\t"
                                    + BaseAnnot.toString(alleles2[i]) + "\t" + genotypes[i] + "\t"
                                    + genotypeflipped + "\t" + covariateData.rawData[q][genotypeToCovariate[i]]
                                    + "\t" + expressionData.rawData[probeId][genotypeToExpression[i]];
                            System.out.println(output);

                            genotypeArr.add(genotypes[i]);

                            covariateArr.add(covariateData.rawData[q][genotypeToCovariate[i]]);
                            expressionArr.add(expressionData.rawData[probeId][genotypeToExpression[i]]);
                            nrCalled++;
                        }

                    }
                }
                System.out.println("");
                //Fill arrays with data in order to be able to perform the ordinary least squares analysis:
                double[] olsY = new double[nrCalled]; //Ordinary least squares: Our gene expression
                double[][] olsXFullWithInteraction = new double[nrCalled][3]; //With interaction term, linear model: y ~ a * SNP + b * CellCount + c + d * SNP * CellCount
                int itr = 0;

                double[] dataExp = new double[nrCalled];
                double[] dataCov = new double[nrCalled];
                int[] dataGen = new int[nrCalled];

                for (int s = 0; s < nrCalled; s++) {
                    byte originalGenotype = genotypeArr.get(s);
                    int genotype = originalGenotype;
                    if (signInteractionEffectDirection == -1) {
                        genotype = 2 - genotype;
                    }

                    olsY[s] = expressionArr.get(s);

                    olsXFullWithInteraction[s][0] = genotype;
                    olsXFullWithInteraction[s][1] = covariateArr.get(s);
                    olsXFullWithInteraction[s][2] = olsXFullWithInteraction[s][0]
                            * olsXFullWithInteraction[s][1];

                    dataExp[s] = olsY[s];
                    dataGen[s] = genotype;
                    dataCov[s] = covariateArr.get(s);

                    itr++;
                }

                regressionFullWithInteraction.newSampleData(olsY, olsXFullWithInteraction);

                double rss2 = regressionFullWithInteraction.calculateResidualSumOfSquares();
                double[] regressionParameters = regressionFullWithInteraction.estimateRegressionParameters();
                double[] regressionStandardErrors = regressionFullWithInteraction
                        .estimateRegressionParametersStandardErrors();

                double betaInteraction = regressionParameters[3];
                double seInteraction = regressionStandardErrors[3];
                double tInteraction = betaInteraction / seInteraction;
                double pValueInteraction = 1;
                double zScoreInteraction = 0;

                if (fDist == null) {
                    fDist = new org.apache.commons.math3.distribution.FDistribution((int) (3 - 2),
                            (int) (olsY.length - 3));
                    randomEngine = new cern.jet.random.tdouble.engine.DRand();
                    tDistColt = new cern.jet.random.tdouble.StudentT(olsY.length - 4, randomEngine);
                }

                if (tInteraction < 0) {
                    pValueInteraction = tDistColt.cdf(tInteraction);
                    if (pValueInteraction < 2.0E-323) {
                        pValueInteraction = 2.0E-323;
                    }
                    zScoreInteraction = cern.jet.stat.tdouble.Probability.normalInverse(pValueInteraction);
                } else {
                    pValueInteraction = tDistColt.cdf(-tInteraction);
                    if (pValueInteraction < 2.0E-323) {
                        pValueInteraction = 2.0E-323;
                    }

                    zScoreInteraction = -cern.jet.stat.tdouble.Probability.normalInverse(pValueInteraction);
                }
                pValueInteraction *= 2;
                String pvalFormatted = "";
                if (pValueInteraction >= 0.001) {
                    pvalFormatted = decFormat.format(pValueInteraction);
                } else {
                    pvalFormatted = decFormatSmall.format(pValueInteraction);
                }
                ScatterPlot scatterPlot = new ScatterPlot(500, 500, dataCov, dataExp, dataGen,
                        genotypeDescriptions, colorarray, ScatterPlot.OUTPUTFORMAT.PDF,
                        "Interaction between SNP " + snp + ", probe " + probe + " and covariate "
                                + covariateData.rowObjects.get(q),
                        "Z: " + decFormat.format(zScoreInteraction) + " Pvalue: " + pvalFormatted + " n: "
                                + nrCalled,
                        outdir + snp + "-" + probe + "-" + covariateData.rowObjects.get(q) + ".pdf", false);

            }

            snpObj.clearGenotypes();
        }
    }

    loader.close();
}

From source file:org.apache.predictionio.examples.java.regression.OLSAlgorithm.java

public Double[] train(TrainingData data) {
    OLSMultipleLinearRegression r = new OLSMultipleLinearRegression();
    // Convert Double[][] to double[][]
    double[][] x = new double[data.r][data.c];
    double[] y = new double[data.r];

    for (int i = 0; i < data.r; i++) {
        for (int j = 0; j < data.c; j++) {
            x[i][j] = data.x[i][j].doubleValue();
        }//from  w w  w  .  j av  a2  s .  c  om
        y[i] = data.y[i].doubleValue();
    }

    r.newSampleData(y, x);
    // Fixme. Add intercept
    double[] p = r.estimateRegressionParameters();
    Double[] pp = new Double[data.c];
    for (int j = 0; j < data.c; j++) {
        pp[j] = p[j];
    }
    System.out.println("Regression Algo: " + Arrays.toString(pp));
    return pp;
}