Kendall tau VBA

G Angie

New Member
Joined
May 6, 2022
Messages
6
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hello, I used the code that works fine. But there is a small problem, as soon as I exceed 412 lines, I get a calculation error. my database is 2200 lines. can i have help
Option Explicit

' Example data from
' Kendall's Rank Correlation - StatsDirect

' --A--- --B-- C --D--- -----------E-----------
' 1 Career Psych
' 2 4 5 0.5111 D2: =KendallTau(A2:B11)
' 3 10 8
' 4 3 6
' 5 1 2
' 6 9 10
' 7 2 3
' 8 6 9
' 9 7 4
' 10 8 7
' 11 5 1

VBA Code:
Function KendallTau(r As Range) As Variant
    If WorksheetFunction.Count(r.Value) <> r.Count Then
        KendallTau = CVErr(xlErrValue)
    Else
        KendallTau = dKendallTau(r.Value)
    End If
End Function
 
Function dKendallTau(avXY As Variant) As Double
    ' shg 2010
 
    Dim n           As Long     ' number of pairs
    Dim i           As Long     ' outer loop index
    Dim j           As Long     ' inner loop index
    Dim nCon        As Long     ' number of concordant pairs
    Dim nDis        As Long     ' dumber of discordant pairs
    Dim nX          As Long     ' number of X ties
    Dim nY          As Long     ' number of Y ties
    Dim nC2         As Long     ' n Choose 2
 
    n = UBound(avXY)
 
    For i = 1 To n - 1
        For j = i + 1 To n
            Select Case Choose(Sgn(avXY(i, 1) - avXY(j, 1)) + 2, "<", "=", ">") & _
                        Choose(Sgn(avXY(i, 2) - avXY(j, 2)) + 2, "<", "=", ">")
                Case ">>", "<<"
                    nCon = nCon + 1
                Case "<>", "><"
                    nDis = nDis + 1
                Case "=<", "=>"
                    nX = nX + 1
                Case "<=", ">="
                    nY = nY + 1
                Case "=="
                    nX = nX + 1
                    nY = nY + 1
               
            End Select
        Next j
    Next i
 nC2 = n * (n - 1) / 2
    dKendallTau = (nCon - nDis) / Sqr(((nC2 - nX) * (nC2 - nY)))
End Function
 
Last edited by a moderator:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi & welcome to MrExcel.
Try changing the last line to
VBA Code:
    dKendallTau = (nCon - nDis) / Sqr((CDbl(nC2 - nX) * CDbl(nC2 - nY)))
 
Upvote 0
Solution
Hi & welcome to MrExcel.
Try changing the last line to
VBA Code:
    dKendallTau = (nCon - nDis) / Sqr((CDbl(nC2 - nX) * CDbl(nC2 - nY)))
Thank youuuuuu!!! It's work ?
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
You're welcome & thanks for the feedback.
Hello, I used the code that works fine. But there is a small problem, as soon as I exceed 412 lines, I get a calculation error. my database is 2200 lines. can i have help
Option Explicit

' Example data from
' Kendall's Rank Correlation - StatsDirect

' --A--- --B-- C --D--- -----------E-----------
' 1 Career Psych
' 2 4 5 0.5111 D2: =KendallTau(A2:B11)
' 3 10 8
' 4 3 6
' 5 1 2
' 6 9 10
' 7 2 3
' 8 6 9
' 9 7 4
' 10 8 7
' 11 5 1

VBA Code:
Function KendallTau(r As Range) As Variant
    If WorksheetFunction.Count(r.Value) <> r.Count Then
        KendallTau = CVErr(xlErrValue)
    Else
        KendallTau = dKendallTau(r.Value)
    End If
End Function
 
Function dKendallTau(avXY As Variant) As Double
    ' shg 2010
 
    Dim n           As Long     ' number of pairs
    Dim i           As Long     ' outer loop index
    Dim j           As Long     ' inner loop index
    Dim nCon        As Long     ' number of concordant pairs
    Dim nDis        As Long     ' dumber of discordant pairs
    Dim nX          As Long     ' number of X ties
    Dim nY          As Long     ' number of Y ties
    Dim nC2         As Long     ' n Choose 2
 
    n = UBound(avXY)
 
    For i = 1 To n - 1
        For j = i + 1 To n
            Select Case Choose(Sgn(avXY(i, 1) - avXY(j, 1)) + 2, "<", "=", ">") & _
                        Choose(Sgn(avXY(i, 2) - avXY(j, 2)) + 2, "<", "=", ">")
                Case ">>", "<<"
                    nCon = nCon + 1
                Case "<>", "><"
                    nDis = nDis + 1
                Case "=<", "=>"
                    nX = nX + 1
                Case "<=", ">="
                    nY = nY + 1
                Case "=="
                    nX = nX + 1
                    nY = nY + 1
              
            End Select
        Next j
    Next i
 nC2 = n * (n - 1) / 2
    dKendallTau = (nCon - nDis) / Sqr((nC2 - nX) * (nC2 - nY))
End Function
Good evening, sorry but I had forgotten another question, when my two columns do not have the same number of filled cells, is there a code that excludes empty spaces? I have to create other sheets that filter out voids and remove them to automatically calculate kendall's rate.
 
Upvote 0
As that is a totally different question, it needs a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,772
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top