Logo

SQL Server KENDALLT Function

Updated 2023-11-06 15:28:16.273000

Description

Use the scalar function KENDALLT to calculate Kendall's tau (๐œ), a non-parametric measure of association based on the number of concordances and discordances in paired observations. Concordance occurs when paired observations vary together and discordance occurs when paired observations vary differently. The equation for Kendall's tau is:

\tau_b = \frac{C-D}{\sqrt{(\binom{n}{2} - T)(\binom{n}{2} - U)}

Where

column 1column 2
Cis the number of concordant pairs
Dis the number of discordant pairs
nis the number of pairs
Tis the number of x ties
Uis the number of y ties

The function also calculates

\tau_a = \frac{C-D}{\binom{n}{2}}

Syntax

SELECT [westclintech].[wct].[KENDALLT](
  <@x_y_Query, nvarchar(max),>
 ,<@RV, nvarchar(4000),>)

Arguments

@x_y_Query

a T-SQL statement, as a string, that specifies the subject, rater and rating values.

@RV

the value to be returned by the function. Use the following values:

RVdescription
'TAU','TAU_B','TAUB'tau ๐‘
'TAU_A','TAUA'tau ๐‘Ž
'Z','Z_B','ZB'the z-statistic for tau ๐‘
'Z_A','ZA'the z-statistic for tau ๐‘Ž
'P','P_B','PB'the p-value for tau ๐‘
'P_A','PA'the p-value for tau ๐‘Ž
'SD','SD_B','SDB'the standard deviation for tau ๐‘
'SD_A','SDB'the standard deviation for tau ๐‘Ž
'C'the number of concordant pairs
'D'the number of discordant pairs
'S'C โ€“ D
'T'the number of x ties
'U'the number of y ties
'N'the number of pairs

Return Type

float

Remarks

The function is insensitive to order.

If x is NULL or y is NULL the pair is not included in the calculations.

To return multiple values, use the table-valued function KENDALLT_TV.

Examples

SELECT *
INTO #k
FROM
(
    SELECT 2.5,
           1
    UNION ALL
    SELECT 2.5,
           1
    UNION ALL
    SELECT 2.5,
           1
    UNION ALL
    SELECT 2.5,
           1
    UNION ALL
    SELECT 5,
           2
    UNION ALL
    SELECT 6.5,
           1
    UNION ALL
    SELECT 6.5,
           1
    UNION ALL
    SELECT 10,
           2
    UNION ALL
    SELECT 10,
           1
    UNION ALL
    SELECT 10,
           1
    UNION ALL
    SELECT 10,
           1
    UNION ALL
    SELECT 10,
           1
    UNION ALL
    SELECT 14,
           1
    UNION ALL
    SELECT 14,
           1
    UNION ALL
    SELECT 14,
           2
    UNION ALL
    SELECT 16,
           2
    UNION ALL
    SELECT 17,
           2
) n(x, y);
SELECT p.stat,
       wct.KENDALLT('SELECT x,y FROM #k', p.stat) k
FROM
(
    SELECT 'tau_a'
    UNION ALL
    SELECT 'tau_b'
    UNION ALL
    SELECT 'C'
    UNION ALL
    SELECT 'D'
    UNION ALL
    SELECT 'S'
    UNION ALL
    SELECT 'T'
    UNION ALL
    SELECT 'U'
    UNION ALL
    SELECT 'za'
    UNION ALL
    SELECT 'zb'
    UNION ALL
    SELECT 'SDa'
    UNION ALL
    SELECT 'SDb'
    UNION ALL
    SELECT 'pa'
    UNION ALL
    SELECT 'pb'
    UNION ALL
    SELECT 'N'
) p(stat);
DROP TABLE #k;

This produces the following result.

statk
tau_a0.25
tau_b0.407543806262577
C44
D10
S34
T20
U76
za1.40054934277178
zb1.77779733948549
SDa24.2761886080442
SDb18.5622957505103
pa0.0806744391578792
pb0.0754371456202263
N17