G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02BQF

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

G02BQF computes Kendall and/or Spearman nonparametric rank correlation coefficients for a set of data; the data array is preserved, and the ranks of the observations are not available on exit from the routine.

## 2  Specification

 SUBROUTINE G02BQF ( N, M, X, LDX, ITYPE, RR, LDRR, KWORKA, KWORKB, WORK1, WORK2, IFAIL)
 INTEGER N, M, LDX, ITYPE, LDRR, KWORKA(N), KWORKB(N), IFAIL REAL (KIND=nag_wp) X(LDX,M), RR(LDRR,M), WORK1(N), WORK2(N)

## 3  Description

The input data consists of $n$ observations for each of $m$ variables, given as an array
 $xij, i=1,2,…,nn≥2,j=1,2,…,mm≥2,$
where ${x}_{ij}$ is the $i$th observation on the $j$th variable.
The observations are first ranked, as follows.
For a given variable, $j$ say, each of the $n$ observations, ${x}_{1j},{x}_{2j},\dots ,{x}_{nj}$, has associated with it an additional number, the ‘rank’ of the observation, which indicates the magnitude of that observation relative to the magnitude of the other $n-1$ observations on that same variable.
The smallest observation for variable $j$ is assigned the rank $1$, the second smallest observation for variable $j$ the rank $2$, the third smallest the rank $3$, and so on until the largest observation for variable $j$ is given the rank $n$.
If a number of cases all have the same value for the given variable, $j$, then they are each given an ‘average’ rank – e.g., if in attempting to assign the rank $h+1$, $k$ observations were found to have the same value, then instead of giving them the ranks
 $h+1,h+2,…,h+k,$
all $k$ observations would be assigned the rank
 $2h+k+12$
and the next value in ascending order would be assigned the rank
 $h+k+ 1.$
The process is repeated for each of the $m$ variables.
Let ${y}_{ij}$ be the rank assigned to the observation ${x}_{ij}$ when the $j$th variable is being ranked.
The quantities calculated are:
(a) Kendall's tau rank correlation coefficients:
 $Rjk=∑h=1n∑i=1nsignyhj-yijsignyhk-yik nn-1-Tjnn-1-Tk , j,k=1,2,…,m,$
 and $\mathrm{sign}u=1$ if $u>0$ $\mathrm{sign}u=0$ if $u=0$ $\mathrm{sign}u=-1$ if $u<0$
and ${T}_{j}=\sum {t}_{j}\left({t}_{j}-1\right)$, ${t}_{j}$ being the number of ties of a particular value of variable $j$, and the summation being over all tied values of variable $j$.
(b) Spearman's rank correlation coefficients:
 $Rjk*=nn2-1-6∑i=1n yij-yik 2-12Tj*+Tk* nn2-1-Tj*nn2-1-Tk* , j,k=1,2,…,m,$
where ${T}_{j}^{*}=\sum {t}_{j}\left({t}_{j}^{2}-1\right)$ where ${t}_{j}$ is the number of ties of a particular value of variable $j$, and the summation is over all tied values of variable $j$.

## 4  References

Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the number of observations or cases.
Constraint: ${\mathbf{N}}\ge 2$.
2:     M – INTEGERInput
On entry: $m$, the number of variables.
Constraint: ${\mathbf{M}}\ge 2$.
3:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{X}}\left(\mathit{i},\mathit{j}\right)$ must be set to data value ${x}_{\mathit{i}\mathit{j}}$, the value of the $\mathit{i}$th observation on the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
4:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G02BQF is called.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
5:     ITYPE – INTEGERInput
On entry: the type of correlation coefficients which are to be calculated.
${\mathbf{ITYPE}}=-1$
Only Kendall's tau coefficients are calculated.
${\mathbf{ITYPE}}=0$
Both Kendall's tau and Spearman's coefficients are calculated.
${\mathbf{ITYPE}}=1$
Only Spearman's coefficients are calculated.
Constraint: ${\mathbf{ITYPE}}=-1$, $0$ or $1$.
6:     RR(LDRR,M) – REAL (KIND=nag_wp) arrayOutput
On exit: the requested correlation coefficients.
If only Kendall's tau coefficients are requested (${\mathbf{ITYPE}}=-1$), ${\mathbf{RR}}\left(j,k\right)$ contains Kendall's tau for the $j$th and $k$th variables.
If only Spearman's coefficients are requested (${\mathbf{ITYPE}}=1$), ${\mathbf{RR}}\left(j,k\right)$ contains Spearman's rank correlation coefficient for the $j$th and $k$th variables.
If both Kendall's tau and Spearman's coefficients are requested (${\mathbf{ITYPE}}=0$), the upper triangle of RR contains the Spearman coefficients and the lower triangle the Kendall coefficients. That is, for the $\mathit{j}$th and $\mathit{k}$th variables, where $\mathit{j}$ is less than $\mathit{k}$, ${\mathbf{RR}}\left(\mathit{j},\mathit{k}\right)$ contains the Spearman rank correlation coefficient, and ${\mathbf{RR}}\left(\mathit{k},\mathit{j}\right)$ contains Kendall's tau, for $\mathit{j}=1,2,\dots ,m$ and $\mathit{k}=1,2,\dots ,m$.
(Diagonal terms, ${\mathbf{RR}}\left(j,j\right)$, are unity for all three values of ITYPE.)
7:     LDRR – INTEGERInput
On entry: the first dimension of the array RR as declared in the (sub)program from which G02BQF is called.
Constraint: ${\mathbf{LDRR}}\ge {\mathbf{M}}$.
8:     KWORKA(N) – INTEGER arrayWorkspace
9:     KWORKB(N) – INTEGER arrayWorkspace
10:   WORK1(N) – REAL (KIND=nag_wp) arrayWorkspace
11:   WORK2(N) – REAL (KIND=nag_wp) arrayWorkspace
12:   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, if you are not familiar with this parameter, 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).

## 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).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}<2$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{M}}<2$.
${\mathbf{IFAIL}}=3$
 On entry, ${\mathbf{LDX}}<{\mathbf{N}}$, or ${\mathbf{LDRR}}<{\mathbf{M}}$.
${\mathbf{IFAIL}}=4$
 On entry, ${\mathbf{ITYPE}}<-1$, or ${\mathbf{ITYPE}}>1$.

## 7  Accuracy

The method used is believed to be stable.

The time taken by G02BQF depends on $n$ and $m$.

## 9  Example

This example reads in a set of data consisting of nine observations on each of three variables. The program then calculates and prints both Kendall's tau and Spearman's rank correlation coefficients for all three variables.

### 9.1  Program Text

Program Text (g02bqfe.f90)

### 9.2  Program Data

Program Data (g02bqfe.d)

### 9.3  Program Results

Program Results (g02bqfe.r)