Implements ordinary least squares (OLS) to estimate the parameters of a multiple linear regression model.
The regression coefficients, b, satisfy the normal equations:
XT X b = XT y
To solve the normal equations, this implementation uses QR decomposition
of the X matrix. (See QRDecomposition for details on the
decomposition algorithm.) The X matrix, also known as the design matrix,
has rows corresponding to sample observations and columns corresponding to independent
variables. When the model is estimated using an intercept term (i.e. when
isNoIntercept is false as it is by default), the X
matrix includes an initial column identically equal to 1. We solve the normal equations
as follows:
XTX b = XT y
(QR)T (QR) b = (QR)Ty
RT (QTQ) R b = RT QT y
RT R b = RT QT y
(RT)-1 RT R b = (RT)-1 RT QT y
R b = QT y
Given Q and R, the last equation is solved by back-substitution.
| Modifier and Type | Field and Description |
|---|---|
| private QRDecomposition | qr
Cached QR decomposition of X matrix |
| private final double | threshold
Singularity threshold for QR decomposition |
| Access | Constructor and Description |
|---|---|
| public | |
| public | OLSMultipleLinearRegression(final double
the singularity threshold threshold)Create an empty OLSMultipleLinearRegression instance, using the given singularity threshold for the QR decomposition. |
| Modifier and Type | Method and Description |
|---|---|
| public double | Returns: adjusted R-Squared statisticReturns the adjusted R-squared statistic, defined by the formula R2adj = 1 - [SSR (n - 1)] / [SSTO (n - p)] |
| protected RealVector | Returns: betaImplements abstract org. Calculates the regression coefficients using OLS. |
| protected RealMatrix | Returns: The beta variance-covariance matrixImplements abstract org. Calculates the variance-covariance matrix of the regression parameters. |
| public RealMatrix | |
| public double | Returns: residual sum of squaresReturns the sum of squared residuals. |
| public double | Returns: R-square statisticReturns the R-Squared statistic, defined by the formula R2 = 1 - SSR / SSTO |
| public double | Returns: SSTO - the total sum of squaresReturns the sum of squared deviations of Y from its mean. |
| public void | newSampleData(double[]
the [n,1] array representing the y sample y, double[][] the [n,k] array representing the x sample x)Loads model x and y sample data, overriding any previous sample. |
| public void | newSampleData(double[]
input data array data, int number of observations (rows) nobs, int number of independent variables (columns, not counting y) nvars)Overrides org. 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)Overrides org. Loads new x sample data, overriding any previous data. |
| qr | back to summary |
|---|---|
| private QRDecomposition qr Cached QR decomposition of X matrix | |
| threshold | back to summary |
|---|---|
| private final double threshold Singularity threshold for QR decomposition | |
| OLSMultipleLinearRegression | back to summary |
|---|---|
| public OLSMultipleLinearRegression() Create an empty OLSMultipleLinearRegression instance. | |
| OLSMultipleLinearRegression | back to summary |
|---|---|
| public OLSMultipleLinearRegression(final double threshold) Create an empty OLSMultipleLinearRegression instance, using the given singularity threshold for the QR decomposition.
| |
| calculateAdjustedRSquared | back to summary |
|---|---|
| public double calculateAdjustedRSquared() Returns the adjusted R-squared statistic, defined by the formula R2adj = 1 - [SSR (n - 1)] / [SSTO (n - p)]where SSR is the sum of squared residuals,
SSTO is the total sum of squares, n is the number
of observations and p is the number of parameters estimated (including the intercept).
If the regression is estimated without an intercept term, what is returned is
If there is no variance in y, i.e., SSTO = 0, NaN is returned.
| |
| calculateBeta | back to summary |
|---|---|
| protected RealVector calculateBeta() Implements abstract org. Calculates the regression coefficients using OLS. Data for the model must have been successfully loaded using one of
the
| |
| calculateBetaVariance | back to summary |
|---|---|
| protected RealMatrix calculateBetaVariance() Implements abstract org. Calculates the variance-covariance matrix of the regression parameters. Var(b) = (XTX)-1 Uses QR decomposition to reduce (XTX)-1 to (RTR)-1, with only the top p rows of R included, where p = the length of the beta vector. Data for the model must have been successfully loaded using one of
the
| |
| calculateHat | back to summary |
|---|---|
| public RealMatrix calculateHat() Compute the "hat" matrix. The hat matrix is defined in terms of the design matrix X by X(XTX)-1XT The implementation here uses the QR decomposition to compute the hat matrix as Q IpQT where Ip is the p-dimensional identity matrix augmented by 0's. This computational formula is from "The Hat Matrix in Regression and ANOVA", David C. Hoaglin and Roy E. Welsch, The American Statistician, Vol. 32, No. 1 (Feb., 1978), pp. 17-22. Data for the model must have been successfully loaded using one of
the
| |
| calculateResidualSumOfSquares | back to summary |
|---|---|
| public double calculateResidualSumOfSquares() Returns the sum of squared residuals.
| |
| calculateRSquared | back to summary |
|---|---|
| public double calculateRSquared() Returns the R-Squared statistic, defined by the formula R2 = 1 - SSR / SSTOwhere SSR is the sum of squared residuals
and SSTO is the total sum of squares
If there is no variance in y, i.e., SSTO = 0, NaN is returned.
| |
| calculateTotalSumOfSquares | back to summary |
|---|---|
| public double calculateTotalSumOfSquares() Returns the sum of squared deviations of Y from its mean. If the model has no intercept term, The value returned by this method is the SSTO value used in
the
| |
| newSampleData | back to summary |
|---|---|
| public void newSampleData(double[] y, double[][] x) throws MathIllegalArgumentException Loads model x and y sample data, overriding any previous sample. Computes and caches QR decomposition of the X matrix.
| |
| newSampleData | back to summary |
|---|---|
| public void newSampleData(double[] data, int nobs, int nvars) Overrides org. Doc from org. 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
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 Throws IllegalArgumentException if any of the following preconditions fail:
This implementation computes and caches the QR decomposition of the X matrix.
| |
| newXSampleData | back to summary |
|---|---|
| protected void newXSampleData(double[][] x) Overrides org. Doc from org. Loads new x sample data, overriding any previous data. The inputx array should have one row for each sample
observation, with columns corresponding to independent variables.
For example, if
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. This implementation computes and caches the QR decomposition of the X matrix once it is successfully loaded.
| |