Top Description Fields Constructors Methods
org.apache.commons.math3.stat.regression

public abstract Class AbstractMultipleLinearRegression

extends Object
implements MultipleLinearRegression
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.stat.regression.MultipleLinearRegression
Known Direct Subclasses
org.apache.commons.math3.stat.regression.GLSMultipleLinearRegression, org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression
Imports
org.apache.commons.math3.exception.DimensionMismatchException, .InsufficientDataException, .MathIllegalArgumentException, .NoDataException, .NullArgumentException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.linear.NonSquareMatrixException, .RealMatrix, .Array2DRowRealMatrix, .RealVector, .ArrayRealVector, org.apache.commons.math3.stat.descriptive.moment.Variance, org.apache.commons.math3.util.FastMath

Abstract base class for implementations of MultipleLinearRegression.
Since
2.0

Field Summary

Modifier and TypeField and Description
private boolean
noIntercept

Whether or not the regression model includes an intercept.

private RealMatrix
xMatrix

X sample data.

private RealVector
yVector

Y sample data.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected abstract RealVector

Returns:

beta
calculateBeta
()

Calculates the beta of multiple linear regression in matrix notation.

protected abstract RealMatrix

Returns:

beta variance
calculateBetaVariance
()

Calculates the beta variance of multiple linear regression in matrix notation.

protected double

Returns:

error variance estimate
calculateErrorVariance
()

Calculates the variance of the error term.

protected RealVector

Returns:

The residuals [n,1] matrix
calculateResiduals
()

Calculates the residuals of multiple linear regression in matrix notation.

protected double

Returns:

Y variance
calculateYVariance
()

Calculates the variance of the y values.

public double

Returns:

estimate of the error variance
estimateErrorVariance
()

Estimates the variance of the error.

public double
public double[]
public double[]
public double[][]
public double

Returns:

regression standard error
estimateRegressionStandardError
()

Estimates the standard error of the regression.

public double[]
protected RealMatrix

Returns:

the X sample data.
getX
()

protected RealVector

Returns:

the Y sample data.
getY
()

public boolean

Returns:

true if the model has no intercept term; false otherwise
isNoIntercept
()

public void
newSampleData(double[]
input data array
data
,
int
number of observations (rows)
nobs
,
int
number of independent variables (columns, not counting y)
nvars
)

Loads model x and y sample data from a flat input array, overriding any previous sample.

protected void
newXSampleData(double[][]
the rectangular array representing the x sample
x
)

Loads new x sample data, overriding any previous data.

protected void
newYSampleData(double[]
the array representing the y sample
y
)

Loads new y sample data, overriding any previous data.

public void
setNoIntercept(boolean
true means the model is to be estimated without an intercept term
noIntercept
)

protected void
validateCovarianceData(double[][]
the [n,k] array representing the x sample
x
,
double[][]
the [n,n] array representing the covariance matrix
covariance
)

Validates that the x data and covariance matrix have the same number of rows and that the covariance matrix is square.

protected void
validateSampleData(double[][]
the [n,k] array representing the x data
x
,
double[]
the [n,1] array representing the y data
y
)

Validates sample data.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

noInterceptback to summary
private boolean noIntercept

Whether or not the regression model includes an intercept. True means no intercept.

xMatrixback to summary
private RealMatrix xMatrix

X sample data.

yVectorback to summary
private RealVector yVector

Y sample data.

Constructor Detail

AbstractMultipleLinearRegressionback to summary
public AbstractMultipleLinearRegression()

Method Detail

calculateBetaback to summary
protected abstract RealVector calculateBeta()

Calculates the beta of multiple linear regression in matrix notation.

Returns:RealVector

beta

calculateBetaVarianceback to summary
protected abstract RealMatrix calculateBetaVariance()

Calculates the beta variance of multiple linear regression in matrix notation.

Returns:RealMatrix

beta variance

calculateErrorVarianceback to summary
protected double calculateErrorVariance()

Calculates the variance of the error term.

Uses the formula
var(u) = u · u / (n - k)
where n and k are the row and column dimensions of the design matrix X.
Returns:double

error variance estimate

Since
2.2
calculateResidualsback to summary
protected RealVector calculateResiduals()

Calculates the residuals of multiple linear regression in matrix notation.

u = y - X * b
Returns:RealVector

The residuals [n,1] matrix

calculateYVarianceback to summary
protected double calculateYVariance()

Calculates the variance of the y values.

Returns:double

Y variance

estimateErrorVarianceback to summary
public double estimateErrorVariance()

Estimates the variance of the error.

Returns:double

estimate of the error variance

Since
2.2
estimateRegressandVarianceback to summary
public double estimateRegressandVariance()

Implements org.apache.commons.math3.stat.regression.MultipleLinearRegression.estimateRegressandVariance.

Doc from org.apache.commons.math3.stat.regression.MultipleLinearRegression.estimateRegressandVariance.

Returns the variance of the regressand, ie Var(y).

Returns:double

The double representing the variance of y

estimateRegressionParametersback to summary
public double[] estimateRegressionParameters()

Implements org.apache.commons.math3.stat.regression.MultipleLinearRegression.estimateRegressionParameters.

Doc from org.apache.commons.math3.stat.regression.MultipleLinearRegression.estimateRegressionParameters.

Estimates the regression parameters b.

Returns:double[]

The [k,1] array representing b

estimateRegressionParametersStandardErrorsback to summary
public double[] estimateRegressionParametersStandardErrors()

Implements org.apache.commons.math3.stat.regression.MultipleLinearRegression.estimateRegressionParametersStandardErrors.

Doc from org.apache.commons.math3.stat.regression.MultipleLinearRegression.estimateRegressionParametersStandardErrors.

Returns the standard errors of the regression parameters.

Returns:double[]

standard errors of estimated regression parameters

estimateRegressionParametersVarianceback to summary
public double[][] estimateRegressionParametersVariance()

Implements org.apache.commons.math3.stat.regression.MultipleLinearRegression.estimateRegressionParametersVariance.

Doc from org.apache.commons.math3.stat.regression.MultipleLinearRegression.estimateRegressionParametersVariance.

Estimates the variance of the regression parameters, ie Var(b).

Returns:double[][]

The [k,k] array representing the variance of b

estimateRegressionStandardErrorback to summary
public double estimateRegressionStandardError()

Estimates the standard error of the regression.

Returns:double

regression standard error

Since
2.2
estimateResidualsback to summary
public double[] estimateResiduals()

Implements org.apache.commons.math3.stat.regression.MultipleLinearRegression.estimateResiduals.

Doc from org.apache.commons.math3.stat.regression.MultipleLinearRegression.estimateResiduals.

Estimates the residuals, ie u = y - X*b.

Returns:double[]

The [n,1] array representing the residuals

getXback to summary
protected RealMatrix getX()
Returns:RealMatrix

the X sample data.

getYback to summary
protected RealVector getY()
Returns:RealVector

the Y sample data.

isNoInterceptback to summary
public boolean isNoIntercept()
Returns:boolean

true if the model has no intercept term; false otherwise

Since
2.2
newSampleDataback to summary
public void newSampleData(double[] data, int nobs, int nvars)

Loads model x and y sample data from a flat input array, overriding any previous sample.

Assumes that rows are concatenated with y values first in each row. For example, an input data array containing the sequence of values (1, 2, 3, 4, 5, 6, 7, 8, 9) with nobs = 3 and nvars = 2 creates a regression dataset with two independent variables, as below:

  y   x[0]  x[1]
  --------------
  1     2     3
  4     5     6
  7     8     9

Note that there is no need to add an initial unitary column (column of 1's) when specifying a model including an intercept term. If isNoIntercept() is true, the X matrix will be created without an initial column of "1"s; otherwise this column will be added.

Throws IllegalArgumentException if any of the following preconditions fail:

  • data cannot be null
  • data.length = nobs * (nvars + 1)
  • nobs > nvars

Parameters
data:double[]

input data array

nobs:int

number of observations (rows)

nvars:int

number of independent variables (columns, not counting y)

Exceptions
NullArgumentException:
if the data array is null
DimensionMismatchException:
if the length of the data array is not equal to nobs * (nvars + 1)
InsufficientDataException:
if nobs is less than nvars + 1
newXSampleDataback to summary
protected void newXSampleData(double[][] x)

Loads new x sample data, overriding any previous data.

The input x array should have one row for each sample observation, with columns corresponding to independent variables. For example, if
 x = new double[][] {{1, 2}, {3, 4}, {5, 6}} 
then setXSampleData(x) results in a model with two independent variables and 3 observations:
  x[0]  x[1]
  ----------
    1    2
    3    4
    5    6

Note that there is no need to add an initial unitary column (column of 1's) when specifying a model including an intercept term.

Parameters
x:double[][]

the rectangular array representing the x sample

Exceptions
NullArgumentException:
if x is null
NoDataException:
if x is empty
DimensionMismatchException:
if x is not rectangular
newYSampleDataback to summary
protected void newYSampleData(double[] y)

Loads new y sample data, overriding any previous data.

Parameters
y:double[]

the array representing the y sample

Exceptions
NullArgumentException:
if y is null
NoDataException:
if y is empty
setNoInterceptback to summary
public void setNoIntercept(boolean noIntercept)
Parameters
noIntercept:boolean

true means the model is to be estimated without an intercept term

Since
2.2
validateCovarianceDataback to summary
protected void validateCovarianceData(double[][] x, double[][] covariance)

Validates that the x data and covariance matrix have the same number of rows and that the covariance matrix is square.

Parameters
x:double[][]

the [n,k] array representing the x sample

covariance:double[][]

the [n,n] array representing the covariance matrix

Exceptions
DimensionMismatchException:
if the number of rows in x is not equal to the number of rows in covariance
NonSquareMatrixException:
if the covariance matrix is not square
validateSampleDataback to summary
protected void validateSampleData(double[][] x, double[] y) throws MathIllegalArgumentException

Validates sample data. Checks that

  • Neither x nor y is null or empty;
  • The length (i.e. number of rows) of x equals the length of y
  • x has at least one more row than it has columns (i.e. there is sufficient data to estimate regression coefficients for each of the columns in x plus an intercept.
Parameters
x:double[][]

the [n,k] array representing the x data

y:double[]

the [n,1] array representing the y data

Exceptions
MathIllegalArgumentException:
if the number of rows of x is not larger than the number of columns + 1
NullArgumentException:
if x or y is null
DimensionMismatchException:
if x and y do not have the same length
NoDataException:
if x or y are zero-length