time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (5064B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2020 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 # Chi-square independence test
     22 
     23 > Perform a chi-square independence test.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var chi2test = require( '@stdlib/stats/chi2test' );
     31 ```
     32 
     33 #### chi2test( x\[, opts] )
     34 
     35 Computes a chi-square independence test for the **null hypothesis** that the joint distribution of the cell counts in two-dimensional `ndarray` or `array` of `arrays` `x` is the product of the row and column marginals, i.e. that the row and column variables are independent.
     36 
     37 ```javascript
     38 // 2x2 table
     39 var x = [
     40     [ 20, 30 ],
     41     [ 30, 20 ]
     42 ];
     43 
     44 var out = chi2test( x );
     45 /* returns
     46     {
     47         'rejected': false,
     48         'alpha': 0.05,
     49         'pValue': ~0.072,
     50         'df': 1,
     51         'statistic': 3.24,
     52         ...
     53     }
     54 */
     55 ```
     56 
     57 The function accepts the following `options`:
     58 
     59 -   **alpha**: significance level of the hypothesis test. Must be on the interval `[0,1]`. Default: `0.05`.
     60 -   **correct**: `boolean` indicating whether to use Yates' continuity correction when provided a 2x2 contingency table. Default: `true`.
     61 
     62 By default, the test is performed at a significance level of `0.05`. To adjust the significance level, set the `alpha` option.
     63 
     64 ```javascript
     65 var x = [
     66     [ 20, 30 ],
     67     [ 30, 20 ]
     68 ];
     69 var opts = {
     70     'alpha': 0.1
     71 };
     72 var out = chi2test( x, opts );
     73 /* returns
     74     {
     75         'rejected': true,
     76         'alpha': 0.1,
     77         'pValue': ~0.072,
     78         'df': 1,
     79         'statistic': 3.24,
     80         ...
     81     }
     82 */
     83 ```
     84 
     85 For 2x2 contingency tables, the function by default applies Yates' continuity correction. To disable the continuity correction, set `correct` to `false`.
     86 
     87 ```javascript
     88 var x = [
     89     [ 20, 30 ],
     90     [ 30, 20 ]
     91 ];
     92 var opts = {
     93     'correct': false
     94 };
     95 var out = chi2test( x, opts );
     96 /* returns
     97     {
     98         'rejected': true,
     99         'alpha': 0.05,
    100         'pValue': ~0.046,
    101         'df': 1,
    102         'statistic': 4,
    103         ...
    104     }
    105 */
    106 ```
    107 
    108 The function returns an `object` having the following properties:
    109 
    110 -   **alpha**: significance level.
    111 -   **rejected**: `boolean` indicating the test decision.
    112 -   **pValue**: test p-value.
    113 -   **statistic**: test statistic.
    114 -   **df**: degrees of freedom.
    115 -   **expected**: expected cell counts.
    116 -   **method**: test name.
    117 -   **print**: method for printing formatted test output.
    118 
    119 To print formatted test output, invoke the `print` method. `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.
    120 
    121 <!-- run-disable -->
    122 
    123 ```javascript
    124 var x = [
    125     [ 20, 30 ],
    126     [ 30, 20 ]
    127 ];
    128 var out = chi2test( x );
    129 console.log( out.print() );
    130 /* =>
    131 *    Chi-square independence test
    132 *
    133 *    Null hypothesis: the two variables are independent
    134 *
    135 *        pValue: 0.0719
    136 *        statistic: 3.24
    137 *        degrees of freedom: 1
    138 *
    139 *    Test Decision: Fail to reject null in favor of alternative at 5% significance level
    140 */
    141 
    142 console.log( out.print({
    143     'digits': 6
    144 }) );
    145 /* =>
    146 * Chi-square independence test
    147 *
    148 * Null hypothesis: the two variables are independent
    149 *
    150 *     pValue: 0.071861
    151 *     statistic: 3.24
    152 *     degrees of freedom: 1
    153 *
    154 * Test Decision: Fail to reject null in favor of alternative at 5% significance level
    155 */
    156 ```
    157 
    158 </section>
    159 
    160 <!-- /.usage -->
    161 
    162 <section class="notes">
    163 
    164 ## Notes
    165 
    166 -   The chi-square approximation may be incorrect if the observed or expected frequencies in each category are too small. Common practice is to require frequencies greater than five. The Yates' continuity correction is enabled by default for 2x2 tables to account for this, although it tends to over-correct.
    167 
    168 </section>
    169 
    170 <!-- /.notes -->
    171 
    172 <section class="examples">
    173 
    174 ## Examples
    175 
    176 <!-- eslint no-undef: "error" -->
    177 
    178 ```javascript
    179 var array = require( '@stdlib/ndarray/array' );
    180 var chi2test = require( '@stdlib/stats/chi2test' );
    181 
    182 var table;
    183 var out;
    184 
    185 /*
    186 * Data from students in grades 4-6 on whether good grades, athletic ability, or popularity are most important to them:
    187 *
    188 * Source: Chase, M.A and Dummer, G.M. (1992), "The Role of Sports as a Social Determinant for Children"
    189 */
    190 table = array([
    191     /* Grades Popular Sports */
    192     [ 63, 31, 25 ], // 4th
    193     [ 88, 55, 33 ], // 5th
    194     [ 96, 55, 32 ] // 6th
    195 ]);
    196 
    197 // Assess whether the grade level and the students' goals are independent of each other:
    198 out = chi2test( table );
    199 // returns {...}
    200 
    201 console.log( out.print() );
    202 ```
    203 
    204 </section>
    205 
    206 <!-- /.examples -->
    207 
    208 <section class="links">
    209 
    210 </section>
    211 
    212 <!-- /.links -->