README.md (5704B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # kruskalTest 22 23 > Compute the Kruskal-Wallis test for equal medians. 24 25 <section class="intro"> 26 27 The Kruskal-Wallis rank sum test evaluates for multiple samples the null hypothesis that their medians are identical. The Kruskal-Wallis test is a nonparametric test which does not require the data to be normally distributed. 28 29 To carry out the test, the rank sums `S_h` of the individual groups are calculated. The test statistic is then calculated as 30 31 <!-- <equation class="equation" label="eq:kruskal_test_statistic" align="center" raw="H = \frac{\tfrac{12}{N(N+1)}\sum_h\tfrac{S_h^2}{n_h}-3(N+1)}{1-\tfrac{1}{(N^3-N)} \sum t_{r(i)}^3 - t_{r(i)}}" alt="Equation for the Kruskal-Wallis test statistic."> --> 32 33 <div class="equation" align="center" data-raw-text="H = \frac{\tfrac{12}{N(N+1)}\sum_h\tfrac{S_h^2}{n_h}-3(N+1)}{1-\tfrac{1}{(N^3-N)} \sum t_{r(i)}^3 - t_{r(i)}}" data-equation="eq:kruskal_test_statistic"> 34 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@e1fbdee688c5409e4cc6b0cd06d90b1cd2abd67c/lib/node_modules/@stdlib/stats/kruskal-test/docs/img/equation_kruskal_test_statistic.svg" alt="Equation for the Kruskal-Wallis test statistic."> 35 <br> 36 </div> 37 38 <!-- </equation> --> 39 40 where `N` denotes the total number of observations and `t_{r(i)}` are the number of tied observations with rank _i_. 41 42 </section> 43 44 <!-- /.intro --> 45 46 <section class="usage"> 47 48 ## Usage 49 50 ```javascript 51 var kruskalTest = require( '@stdlib/stats/kruskal-test' ); 52 ``` 53 54 #### kruskalTest( a\[,b,...,k]\[, opts] ) 55 56 For input arrays `a`, `b`, ... holding numeric observations, this function calculates the Kruskal-Wallis rank sums test, which tests the null hypothesis that the medians in all `k` groups are the same. 57 58 ```javascript 59 // Data from Hollander & Wolfe (1973), p. 116: 60 var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ]; 61 var y = [ 3.8, 2.7, 4.0, 2.4 ]; 62 var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ]; 63 64 var out = kruskalTest( x, y, z ); 65 /* returns 66 { 67 'rejected': false, 68 'alpha': 0.05, 69 'df': 2, 70 'pValue': ~0.68, 71 'statistic': ~0.771, 72 ... 73 } 74 */ 75 ``` 76 77 The function accepts the following `options`: 78 79 - **alpha**: `number` in the interval `[0,1]` giving the significance level of the hypothesis test. Default: `0.05`. 80 - **groups**: an `array` of group indicators. If set, the function assumes that only a single numeric array is provided holding all observations. 81 82 By default, the test is carried out at a significance level of `0.05`. To choose a custom significance level, set the `alpha` option. 83 84 ```javascript 85 var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ]; 86 var y = [ 3.8, 2.7, 4.0, 2.4 ]; 87 var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ]; 88 89 var out = kruskalTest( x, y, z, { 90 'alpha': 0.01 91 }); 92 /* returns 93 { 94 'rejected': false, 95 'alpha': 0.01, 96 'df': 2, 97 'pValue': ~0.68, 98 'statistic': ~0.771, 99 ... 100 } 101 */ 102 ``` 103 104 The function provides an alternate interface by supplying an array of group indicators to the `groups` option. In this case, it is assumed that only a single numeric array holding all observations is provided to the function. 105 106 <!-- eslint-disable array-element-newline --> 107 108 ```javascript 109 var arr = [ 110 2.9, 3.0, 2.5, 2.6, 3.2, 111 3.8, 2.7, 4.0, 2.4, 112 2.8, 3.4, 3.7, 2.2, 2.0 113 ]; 114 var groups = [ 115 'a', 'a', 'a', 'a', 'a', 116 'b', 'b', 'b', 'b', 117 'c', 'c', 'c', 'c', 'c' 118 ]; 119 var out = kruskalTest( arr, { 120 'groups': groups 121 }); 122 ``` 123 124 The returned object comes with a `.print()` method which when invoked will print a formatted output of the results of the hypothesis test. `print` accepts a `digits` option that controls the number of decimal digits displayed for the outputs and a `decision` option, which when set to `false` will hide the test decision. 125 126 ```javascript 127 var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ]; 128 var y = [ 3.8, 2.7, 4.0, 2.4 ]; 129 var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ]; 130 131 var out = kruskalTest( x, y, z ); 132 console.log( out.print() ); 133 /* => 134 Kruskal-Wallis Test 135 136 Null hypothesis: the medians of all groups are the same 137 138 pValue: 0.68 139 statistic: 0.7714 df: 2 140 141 Test Decision: Fail to reject null in favor of alternative at 5% significance level 142 */ 143 ``` 144 145 </section> 146 147 <!-- /.usage --> 148 149 <section class="examples"> 150 151 ## Examples 152 153 <!-- eslint no-undef: "error" --> 154 155 ```javascript 156 var kruskalTest = require( '@stdlib/stats/kruskal-test' ); 157 158 // Data from Hollander & Wolfe (1973), p. 116: 159 var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ]; 160 var y = [ 3.8, 2.7, 4.0, 2.4 ]; 161 var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ]; 162 163 var out = kruskalTest( x, y, z ); 164 /* returns 165 { 166 'rejected': false, 167 'alpha': 0.05, 168 'df': 2, 169 'pValue': ~0.68, 170 'statistic': ~0.771, 171 ... 172 } 173 */ 174 175 var table = out.print(); 176 /* returns 177 Kruskal-Wallis Test 178 179 Null hypothesis: the medians of all groups are the same 180 181 pValue: 0.68 182 statistic: 0.7714 df: 2 183 184 Test Decision: Fail to reject null in favor of alternative at 5% significance level 185 */ 186 ``` 187 188 </section> 189 190 <!-- /.examples --> 191 192 <section class="references"> 193 194 </section> 195 196 <!-- /.references --> 197 198 <section class="links"> 199 200 </section> 201 202 <!-- /.links -->