Class AbstractMultipleLinearRegression
- All Implemented Interfaces:
MultipleLinearRegression
- Direct Known Subclasses:
GLSMultipleLinearRegression,OLSMultipleLinearRegression
- Since:
- 2.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract RealVectorCalculates the beta of multiple linear regression in matrix notation.protected abstract RealMatrixCalculates the beta variance of multiple linear regression in matrix notation.protected doubleCalculates the variance of the error term.protected RealVectorCalculates the residuals of multiple linear regression in matrix notation.protected doubleCalculates the variance of the y values.doubleEstimates the variance of the error.doubleReturns the variance of the regressand, ie Var(y).double[]Estimates the regression parameters b.double[]Returns the standard errors of the regression parameters.double[][]Estimates the variance of the regression parameters, ie Var(b).doubleEstimates the standard error of the regression.double[]Estimates the residuals, ie u = y - X*b.protected RealMatrixgetX()protected RealVectorgetY()booleanvoidnewSampleData(double[] data, int nobs, int nvars) Loads model x and y sample data from a flat input array, overriding any previous sample.protected voidnewXSampleData(double[][] x) Loads new x sample data, overriding any previous data.protected voidnewYSampleData(double[] y) Loads new y sample data, overriding any previous data.voidsetNoIntercept(boolean noIntercept) protected voidvalidateCovarianceData(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.protected voidvalidateSampleData(double[][] x, double[] y) Validates sample data.
-
Constructor Details
-
AbstractMultipleLinearRegression
public AbstractMultipleLinearRegression()
-
-
Method Details
-
getX
- Returns:
- the X sample data.
-
getY
- Returns:
- the Y sample data.
-
isNoIntercept
- Returns:
- true if the model has no intercept term; false otherwise
- Since:
- 2.2
-
setNoIntercept
- Parameters:
noIntercept- true means the model is to be estimated without an intercept term- Since:
- 2.2
-
newSampleData
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
dataarray containing the sequence of values (1, 2, 3, 4, 5, 6, 7, 8, 9) withnobs = 3andnvars = 2creates 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()istrue, 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:
datacannot be nulldata.length = nobs * (nvars + 1)nobs > nvars
- Parameters:
data- input data arraynobs- number of observations (rows)nvars- number of independent variables (columns, not counting y)- Throws:
NullArgumentException- if the data array is nullDimensionMismatchException- if the length of the data array is not equal tonobs * (nvars + 1)InsufficientDataException- ifnobsis less thannvars + 1
-
newYSampleData
Loads new y sample data, overriding any previous data.- Parameters:
y- the array representing the y sample- Throws:
NullArgumentException- if y is nullNoDataException- if y is empty
-
newXSampleData
Loads new x sample data, overriding any previous data.
The inputxarray should have one row for each sample observation, with columns corresponding to independent variables. For example, if
thenx = new double[][] {{1, 2}, {3, 4}, {5, 6}}setXSampleData(x)results in a model with two independent variables and 3 observations:x[0] x[1] ---------- 1 2 3 4 5 6Note 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- the rectangular array representing the x sample- Throws:
NullArgumentException- if x is nullNoDataException- if x is emptyDimensionMismatchException- if x is not rectangular
-
validateSampleData
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- the [n,k] array representing the x datay- the [n,1] array representing the y data- Throws:
NullArgumentException- ifxoryis nullDimensionMismatchException- ifxandydo not have the same lengthNoDataException- ifxoryare zero-lengthMathIllegalArgumentException- if the number of rows ofxis not larger than the number of columns + 1
-
validateCovarianceData
Validates that the x data and covariance matrix have the same number of rows and that the covariance matrix is square.- Parameters:
x- the [n,k] array representing the x samplecovariance- the [n,n] array representing the covariance matrix- Throws:
DimensionMismatchException- if the number of rows in x is not equal to the number of rows in covarianceNonSquareMatrixException- if the covariance matrix is not square
-
estimateRegressionParameters
Estimates the regression parameters b.- Specified by:
estimateRegressionParametersin interfaceMultipleLinearRegression- Returns:
- The [k,1] array representing b
-
estimateResiduals
Estimates the residuals, ie u = y - X*b.- Specified by:
estimateResidualsin interfaceMultipleLinearRegression- Returns:
- The [n,1] array representing the residuals
-
estimateRegressionParametersVariance
Estimates the variance of the regression parameters, ie Var(b).- Specified by:
estimateRegressionParametersVariancein interfaceMultipleLinearRegression- Returns:
- The [k,k] array representing the variance of b
-
estimateRegressionParametersStandardErrors
Returns the standard errors of the regression parameters.- Specified by:
estimateRegressionParametersStandardErrorsin interfaceMultipleLinearRegression- Returns:
- standard errors of estimated regression parameters
-
estimateRegressandVariance
Returns the variance of the regressand, ie Var(y).- Specified by:
estimateRegressandVariancein interfaceMultipleLinearRegression- Returns:
- The double representing the variance of y
-
estimateErrorVariance
Estimates the variance of the error.- Returns:
- estimate of the error variance
- Since:
- 2.2
-
estimateRegressionStandardError
Estimates the standard error of the regression.- Returns:
- regression standard error
- Since:
- 2.2
-
calculateBeta
Calculates the beta of multiple linear regression in matrix notation.- Returns:
- beta
-
calculateBetaVariance
Calculates the beta variance of multiple linear regression in matrix notation.- Returns:
- beta variance
-
calculateYVariance
Calculates the variance of the y values.- Returns:
- Y variance
-
calculateErrorVariance
Calculates the variance of the error term.
Uses the formulavar(u) = u · u / (n - k)
where n and k are the row and column dimensions of the design matrix X.- Returns:
- error variance estimate
- Since:
- 2.2
-
calculateResiduals
Calculates the residuals of multiple linear regression in matrix notation.u = y - X * b
- Returns:
- The residuals [n,1] matrix
-