EffectSizeTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace MathPHP\Tests\Statistics;
  3. use MathPHP\Statistics\EffectSize;
  4. use MathPHP\Exception;
  5. class EffectSizeTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test etaSquared
  9. * @dataProvider dataProviderForEtaSquared
  10. * @param float $SSt
  11. * @param float $SST
  12. * @param float $expected
  13. */
  14. public function testEtaSquared(float $SSt, float $SST, float $expected)
  15. {
  16. // When
  17. $η² = EffectSize::etaSquared($SSt, $SST);
  18. // Then
  19. $this->assertEqualsWithDelta($expected, $η², 0.0000000001);
  20. }
  21. /**
  22. * @return array [SSt, SST, expected]
  23. */
  24. public function dataProviderForEtaSquared(): array
  25. {
  26. return [
  27. // Test data: http://www.statisticshowto.com/eta-squared/
  28. [4.08, 62.29, 0.06550008026971],
  29. [9.2, 62.29, 0.14769625943169],
  30. [19.54, 62.29, 0.31369401187992],
  31. // Test data: http://wilderdom.com/research/ExampleCalculationOfEta-SquaredFromSPSSOutput.pdf
  32. [4.412, 301.70, 0.01462379847531],
  33. [26.196, 301.70, 0.08682797480941],
  34. [0.090, 301.70, 0.00029830957905],
  35. [271.2, 301.70, 0.89890619821014],
  36. // Test data: http://www.uccs.edu/lbecker/glm_effectsize.html
  37. [24, 610, 0.03934426229508],
  38. [112, 610, 0.18360655737705],
  39. [144, 610, 0.23606557377049],
  40. ];
  41. }
  42. /**
  43. * @test partialEtaSquared
  44. * @dataProvider dataProviderForPartialEtaSquared
  45. * @param float $SSt
  46. * @param float $SSE
  47. * @param float $expected
  48. */
  49. public function testPartialEtaSquared(float $SSt, float $SSE, float $expected)
  50. {
  51. // When
  52. $η²p = EffectSize::partialEtaSquared($SSt, $SSE);
  53. // Then
  54. $this->assertEqualsWithDelta($expected, $η²p, 0.000000001);
  55. }
  56. /**
  57. * @return array [SSt, SSE, expected]
  58. */
  59. public function dataProviderForPartialEtaSquared(): array
  60. {
  61. return [
  62. // Test data: http://jalt.org/test/bro_28.htm
  63. [158.372, 3068.553, 0.049078302],
  64. [0.344, 3003.548, 0.000114518],
  65. [137.572, 3003.548, 0.043797116],
  66. // Test data: http://www.uccs.edu/lbecker/glm_effectsize.html
  67. [24, 330, 0.06779661016949],
  68. [112, 330, 0.25339366515837],
  69. [144, 330, 0.30379746835443],
  70. ];
  71. }
  72. /**
  73. * @test omegaSquared
  74. * @dataProvider dataProviderForOmegaSquared
  75. * @param float $SSt
  76. * @param int $dft
  77. * @param float $SST
  78. * @param float $MSE
  79. * @param float $expected
  80. */
  81. public function testOmegaSquared(float $SSt, int $dft, float $SST, float $MSE, float $expected)
  82. {
  83. // When
  84. $ω² = EffectSize::omegaSquared($SSt, $dft, $SST, $MSE);
  85. // Then
  86. $this->assertEqualsWithDelta($expected, $ω², 0.000001);
  87. }
  88. /**
  89. * @return array [SSt, dft, SST, MSE, expected]
  90. */
  91. public function dataProviderForOmegaSquared(): array
  92. {
  93. return [
  94. // Test data: http://www.uccs.edu/lbecker/glm_effectsize.html
  95. [24, 1, 610, 18.333, 0.00901910292791],
  96. [112, 2, 610, 18.333, 0.11989502381699],
  97. [144, 2, 610, 18.333, 0.17082343279758],
  98. ];
  99. }
  100. /**
  101. * @test cohensF
  102. * @dataProvider dataProviderForCohensF
  103. * @param float $measure_of_variance_explained
  104. * @param float $expected
  105. */
  106. public function testCohensF(float $measure_of_variance_explained, float $expected)
  107. {
  108. // When
  109. $ƒ² = EffectSize::cohensF($measure_of_variance_explained);
  110. // Then
  111. $this->assertEqualsWithDelta($expected, $ƒ², 0.0000001);
  112. }
  113. /**
  114. * @return array [measure of variance explained, expected]
  115. */
  116. public function dataProviderForCohensF(): array
  117. {
  118. return [
  119. [0.06550008026971, 0.07009104964783],
  120. [0.01462379847531, 0.01484082774953],
  121. [0.18360655737705, 0.22489959839358],
  122. [0.00901910292791, 0.00910118747451],
  123. [0.25, 0.33333333],
  124. [0.00001, 0.000010],
  125. [0.99999, 99999.00000046],
  126. ];
  127. }
  128. /**
  129. * @test cohensQ
  130. * @dataProvider dataProviderForCohensQ
  131. * @param float $r₁
  132. * @param float $r₂
  133. * @param float $expected
  134. */
  135. public function testCohensQ(float $r₁, float $r₂, float $expected)
  136. {
  137. // When
  138. $q = EffectSize::cohensQ($r₁, $r₂);
  139. // Then
  140. $this->assertEqualsWithDelta($expected, $q, 0.001);
  141. }
  142. /**
  143. * @return array [r₁, r₂, expected]
  144. */
  145. public function dataProviderForCohensQ(): array
  146. {
  147. return [
  148. [0.1, 0.1, 0],
  149. [0.5, 0.5, 0],
  150. [0.1, 0.2, 0.102],
  151. [0.2, 0.1, 0.102],
  152. [0.1, 0.5, 0.449],
  153. [0.1, 0.9, 1.372],
  154. [0.1, 0, 0.1],
  155. [0.1, -0.1, 0.201],
  156. ];
  157. }
  158. /**
  159. * @test cohensQ R out of bounds
  160. */
  161. public function testCohensQExceptionROutOfBounds()
  162. {
  163. // Then
  164. $this->expectException(Exception\OutOfBoundsException::class);
  165. // When
  166. EffectSize::cohensQ(0.1, 2);
  167. }
  168. /**
  169. * @test cohensD
  170. * @dataProvider dataProviderForCohensD
  171. * @param float $μ₁
  172. * @param float $μ₂
  173. * @param float $s₁
  174. * @param float $s₂
  175. * @param float $expected
  176. */
  177. public function testCohensD(float $μ₁, float $μ₂, float $s₁, float $s₂, float $expected)
  178. {
  179. // When
  180. $d = EffectSize::cohensD($μ₁, $μ₂, $s₁, $s₂);
  181. // Then
  182. $this->assertEqualsWithDelta($expected, $d, 0.00001);
  183. }
  184. /**
  185. * @return array [μ₁, μ₂, s₁, s₂, expected]
  186. */
  187. public function dataProviderForCohensD(): array
  188. {
  189. return [
  190. // Test data: http://www.uccs.edu/~lbecker/
  191. [3, 3, 1.5811388300842, 1.5811388300842, 0],
  192. [3, 4, 1.5811388300842, 1.5811388300842, -0.6324555320336718],
  193. [6, 4.9166666666667, 1.5954480704349, 2.5030284687058, 0.5161479565960618],
  194. [40, 57.727272727273, 21.275964529644, 30.763910379179, -0.6702470286592815],
  195. [6.7, 6, 1.2, 1, 0.6337502222976299],
  196. [9, 3.5, 1.2, 1.5, 4.049155956077707],
  197. [108, 118, 15, 14.83239697419133, -0.6704015],
  198. ];
  199. }
  200. /**
  201. * @test hedgesG
  202. * @dataProvider dataProviderForHedgesG
  203. * @param float $μ₁
  204. * @param float $μ₂
  205. * @param float $s₁
  206. * @param float $s₂
  207. * @param int $n₁
  208. * @param int $n₂
  209. * @param float $expected
  210. */
  211. public function testHedgesG(float $μ₁, float $μ₂, float $s₁, float $s₂, int $n₁, int $n₂, float $expected)
  212. {
  213. // When
  214. $g = EffectSize::hedgesG($μ₁, $μ₂, $s₁, $s₂, $n₁, $n₂);
  215. // Then
  216. $this->assertEqualsWithDelta($expected, $g, 0.00001);
  217. }
  218. /**
  219. * @return array [μ₁, μ₂, s₁, s₂, n₁, n₂, expected]
  220. */
  221. public function dataProviderForHedgesG(): array
  222. {
  223. return [
  224. // Test data: http://www.polyu.edu.hk/mm/effectsizefaqs/calculator/calculator.html
  225. [3, 3, 1.5811388300842, 1.5811388300842, 5, 5, 0],
  226. [3, 4, 1.5811388300842, 1.5811388300842, 5, 5, -0.57125016],
  227. [6, 4.9166666666667, 1.5954480704349, 2.5030284687058, 12, 12, 0.49834975],
  228. [40, 57.727272727273, 21.275964529644, 30.763910379179, 7, 11, -0.61190744],
  229. [6.7, 6, 1.2, 1, 15, 15, 0.61662184],
  230. [6.7, 6, 1.2, 1, 16, 15, 0.61530752],
  231. [6.7, 6, 1.2, 1, 45, 15, 0.59824169],
  232. [9, 3.5, 1.2, 1.5, 13, 15, 3.89844347],
  233. [108, 118, 15, 14.83239697419133, 21, 18, -0.65642092],
  234. ];
  235. }
  236. /**
  237. * @test glassDelta
  238. * @dataProvider dataProviderForGlassDelta
  239. * @param float $μ₁
  240. * @param float $μ₂
  241. * @param float $s₂
  242. * @param float $expected
  243. */
  244. public function testGlassDelta(float $μ₁, float $μ₂, float $s₂, float $expected)
  245. {
  246. // When
  247. $Δ = EffectSize::glassDelta($μ₁, $μ₂, $s₂);
  248. // Then
  249. $this->assertEqualsWithDelta($expected, $Δ, 0.00001);
  250. }
  251. /**
  252. * @return array [μ₁, μ₂, s₂, expected]
  253. */
  254. public function dataProviderForGlassDelta(): array
  255. {
  256. return [
  257. [40, 57.727272727273, 30.763910379179, -0.57623600],
  258. [3, 4, 1.5811388300842, -0.63245553],
  259. [3, 3, 1.5, 0],
  260. ];
  261. }
  262. }