User Defined Function for Spearman's Rank Correlation Coefficient

edwinsampang

New Member
Joined
Oct 12, 2012
Messages
5
Is there a VBA code for the Spearman's Rank Correlation Coefficient, where its syntax is
=Spearman(Range1, Range2)?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to MrExcel.

Try:

Code:
Function Spearman(Rng1 As Range, Rng2 As Range) As Double
    Dim WF As WorksheetFunction
    Dim dSquared() As Long
    Dim r As Long
    Set WF = WorksheetFunction
    ReDim Preserve dSquared(1 To Rng1.Rows.Count)
    For r = LBound(dSquared) To UBound(dSquared)
        dSquared(r) = (WF.Rank(Rng1.Cells(r, 1), Rng1) - WF.Rank(Rng2.Cells(r, 1), Rng2)) ^ 2
    Next r
    Spearman = 1 - ((6 * WF.Sum(dSquared)) / ((Rng1.Rows.Count ^ 3) - Rng1.Rows.Count))
End Function

I tested it with the data here:

http://www.rgs.org/NR/rdonlyres/484...3C28FAC087/0/OASpearmansRankExcelGuidePDF.pdf
 
Upvote 0
Thanks, but why is it that when I used your UDF (see example below) I got different answers?
XYRank XRank Yd^2
11109.50.25
2199.50.25
32871
42770
52671
63541
73440
83341
9421.50.25
10411.50.25
5
Spearman Rho0.966667
SpearmanRho(User Defined Function)0.927273

<colgroup><col style="width: 48pt;" span="5" width="64"> <tbody>
</tbody>
 
Upvote 0
I don't think so, because Excel doesn't average tied ranks. I also wonder why the Excel developers does not amend the User defined function for ranking.
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,408
Members
449,448
Latest member
Andrew Slatter

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