Improved or corrected javadoc; added some tests.
This commit is contained in:
parent
e42ab0ec6c
commit
3a7519fcb1
|
@ -156,12 +156,11 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* the {@link #calculateRSquared() R-squared} computation.</p>
|
||||
*
|
||||
* @return SSTO - the total sum of squares
|
||||
* @throws MathIllegalArgumentException if the sample has not been set or does
|
||||
* not contain at least 3 observations
|
||||
* @throws NullPointerException if the sample has not been set
|
||||
* @see #isNoIntercept()
|
||||
* @since 2.2
|
||||
*/
|
||||
public double calculateTotalSumOfSquares() throws MathIllegalArgumentException {
|
||||
public double calculateTotalSumOfSquares() {
|
||||
if (isNoIntercept()) {
|
||||
return StatUtils.sumSq(getY().toArray());
|
||||
} else {
|
||||
|
@ -174,6 +173,8 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
*
|
||||
* @return residual sum of squares
|
||||
* @since 2.2
|
||||
* @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix is singular
|
||||
* @throws NullPointerException if the data for the model have not been loaded
|
||||
*/
|
||||
public double calculateResidualSumOfSquares() {
|
||||
final RealVector residuals = calculateResiduals();
|
||||
|
@ -188,12 +189,14 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* 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 MathIllegalArgumentException if the sample has not been set or does
|
||||
* not contain at least 3 observations
|
||||
* @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() throws MathIllegalArgumentException {
|
||||
public double calculateRSquared() {
|
||||
return 1 - calculateResidualSumOfSquares() / calculateTotalSumOfSquares();
|
||||
}
|
||||
|
||||
|
@ -209,13 +212,15 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* <code> 1 - (1 - {@link #calculateRSquared()}) * (n / (n - p)) </code>
|
||||
* </pre></p>
|
||||
*
|
||||
* <p>If there is no variance in y, i.e., SSTO = 0, NaN is returned.</p>
|
||||
*
|
||||
* @return adjusted R-Squared statistic
|
||||
* @throws MathIllegalArgumentException if the sample has not been set or does
|
||||
* not contain at least 3 observations
|
||||
* @throws NullPointerException if the sample has not been set
|
||||
* @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix is singular
|
||||
* @see #isNoIntercept()
|
||||
* @since 2.2
|
||||
*/
|
||||
public double calculateAdjustedRSquared() throws MathIllegalArgumentException {
|
||||
public double calculateAdjustedRSquared() {
|
||||
final double n = getX().getRowDimension();
|
||||
if (isNoIntercept()) {
|
||||
return 1 - (1 - calculateRSquared()) * (n / (n - getX().getColumnDimension()));
|
||||
|
@ -244,6 +249,8 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* a {@code NullPointerException} will be thrown.</p>
|
||||
*
|
||||
* @return beta
|
||||
* @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix is singular
|
||||
* @throws NullPointerException if the data for the model have not been loaded
|
||||
*/
|
||||
@Override
|
||||
protected RealVector calculateBeta() {
|
||||
|
@ -264,6 +271,8 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* a {@code NullPointerException} will be thrown.</p>
|
||||
*
|
||||
* @return The beta variance-covariance matrix
|
||||
* @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix is singular
|
||||
* @throws NullPointerException if the data for the model have not been loaded
|
||||
*/
|
||||
@Override
|
||||
protected RealMatrix calculateBetaVariance() {
|
||||
|
|
|
@ -782,4 +782,39 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
|
|||
TestUtils.assertEquals(835542680000.000, model.calculateResidualSumOfSquares(), 1.0e-3);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Anything requiring beta calculation should advertise SME.
|
||||
*/
|
||||
@Test(expected=org.apache.commons.math3.linear.SingularMatrixException.class)
|
||||
public void testSingularCalculateBeta() {
|
||||
OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
|
||||
model.newSampleData(new double[] {1, 2, 3, 1, 2, 3, 1, 2, 3}, 3, 2);
|
||||
model.calculateBeta();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSSTOCalculateRsquare() {
|
||||
OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
|
||||
model.newSampleData(new double[] {1, 2, 3, 1, 7, 8, 1, 10, 12}, 3, 2);
|
||||
Assert.assertTrue(Double.isNaN(model.calculateRSquared()));
|
||||
}
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void testNoDataNPECalculateBeta() {
|
||||
OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
|
||||
model.calculateBeta();
|
||||
}
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void testNoDataNPECalculateHat() {
|
||||
OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
|
||||
model.calculateHat();
|
||||
}
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void testNoDataNPESSTO() {
|
||||
OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
|
||||
model.calculateTotalSumOfSquares();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue