@Override public LeastSquaresRegressionResult regress(double[][] x, double[][] weights, double[] y, boolean useIntercept) { if (weights == null) { throw new IllegalArgumentException("Cannot perform WLS regression without an array of weights"); } checkData(x, weights, y); log .info("Have a two-dimensional array for what should be a one-dimensional array of weights. " + "The weights used in this regression will be the diagonal elements only"); double[] w = new double[weights.length]; for (int i = 0; i < w.length; i++) { w[i] = weights[i][i]; } return regress(x, w, y, useIntercept); }
public LeastSquaresRegressionResult regress(double[][] x, double[] weights, double[] y, boolean useIntercept) { if (weights == null) { throw new IllegalArgumentException("Cannot perform WLS regression without an array of weights"); } checkData(x, weights, y); double[][] dep = addInterceptVariable(x, useIntercept); double[] w = new double[weights.length]; for (int i = 0; i < y.length; i++) { w[i] = weights[i]; } DoubleMatrix matrix = DoubleMatrix.copyOf(dep); DoubleArray vector = DoubleArray.copyOf(y); RealMatrix wDiag = new DiagonalMatrix(w); DoubleMatrix transpose = ALGEBRA.getTranspose(matrix); DoubleMatrix wDiagTimesMatrix = DoubleMatrix.ofUnsafe(wDiag.multiply( new Array2DRowRealMatrix(matrix.toArrayUnsafe())).getData()); DoubleMatrix tmp = (DoubleMatrix) ALGEBRA.multiply( ALGEBRA.getInverse(ALGEBRA.multiply(transpose, wDiagTimesMatrix)), transpose); DoubleMatrix wTmpTimesDiag = DoubleMatrix.copyOf(wDiag.preMultiply(new Array2DRowRealMatrix(tmp.toArrayUnsafe())).getData()); DoubleMatrix betasVector = (DoubleMatrix) ALGEBRA.multiply(wTmpTimesDiag, vector); double[] yModel = super.writeArrayAsVector(((DoubleMatrix) ALGEBRA.multiply(matrix, betasVector)).toArray()); double[] betas = super.writeArrayAsVector(betasVector.toArray()); return getResultWithStatistics(x, convertArray(wDiag.getData()), y, betas, yModel, transpose, matrix, useIntercept); }
w2[i] = 1.; final WeightedLeastSquaresRegression wlsRegression = new WeightedLeastSquaresRegression(); final OrdinaryLeastSquaresRegression olsRegression = new OrdinaryLeastSquaresRegression(); try { wlsRegression.regress(x, (double[]) null, yNoIntercept, false); Assert.fail(); } catch (final IllegalArgumentException e) { LeastSquaresRegressionResult wls = wlsRegression.regress(x, w1, yIntercept, true); LeastSquaresRegressionResult ols = olsRegression.regress(x, yIntercept, true); assertRegressions(n, 4, wls, ols); wls = wlsRegression.regress(x, w1, yNoIntercept, false); ols = olsRegression.regress(x, yNoIntercept, false); assertRegressions(n, 3, wls, ols); wls = wlsRegression.regress(x, w2, yIntercept, true); ols = olsRegression.regress(x, yIntercept, true); assertRegressions(n, 4, wls, ols); wls = wlsRegression.regress(x, w2, yNoIntercept, false); ols = olsRegression.regress(x, yNoIntercept, false); assertRegressions(n, 3, wls, ols);
double[][] covarianceBetas = convertArray(ALGEBRA.getInverse(ALGEBRA.multiply(transpose, matrix)).toArray()); double rSquared = regressionSumOfSquares / totalSumOfSquares; double adjustedRSquared = 1. - (1 - rSquared) * (n - 1) / (n - k);