NAG Library Routine Document
G02CGF
1 Purpose
G02CGF performs a multiple linear regression on a set of variables whose means, sums of squares and cross-products of deviations from means, and Pearson product-moment correlation coefficients are given.
2 Specification
SUBROUTINE G02CGF ( |
N, K1, K, XBAR, SSP, LDSSP, R, LDR, RESULT, COEF, LDCOEF, CON, RINV, LDRINV, C, LDC, WKZ, LDWKZ, IFAIL) |
INTEGER |
N, K1, K, LDSSP, LDR, LDCOEF, LDRINV, LDC, LDWKZ, IFAIL |
REAL (KIND=nag_wp) |
XBAR(K1), SSP(LDSSP,K1), R(LDR,K1), RESULT(13), COEF(LDCOEF,3), CON(3), RINV(LDRINV,K), C(LDC,K), WKZ(LDWKZ,K) |
|
3 Description
G02CGF fits a curve of the form
to the data points
such that
The routine calculates the regression coefficients,
, the regression constant,
, and various other statistical quantities by minimizing
The actual data values
are not provided as input to the routine. Instead, input consists of:
(i) |
The number of cases, , on which the regression is based. |
(ii) |
The total number of variables, dependent and independent, in the regression, . |
(iii) |
The number of independent variables in the regression, . |
(iv) |
The means of all variables in the regression, both the independent variables and the dependent variable , which is the th variable: i.e., . |
(v) |
The by matrix [] of sums of squares and cross-products of deviations from means of all the variables in the regression; the terms involving the dependent variable, , appear in the th row and column. |
(vi) |
The by matrix [] of the Pearson product-moment correlation coefficients for all the variables in the regression; the correlations involving the dependent variable, , appear in the th row and column. |
The quantities calculated are:
(a) |
The inverse of the by partition of the matrix of correlation coefficients, [], involving only the independent variables. The inverse is obtained using an accurate method which assumes that this sub-matrix is positive definite. |
(b) |
The modified inverse matrix, , where
where is the th element of the inverse matrix of [] as described in (a) above. Each element of is thus the corresponding element of the matrix of correlation coefficients multiplied by the corresponding element of the inverse of this matrix, divided by the corresponding element of the matrix of sums of squares and cross-products of deviations from means. |
(c) |
The regression coefficients:
where is the sum of cross-products of deviations from means for the independent variable and the dependent variable . |
(d) |
The sum of squares attributable to the regression, , the sum of squares of deviations about the regression, , and the total sum of squares, :
- , the sum of squares of deviations from the mean for the dependent variable, ;
|
(e) |
The degrees of freedom attributable to the regression, , the degrees of freedom of deviations about the regression, , and the total degrees of freedom, :
|
(f) |
The mean square attributable to the regression, , and the mean square of deviations about the regression, :
|
(g) |
The values for the analysis of variance:
|
(h) |
The standard error estimate:
|
(i) |
The coefficient of multiple correlation, , the coefficient of multiple determination, and the coefficient of multiple determination corrected for the degrees of freedom, ;
|
(j) |
The standard error of the regression coefficients:
|
(k) |
The values for the regression coefficients:
|
(l) |
The regression constant, , its standard error, , and its value, :
|
4 References
Draper N R and Smith H (1985) Applied Regression Analysis (2nd Edition) Wiley
5 Parameters
- 1: N – INTEGERInput
On entry: the number of cases , used in calculating the sums of squares and cross-products and correlation coefficients.
- 2: K1 – INTEGERInput
On entry: the total number of variables, independent and dependent, , in the regression.
Constraint:
.
- 3: K – INTEGERInput
On entry: the number of independent variables in the regression.
Constraint:
.
- 4: XBAR(K1) – REAL (KIND=nag_wp) arrayInput
On entry: must be set to , the mean value of the th variable, for ; the mean of the dependent variable must be contained in .
- 5: SSP(LDSSP,K1) – REAL (KIND=nag_wp) arrayInput
On entry: must be set to , the sum of cross-products of deviations from means for the th and th variables, for and ; terms involving the dependent variable appear in row and column .
- 6: LDSSP – INTEGERInput
On entry: the first dimension of the array
SSP as declared in the (sub)program from which G02CGF is called.
Constraint:
.
- 7: R(LDR,K1) – REAL (KIND=nag_wp) arrayInput
On entry: must be set to , the Pearson product-moment correlation coefficient for the th and th variables, for and ; terms involving the dependent variable appear in row and column .
- 8: LDR – INTEGERInput
On entry: the first dimension of the array
R as declared in the (sub)program from which G02CGF is called.
Constraint:
.
- 9: RESULT() – REAL (KIND=nag_wp) arrayOutput
On exit: the following information:
| , the sum of squares attributable to the regression; |
| , the degrees of freedom attributable to the regression; |
| , the mean square attributable to the regression; |
| , the value for the analysis of variance; |
| , the sum of squares of deviations about the regression; |
| , the degrees of freedom of deviations about the regression; |
| , the mean square of deviations about the regression; |
| , the total sum of squares; |
| , the total degrees of freedom; |
| , the standard error estimate; |
| , the coefficient of multiple correlation; |
| , the coefficient of multiple determination; |
| , the coefficient of multiple determination corrected for the degrees of freedom. |
- 10: COEF(LDCOEF,) – REAL (KIND=nag_wp) arrayOutput
On exit: for
, the following information:
- , the regression coefficient for the th variable.
- , the standard error of the regression coefficient for the th variable.
- , the value of the regression coefficient for the th variable.
- 11: LDCOEF – INTEGERInput
On entry: the first dimension of the array
COEF as declared in the (sub)program from which G02CGF is called.
Constraint:
.
- 12: CON() – REAL (KIND=nag_wp) arrayOutput
On exit: the following information:
| , the regression constant; |
| , the standard error of the regression constant; |
| , the value for the regression constant. |
- 13: RINV(LDRINV,K) – REAL (KIND=nag_wp) arrayOutput
On exit: the inverse of the matrix of correlation coefficients for the independent variables; that is, the inverse of the matrix consisting of the first
rows and columns of
R.
- 14: LDRINV – INTEGERInput
On entry: the first dimension of the array
RINV as declared in the (sub)program from which G02CGF is called.
Constraint:
.
- 15: C(LDC,K) – REAL (KIND=nag_wp) arrayOutput
On exit: the modified inverse matrix, where
- , for and .
- 16: LDC – INTEGERInput
On entry: the first dimension of the array
C as declared in the (sub)program from which G02CGF is called.
Constraint:
.
- 17: WKZ(LDWKZ,K) – REAL (KIND=nag_wp) arrayWorkspace
- 18: LDWKZ – INTEGERInput
On entry: the first dimension of the array
WKZ as declared in the (sub)program from which G02CGF is called.
Constraint:
.
- 19: IFAIL – INTEGERInput/Output
-
On entry:
IFAIL must be set to
,
. If you are unfamiliar with this parameter you should refer to
Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
.
When the value is used it is essential to test the value of IFAIL on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6 Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
X04AAF).
Errors or warnings detected by the routine:
-
-
On entry, | , |
or | , |
or | , |
or | , |
or | , |
or | . |
The by partition of the matrix which is to be inverted is not positive definite.
The refinement following the actual inversion fails, indicating that the
by
partition of the matrix
, which is to be inverted, is ill-conditioned. The use of
G02DAF, which employs a different numerical technique, may avoid this difficulty (an extra ‘variable’ representing the constant term must be introduced for
G02DAF).
Unexpected error in
F04ABF.
7 Accuracy
The accuracy of any regression routine is almost entirely dependent on the accuracy of the matrix inversion method used. In G02CGF, it is the matrix of correlation coefficients rather than that of the sums of squares and cross-products of deviations from means that is inverted; this means that all terms in the matrix for inversion are of a similar order, and reduces the scope for computational error. For details on absolute accuracy, the relevant section of the document describing the inversion routine used,
F04ABF, should be consulted.
G02DAF uses a different method, based on
F04AMF, and that routine may well prove more reliable numerically. It does not handle missing values, nor does it provide the same output as this routine. (In particular it is necessary to include explicitly the constant in the regression equation as another ‘variable’.)
If, in calculating
,
, or any of the
(see
Section 3), the numbers involved are such that the result would be outside the range of numbers which can be stored by the machine, then the answer is set to the largest quantity which can be stored as a real variable, by means of a call to
X02ALF.
The time taken by G02CGF depends on .
This routine assumes that the matrix of correlation coefficients for the independent variables in the regression is positive definite; it fails if this is not the case.
This correlation matrix will in fact be positive definite whenever the correlation matrix and the sums of squares and cross-products (of deviations from means) matrix have been formed either without regard to missing values, or by eliminating
completely any cases involving missing values, for any variable. If, however, these matrices are formed by eliminating cases with missing values from only those calculations involving the variables for which the values are missing, no such statement can be made, and the correlation matrix may or may not be positive definite. You should be aware of the possible dangers of using correlation matrices formed in this way (see the
G02 Chapter Introduction), but if they nevertheless wish to carry out regression using such matrices, this routine is capable of handling the inversion of such matrices provided they are positive definite.
If a matrix is positive definite, its subsequent re-organisation by either
G02CEF or
G02CFF will not affect this property, and the new matrix can safely be used in this routine. Thus correlation matrices produced by any of
G02BAF,
G02BBF,
G02BGF or
G02BHF, even if subsequently modified by either
G02CEF or
G02CFF, can be handled by this routine.
It should be noted that in forming the sums of squares and cross-products matrix and the correlation matrix a column of constants should not be added to the data as an additional ‘variable’ in order to obtain a constant term in the regression. This routine automatically calculates the regression constant, , and any attempt to insert such a ‘dummy variable’ is likely to cause the routine to fail.
It should also be noted that the routine requires the dependent variable to be the last of the
variables whose statistics are provided as input to the routine. If this variable is not correctly positioned in the original data, the means, standard deviations, sums of squares and cross-products of deviations from means, and correlation coefficients can be manipulated by using
G02CEF or
G02CFF to reorder the variables as necessary.
9 Example
This example reads in the means, sums of squares and cross-products of deviations from means, and correlation coefficients for three variables. A multiple linear regression is then performed with the third and final variable as the dependent variable. Finally the results are printed.
9.1 Program Text
Program Text (g02cgfe.f90)
9.2 Program Data
Program Data (g02cgfe.d)
9.3 Program Results
Program Results (g02cgfe.r)