README.md (10374B)
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 # incrgrubbs 22 23 > [Grubbs' test][grubbs-test] for outliers. 24 25 <section class="intro"> 26 27 [Grubbs' test][grubbs-test] (also known as the **maximum normalized residual test** or **extreme studentized deviate test**) is a statistical test used to detect outliers in a univariate dataset assumed to come from a normally distributed population. [Grubbs' test][grubbs-test] is defined for the hypothesis: 28 29 - **H_0**: the dataset does **not** contain outliers. 30 - **H_1**: the dataset contains **exactly** one outlier. 31 32 The [Grubbs' test][grubbs-test] statistic for a two-sided alternative hypothesis is defined as 33 34 <!-- <equation class="equation" label="eq:grubbs_test_statistic" align="center" raw="G = \frac{\max_{i=0,\ldots,N-1} |Y_i - \bar{Y}|}{s}" alt="Grubbs' test statistic."> --> 35 36 <div class="equation" align="center" data-raw-text="G = \frac{\max_{i=0,\ldots,N-1} |Y_i - \bar{Y}|}{s}" data-equation="eq:grubbs_test_statistic"> 37 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@d6af7da0801d2116a9507668d13ef7bf607fd275/lib/node_modules/@stdlib/stats/incr/grubbs/docs/img/equation_grubbs_test_statistic.svg" alt="Grubbs' test statistic."> 38 <br> 39 </div> 40 41 <!-- </equation> --> 42 43 where `s` is the sample standard deviation. The [Grubbs test][grubbs-test] statistic is thus the largest absolute deviation from the sample mean in units of the sample standard deviation. 44 45 The [Grubbs' test][grubbs-test] statistic for the alternative hypothesis that the minimum value is an outlier is defined as 46 47 <!-- <equation class="equation" label="eq:grubbs_test_statistic_min" align="center" raw="G = \frac{\bar{Y} - Y_{\textrm{min}}}{s}" alt="Grubbs' test statistic for testing whether the minimum value is an outlier."> --> 48 49 <div class="equation" align="center" data-raw-text="G = \frac{\bar{Y} - Y_{\textrm{min}}}{s}" data-equation="eq:grubbs_test_statistic_min"> 50 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@d6af7da0801d2116a9507668d13ef7bf607fd275/lib/node_modules/@stdlib/stats/incr/grubbs/docs/img/equation_grubbs_test_statistic_min.svg" alt="Grubbs' test statistic for testing whether the minimum value is an outlier."> 51 <br> 52 </div> 53 54 <!-- </equation> --> 55 56 The [Grubbs' test][grubbs-test] statistic for the alternative hypothesis that the maximum value is an outlier is defined as 57 58 <!-- <equation class="equation" label="eq:grubbs_test_statistic_max" align="center" raw="G = \frac{Y_{\textrm{max}} - \bar{Y}}{s}" alt="Grubbs' test statistic for testing whether the maximum value is an outlier."> --> 59 60 <div class="equation" align="center" data-raw-text="G = \frac{Y_{\textrm{max}} - \bar{Y}}{s}" data-equation="eq:grubbs_test_statistic_max"> 61 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@d6af7da0801d2116a9507668d13ef7bf607fd275/lib/node_modules/@stdlib/stats/incr/grubbs/docs/img/equation_grubbs_test_statistic_max.svg" alt="Grubbs' test statistic for testing whether the maximum value is an outlier."> 62 <br> 63 </div> 64 65 <!-- </equation> --> 66 67 For a two-sided test, the hypothesis that a dataset does **not** contain an outlier is rejected at significance level α if 68 69 <!-- <equation class="equation" label="eq:grubbs_test_two_sided" align="center" raw="G > \frac{N-1}{\sqrt{N}} \sqrt{\frac{t^2_{\alpha/(2N),N-2}}{N - 2 + t^2_{\alpha/(2N),N-2}}}" alt="Two-sided Grubbs' test."> --> 70 71 <div class="equation" align="center" data-raw-text="G > \frac{N-1}{\sqrt{N}} \sqrt{\frac{t^2_{\alpha/(2N),N-2}}{N - 2 + t^2_{\alpha/(2N),N-2}}}" data-equation="eq:grubbs_test_two_sided"> 72 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@d6af7da0801d2116a9507668d13ef7bf607fd275/lib/node_modules/@stdlib/stats/incr/grubbs/docs/img/equation_grubbs_test_two_sided.svg" alt="Two-sided Grubbs' test."> 73 <br> 74 </div> 75 76 <!-- </equation> --> 77 78 where `t` denotes the upper critical value of the _t_-distribution with `N-2` degrees of freedom and a significance level of `α/(2N)`. 79 80 For a one-sided test, the hypothesis that a dataset does **not** contain an outlier is rejected at significance level α if 81 82 <!-- <equation class="equation" label="eq:grubbs_test_one_sided" align="center" raw="G > \frac{N-1}{\sqrt{N}} \sqrt{\frac{t^2_{\alpha/N,N-2}}{N - 2 + t^2_{\alpha/N,N-2}}}" alt="One-sided Grubbs' test."> --> 83 84 <div class="equation" align="center" data-raw-text="G > \frac{N-1}{\sqrt{N}} \sqrt{\frac{t^2_{\alpha/N,N-2}}{N - 2 + t^2_{\alpha/N,N-2}}}" data-equation="eq:grubbs_test_one_sided"> 85 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@d6af7da0801d2116a9507668d13ef7bf607fd275/lib/node_modules/@stdlib/stats/incr/grubbs/docs/img/equation_grubbs_test_one_sided.svg" alt="One-sided Grubbs' test."> 86 <br> 87 </div> 88 89 <!-- </equation> --> 90 91 where `t` denotes the upper critical value of the _t_-distribution with `N-2` degrees of freedom and a significance level of `α/N`. 92 93 </section> 94 95 <!-- /.intro --> 96 97 <section class="usage"> 98 99 ## Usage 100 101 ```javascript 102 var incrgrubbs = require( '@stdlib/stats/incr/grubbs' ); 103 ``` 104 105 #### incrgrubbs( \[options] ) 106 107 Returns an accumulator `function` which incrementally performs [Grubbs' test][grubbs-test] for outliers. 108 109 ```javascript 110 var accumulator = incrgrubbs(); 111 ``` 112 113 The function accepts the following `options`: 114 115 - **alpha**: significance level. Default: `0.05`. 116 117 - **alternative**: alternative hypothesis. The option may be one of the following values: 118 119 - `'two-sided'`: test whether the minimum or maximum value is an outlier. 120 - `'min'`: test whether the minimum value is an outlier. 121 - `'max'`: test whether the maximum value is an outlier. 122 123 Default: `'two-sided'`. 124 125 - **init**: number of data points the accumulator should use to compute initial statistics **before** testing for an outlier. Until the accumulator is provided the number of data points specified by this option, the accumulator returns `null`. Default: `100`. 126 127 #### accumulator( \[x] ) 128 129 If provided an input value `x`, the accumulator function returns updated test results. If not provided an input value `x`, the accumulator function returns the current test results. 130 131 ```javascript 132 var rnorm = require( '@stdlib/random/base/normal' ); 133 134 var opts = { 135 'init': 0 136 }; 137 var accumulator = incrgrubbs( opts ); 138 139 var results = accumulator( rnorm( 10.0, 5.0 ) ); 140 // returns null 141 142 results = accumulator( rnorm( 10.0, 5.0 ) ); 143 // returns null 144 145 results = accumulator( rnorm( 10.0, 5.0 ) ); 146 // returns <Object> 147 148 results = accumulator(); 149 // returns <Object> 150 ``` 151 152 The accumulator function returns an `object` having the following fields: 153 154 - **rejected**: boolean indicating whether the null hypothesis should be rejected. 155 - **alpha**: significance level. 156 - **criticalValue**: critical value. 157 - **statistic**: test statistic. 158 - **df**: degrees of freedom. 159 - **mean**: sample mean. 160 - **sd**: corrected sample standard deviation. 161 - **min**: minimum value. 162 - **max**: maximum value. 163 - **alt**: alternative hypothesis. 164 - **method**: method name. 165 - **print**: method for pretty-printing test output. 166 167 The `print` method accepts the following options: 168 169 - **digits**: number of digits after the decimal point. Default: `4`. 170 - **decision**: `boolean` indicating whether to print the test decision. Default: `true`. 171 172 </section> 173 174 <!-- /.usage --> 175 176 <section class="notes"> 177 178 ## Notes 179 180 - [Grubbs' test][grubbs-test] **assumes** that data is normally distributed. Accordingly, one should first **verify** that the data can be _reasonably_ approximated by a normal distribution before applying the [Grubbs' test][grubbs-test]. 181 - The accumulator must be provided **at least** three data points before performing [Grubbs' test][grubbs-test]. Until at least three data points are provided, the accumulator returns `null`. 182 - Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the test statistic is `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. 183 184 </section> 185 186 <!-- /.notes --> 187 188 <section class="examples"> 189 190 ## Examples 191 192 <!-- eslint no-undef: "error" --> 193 194 ```javascript 195 var incrgrubbs = require( '@stdlib/stats/incr/grubbs' ); 196 197 var data; 198 var opts; 199 var acc; 200 var i; 201 202 // Define a data set (8 mass spectrometer measurements of a uranium isotope; see Tietjen and Moore. 1972. "Some Grubbs-Type Statistics for the Detection of Several Outliers".) 203 data = [ 199.31, 199.53, 200.19, 200.82, 201.92, 201.95, 202.18, 245.57 ]; 204 205 // Create a new accumulator: 206 opts = { 207 'init': data.length, 208 'alternative': 'two-sided' 209 }; 210 acc = incrgrubbs( opts ); 211 212 // Update the accumulator: 213 for ( i = 0; i < data.length; i++ ) { 214 acc( data[ i ] ); 215 } 216 217 // Print the test results: 218 console.log( acc().print() ); 219 /* e.g., => 220 Grubbs' Test 221 222 Alternative hypothesis: The maximum value (245.57) is an outlier 223 224 criticalValue: 2.1266 225 statistic: 2.4688 226 df: 6 227 228 Test Decision: Reject null in favor of alternative at 5% significance level 229 230 */ 231 ``` 232 233 </section> 234 235 <!-- /.examples --> 236 237 <section class="references"> 238 239 * * * 240 241 ## References 242 243 - Grubbs, Frank E. 1950. "Sample Criteria for Testing Outlying Observations." _The Annals of Mathematical Statistics_ 21 (1). The Institute of Mathematical Statistics: 27–58. doi:[10.1214/aoms/1177729885][@grubbs:1950a]. 244 - Grubbs, Frank E. 1969. "Procedures for Detecting Outlying Observations in Samples." _Technometrics_ 11 (1). Taylor & Francis: 1–21. doi:[10.1080/00401706.1969.10490657][@grubbs:1969a]. 245 246 </section> 247 248 <!-- /.references --> 249 250 <section class="links"> 251 252 [grubbs-test]: https://en.wikipedia.org/wiki/Grubbs%27_test_for_outliers 253 254 [@grubbs:1950a]: https://doi.org/10.1214/aoms/1177729885 255 256 [@grubbs:1969a]: https://doi.org/10.1080/00401706.1969.10490657 257 258 </section> 259 260 <!-- /.links -->