Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
Hi,
When I am passing a range to the below UDF, say A1=CorrelationCoefficient(B1:C100, 1, 2, 0) I am getting an VALUE error. Any reason why?
The code is:
The Correlation UDF is from the following site:
When I am passing a range to the below UDF, say A1=CorrelationCoefficient(B1:C100, 1, 2, 0) I am getting an VALUE error. Any reason why?
The code is:
Code:
Public Function CorrelationCoefficient(MyArray() As Double, Optional asX As Long = -1, _
Optional asY As Long = -1, Optional asWeight As Long = 0) As Double
Dim i As Long
Dim SumX As Double
Dim SumX2 As Double
Dim SumY As Double
Dim SumY2 As Double
Dim SumXY As Double
Dim SumW As Double
Dim weight As Double
Dim Numerator As Double
Dim Denominator As Double
On Error GoTo ErrHandler_CorCoef
'can correlation coefficient be calculated?
If MultiDimensional(MyArray) = True And UBound(MyArray, 1) > 1 Then
'correct weightfactor?
If asWeight < 0 Then asWeight = 0
If asWeight > 4 Then asWeight = 0
'initialise
SumX = 0
SumX2 = 0
SumY = 0
SumY2 = 0
SumXY = 0
SumW = 0
'determine which columns need to be correlated
If asX < 0 Then asX = 0
If asX > UBound(MyArray, 2) Then asX = 0
If asY < 0 Then asY = 1
If asY > UBound(MyArray, 2) Then asY = 1
'calculate SumX, SumX2, SumY, SumY2 and SumXY
For i = 0 To UBound(MyArray, 1)
Select Case asWeight
Case 0 'equal weighting
weight = 1
Case 1 '1/x
If MyArray(i, asX) <> 0 Then
weight = Abs(1 / MyArray(i, asX))
Else
weight = 1 'discutable
End If
Case 2 '1/x^2
If (MyArray(i, asX) * MyArray(i, asX)) <> 0 Then
weight = Abs(1 / (MyArray(i, asX) * MyArray(i, asX)))
Else
weight = 1 'discutable
End If
Case 3 '1/y
If MyArray(i, asY) <> 0 Then
weight = Abs(1 / MyArray(i, asY))
Else
weight = 1 'discutable
End If
Case 4 '1/y^2
If (MyArray(i, asY) * MyArray(i, asY)) <> 0 Then
weight = Abs(1 / (MyArray(i, asY) * MyArray(i, asY)))
Else
weight = 1 'discutable
End If
End Select
SumX = SumX + MyArray(i, asX) * weight
SumX2 = SumX2 + MyArray(i, asX) * MyArray(i, asX) * weight
SumY = SumY + MyArray(i, asY) * weight
SumY2 = SumY2 + MyArray(i, asY) * MyArray(i, asY) * weight
SumXY = SumXY + MyArray(i, asX) * MyArray(i, asY) * weight
SumW = SumW + weight
Next i
Numerator = SumXY - (SumX * SumY / SumW)
Denominator = Sqr((SumX2 - SumX * SumX / SumW) * (SumY2 - SumY * SumY / SumW))
If Denominator <> 0 Then
CorrelationCoefficient = Numerator / Denominator
Else
'
End If
Else
CorrelationCoefficient = 99 ' Error value
End If
Exit Function
ErrHandler_CorCoef:
MsgBox "Error in module CorrelationCoefficient!", vbOKOnly & vbExclamation, "Attention!"
End Function
Private Function MultiDimensional(CheckArray() As Double) As Boolean
On Error GoTo ErrHandler_MultiDimensional
If UBound(CheckArray, 2) > 0 Then
MultiDimensional = True
End If
Exit Function
ErrHandler_MultiDimensional:
MultiDimensional = False
End Function
The Correlation UDF is from the following site:
HTML:
http://www.freevbcode.com/ShowCode.asp?ID=4543
Last edited: