/** Replace sample data, overriding any previous sample. * @param y y values of the sample * @param x x values of the sample * @param covariance array representing the covariance matrix */ public void newSampleData(double[] y, double[][] x, double[][] covariance) { validateSampleData(x, y); newYSampleData(y); newXSampleData(x); validateCovarianceData(x, covariance); newCovarianceData(covariance); }
/** * Conducts a regression on the data in the model, using all regressors. * * @return RegressionResults the structure holding all regression results * @exception ModelSpecificationException - thrown if number of observations is * less than the number of variables */ public RegressionResults regress() throws ModelSpecificationException { return regress(this.nvars); }
/** * {@inheritDoc} */ public double estimateRegressandVariance() { return calculateYVariance(); }
@Override public 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.getSlope(); }
@Override public 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(); }
/** * Adds one observation to the regression model. * * @param x the independent variables which form the design matrix * @param y the dependent or response variable * @throws ModelSpecificationException if the length of {@code x} does not equal * the number of independent variables in the model */ public void addObservation(final double[] x,final double y) throws ModelSpecificationException { if( x == null || x.length == 0 ){ throw new ModelSpecificationException(LocalizedFormats.INVALID_REGRESSION_OBSERVATION,x!=null?x.length:0, 1); } addData( x[0], y ); }
/** * Returns the sum of squared deviations of the predicted y values about * their mean (which equals the mean of y). * <p> * This is usually abbreviated SSR or SSM. It is defined as SSM * <a href="http://www.xycoon.com/SumOfSquares.htm">here</a></p> * <p> * <strong>Preconditions</strong>: <ul> * <li>At least two observations (with at least two different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, <code>Double.NaN</code> is * returned. * </li></ul></p> * * @return sum of squared deviations of predicted y values */ public double getRegressionSumSquares() { return getRegressionSumSquares(getSlope()); }
/** * Returns the <a href="http://www.xycoon.com/coefficient1.htm"> * coefficient of determination</a>, * usually denoted r-square. * <p> * <strong>Preconditions</strong>: <ul> * <li>At least two observations (with at least two different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, <code>Double,NaN</code> is * returned. * </li></ul></p> * * @return r-square */ public double getRSquare() { double ssto = getTotalSumSquares(); return (ssto - getSumSquaredErrors()) / ssto; }
/** * Loads model x and y sample data, overriding any previous sample. * * Computes and caches QR decomposition of the X matrix. * @param y the [n,1] array representing the y sample * @param x the [n,k] array representing the x sample * @throws MathIllegalArgumentException if the x and y array data are not * compatible for the regression */ public void newSampleData(double[] y, double[][] x) throws MathIllegalArgumentException { validateSampleData(x, y); newYSampleData(y); newXSampleData(x); }
/** * Returns the R-Squared statistic, defined by the formula <pre> * R<sup>2</sup> = 1 - SSR / SSTO * </pre> * where SSR is the {@link #calculateResidualSumOfSquares() sum of squared residuals} * and SSTO is the {@link #calculateTotalSumOfSquares() total sum of squares} * * <p>If there is no variance in y, i.e., SSTO = 0, NaN is returned.</p> * * @return R-square statistic * @throws NullPointerException if the sample has not been set * @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix is singular * @since 2.2 */ public double calculateRSquared() { return 1 - calculateResidualSumOfSquares() / calculateTotalSumOfSquares(); }
/** * {@inheritDoc} * <p>This implementation computes and caches the QR decomposition of the X matrix * once it is successfully loaded.</p> */ @Override protected void newXSampleData(double[][] x) { super.newXSampleData(x); qr = new QRDecomposition(getX(), threshold); }
/** * {@inheritDoc} * <p>This implementation computes and caches the QR decomposition of the X matrix.</p> */ @Override public void newSampleData(double[] data, int nobs, int nvars) { super.newSampleData(data, nobs, nvars); qr = new QRDecomposition(getX(), threshold); }
/** * Returns the sum of squared errors divided by the degrees of freedom, * usually abbreviated MSE. * <p> * If there are fewer than <strong>three</strong> data pairs in the model, * or if there is no variation in <code>x</code>, this returns * <code>Double.NaN</code>.</p> * * @return sum of squared deviations of y values */ public double getMeanSquareError() { if (n < 3) { return Double.NaN; } return hasIntercept ? (getSumSquaredErrors() / (n - 2)) : (getSumSquaredErrors() / (n - 1)); }
/** * Removes observations represented by the elements in <code>data</code>. * <p> * If the array is larger than the current n, only the first n elements are * processed. This method permits the use of SimpleRegression instances in * streaming mode where the regression is applied to a sliding "window" of * observations, however the caller is responsible for maintaining the set * of observations in the window.</p> * <p> * To remove all data, use <code>clear()</code>.</p> * * @param data array of observations to be removed */ public void removeData(double[][] data) { for (int i = 0; i < data.length && n > 0; i++) { removeData(data[i][0], data[i][1]); } }
/** * Estimates the variance of the error. * * @return estimate of the error variance * @since 2.2 */ public double estimateErrorVariance() { return calculateErrorVariance(); }
/** * {@inheritDoc} */ public double[] estimateRegressionParameters() { RealVector b = calculateBeta(); return b.toArray(); }
/** * Estimates the standard error of the regression. * * @return regression standard error * @since 2.2 */ public double estimateRegressionStandardError() { return FastMath.sqrt(estimateErrorVariance()); }
/** * {@inheritDoc} */ public double[][] estimateRegressionParametersVariance() { return calculateBetaVariance().getData(); }
@Override public 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 (float) regression.getSlope(); }
@Override public 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 (float) regression.getIntercept(); }