﻿ g02bq Method
g02bq 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 method.

# Syntax

C#
```public static void g02bq(
int n,
int m,
double[,] x,
int itype,
double[,] rr,
out int ifail
)```
Visual Basic
```Public Shared Sub g02bq ( _
n As Integer, _
m As Integer, _
x As Double(,), _
itype As Integer, _
rr As Double(,), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g02bq(
int n,
int m,
array<double,2>^ x,
int itype,
array<double,2>^ rr,
[OutAttribute] int% ifail
)```
F#
```static member g02bq :
n : int *
m : int *
x : float[,] *
itype : int *
rr : float[,] *
ifail : int byref -> unit
```

#### Parameters

n
Type: System..::..Int32
On entry: $n$, the number of observations or cases.
Constraint: ${\mathbf{n}}\ge 2$.
m
Type: System..::..Int32
On entry: $m$, the number of variables.
Constraint: ${\mathbf{m}}\ge 2$.
x
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: ${\mathbf{x}}\left[\mathit{i}-1,\mathit{j}-1\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$.
itype
Type: System..::..Int32
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$.
rr
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{m}}$
On exit: the requested correlation coefficients.
If only Kendall's tau coefficients are requested (${\mathbf{itype}}=-1$), ${\mathbf{rr}}\left[j-1,k-1\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-1,k-1\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}-1,\mathit{k}-1\right]$ contains the Spearman rank correlation coefficient, and ${\mathbf{rr}}\left[\mathit{k}-1,\mathit{j}-1\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-1,j-1\right]$, are unity for all three values of itype.)
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# 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-yiknn-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=1nyij-yik2-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$.

# References

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

# Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDX, LDRR) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<2$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{m}}<2$.
${\mathbf{ifail}}=4$
 On entry, ${\mathbf{itype}}<-1$, or ${\mathbf{itype}}>1$.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

The method used is believed to be stable.

# Parallelism and Performance

None.

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

# 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.

Example program (C#): g02bqe.cs

Example program data: g02bqe.d

Example program results: g02bqe.r