# NAG FL Interfaceg02chf (linregm_​coeffs_​noconst)

## 1Purpose

g02chf performs a multiple linear regression with no constant on a set of variables whose sums of squares and cross-products about zero and correlation-like coefficients are given.

## 2Specification

Fortran Interface
 Subroutine g02chf ( n, k1, k, sspz, rz, ldrz, coef, rznv, cz, ldcz, wkz,
 Integer, Intent (In) :: n, k1, k, ldsspz, ldrz, ldcoef, ldrznv, ldcz, ldwkz Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: sspz(ldsspz,k+1) Real (Kind=nag_wp), Intent (Inout) :: rz(ldrz,k+1), coef(ldcoef,3), rznv(ldrznv,k), cz(ldcz,k), wkz(ldwkz,k) Real (Kind=nag_wp), Intent (Out) :: result(13)
#include <nag.h>
 void g02chf_ (const Integer *n, const Integer *k1, const Integer *k, const double sspz[], const Integer *ldsspz, double rz[], const Integer *ldrz, double result[], double coef[], const Integer *ldcoef, double rznv[], const Integer *ldrznv, double cz[], const Integer *ldcz, double wkz[], const Integer *ldwkz, Integer *ifail)
The routine may be called by the names g02chf or nagf_correg_linregm_coeffs_noconst.

## 3Description

g02chf fits a curve of the form
 $y=b1x1+b2x2+⋯+bkxk$
to the data points
 $x11,x21,…,xk1,y1 x12,x22,…,xk2,y2 ⋮ x1n,x2n,…,xkn,yn$
such that
 $yi=b1x1i+b2x2i+⋯+bkxki+ei, i=1,2,…,n.$
The routine calculates the regression coefficients, ${b}_{1},{b}_{2},\dots ,{b}_{k}$, (and various other statistical quantities) by minimizing
 $∑i=1nei2.$
The actual data values $\left({x}_{1i},{x}_{2i},\dots ,{x}_{ki},{y}_{i}\right)$ are not provided as input to the routine. Instead, input to the routine 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, $\left(k+1\right)$.
3. (iii)The number of independent variables in the regression, $k$.
4. (iv)The $\left(k+1\right)$ by $\left(k+1\right)$ matrix $\left[{\stackrel{~}{S}}_{ij}\right]$ of sums of squares and cross-products about zero of all the variables in the regression; the terms involving the dependent variable, $y$, appear in the $\left(k+1\right)$th row and column.
5. (v)The $\left(k+1\right)$ by $\left(k+1\right)$ matrix $\left[{\stackrel{~}{R}}_{ij}\right]$ of correlation-like coefficients for all the variables in the regression; the correlations involving the dependent variable, $y$, appear in the $\left(k+1\right)$th row and column.
The quantities calculated are:
1. (a)The inverse of the $k$ by $k$ partition of the matrix of correlation-like coefficients, $\left[{\stackrel{~}{R}}_{ij}\right]$, involving only the independent variables. The inverse is obtained using an accurate method which assumes that this sub-matrix is positive definite (see Section 9).
2. (b)The modified matrix, $C=\left[{c}_{ij}\right]$, where
 $cij=R~ijr~ijS~ij, i,j=1,2,…,k,$
where ${\stackrel{~}{r}}^{ij}$ is the $\left(i,j\right)$th element of the inverse matrix of $\left[{\stackrel{~}{R}}_{ij}\right]$ as described in (a) above. Each element of $C$ is thus the corresponding element of the matrix of correlation-like 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 about zero.
3. (c)The regression coefficients:
 $bi=∑j=1kcijS~jk+1, i=1,2,…,k,$
where ${\stackrel{~}{S}}_{j\left(k+1\right)}$ is the sum of cross-products about zero for the independent variable ${x}_{j}$ 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={\stackrel{~}{S}}_{\left(k+1\right)\left(k+1\right)}$, the sum of squares about zero for the dependent variable, $y$;
• $SSR=\sum _{j=1}^{k}{b}_{j}{\stackrel{~}{S}}_{j\left(k+1\right)}\text{; }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; DFT=n.$
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$ value 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, ${R}^{2}$, and the coefficient of multiple determination corrected for the degrees of freedom, ${\overline{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:
 $sebi=MSD×cii, i= 1,2,…,k.$
11. (k)The $t$ values for the regression coefficients:
 $tbi=bi sebi , i=1,2,…,k.$

## 4References

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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the number of cases used in calculating the sums of squares and cross-products and correlation-like coefficients.
2: $\mathbf{k1}$Integer Input
On entry: is no longer required by g02chf but is retained for backwards compatibility.
3: $\mathbf{k}$Integer Input
On entry: $k$, the number of independent variables in the regression.
Constraint: $1\le {\mathbf{k}}\le {\mathbf{n}}-1$.
4: $\mathbf{sspz}\left({\mathbf{ldsspz}},{\mathbf{k}}+1\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{sspz}}\left(\mathit{i},\mathit{j}\right)$ must be set to ${\stackrel{~}{S}}_{\mathit{i}\mathit{j}}$, the sum of cross-products about zero for the $\mathit{i}$th and $\mathit{j}$th variables, for $\mathit{i}=1,2,\dots ,k+1$ and $\mathit{j}=1,2,\dots ,k+1$; terms involving the dependent variable appear in row $k+1$ and column $k+1$.
5: $\mathbf{ldsspz}$Integer Input
On entry: the first dimension of the array sspz as declared in the (sub)program from which g02chf is called.
Constraint: ${\mathbf{ldsspz}}\ge {\mathbf{k}}+1$.
6: $\mathbf{rz}\left({\mathbf{ldrz}},{\mathbf{k}}+1\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{rz}}\left(\mathit{i},\mathit{j}\right)$ must be set to ${\stackrel{~}{R}}_{\mathit{i}\mathit{j}}$, the correlation-like coefficient for the $\mathit{i}$th and $\mathit{j}$th variables, for $\mathit{i}=1,2,\dots ,k+1$ and $\mathit{j}=1,2,\dots ,k+1$; coefficients involving the dependent variable appear in row $k+1$ and column $k+1$.
7: $\mathbf{ldrz}$Integer Input
On entry: the first dimension of the array rz as declared in the (sub)program from which g02chf is called.
Constraint: ${\mathbf{ldrz}}\ge {\mathbf{k}}+1$.
8: $\mathbf{result}\left(13\right)$Real (Kind=nag_wp) array Output
On exit: the following information:
 ${\mathbf{result}}\left(1\right)$ $SSR$, the sum of squares attributable to the regression; ${\mathbf{result}}\left(2\right)$ $DFR$, the degrees of freedom attributable to the regression; ${\mathbf{result}}\left(3\right)$ $MSR$, the mean square attributable to the regression; ${\mathbf{result}}\left(4\right)$ $F$, the $F$ value for the analysis of variance; ${\mathbf{result}}\left(5\right)$ $SSD$, the sum of squares of deviations about the regression; ${\mathbf{result}}\left(6\right)$ $DFD$, the degrees of freedom of deviations about the regression; ${\mathbf{result}}\left(7\right)$ $MSD$, the mean square of deviations about the regression; ${\mathbf{result}}\left(8\right)$ $SST$, the total sum of squares; ${\mathbf{result}}\left(9\right)$ $DFT$, the total degrees of freedom; ${\mathbf{result}}\left(10\right)$ $s$, the standard error estimate; ${\mathbf{result}}\left(11\right)$ $R$, the coefficient of multiple correlation; ${\mathbf{result}}\left(12\right)$ ${R}^{2}$, the coefficient of multiple determination; ${\mathbf{result}}\left(13\right)$ ${\overline{R}}^{2}$, the coefficient of multiple determination corrected for the degrees of freedom.
9: $\mathbf{coef}\left({\mathbf{ldcoef}},3\right)$Real (Kind=nag_wp) array Output
On exit: for $i=1,2,\dots ,k$, the following information:
${\mathbf{coef}}\left(i,1\right)$
${b}_{i}$, the regression coefficient for the $i$th variable.
${\mathbf{coef}}\left(i,2\right)$
$se\left({b}_{i}\right)$, the standard error of the regression coefficient for the $i$th variable.
${\mathbf{coef}}\left(i,3\right)$
$t\left({b}_{i}\right)$, the $t$ value of the regression coefficient for the $i$th variable.
10: $\mathbf{ldcoef}$Integer Input
On entry: the first dimension of the array coef as declared in the (sub)program from which g02chf is called.
Constraint: ${\mathbf{ldcoef}}\ge {\mathbf{k}}$.
11: $\mathbf{rznv}\left({\mathbf{ldrznv}},{\mathbf{k}}\right)$Real (Kind=nag_wp) array Output
On exit: the inverse of the matrix of correlation-like coefficients for the independent variables; that is, the inverse of the matrix consisting of the first $k$ rows and columns of rz.
12: $\mathbf{ldrznv}$Integer Input
On entry: the first dimension of the array rznv as declared in the (sub)program from which g02chf is called.
Constraint: ${\mathbf{ldrznv}}\ge {\mathbf{k}}$.
13: $\mathbf{cz}\left({\mathbf{ldcz}},{\mathbf{k}}\right)$Real (Kind=nag_wp) array Output
On exit: the modified inverse matrix, $C$, where
 $czij=rzij×rznvij sspzij , i,j=1,2,…,k.$
14: $\mathbf{ldcz}$Integer Input
On entry: the first dimension of the array cz as declared in the (sub)program from which g02chf is called.
Constraint: ${\mathbf{ldcz}}\ge {\mathbf{k}}$.
15: $\mathbf{wkz}\left({\mathbf{ldwkz}},{\mathbf{k}}\right)$Real (Kind=nag_wp) array Workspace
16: $\mathbf{ldwkz}$Integer Input
On entry: the first dimension of the array wkz as declared in the (sub)program from which g02chf is called.
Constraint: ${\mathbf{ldwkz}}\ge {\mathbf{k}}$.
17: $\mathbf{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 $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{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:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{k}}\ge 1$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge {\mathbf{k}}+1$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{ldcoef}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldcoef}}\ge {\mathbf{k}}$.
On entry, ${\mathbf{ldcz}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldcz}}\ge {\mathbf{k}}$.
On entry, ${\mathbf{ldrz}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldrz}}\ge {\mathbf{k}}+1$.
On entry, ${\mathbf{ldrznv}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldrznv}}\ge {\mathbf{k}}$.
On entry, ${\mathbf{ldsspz}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldsspz}}\ge {\mathbf{k}}+1$.
On entry, ${\mathbf{ldwkz}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldwkz}}\ge {\mathbf{k}}$.
${\mathbf{ifail}}=5$
The $\mathit{K}$ by $\mathit{K}$ partition of rz which requires inversion is not positive definite.
${\mathbf{ifail}}=6$
The refinement following the actual inversion has failed. This indicates that the $k$ by $k$ partition of the matrix held in rz, which is to be inverted, is ill-conditioned. The use of g02daf, which employs a different numerical technique, may avoid the difficulty.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{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.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

The accuracy of g02chf is almost entirely dependent on the accuracy of the matrix inversion method used. As g02chf 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$ or any of the $t\left({b}_{i}\right)$ (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.

## 8Parallelism and Performance

g02chf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g02chf 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.

The time taken by g02chf depends on $k$.
This routine assumes that the matrix of correlation-like 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-like matrix and the sums of squares and cross-products (about zero) 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-like 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 regressions 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 of 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 g02bdf, g02bef, g02bkf or g02blf, even if subsequently modified by either g02cef or g02cff, can be handled by this routine.
It should 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 about zero, and correlation-like coefficients can be manipulated by using g02cef or g02cff to reorder the variables as necessary.

## 10Example

This example reads in the sums of squares and cross-products about zero, and correlation-like coefficients for three variables. A multiple linear regression with no constant is then performed with the third and final variable as the dependent variable. Finally the results are printed.

### 10.1Program Text

Program Text (g02chfe.f90)

### 10.2Program Data

Program Data (g02chfe.d)

### 10.3Program Results

Program Results (g02chfe.r)