### Function Reference: `chi2test`

statistics: pval = chi2test (x)
statistics: [pval, chisq] = chi2test (x)
statistics: [pval, chisq, dF] = chi2test (x)
statistics: [pval, chisq, dF, E] = chi2test (x)
statistics: […] = chi2test (x, name, value)

Perform a chi-squared test (for independence or homogeneity).

For 2-way contingency tables, `chi2test` performs and a chi-squared test for independence or homogeneity, according to the sampling scheme and related question. Independence means that the the two variables forming the 2-way table are not associated, hence you cannot predict from one another. Homogeneity refers to the concept of similarity, hence they all come from the same distribution.

Both tests are computationally identical and will produce the same result. Nevertheless, they anwser to different questions. Consider two variables, one for gender and another for smoking. To test independence (whether gender and smoking is associated), we would randomly sample from the general population and break them down into categories in the table. To test homogeneity (whether men and women share the same smoking habits), we would sample individuals from within each gender, and then measure their smoking habits (e.g. smokers vs non-smokers).

When `chi2test` is called without any output arguments, it will print the result in the terminal including p-value, chi^2 statistic, and degrees of freedom. Otherwise it can return the following output arguments:

| pval | the p-value of the relevant test.
| chisq | the chi^2 statistic of the relevant test.
| dF | the degrees of freedom of the relevant test.
| E | the EXPECTED values of the original contigency table.

Unlike MATLAB, in GNU Octave `chi2test` also supports 3-way tables, which involve three categorical variables (each in a different dimension of x. In its simplest form, `[…] = chi2test (x)` will will test for mutual independence among the three variables. Alternatively, when called in the form `[…] = chi2test (x, name, value)`, it can perform the following tests:

name | value | Description
--- | --- | ---
"mutual" | [] | Mutual independence. All variables are independent from each other, (A, B, C). Value must be an empty matrix.
"joint" | scalar | Joint independence. Two variables are jointly independent of the third, (AB, C). The scalar value corresponds to the dimension of the independent variable (i.e. 3 for C).
"marginal" | scalar | Marginal independence. Two variables are independent if you ignore the third, (A, C). The scalar value corresponds to the dimension of the variable to be ignored (i.e. 2 for B).
"conditional" | scalar | Conditional independence. Two variables are independent given the third, (AC, BC). The scalar value corresponds to the dimension of the variable that forms the conditional dependence (i.e. 3 for C).
"homogeneous" | [] | Homogeneous associations. Conditional (partial) odds-ratios are not related on the value of the third, (AB, AC, BC). Value must be an empty matrix.

When testing for homogeneous associations in 3-way tables, the iterative proportional fitting procedure is used. For small samples it is better to use the Cochran-Mantel-Haenszel Test. K-way tables for k > 3 are supported only for testing mutual independence. Similar to 2-way tables, no optional parameters are required for k > 3 multi-way tables.

`chi2test` produces a warning if any cell of a 2x2 table has an expected frequency less than 5 or if more than 20% of the cells in larger 2-way tables have expected frequencies less than 5 or any cell with expected frequency less than 1. In such cases, use `fishertest`.

See also: crosstab, fishertest, mcnemar_test

Source Code: chi2test