UNPKG

@stdlib/stats

Version:

Standard library statistical functions.

90 lines (66 loc) 2.44 kB
{{alias}}( x[, options] ) Performs a chi-square independence test. For a two-way contingency table, the function computes a chi-square independence test for the null hypothesis that the joint distribution of the cell counts is the product of the row and column marginals (i.e. that the row and column variables are independent). 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. The function returns an object containing the test statistic, p-value, and decision. Parameters ---------- x: ndarray|Array<Collection<number>> Two-way table of observed frequencies. options: Object (optional) Options. options.alpha: number (optional) Significance level of the hypothesis test. Must be on the interval [0,1]. Default: 0.05. options.correct: boolean (optional) Boolean indicating whether to use Yates' continuity correction when provided a 2x2 contingency table. Default: true. Returns ------- out: Object Test results object. out.alpha: number Significance level. out.rejected: boolean Test decision. out.pValue: number Test p-value. out.statistic: number Test statistic. out.df: number Degrees of freedom. out.expected: ndarray Expected frequencies. out.method: string Test name. out.toString: Function Serializes results as formatted output. out.toJSON: Function Serializes results as JSON. Examples -------- > var x = [ [ 20, 30 ], [ 30, 20 ] ]; > var out = {{alias}}( x ); > var o = out.toJSON() { 'rejected': false, 'pValue': ~0.072, 'statistic': 3.24, ... } > out.toString() // Set significance level... > var opts = { 'alpha': 0.1 }; > out = {{alias}}( x, opts ); > o = out.toJSON() { 'rejected': true, 'pValue': ~0.072, 'statistic': 3.24, ... } > out.toString() // Disable Yates' continuity correction (primarily used with small counts): > opts = { 'correct': false }; > out = {{alias}}( x, opts ); > out.toString() See Also --------