README.md (6129B)
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 # One Way ANOVA 22 23 > Perform a one-way analysis of variance. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var anova1 = require( '@stdlib/stats/anova1' ); 31 ``` 32 33 #### anova1( x, factor\[, opts] ) 34 35 For an [array][mdn-array] or [typed array][mdn-typed-array] of numeric values `x` and an [array][mdn-array] of classifications `factor`, a one-way analysis of variance is performed. The hypotheses are given as follows: 36 37 <!-- <equation class="equation" label="eq:hypotheses" align="center" raw="\begin{align*} H_{0}:& \; \mu_{1} = \mu_{2} = \dots = \mu_{k} \\ H_{a}:& \; \text{at least one} \; \mu_{i} \; \text{not equal to the others} \end{align*}" alt="Hypotheses of ANOVA"> --> 38 39 <div class="equation" align="center" data-raw-text="\begin{align*} H_{0}:& \; \mu_{1} = \mu_{2} = \dots = \mu_{k} \\ H_{a}:& \; \text{at least one} \; \mu_{i} \; \text{not equal to the others} \end{align*}" data-equation="eq:hypotheses"> 40 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@7e0a95722efd9c771b129597380c63dc6715508b/lib/node_modules/@stdlib/stats/anova1/docs/img/equation_hypotheses.svg" alt="Hypotheses of ANOVA"> 41 <br> 42 </div> 43 44 <!-- </equation> --> 45 46 The function returns an object containing the treatment and error squared errors, degrees of freedom, mean squared errors, and both the p-value and F score. 47 48 ```javascript 49 var out; 50 var x; 51 var y; 52 53 x = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]; 54 y = [ 'Treatment A', 'Treatment B', 'Treatment C', 'Control', 'Treatment A', 'Treatment B', 'Treatment C', 'Control', 'Treatment A', 'Treatment B', 'Treatment C', 'Control' ]; 55 56 out = anova1( x, y ); 57 /* returns 58 { 59 'treatment': { 'df': 11, 'ss': 15, 'ms': 5 }, 60 'error': { 'df': 8, 'ss': 128, 'ms': 16 }, 61 'statistic': 0.3125, 62 'pValue': 0.81607947904798, 63 'means': 64 { 'Treatment A': { 'mean': 5, 'sampleSize': 3, 'SD': 4 }, 65 'Treatment B': { 'mean': 6, 'sampleSize': 3, 'SD': 4 }, 66 'Treatment C': { 'mean': 7, 'sampleSize': 3, 'SD': 4 }, 67 'Control': { 'mean': 8, 'sampleSize': 3, 'SD': 4 } }, 68 'method': 'One-Way ANOVA' 69 } 70 */ 71 ``` 72 73 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. 74 75 ```javascript 76 var out; 77 var x; 78 var y; 79 80 x = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]; 81 y = [ 'Treatment A', 'Treatment B', 'Treatment C', 'Control', 'Treatment A', 'Treatment B', 'Treatment C', 'Control', 'Treatment A', 'Treatment B', 'Treatment C', 'Control' ]; 82 83 out = anova1( x, y ); 84 console.log( out.print() ); 85 /* => 86 One-Way ANOVA 87 88 Null Hypothesis: All Means Equal 89 Alternate Hypothesis: At Least one Mean not Equal 90 91 df SS MS F Score P Value 92 Treatment 3 15 5 0.3125 0.8161 93 Errors 8 128 16 94 95 Fail to Reject Null: 0.8161 >= 0.05 96 */ 97 ``` 98 99 The function accepts the following `options`: 100 101 - **alpha**: `number` in the interval `[0,1]` giving the significance level of the hypothesis test. Default: `0.05`. 102 - **decision**: a `boolean` value indicating if function is to return a decision of either _rejection of the null hypothesis_ or _failure to reject the null hypothesis_. Default: `false` 103 104 By default, the test is carried out at a significance level of `0.05`. To choose a custom significance level, set the `alpha` option. 105 106 ```javascript 107 var x = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]; 108 var y = [ 'Treatment A', 'Treatment B', 'Treatment C', 'Control', 'Treatment A', 'Treatment B', 'Treatment C', 'Control', 'Treatment A', 'Treatment B', 'Treatment C', 'Control' ]; 109 110 var out = anova1( x, y ); 111 var table = out.print(); 112 /* e.g., returns 113 One-Way ANOVA 114 115 Null Hypothesis: All Means Equal 116 Alternate Hypothesis: At Least one Mean not Equal 117 118 df SS MS F Score P Value 119 Treatment 3 15 5 0.3125 0.8161 120 Errors 8 128 16 121 122 Fail to Reject Null: 0.8161 >= 0.05 123 */ 124 125 out = anova1( x, y, { 126 'alpha': 0.9 127 }); 128 table = out.print(); 129 /* e.g., returns 130 One-Way ANOVA 131 132 Null Hypothesis: All Means Equal 133 Alternate Hypothesis: At Least one Mean not Equal 134 135 df SS MS F Score P Value 136 Treatment 3 15 5 0.3125 0.8161 137 Errors 8 128 16 138 139 Reject Null: 0.8161 <= 0.9 140 */ 141 ``` 142 143 </section> 144 145 <!-- /.usage --> 146 147 <section class="notes"> 148 149 ## Notes 150 151 - The calculation for the p value is based on [an F distribution][anova-nist]. 152 153 </section> 154 155 <!-- /.notes --> 156 157 <section class="examples"> 158 159 ## Examples 160 161 <!-- eslint no-undef: "error" --> 162 163 ```javascript 164 var anova1 = require( '@stdlib/stats/anova1' ); 165 166 var x = [ 3, 4, 5, 6, 2, 5, 10, 12, 8, 10 ]; 167 var f = [ 'control', 'treatA', 'treatB', 'control', 'treatA', 'treatB', 'control', 'treatA', 'treatB', 'control' ]; 168 169 var out = anova1( x, f, { 170 'decision': true 171 }); 172 173 console.log( out.print() ); 174 175 out = anova1( x, f, { 176 'alpha': 0.9 177 }); 178 179 console.log( out.print() ); 180 ``` 181 182 </section> 183 184 <!-- /.examples --> 185 186 <section class="links"> 187 188 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 189 190 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays 191 192 [anova-nist]: https://www.itl.nist.gov/div898/handbook/ppc/section2/ppc231.htm 193 194 </section> 195 196 <!-- /.links -->