Function 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.

TestRes chiSquareContingency(T...) (
  T inputData
);

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.

Examples

// Test to see whether the relative frequency of outcome 0, 1, and 2
// depends on the treatment (drug1, drug2 or placebo) in some hypothetical
// experiment.  For example, 1500 people had outcome 2 if they were treated
// with drug1 and 1100 had outcome 1 if treated with placebo.
uint[] drug1 = [1000, 2000, 1500];
uint[] drug2 = [1500, 3000, 2300];
uint[] placebo = [500, 1100, 750];
auto result1 = chiSquareContingency(drug1, drug2, placebo);

// The following uses a range of ranges instead of an array of ranges,
// and will produce equivalent results.
auto rangeOfRanges = [drug1, drug2, placebo];
auto result2 = chiSquareContingency(rangeOfRanges);

References

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