# NAG Library Routine Document

## 1Purpose

g02cff reorders the elements in two vectors (typically vectors of means and standard deviations), and the rows and columns in two matrices (typically either matrices of sums of squares and cross-products of deviations from means and Pearson product-moment correlation coefficients, or matrices of sums of squares and cross-products about zero and correlation-like coefficients).

## 2Specification

Fortran Interface
 Subroutine g02cff ( n, xbar, std, ssp, r, ldr,
 Integer, Intent (In) :: n, korder(n), ldssp, ldr Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: kwork(n) Real (Kind=nag_wp), Intent (Inout) :: xbar(n), std(n), ssp(ldssp,n), r(ldr,n)
#include nagmk26.h
 void g02cff_ (const Integer *n, const Integer korder[], double xbar[], double std[], double ssp[], const Integer *ldssp, double r[], const Integer *ldr, Integer kwork[], Integer *ifail)

## 3Description

Input to the routine consists of:
(a) A list of the order in which the $n$ variables are to be arranged on exit:
 $i1,i2,i3,…,in.$
(b) A vector of means:
 $x-1,x-2,x-3,…,x-n .$
(c) A vector of standard deviations:
 $s1,s2,s3,…,sn.$
(d) A matrix of sums of squares and cross-products of deviations from means:
 $S11 S12 S13 . . . S1n S21 S22 . S31 . . . . . . . Sn1 Sn2 . . . . Snn .$
(e) A matrix of correlation coefficients:
 $R11 R12 R13 . . . R1n R21 R22 . R31 . . . . . . . Rn1 Rn2 . . . . Rnn .$
On exit from the routine, these same vectors and matrices are reordered, in the manner specified, and contain the following information:
(i) The vector of means:
 $x-i1,x-i2,x-i3,…,x-in .$
(ii) The vector of standard deviations:
 $si1,si2,si3,…sin.$
(iii) The matrix of sums of squares and cross-products of deviations from means:
 $Si1i1 Si1i2 Si1i3 . . . Si1in Si2i1 Si2i2 . Si3i1 . . . . . . . Sini1 Sini2 . . . . Sinin .$
(iv) The matrix of correlation coefficients:
 $Ri1i1 Ri1i2 Ri1i3 . . . Ri1in Ri2i1 Ri2i2 . Ri3i1 . . . . . . . Rini1 Rini2 . . . . Rinin .$
Note:  for sums of squares of cross-products of deviations about zero and correlation-like coefficients ${S}_{ij}$ and ${R}_{ij}$ should be replaced by ${\stackrel{~}{S}}_{ij}$ and ${\stackrel{~}{R}}_{ij}$ in the description of the input and output above.

None.

## 5Arguments

1:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of variables in the input data.
Constraint: ${\mathbf{n}}\ge 2$.
2:     $\mathbf{korder}\left({\mathbf{n}}\right)$ – Integer arrayInput
On entry: ${\mathbf{korder}}\left(\mathit{i}\right)$ must be set to the number of the original variable which is to be the $\mathit{i}$th variable in the re-arranged data, for $\mathit{i}=1,2,\dots ,n$.
Constraint: $1\le {\mathbf{korder}}\left(\mathit{i}\right)\le {\mathbf{n}}$, for $\mathit{i}=1,2,\dots ,n$.
3:     $\mathbf{xbar}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: ${\mathbf{xbar}}\left(\mathit{i}\right)$ must be set to the mean of variable $\mathit{i}$, for $\mathit{i}=1,2,\dots ,n$.
On exit: ${\mathbf{xbar}}\left(\mathit{i}\right)$ contains the mean of variable $k$ where $k={\mathbf{korder}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,n$.
4:     $\mathbf{std}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: ${\mathbf{std}}\left(\mathit{i}\right)$ must be set to the standard deviation of variable $\mathit{i}$, for $\mathit{i}=1,2,\dots ,n$.
On exit: ${\mathbf{std}}\left(\mathit{i}\right)$ contains the standard deviation of variable $k$ where $k={\mathbf{korder}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,n$.
5:     $\mathbf{ssp}\left({\mathbf{ldssp}},{\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: ${\mathbf{ssp}}\left(\mathit{i},\mathit{j}\right)$ must be set to the sum of cross-products of deviations from means ${S}_{\mathit{i}\mathit{j}}$ (or about zero ${\stackrel{~}{S}}_{\mathit{i}\mathit{j}}$) for variables $\mathit{i}$ and $\mathit{j}$, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,n$.
On exit: ${\mathbf{ssp}}\left(i,j\right)$ contains the sum of cross-products of deviations from means ${S}_{kl}$ (or about zero ${\stackrel{~}{S}}_{kl}$) for variables $k$ and $l$, where $k={\mathbf{korder}}\left(i\right)$, and $l={\mathbf{korder}}\left(j\right)$, $i,j=1,2,\dots ,n$.
6:     $\mathbf{ldssp}$ – IntegerInput
On entry: the first dimension of the array ssp as declared in the (sub)program from which g02cff is called.
Constraint: ${\mathbf{ldssp}}\ge {\mathbf{n}}$.
7:     $\mathbf{r}\left({\mathbf{ldr}},{\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: ${\mathbf{r}}\left(\mathit{i},\mathit{j}\right)$ must be set to the Pearson product-moment correlation coefficient ${R}_{\mathit{i}\mathit{j}}$ (or the correlation-like coefficient ${\stackrel{~}{R}}_{\mathit{i}\mathit{j}}$) for variables $\mathit{i}$ and $\mathit{j}$, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,n$.
On exit: ${\mathbf{r}}\left(\mathit{i},\mathit{j}\right)$ contains the Pearson product-moment correlation coefficient ${R}_{kl}$ (or the correlation-like coefficient ${\stackrel{~}{R}}_{kl}$) for variables $k$ and $l$, where $k={\mathbf{korder}}\left(\mathit{i}\right)$ and $l={\mathbf{korder}}\left(\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,n$.
8:     $\mathbf{ldr}$ – IntegerInput
On entry: the first dimension of the array r as declared in the (sub)program from which g02cff is called.
Constraint: ${\mathbf{ldr}}\ge {\mathbf{n}}$.
9:     $\mathbf{kwork}\left({\mathbf{n}}\right)$ – Integer arrayWorkspace
10:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation 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, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 2$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{ldr}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldr}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{ldssp}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldssp}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=3$
On entry, $\mathit{i}=〈\mathit{\text{value}}〉$, ${\mathbf{korder}}\left(\mathit{i}\right)=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{korder}}\left(\mathit{i}\right)\ge 1$ and ${\mathbf{korder}}\left(\mathit{i}\right)\le {\mathbf{n}}$.
${\mathbf{ifail}}=4$
On entry, there is not a one-to-one correspondence between the old variables and the new variables; at least one of the original variables is not included in the new set, and consequently at least one other variable has been included more than once.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

Not applicable.

## 8Parallelism and Performance

g02cff is not threaded in any implementation.

The time taken by g02cff depends on $n$ and the amount of re-arrangement involved.
The routine is intended primarily for use when a set of variables is to be reordered for use in a regression, and is described accordingly. There is however no reason why the routine should not also be used to reorder vectors and matrices which contain any other non-statistical information; the matrices need not be symmetric.
The routine may be used either with sums of squares and cross-products of deviations from means and Pearson product-moment correlation coefficients in connection with a regression involving a constant, or with sums of squares and cross-products about zero and correlation-like coefficients in connection with a regression with no constant.

## 10Example

This example reads in the means, standard deviations, sums of squares and cross-products, and correlation coefficients for three variables. The vectors and matrices are reordered so that they contain the means, standard deviations, sums of squares and cross-products, and correlation coefficients for the first, third and second variables (in that order). Finally the reordered vectors and matrices are printed.

### 10.1Program Text

Program Text (g02cffe.f90)

### 10.2Program Data

Program Data (g02cffe.d)

### 10.3Program Results

Program Results (g02cffe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017