G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02BWF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G02BWF calculates a matrix of Pearson product-moment correlation coefficients from sums of squares and cross-products of deviations about the mean.

## 2  Specification

 SUBROUTINE G02BWF ( M, R, IFAIL)
 INTEGER M, IFAIL REAL (KIND=nag_wp) R((M*M+M)/2)

## 3  Description

G02BWF calculates a matrix of Pearson product-moment correlation coefficients from sums of squares and cross-products about the mean for observations on $m$ variables which can be computed by a single call to G02BUF or a series of calls to G02BTF. The sums of squares and cross-products are stored in an array packed by column and are overwritten by the correlation coefficients.
Let ${c}_{jk}$ be the cross-product of deviations from the mean, for $\mathit{j}=1,2,\dots ,m$ and $\mathit{k}=j,\dots ,m$, then the product-moment correlation coefficient, ${r}_{jk}$ is given by
 $rjk=cjkcjjckk .$

None.

## 5  Parameters

1:     M – INTEGERInput
On entry: $m$, the number of variables.
Constraint: ${\mathbf{M}}\ge 1$.
2:     R($\left({\mathbf{M}}×{\mathbf{M}}+{\mathbf{M}}\right)/2$) – REAL (KIND=nag_wp) arrayInput/Output
On entry: contains the upper triangular part of the sums of squares and cross-products matrix of deviations from the mean. These are stored packed by column, i.e., the cross-product between variable $j$ and $k$, $k\ge j$, is stored in ${\mathbf{R}}\left(\left(k×\left(k-1\right)/2+j\right)\right)$.
On exit: Pearson product-moment correlation coefficients.
These are stored packed by column corresponding to the input cross-products.
3:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. 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 $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: G02BWF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{M}}<1$.
${\mathbf{IFAIL}}=2$
A variable has a zero variance. All correlations involving the variable with zero variance will be returned as zero.

## 7  Accuracy

The accuracy of G02BWF is entirely dependent upon the accuracy of the elements of array R.

G02BWF may also be used to calculate the correlations between parameter estimates from the variance-covariance matrix of the parameter estimates as is given by several routines in this chapter.

## 9  Example

A program to calculate the correlation matrix from raw data. The sum of squares and cross-products about the mean are calculated from the raw data by a call to G02BUF. The correlation matrix is then calculated from these values.

### 9.1  Program Text

Program Text (g02bwfe.f90)

### 9.2  Program Data

Program Data (g02bwfe.d)

### 9.3  Program Results

Program Results (g02bwfe.r)