# NAG FL Interfaceg02bwf (ssqmat_​to_​corrmat)

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g02bwf ( m, r,
 Integer, Intent (In) :: m Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (Inout) :: r((m*m+m)/2)
#include <nag.h>
 void g02bwf_ (const Integer *m, double r[], Integer *ifail)
The routine may be called by the names g02bwf or nagf_correg_ssqmat_to_corrmat.

## 3Description

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.

## 5Arguments

1: $\mathbf{m}$Integer Input
On entry: $m$, the number of variables.
Constraint: ${\mathbf{m}}\ge 1$.
2: $\mathbf{r}\left(\left({\mathbf{m}}×{\mathbf{m}}+{\mathbf{m}}\right)/2\right)$Real (Kind=nag_wp) array Input/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: the Pearson product-moment correlation coefficients.
These are stored packed by column corresponding to the input cross-products.
3: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 4 in the Introduction to the NAG Library FL Interface 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 $1$ is recommended. Otherwise, because for this routine the values of the output arguments may be useful even if ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value 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:
Note: in some cases g02bwf may return useful information.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 1$.
${\mathbf{ifail}}=2$
A variable has a zero variance. All correlations involving the variable with zero variance will be returned as zero.
${\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 g02bwf is entirely dependent upon the accuracy of the elements of array r.

## 8Parallelism and Performance

g02bwf is not threaded in any implementation.

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.

## 10Example

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.

### 10.1Program Text

Program Text (g02bwfe.f90)

### 10.2Program Data

Program Data (g02bwfe.d)

### 10.3Program Results

Program Results (g02bwfe.r)