Login     Register

        Contact Us     Search

XLeratorDB/statistics Documentation

SQL Server Spearman's rho function


SPEARMAN

Updated: 24 May 2013


Use the scalar function SPEARMAN to calculate Spearman’s rank correlation coefficient (ρ). Spearman’s ρ is defined as Pearson’s correlation coefficient for the ranks of x- and y-values. Ranks are calculated such that tied ranks receive the average of the ranks for the rank interval populated by the tie.
Syntax
SELECT [wctStatistics].[wct].[SPEARMAN](
  <@x_y_Query, nvarchar(max),>
 ,<@RV, nvarchar(4000),>)
Arguments
@x_y_Query
a T-SQL statement, as a string, that specifies the x- and y-values. The x- and y-values must be of the type float or of a type that implicitly converts to float.
@RV
the value to be returned by the function. Use the following values:

'R'
Spearman’s rho
'P'
the p-value
'T'
the t-statistic
'DF'
the number of pairs minus 2
 
Return Type
float
Remarks
·         The function is insensitive to order.
·         The function only includes pairs where x-value is NOT NULL and y-value is NOT NULL
·         Use the RANK_AVG table-valued function to see the x- and y-ranks
Examples
SELECT *
INTO #s
FROM (
      SELECT 106,7 UNION ALL
      SELECT 86,0 UNION ALL
      SELECT 100,27 UNION ALL
      SELECT 101,50 UNION ALL
      SELECT 99,28 UNION ALL
      SELECT 103,29 UNION ALL
      SELECT 97,20 UNION ALL
      SELECT 113,12 UNION ALL
      SELECT 112,6 UNION ALL
      SELECT 110,17
      )n(x,y)
 
SELECT p.stat
,wct.SPEARMAN('SELECT x,y from #s', p.stat) as stat_value
FROM (
      SELECT 'P' UNION ALL
      SELECT 'R' UNION ALL
      SELECT 'T' UNION all
      SELECT 'DF')p(stat)
 
DROP TABLE #s
This produces the following result.

 


Copyright 2008-2017 Westclintech LLC         Privacy Policy        Terms of Service