Logo

SQL Server KENDALLT_TV Function

Updated 2023-11-06 16:06:17.407000

Description

Use the table-valued function KENDALL_TV 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 * FROM [westclintech].[wct].[KENDALLT_TV](
   <@x_y_Query, nvarchar(max),>)

Arguments

@x_y_Query

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

Return Type

table

colNamecolDatatypecolDesc
Tau_bfloatthe Kendall tau b statistic
Zbfloatthe z-value for tau b
Pbfloatthe p-value for tau b
SDbfloatthe standard deviation for tau b
Tau_afloatthe Kendall tau a statistic
Zafloatthe z-value for tau a
Pafloatthe p-value for tau a
SDafloatthe standard deviation for tau a
Cfloatthe number of concordant pairs
Dfloatthe number of discordant pairs
SfloatC - D
Tfloatthe number of x ties
Ufloatthe number of y ties
Nfloatthe number of pairs

Remarks

The function is insensitive to order.

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

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.*
FROM wct.KENDALLT_TV('SELECT x,y FROM #k') k
    --This CROSS APPLY UNPIVOTS the tvf columns for formatting
    CROSS APPLY
(
    SELECT 'tau_a',
           tau_a
    UNION ALL
    SELECT 'tau_b',
           tau_b
    UNION ALL
    SELECT 'C',
           C
    UNION ALL
    SELECT 'D',
           D
    UNION ALL
    SELECT 'S',
           S
    UNION ALL
    SELECT 'T',
           T
    UNION ALL
    SELECT 'U',
           U
    UNION ALL
    SELECT 'za',
           za
    UNION ALL
    SELECT 'zb',
           zb
    UNION ALL
    SELECT 'SDa',
           SDa
    UNION ALL
    SELECT 'SDb',
           SDb
    UNION ALL
    SELECT 'pa',
           pa
    UNION ALL
    SELECT 'pb',
           pb
    UNION ALL
    SELECT 'N',
           N
) p(stat, value_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