Search in sources :

Example 1 with RegressionResults

use of org.apache.commons.math3.stat.regression.RegressionResults in project knime-core by knime.

the class Learner method perform.

/**
 * @param data The data table.
 * @param exec The execution context used for reporting progress.
 * @return An object which holds the results.
 * @throws CanceledExecutionException When method is cancelled
 * @throws InvalidSettingsException When settings are inconsistent with the data
 */
@Override
public PolyRegContent perform(final BufferedDataTable data, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
    exec.checkCanceled();
    RegressionTrainingData trainingData = new RegressionTrainingData(data, m_outSpec, m_failOnMissing);
    int regressorCount = trainingData.getRegressorCount() * m_maxExponent;
    SummaryStatistics[] stats = new SummaryStatistics[regressorCount];
    UpdatingMultipleLinearRegression regr = initStatistics(regressorCount, stats);
    exec.setProgress(0, "Estimating polynomial regression model.");
    processTable(exec, trainingData, stats, regr);
    RegressionResults result = regr.regress();
    RealMatrix beta = MatrixUtils.createRowRealMatrix(result.getParameterEstimates());
    List<String> factorList = new ArrayList<String>();
    List<String> covariateList = createCovariateListAndFillFactors(data, trainingData, factorList);
    // The covariance matrix
    RealMatrix covMat = createCovarianceMatrix(result);
    PolyRegContent content = new PolyRegContent(m_outSpec, (int) stats[0].getN(), factorList, covariateList, beta, m_offsetValue, covMat, result.getRSquared(), result.getAdjustedRSquared(), stats, m_maxExponent);
    return content;
}
Also used : RealMatrix(org.apache.commons.math3.linear.RealMatrix) ArrayList(java.util.ArrayList) RegressionTrainingData(org.knime.base.node.mine.regression.RegressionTrainingData) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) RegressionResults(org.apache.commons.math3.stat.regression.RegressionResults) UpdatingMultipleLinearRegression(org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression)

Example 2 with RegressionResults

use of org.apache.commons.math3.stat.regression.RegressionResults in project knime-core by knime.

the class RegressionStatisticsLearner method createCovarianceMatrix.

/**
 * Creates the covariance matrix from the {@link RegressionResults}.
 *
 * @param result A {@link RegressionResults} object.
 * @return The covariance {@link RealMatrix}.
 * @since 3.4
 */
protected RealMatrix createCovarianceMatrix(final RegressionResults result) {
    // The covariance matrix
    int dim = result.getNumberOfParameters();
    RealMatrix covMat = MatrixUtils.createRealMatrix(dim, dim);
    for (int i = 0; i < dim; i++) {
        for (int k = 0; k < dim; k++) {
            covMat.setEntry(i, k, result.getCovarianceOfParameters(i, k));
        }
    }
    return covMat;
}
Also used : RealMatrix(org.apache.commons.math3.linear.RealMatrix)

Example 3 with RegressionResults

use of org.apache.commons.math3.stat.regression.RegressionResults in project knime-core by knime.

the class Learner method perform.

/**
 * @param data The data table.
 * @param exec The execution context used for reporting progress.
 * @return An object which holds the results.
 * @throws CanceledExecutionException When method is cancelled
 * @throws InvalidSettingsException When settings are inconsistent with the data
 */
@Override
public LinearRegressionContent perform(final BufferedDataTable data, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
    exec.checkCanceled();
    RegressionTrainingData trainingData = new RegressionTrainingData(data, m_outSpec, m_failOnMissing);
    final int regressorCount = Math.max(1, trainingData.getRegressorCount());
    SummaryStatistics[] stats = new SummaryStatistics[regressorCount];
    UpdatingMultipleLinearRegression regr = initStatistics(regressorCount, stats);
    processTable(exec, trainingData, stats, regr);
    List<String> factorList = new ArrayList<String>();
    List<String> covariateList = createCovariateListAndFillFactors(data, trainingData, factorList);
    try {
        RegressionResults result = regr.regress();
        RealMatrix beta = MatrixUtils.createRowRealMatrix(result.getParameterEstimates());
        // The covariance matrix
        RealMatrix covMat = createCovarianceMatrix(result);
        LinearRegressionContent content = new LinearRegressionContent(m_outSpec, (int) stats[0].getN(), factorList, covariateList, beta, m_includeConstant, m_offsetValue, covMat, result.getRSquared(), result.getAdjustedRSquared(), stats, null);
        return content;
    } catch (ModelSpecificationException e) {
        int dim = (m_includeConstant ? 1 : 0) + trainingData.getRegressorCount() + (factorList.size() > 0 ? Math.max(1, data.getDataTableSpec().getColumnSpec(factorList.get(0)).getDomain().getValues().size() - 1) : 0);
        RealMatrix beta = MatrixUtils.createRealMatrix(1, dim);
        RealMatrix covMat = MatrixUtils.createRealMatrix(dim, dim);
        // fillWithNaNs(beta);
        fillWithNaNs(covMat);
        return new LinearRegressionContent(m_outSpec, (int) stats[0].getN(), factorList, covariateList, beta, m_includeConstant, m_offsetValue, covMat, Double.NaN, Double.NaN, stats, e.getMessage());
    }
}
Also used : RealMatrix(org.apache.commons.math3.linear.RealMatrix) ArrayList(java.util.ArrayList) RegressionTrainingData(org.knime.base.node.mine.regression.RegressionTrainingData) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) RegressionResults(org.apache.commons.math3.stat.regression.RegressionResults) UpdatingMultipleLinearRegression(org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression) ModelSpecificationException(org.apache.commons.math3.stat.regression.ModelSpecificationException)

Aggregations

RealMatrix (org.apache.commons.math3.linear.RealMatrix)3 ArrayList (java.util.ArrayList)2 SummaryStatistics (org.apache.commons.math3.stat.descriptive.SummaryStatistics)2 RegressionResults (org.apache.commons.math3.stat.regression.RegressionResults)2 UpdatingMultipleLinearRegression (org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression)2 RegressionTrainingData (org.knime.base.node.mine.regression.RegressionTrainingData)2 ModelSpecificationException (org.apache.commons.math3.stat.regression.ModelSpecificationException)1