NAG FL Interface
g02cgf (linregm_​coeffs_​const)

Settings help

FL Name Style:


FL Specification Language:


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

Fortran Interface
Subroutine g02cgf ( n, k1, k, xbar, ssp, ldssp, r, ldr, result, coef, ldcoef, con, rinv, ldrinv, c, ldc, wkz, ldwkz, ifail)
Integer, Intent (In) :: n, k1, k, ldssp, ldr, ldcoef, ldrinv, ldc, ldwkz
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: xbar(k+1), ssp(ldssp,k+1)
Real (Kind=nag_wp), Intent (Inout) :: r(ldr,k+1), coef(ldcoef,3), rinv(ldrinv,k), c(ldc,k), wkz(ldwkz,k)
Real (Kind=nag_wp), Intent (Out) :: result(13), con(3)
C Header Interface
#include <nag.h>
void  g02cgf_ (const Integer *n, const Integer *k1, const Integer *k, const double xbar[], const double ssp[], const Integer *ldssp, double r[], const Integer *ldr, double result[], double coef[], const Integer *ldcoef, double con[], double rinv[], const Integer *ldrinv, double c[], const Integer *ldc, double wkz[], const Integer *ldwkz, Integer *ifail)
The routine may be called by the names g02cgf or nagf_correg_linregm_coeffs_const.

3 Description

g02cgf fits a curve of the form
y=a+b1x1+b2x2++bkxk  
to the data points
(x11,x21,,xk1,y1) (x12,x22,,xk2,y2) (x1n,x2n,,xkn,yn)  
such that
yi=a+b1x1i+b2x2i++bkxki+ei,  i=1,2,,n.  
The routine calculates the regression coefficients, b1,b2,,bk, the regression constant, a, and various other statistical quantities by minimizing
i=1nei2.  
The actual data values (x1i,x2i,,xki,yi) are not provided as input to the routine. Instead, input consists of:
  1. (i)The number of cases, n, on which the regression is based.
  2. (ii)The total number of variables, dependent and independent, in the regression, (k+1).
  3. (iii)The number of independent variables in the regression, k.
  4. (iv)The means of all k+1 variables in the regression, both the independent variables (x1,x2,,xk) and the dependent variable (y), which is the (k+1)th variable: i.e., x¯1,x¯2,,x¯k,y¯.
  5. (v)The (k+1)×(k+1) matrix [Sij] of sums of squares and cross-products of deviations from means of all the variables in the regression; the terms involving the dependent variable, y, appear in the (k+1)th row and column.
  6. (vi)The (k+1)×(k+1) matrix [Rij] of the Pearson product-moment correlation coefficients for all the variables in the regression; the correlations involving the dependent variable, y, appear in the (k+1)th row and column.
The quantities calculated are:
  1. (a)The inverse of the k×k partition of the matrix of correlation coefficients, [Rij], involving only the independent variables. The inverse is obtained using an accurate method which assumes that this sub-matrix is positive definite.
  2. (b)The modified inverse matrix, C=[cij], where
    cij=RijrijSij,  i,j=1,2,,k,  
    where rij is the (i,j)th element of the inverse matrix of [Rij] as described in (a) above. Each element of C 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.
  3. (c)The regression coefficients:
    bi=j=ikcijSj(k+1),  i=1,2,,k,  
    where Sj(k+1) is the sum of cross-products of deviations from means for the independent variable xj and the dependent variable y.
  4. (d)The sum of squares attributable to the regression, SSR, the sum of squares of deviations about the regression, SSD, and the total sum of squares, SST:
    • SST=S(k+1)(k+1), the sum of squares of deviations from the mean for the dependent variable, y;
    • SSR=j=1kbjSj(k+1);  SSD=SST-SSR
  5. (e)The degrees of freedom attributable to the regression, DFR, the degrees of freedom of deviations about the regression, DFD, and the total degrees of freedom, DFT:
    DFR=k;  DFD=n-k-1;  DFT=n-1.  
  6. (f)The mean square attributable to the regression, MSR, and the mean square of deviations about the regression, MSD:
    MSR=SSR/DFR;  MSD=SSD/DFD.  
  7. (g)The F values for the analysis of variance:
    F=MSR/MSD.  
  8. (h)The standard error estimate:
    s=MSD.  
  9. (i)The coefficient of multiple correlation, R, the coefficient of multiple determination, R2 and the coefficient of multiple determination corrected for the degrees of freedom, R¯2;
    R=1-SSD SST ;  R2=1-SSD SST ;  R¯2=1-SSD×DFT SST×DFD .  
  10. (j)The standard error of the regression coefficients:
    se(bi)=MSD×cii,   i= 1,2,,k.  
  11. (k)The t values for the regression coefficients:
    t(bi)=bi se(bi) ,  i=1,2,,k.  
  12. (l)The regression constant, a, its standard error, se(a), and its t value, t(a):
    a=y¯-i=1kbix¯i;  se(a)=MSD×(1n+i=1kj=1kx¯icijx¯j) ;  t(a)=ase(a) .  

4 References

Draper N R and Smith H (1985) Applied Regression Analysis (2nd Edition) Wiley

5 Arguments

1: n Integer Input
On entry: the number of cases n, used in calculating the sums of squares and cross-products and correlation coefficients.
2: k1 Integer Input
On entry: is no longer required by g02cgf but is retained for backwards compatibility.
3: k Integer Input
On entry: k, the number of independent variables in the regression.
Constraint: 1k<n-1.
4: xbar(k+1) Real (Kind=nag_wp) array Input
On entry: xbar(i) must be set to x¯i, the mean value of the ith variable, for i=1,2,,k+1; the mean of the dependent variable must be contained in xbar(k+1).
5: ssp(ldssp,k+1) Real (Kind=nag_wp) array Input
On entry: ssp(i,j) must be set to Sij, the sum of cross-products of deviations from means for the ith and jth variables, for i=1,2,,k+1 and j=1,2,,k+1; terms involving the dependent variable appear in row k+1 and column k+1.
6: ldssp Integer Input
On entry: the first dimension of the array ssp as declared in the (sub)program from which g02cgf is called.
Constraint: ldsspk+1.
7: r(ldr,k+1) Real (Kind=nag_wp) array Input
On entry: r(i,j) must be set to Rij, the Pearson product-moment correlation coefficient for the ith and jth variables, for i=1,2,,k+1 and j=1,2,,k+1; terms involving the dependent variable appear in row k+1 and column k+1.
8: ldr Integer Input
On entry: the first dimension of the array r as declared in the (sub)program from which g02cgf is called.
Constraint: ldrk+1.
9: result(13) Real (Kind=nag_wp) array Output
On exit: the following information:
result(1) SSR, the sum of squares attributable to the regression;
result(2) DFR, the degrees of freedom attributable to the regression;
result(3) MSR, the mean square attributable to the regression;
result(4) F, the F value for the analysis of variance;
result(5) SSD, the sum of squares of deviations about the regression;
result(6) DFD, the degrees of freedom of deviations about the regression;
result(7) MSD, the mean square of deviations about the regression;
result(8) SST, the total sum of squares;
result(9) DFT, the total degrees of freedom;
result(10) s, the standard error estimate;
result(11) R, the coefficient of multiple correlation;
result(12) R2, the coefficient of multiple determination;
result(13) R¯2, the coefficient of multiple determination corrected for the degrees of freedom.
10: coef(ldcoef,3) Real (Kind=nag_wp) array Output
On exit: for i=1,2,,k, the following information:
coef(i,1)
bi, the regression coefficient for the ith variable.
coef(i,2)
se(bi), the standard error of the regression coefficient for the ith variable.
coef(i,3)
t(bi), the t value of the regression coefficient for the ith variable.
11: ldcoef Integer Input
On entry: the first dimension of the array coef as declared in the (sub)program from which g02cgf is called.
Constraint: ldcoefk.
12: con(3) Real (Kind=nag_wp) array Output
On exit: the following information:
con(1) a, the regression constant;
con(2) se(a), the standard error of the regression constant;
con(3) t(a), the t value for the regression constant.
13: rinv(ldrinv,k) Real (Kind=nag_wp) array Output
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 k rows and columns of r.
14: ldrinv Integer Input
On entry: the first dimension of the array rinv as declared in the (sub)program from which g02cgf is called.
Constraint: ldrinvk.
15: c(ldc,k) Real (Kind=nag_wp) array Output
On exit: the modified inverse matrix, where
  • c(i,j)=r(i,j)×rinv(i,j)/ssp(i,j), for i=1,2,,k and j=1,2,,k.
16: ldc Integer Input
On entry: the first dimension of the array c as declared in the (sub)program from which g02cgf is called.
Constraint: ldck.
17: wkz(ldwkz,k) Real (Kind=nag_wp) array Workspace
18: ldwkz Integer Input
On entry: the first dimension of the array wkz as declared in the (sub)program from which g02cgf is called.
Constraint: ldwkzk.
19: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. When the value -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6 Error Indicators and Warnings

If on entry ifail=0 or −1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
ifail=1
On entry, k=value.
Constraint: k1.
ifail=3
On entry, n=value and k=value.
Constraint: n>k+1.
ifail=4
On entry, ldc=value and k=value.
Constraint: ldck.
On entry, ldcoef=value and k=value.
Constraint: ldcoefk.
On entry, ldr=value and k=value.
Constraint: ldrk+1.
On entry, ldrinv=value and k=value.
Constraint: ldrinvk.
On entry, ldssp=value and k=value.
Constraint: ldsspk+1.
On entry, ldwkz=value and k=value.
Constraint: ldwkzk.
ifail=5
The k by k partition of r which requires inversion is not positive definite.
ifail=6
The refinement following inversion has failed. This indicates that the k×k partition of the matrix R, 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).
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

The accuracy of g02cgf is almost entirely dependent on the accuracy of the matrix inversion method used. As g02cgf works with the matrix of correlation coefficients rather than that of the sums of squares and cross-products of deviations from means all terms in the matrix being inverted are of a similar order and, therefore, the scope for computational error is reduced. An alternative, and potentially more numerically reliable, routine is g02daf. g02daf works directly with the data matrix and, therefore, avoids explicitly performing a matrix inversion. However, g02daf does not handle missing values, nor does it provide the same output as this routine.
If, in calculating F, t(a), or any of the t(bi) (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.

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g02cgf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g02cgf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9 Further Comments

The time taken by g02cgf depends on k.
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, a, 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 k+1 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.

10 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.

10.1 Program Text

Program Text (g02cgfe.f90)

10.2 Program Data

Program Data (g02cgfe.d)

10.3 Program Results

Program Results (g02cgfe.r)