README.md (5821B)
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 # bartlettTest 22 23 > Compute Bartlett’s test for equal variances. 24 25 <section class="intro"> 26 27 Bartlett's test is used to test the null hypothesis that the variances of k groups are equal against the alternative that at least two of them are different. 28 29 For `k` groups each with `n_i` observations, the test statistic is 30 31 <!-- <equation class="equation" label="eq:bartlett-test-statistic" align="center" raw="\chi^2 = \frac{N\ln(S^2) - \sum_{i=0}^{k-1} n_i \ln(S_i^2)}{1 + \frac{1}{3(k-1)}\left(\sum_{i=0}^{k-1} \frac{1}{n_i} - \frac{1}{N}\right)}" alt="Equation for Bartlett's test statistic."> --> 32 33 <div class="equation" align="center" data-raw-text="\chi^2 = \frac{N\ln(S^2) - \sum_{i=0}^{k-1} n_i \ln(S_i^2)}{1 + \frac{1}{3(k-1)}\left(\sum_{i=0}^{k-1} \frac{1}{n_i} - \frac{1}{N}\right)}" data-equation="eq:bartlett-test-statistic"> 34 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@4b1db4ebd815eb54bf53a3fa132b992604743d9c/lib/node_modules/@stdlib/stats/bartlett-test/docs/img/equation_bartlett-test-statistic.svg" alt="Equation for Bartlett's test statistic."> 35 <br> 36 </div> 37 38 <!-- </equation> --> 39 40 where `N` is the total number of observations, `S_i` are the biased group-level variances and `S^2` is a (biased) pooled estimate for the variance. Under the null hypothesis, the test statistic follows a _chi-square_ distribution with `df = k - 1` degrees of freedom. 41 42 </section> 43 44 <!-- /.intro --> 45 46 <section class="usage"> 47 48 ## Usage 49 50 ```javascript 51 var bartlettTest = require( '@stdlib/stats/bartlett-test' ); 52 ``` 53 54 #### bartlettTest( a\[,b,...,k]\[, opts] ) 55 56 For input arrays `a`, `b`, ... holding numeric observations, this function calculates Bartlett’s test, which tests the null hypothesis that the variances 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 = bartlettTest( x, y, z ); 65 /* returns 66 { 67 'rejected': false, 68 'alpha': 0.05, 69 'df': 2, 70 'pValue': ~0.573, 71 'statistic': ~1.112, 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 = bartlettTest( x, y, z, { 90 'alpha': 0.01 91 }); 92 /* returns 93 { 94 'rejected': false, 95 'alpha': 0.01, 96 'df': 2, 97 'pValue': ~0.573, 98 'statistic': ~1.112, 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 = bartlettTest( 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 = bartlettTest( x, y, z ); 132 console.log( out.print() ); 133 /* => 134 Bartlett's test of equal variances 135 136 Null hypothesis: The variances in all groups are the same. 137 138 pValue: 0.5735 139 statistic: 1.1122 140 df: 2 141 142 Test Decision: Fail to reject null in favor of alternative at 5% significance level 143 */ 144 ``` 145 146 </section> 147 148 <!-- /.usage --> 149 150 <section class="examples"> 151 152 ## Examples 153 154 <!-- eslint no-undef: "error" --> 155 156 ```javascript 157 var bartlettTest = require( '@stdlib/stats/bartlett-test' ); 158 159 // Data from Hollander & Wolfe (1973), p. 116: 160 var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ]; 161 var y = [ 3.8, 2.7, 4.0, 2.4 ]; 162 var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ]; 163 164 var out = bartlettTest( x, y, z ); 165 /* returns 166 { 167 'rejected': false, 168 'alpha': 0.05, 169 'df': 2, 170 'pValue': ~0.573, 171 'statistic': ~1.112, 172 ... 173 } 174 */ 175 176 var table = out.print(); 177 /* returns 178 Bartlett's test of equal variances 179 180 Null hypothesis: The variances in all groups are the same. 181 182 pValue: 0.5735 183 statistic: 1.1122 184 df: 2 185 186 Test Decision: Fail to reject null in favor of alternative at 5% significance level 187 */ 188 ``` 189 190 </section> 191 192 <!-- /.examples --> 193 194 <section class="references"> 195 196 </section> 197 198 <!-- /.references --> 199 200 <section class="links"> 201 202 </section> 203 204 <!-- /.links -->