chiSquareContingency

Performs a Pearson's chi-square test on a contingency table of arbitrary dimensions. When the chi-square test is mentioned, this is usually the one being referred to. Takes a set of finite forward ranges, one for each column in the contingency table. These can be expressed either as a tuple of ranges or a range of ranges. Returns a P-value for the alternative hypothesis that frequencies in each row of the contingency table depend on the column against the null that they don't.

Notes: The chi-square test relies on asymptotic statistical properties and is therefore not exact. The typical rule of thumb is that each cell should have an expected value of at least 5. However, this is likely to be unnecessarily stringent.

Yates's continuity correction is never used in this implementation. If you want something that's guaranteed to be conservative, use fisherExact().

This is, for all practical purposes, an inherently non-directional test. Therefore, the one-sided verses two-sided option is not provided.

For 2x2 contingency tables, fisherExact is a more conservative test, in that the type I error rate is guaranteed to never be above the nominal P-value. However, even for small sample sizes this test may produce results closer to the true P-value, at the risk of possibly being non-conservative.

chiSquareContingency
(
T...
)

Examples

1 // Test to see whether the relative frequency of outcome 0, 1, and 2
2 // depends on the treatment (drug1, drug2 or placebo) in some hypothetical
3 // experiment.  For example, 1500 people had outcome 2 if they were treated
4 // with drug1 and 1100 had outcome 1 if treated with placebo.
5 uint[] drug1 = [1000, 2000, 1500];
6 uint[] drug2 = [1500, 3000, 2300];
7 uint[] placebo = [500, 1100, 750];
8 auto result1 = chiSquareContingency(drug1, drug2, placebo);
9 
10 // The following uses a range of ranges instead of an array of ranges,
11 // and will produce equivalent results.
12 auto rangeOfRanges = [drug1, drug2, placebo];
13 auto result2 = chiSquareContingency(rangeOfRanges);

References: http://en.wikipedia.org/wiki/Pearson%27s_chi-square_test

Meta