nag_ken_spe_corr_coeff (g02brc) (PDF version)
g02 Chapter Contents
g02 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_ken_spe_corr_coeff (g02brc)

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_ken_spe_corr_coeff (g02brc) calculates Kendall and Spearman rank correlation coefficients.

2  Specification

#include <nag.h>
#include <nagg02.h>
void  nag_ken_spe_corr_coeff (Integer n, Integer m, const double x[], Integer tdx, const Integer svar[], const Integer sobs[], double corr[], Integer tdc, NagError *fail)

3  Description

nag_ken_spe_corr_coeff (g02brc) calculates both the Kendall rank correlation coefficients and the Spearman rank correlation coefficients.
The data consists of n  observations for each of m  variables:
x ij ,   i = 1 , 2 , , n ​ and ​ j = 1 , 2 , , m   m,n2  
where x ij  is the i th observation on the jth variable. The function eliminates any variable x ij  , for i=1,2,,n, where the argument svar[j-1] = 0 , and any observation x ij , for j=1,2,,m, where the argument sobs[i-1] = 0 .
The observations are first ranked as follows:
For a given variable, j  say, each of the observations x ij  for which sobs[i-1] > 0 , for i=1,2,,n, has associated with it an additional number, the rank of the observation, which indicates the magnitude of that observation relative to the magnitudes of the other observations on that same variable for which sobs[i-1] > 0 .
The smallest of these valid observations for variable j  is assigned the rank 1, the second smallest observation for variable j  the rank 2, and so on until the largest such observation is given the rank n s , where n s  is the number of observations for which sobs[i-1] > 0 .
If a number of cases all have the same value for a given variable, j , then they are each given an ‘average’ rank — e.g., if in attempting to assign the rank h+1 , k  observations for which sobs[i-1] > 0  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 2 h + k + 1 2  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 for which svar[j-1] > 0 .
Let y ij  be the rank assigned to the observation x ij  when the j th variable is being ranked. For those observations, i , for which sobs[i-1] = 0 , y ij = 0  , for j=1,2,,m.
For variables j,k  the following are computed:
(a) Kendall's tau correlation coefficients:
R jk = h=1 n i=1 n sign y hj - y ij sign y hk - y ik n s n s - 1 - T j n s n s - 1 - T k j , k = 1 , 2 , , m ;  
where n s  is the number of observations for which sobs[i-1] > 0 ,
and sign u=1 if u>0,
  sign u=0 if u=0,
  sign u=-1 if u<0,
and Tj = tj tj-1  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 .
(b) Spearman's rank correlation coefficients:
R jk = n s n s 2 - 1 - 6 i=1 n y ij - y ik 2 - 1 2 T j + T k n s n s 2 - 1 - T j n s n s 2 - 1 - T k j , k = 1 , 2 , , m ;  
where n s  is the number of observations for which sobs[i-1] > 0 , and T j = t j t j 2 - 1  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  Arguments

1:     n IntegerInput
On entry: the number of observations in the dataset.
Constraint: n2 .
2:     m IntegerInput
On entry: the number of variables.
Constraint: m2 .
3:     x[n×tdx] const doubleInput
On entry: x[i-1×tdx+j-1]  must contain the i th observation on the j th variable, for i=1,2,,n and j=1,2,,m.
4:     tdx IntegerInput
On entry: the stride separating matrix column elements in the array x.
Constraint: tdxm .
5:     svar[m] const IntegerInput
On entry: svar[j-1]  indicates which variables are to be included, for the j th variable to be included, svar[j-1] > 0 . If all variables are to be included then a NULL pointer (Integer *)0 may be supplied.
Constraint: svar[j-1] 0 , and there is at least one positive element, for j=1,2,,m.
6:     sobs[n] const IntegerInput
On entry: sobs[i-1]  indicates which observations are to be included, for the i th observation to be included, sobs[i-1] > 0 . If all observations are to be included then a NULL pointer (Integer *)0 may be supplied.
Constraint: sobs[i-1] 0 , and there are at least two positive elements, for i=1,2,,n.
7:     corr[m×tdc] doubleOutput
On exit: the upper n s  by n s  part of corr contains the correlation coefficients, the upper triangle contains the Spearman coefficients and the lower triangle, the Kendall coefficients. That is, for the j th and k th variables, where j  is less than k , corr[j-1×tdc+k-1]  contains the Spearman rank correlation coefficient, and corr[k-1×tdc+j-1]  contains Kendall's tau, for j , k = 1 , 2 , , n s . The diagonal will be set to 1.
8:     tdc IntegerInput
On entry: the stride separating matrix column elements in the array corr.
Constraint: tdcm .
9:     fail NagError *Input/Output
The NAG error argument (see Section 2.7 in How to Use the NAG Library and its Documentation).

6  Error Indicators and Warnings

NE_2_INT_ARG_LT
On entry, tdc=value  while m=value . These arguments must satisfy tdcm .
On entry, tdx=value  while m=value . These arguments must satisfy tdxm .
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_INT_ARG_LT
On entry, m=value.
Constraint: m2.
On entry, n=value.
Constraint: n2.
NE_INT_ARRAY_1
Value value given to sobs[value]  not valid. Correct range for elements of sobs is sobs[i] 0 .
Value value given to svar[value]  not valid. Correct range for elements of svar is svar[i] 0 .
NE_INTERNAL_ERROR
An initial error has occurred in this function. Check the function call and any array sizes.
NE_SOBS_LOW
On entry, sobs must contain at least 2 positive elements.
Too few observations have been selected.
NE_SVAR_LOW
No variables have been selected.
On entry, svar must contain at least 1 positive element.

7  Accuracy

The computations are believed to be stable.

8  Parallelism and Performance

nag_ken_spe_corr_coeff (g02brc) is not threaded in any implementation.

9  Further Comments

None.

10  Example

A program to calculate the Kendall and Spearman rank correlation coefficients from a set of data.

10.1  Program Text

Program Text (g02brce.c)

10.2  Program Data

Program Data (g02brce.d)

10.3  Program Results

Program Results (g02brce.r)


nag_ken_spe_corr_coeff (g02brc) (PDF version)
g02 Chapter Contents
g02 Chapter Introduction
NAG Library Manual

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