Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub wilcoxonRS()
- Dim i As Integer
- Dim targetCol As Integer
- Dim colCond As Integer
- Dim lastColumn As Long
- Dim lastRow As Long
- Dim Target As String
- Dim HeaderY As String
- Dim temp As String
- Dim condStr As String
- Dim ListY(1 To 2) As String
- Dim HeaderX() As String
- Dim ListX() As String
- Dim initConstr As String
- Dim conclusion As String
- Dim minPopulation As Integer
- Dim numC As Integer
- Dim alpha As Single
- Dim rankSum(1 To 2) As Double
- Dim nValue(1 To 2) As Double
- Dim medianTarget(1 To 2) As Double
- Dim wStat As Double
- Dim muW As Double
- Dim stdErrorW As Double
- Dim zStat As Double
- Dim pValue As Double
- Dim nameA As String
- Dim diffW As String
- Dim ST1, TT1 As Single
- Set WRS = ActiveWorkbook.Worksheets("Wilcoxon_Rank_Sum")
- ST1 = Timer
- 'Getting sheet values
- Target = WRS.Cells(2, 2).Value
- HeaderY = WRS.Cells(2, 8).Value
- ListY(1) = WRS.Cells(3, 8).Value
- ListY(2) = WRS.Cells(4, 8).Value
- minPopulation = WRS.Cells(11, 2).Value
- numC = WRS.Cells(10, 2).Value
- 'Getting constraints from sheet
- ReDim HeaderX(numC)
- ReDim ListX(numC)
- For i = 1 To numC
- HeaderX(i) = WRS.Cells(i + 1, 4).Value
- ListX(i) = WRS.Cells(i + 1, 6).Value
- Next i
- Call createMetaData
- MD.Activate
- MD.AutoFilterMode = False
- lastColumn = MD.Cells(1, Columns.Count).End(xlToLeft).Column
- lastRow = MD.Cells(Rows.Count, 1).End(xlUp).Row
- 'Getting column numbers for target and headerY columns
- For i = 1 To lastColumn
- temp = MD.Cells(1, i).Value
- If temp = Target Then
- targetCol = i
- ElseIf temp = HeaderY Then
- colCond = i
- End If
- Next i
- 'Assigning rank values
- MD.Cells.Sort Key1:=Columns(targetCol), Order1:=xlAscending, Header:=xlYes
- MD.Cells(1, lastColumn + 1).Value = "Rank"
- For i = 2 To lastRow
- MD.Cells(i, lastColumn + 1).Value = Application.WorksheetFunction.Rank_Avg(MD.Cells(i, targetCol).Value, MD.Columns(targetCol), 1)
- Next i
- 'Filtering for ListY1
- condStr = "=" & Trim(ListY(1))
- MD.Cells.AutoFilter Field:=colCond, Criteria1:=condStr
- nValue(1) = Application.WorksheetFunction.Count(MD.Columns(targetCol).SpecialCells(xlCellTypeVisible))
- rankSum(1) = Application.WorksheetFunction.Sum(MD.Columns(lastColumn + 1).SpecialCells(xlCellTypeVisible))
- medianTarget(1) = Application.WorksheetFunction.median(Columns(targetCol).SpecialCells(xlCellTypeVisible))
- 'Filtering for ListY2
- condStr = "=" & Trim(ListY(2))
- MD.Cells.AutoFilter Field:=colCond, Criteria1:=condStr
- nValue(2) = Application.WorksheetFunction.Count(MD.Columns(targetCol).SpecialCells(xlCellTypeVisible))
- rankSum(2) = Application.WorksheetFunction.Sum(MD.Columns(lastColumn + 1).SpecialCells(xlCellTypeVisible))
- medianTarget(2) = Application.WorksheetFunction.median(Columns(targetCol).SpecialCells(xlCellTypeVisible))
- 'Enforcing parameters
- If (nValue(1) + nValue(2)) < minPopulation Then
- MsgBox "Sample size below threshold!"
- Exit Sub
- Else
- End If
- 'Calculating statistics
- If nValue(1) < nValue(2) Then
- nameA = ListY(1)
- wStat = rankSum(1)
- muW = nValue(1) * (nValue(1) + nValue(2) + 1) / 2
- stdErrorW = Sqr(nValue(2) * muW / 6)
- ElseIf nValue(1) > nValue(2) Then
- nameA = ListY(2)
- wStat = rankSum(2)
- muW = nValue(2) * (nValue(2) + nValue(1) + 1) / 2
- stdErrorW = Sqr(nValue(1) * muW / 6)
- Else
- If rankSum(1) < rankSum(2) Then
- nameA = ListY(1)
- wStat = rankSum(1)
- muW = nValue(1) * (nValue(1) + nValue(2) + 1) / 2
- stdErrorW = Sqr(nValue(2) * muW / 6)
- Else
- nameA = ListY(2)
- wStat = rankSum(2)
- muW = nValue(2) * (nValue(2) + nValue(1) + 1) / 2
- stdErrorW = Sqr(nValue(1) * muW / 6)
- End If
- End If
- zStat = (wStat - muW) / stdErrorW
- If zStat > 0 Then
- pValue = (1 - Application.WorksheetFunction.Norm_S_Dist(zStat, True)) * 2
- Else
- pValue = Application.WorksheetFunction.Norm_S_Dist(zStat, True) * 2
- End If
- 'Populating table
- WRS.Cells(3, 9).Value = rankSum(1)
- WRS.Cells(3, 10).Value = nValue(1)
- WRS.Cells(3, 11).Value = medianTarget(1)
- WRS.Cells(4, 9).Value = rankSum(2)
- WRS.Cells(4, 10).Value = nValue(2)
- WRS.Cells(4, 11).Value = medianTarget(2)
- WRS.Cells(1, 8).Value = "Sample Statistics (" & Target & ")"
- 'Statistics Summary
- With WRS.Range("M1:N1")
- .Merge
- .Font.Bold = True
- .Interior.ColorIndex = 40
- .BorderAround (xlContinuous)
- .Value = "Statistics Summary"
- End With
- WRS.Cells(2, 13) = "W-statistic (" & nameA & ")"
- WRS.Cells(2, 14).Value = wStat
- WRS.Cells(3, 13) = "Expected Value of W"
- WRS.Cells(3, 14).Value = muW
- WRS.Cells(4, 13).Value = "Standard Error of W"
- WRS.Cells(4, 14).Value = stdErrorW
- WRS.Cells(5, 13).Value = "Z-Statistic"
- WRS.Cells(5, 14).Value = zStat
- WRS.Cells(6, 13).Value = "P-value (two-tailed)"
- WRS.Cells(6, 14).Value = pValue
- WRS.Cells(7, 13).Value = "W - E(W)"
- WRS.Cells(7, 14).Value = wStat - muW
- WRS.Range("M2:N7").BorderAround (xlContinuous)
- WRS.Columns("A:N").HorizontalAlignment = xlCenter
- WRS.Columns("A:N").AutoFit
- 'Implication Analysis Report
- With WRS.Range("M11:N11")
- .ColumnWidth = 25
- .Merge
- .Font.Bold = True
- .Interior.ColorIndex = 40
- .BorderAround (xlContinuous)
- .Value = "Implication Analysis Report"
- End With
- initConstr = "{"
- For i = 1 To NumConstraints
- initConstr = initConstr & HeaderX(i) & ": " & ListX(i) & ", "
- Next i
- alpha = 0.05
- If (wStat - muW) > 0 Then
- diffW = "larger than"
- ElseIf (wStat - muW) < 0 Then
- diffW = "smaller than"
- Else
- diffW = "equal to"
- End If
- If NumConstraints <> 0 Then
- initConstr = Left(initConstr, Len(initConstr) - 2) & "}"
- If pValue < alpha Then
- conclusion = "In the context of " & initConstr & ", the distributions of {" & Target & _
- "} between the two samples {" & HeaderY & ": " & ListY(1) & "} and {" & HeaderY & ": " & ListY(2) & _
- "} are not the same. " & "The Wilcoxon rank sum statistic is " & diffW & " its expected value. " & "With a p-value of " & Round(pValue, 3) & _
- ", we reject the null hypothesis at a significance level of " & alpha & "."
- Else
- conclusion = "In the context of " & initConstr & ", we compare the distributions of {" & Target & _
- "} between the two samples {" & HeaderY & ": " & ListY(1) & "} and {" & HeaderY & ": " & ListY(2) & "}." & vbNewLine & vbNewLine & _
- "The Wilcoxon rank sum statistic is " & diffW & " its expected value. " & "With a p-value of " & Round(pValue, 3) & ", we fail to reject the null hypothesis (that the distributions are the same) at a significance level of " & _
- alpha & " and conclude that the results are not statistically significant."
- End If
- Else
- If pValue < alpha Then
- conclusion = "The distributions of {" & Target & _
- "} between the two samples {" & HeaderY & ": " & ListY(1) & "} and {" & HeaderY & ": " & ListY(2) & _
- "} are not the same. " & "The Wilcoxon rank sum statistic is " & diffW & " its expected value. " & "With a p-value of " & Round(pValue, 3) & _
- ", we reject the null hypothesis at a significance level of " & alpha & "."
- Else
- conclusion = "We compare the distributions of {" & Target & _
- "} between the two samples {" & HeaderY & ": " & ListY(1) & "} and {" & HeaderY & ": " & ListY(2) & "}." & vbNewLine & vbNewLine & _
- "The Wilcoxon rank sum statistic is " & diffW & " its expected value. " & "With a p-value of " & Round(pValue, 3) & ", we fail to reject the null hypothesis (that the distributions are the same) at a significance level of " & _
- alpha & " and conclude that the results are not statistically significant."
- End If
- End If
- With WRS.Range("M12:N22")
- .Merge
- .WrapText = True
- .VerticalAlignment = xlTop
- .HorizontalAlignment = xlLeft
- .IndentLevel = 1
- .BorderAround (xlContinuous)
- .Value = conclusion
- End With
- TT1 = Timer - ST1
- Debug.Print "Wilcoxon Rank Sum - " & TT1
- WRS.Activate
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement