Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. Sub wilcoxonRS()
  2.  
  3. Dim i As Integer
  4. Dim targetCol As Integer
  5. Dim colCond As Integer
  6. Dim lastColumn As Long
  7. Dim lastRow As Long
  8.  
  9. Dim Target As String
  10. Dim HeaderY As String
  11. Dim temp As String
  12. Dim condStr As String
  13. Dim ListY(1 To 2) As String
  14. Dim HeaderX() As String
  15. Dim ListX() As String
  16. Dim initConstr As String
  17. Dim conclusion As String
  18. Dim minPopulation As Integer
  19. Dim numC As Integer
  20. Dim alpha As Single
  21.  
  22. Dim rankSum(1 To 2) As Double
  23. Dim nValue(1 To 2) As Double
  24. Dim medianTarget(1 To 2) As Double
  25. Dim wStat As Double
  26. Dim muW As Double
  27. Dim stdErrorW As Double
  28. Dim zStat As Double
  29. Dim pValue As Double
  30. Dim nameA As String
  31. Dim diffW As String
  32.  
  33. Dim ST1, TT1 As Single
  34.  
  35. Set WRS = ActiveWorkbook.Worksheets("Wilcoxon_Rank_Sum")
  36.  
  37. ST1 = Timer
  38.  
  39. 'Getting sheet values
  40. Target = WRS.Cells(2, 2).Value
  41. HeaderY = WRS.Cells(2, 8).Value
  42. ListY(1) = WRS.Cells(3, 8).Value
  43. ListY(2) = WRS.Cells(4, 8).Value
  44. minPopulation = WRS.Cells(11, 2).Value
  45. numC = WRS.Cells(10, 2).Value
  46.  
  47. 'Getting constraints from sheet
  48. ReDim HeaderX(numC)
  49. ReDim ListX(numC)
  50.  
  51. For i = 1 To numC
  52. HeaderX(i) = WRS.Cells(i + 1, 4).Value
  53. ListX(i) = WRS.Cells(i + 1, 6).Value
  54. Next i
  55.  
  56. Call createMetaData
  57.  
  58. MD.Activate
  59. MD.AutoFilterMode = False
  60.  
  61. lastColumn = MD.Cells(1, Columns.Count).End(xlToLeft).Column
  62. lastRow = MD.Cells(Rows.Count, 1).End(xlUp).Row
  63.  
  64. 'Getting column numbers for target and headerY columns
  65. For i = 1 To lastColumn
  66. temp = MD.Cells(1, i).Value
  67. If temp = Target Then
  68. targetCol = i
  69. ElseIf temp = HeaderY Then
  70. colCond = i
  71. End If
  72. Next i
  73.  
  74. 'Assigning rank values
  75. MD.Cells.Sort Key1:=Columns(targetCol), Order1:=xlAscending, Header:=xlYes
  76. MD.Cells(1, lastColumn + 1).Value = "Rank"
  77.  
  78. For i = 2 To lastRow
  79. MD.Cells(i, lastColumn + 1).Value = Application.WorksheetFunction.Rank_Avg(MD.Cells(i, targetCol).Value, MD.Columns(targetCol), 1)
  80. Next i
  81.  
  82. 'Filtering for ListY1
  83. condStr = "=" & Trim(ListY(1))
  84. MD.Cells.AutoFilter Field:=colCond, Criteria1:=condStr
  85.  
  86. nValue(1) = Application.WorksheetFunction.Count(MD.Columns(targetCol).SpecialCells(xlCellTypeVisible))
  87. rankSum(1) = Application.WorksheetFunction.Sum(MD.Columns(lastColumn + 1).SpecialCells(xlCellTypeVisible))
  88. medianTarget(1) = Application.WorksheetFunction.median(Columns(targetCol).SpecialCells(xlCellTypeVisible))
  89.  
  90. 'Filtering for ListY2
  91. condStr = "=" & Trim(ListY(2))
  92. MD.Cells.AutoFilter Field:=colCond, Criteria1:=condStr
  93.  
  94. nValue(2) = Application.WorksheetFunction.Count(MD.Columns(targetCol).SpecialCells(xlCellTypeVisible))
  95. rankSum(2) = Application.WorksheetFunction.Sum(MD.Columns(lastColumn + 1).SpecialCells(xlCellTypeVisible))
  96. medianTarget(2) = Application.WorksheetFunction.median(Columns(targetCol).SpecialCells(xlCellTypeVisible))
  97.  
  98. 'Enforcing parameters
  99. If (nValue(1) + nValue(2)) < minPopulation Then
  100. MsgBox "Sample size below threshold!"
  101. Exit Sub
  102. Else
  103. End If
  104.  
  105. 'Calculating statistics
  106. If nValue(1) < nValue(2) Then
  107. nameA = ListY(1)
  108. wStat = rankSum(1)
  109. muW = nValue(1) * (nValue(1) + nValue(2) + 1) / 2
  110. stdErrorW = Sqr(nValue(2) * muW / 6)
  111. ElseIf nValue(1) > nValue(2) Then
  112. nameA = ListY(2)
  113. wStat = rankSum(2)
  114. muW = nValue(2) * (nValue(2) + nValue(1) + 1) / 2
  115. stdErrorW = Sqr(nValue(1) * muW / 6)
  116. Else
  117. If rankSum(1) < rankSum(2) Then
  118. nameA = ListY(1)
  119. wStat = rankSum(1)
  120. muW = nValue(1) * (nValue(1) + nValue(2) + 1) / 2
  121. stdErrorW = Sqr(nValue(2) * muW / 6)
  122. Else
  123. nameA = ListY(2)
  124. wStat = rankSum(2)
  125. muW = nValue(2) * (nValue(2) + nValue(1) + 1) / 2
  126. stdErrorW = Sqr(nValue(1) * muW / 6)
  127. End If
  128. End If
  129.  
  130. zStat = (wStat - muW) / stdErrorW
  131. If zStat > 0 Then
  132. pValue = (1 - Application.WorksheetFunction.Norm_S_Dist(zStat, True)) * 2
  133. Else
  134. pValue = Application.WorksheetFunction.Norm_S_Dist(zStat, True) * 2
  135. End If
  136.  
  137. 'Populating table
  138. WRS.Cells(3, 9).Value = rankSum(1)
  139. WRS.Cells(3, 10).Value = nValue(1)
  140. WRS.Cells(3, 11).Value = medianTarget(1)
  141. WRS.Cells(4, 9).Value = rankSum(2)
  142. WRS.Cells(4, 10).Value = nValue(2)
  143. WRS.Cells(4, 11).Value = medianTarget(2)
  144.  
  145. WRS.Cells(1, 8).Value = "Sample Statistics (" & Target & ")"
  146.  
  147. 'Statistics Summary
  148. With WRS.Range("M1:N1")
  149. .Merge
  150. .Font.Bold = True
  151. .Interior.ColorIndex = 40
  152. .BorderAround (xlContinuous)
  153. .Value = "Statistics Summary"
  154. End With
  155.  
  156. WRS.Cells(2, 13) = "W-statistic (" & nameA & ")"
  157. WRS.Cells(2, 14).Value = wStat
  158. WRS.Cells(3, 13) = "Expected Value of W"
  159. WRS.Cells(3, 14).Value = muW
  160. WRS.Cells(4, 13).Value = "Standard Error of W"
  161. WRS.Cells(4, 14).Value = stdErrorW
  162. WRS.Cells(5, 13).Value = "Z-Statistic"
  163. WRS.Cells(5, 14).Value = zStat
  164. WRS.Cells(6, 13).Value = "P-value (two-tailed)"
  165. WRS.Cells(6, 14).Value = pValue
  166. WRS.Cells(7, 13).Value = "W - E(W)"
  167. WRS.Cells(7, 14).Value = wStat - muW
  168.  
  169. WRS.Range("M2:N7").BorderAround (xlContinuous)
  170.  
  171. WRS.Columns("A:N").HorizontalAlignment = xlCenter
  172. WRS.Columns("A:N").AutoFit
  173.  
  174. 'Implication Analysis Report
  175. With WRS.Range("M11:N11")
  176. .ColumnWidth = 25
  177. .Merge
  178. .Font.Bold = True
  179. .Interior.ColorIndex = 40
  180. .BorderAround (xlContinuous)
  181. .Value = "Implication Analysis Report"
  182. End With
  183.  
  184. initConstr = "{"
  185. For i = 1 To NumConstraints
  186. initConstr = initConstr & HeaderX(i) & ": " & ListX(i) & ", "
  187. Next i
  188.  
  189. alpha = 0.05
  190.  
  191. If (wStat - muW) > 0 Then
  192. diffW = "larger than"
  193. ElseIf (wStat - muW) < 0 Then
  194. diffW = "smaller than"
  195. Else
  196. diffW = "equal to"
  197. End If
  198.  
  199. If NumConstraints <> 0 Then
  200. initConstr = Left(initConstr, Len(initConstr) - 2) & "}"
  201.  
  202. If pValue < alpha Then
  203. conclusion = "In the context of " & initConstr & ", the distributions of {" & Target & _
  204. "} between the two samples {" & HeaderY & ": " & ListY(1) & "} and {" & HeaderY & ": " & ListY(2) & _
  205. "} are not the same. " & "The Wilcoxon rank sum statistic is " & diffW & " its expected value. " & "With a p-value of " & Round(pValue, 3) & _
  206. ", we reject the null hypothesis at a significance level of " & alpha & "."
  207. Else
  208. conclusion = "In the context of " & initConstr & ", we compare the distributions of {" & Target & _
  209. "} between the two samples {" & HeaderY & ": " & ListY(1) & "} and {" & HeaderY & ": " & ListY(2) & "}." & vbNewLine & vbNewLine & _
  210. "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 " & _
  211. alpha & " and conclude that the results are not statistically significant."
  212. End If
  213. Else
  214. If pValue < alpha Then
  215. conclusion = "The distributions of {" & Target & _
  216. "} between the two samples {" & HeaderY & ": " & ListY(1) & "} and {" & HeaderY & ": " & ListY(2) & _
  217. "} are not the same. " & "The Wilcoxon rank sum statistic is " & diffW & " its expected value. " & "With a p-value of " & Round(pValue, 3) & _
  218. ", we reject the null hypothesis at a significance level of " & alpha & "."
  219. Else
  220. conclusion = "We compare the distributions of {" & Target & _
  221. "} between the two samples {" & HeaderY & ": " & ListY(1) & "} and {" & HeaderY & ": " & ListY(2) & "}." & vbNewLine & vbNewLine & _
  222. "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 " & _
  223. alpha & " and conclude that the results are not statistically significant."
  224. End If
  225. End If
  226.  
  227. With WRS.Range("M12:N22")
  228. .Merge
  229. .WrapText = True
  230. .VerticalAlignment = xlTop
  231. .HorizontalAlignment = xlLeft
  232. .IndentLevel = 1
  233. .BorderAround (xlContinuous)
  234. .Value = conclusion
  235. End With
  236.  
  237. TT1 = Timer - ST1
  238. Debug.Print "Wilcoxon Rank Sum - " & TT1
  239.  
  240. WRS.Activate
  241.  
  242. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement