Categories &

Functions List

Function Reference: chi2test

statistics: h = chi2test (x)

statistics: [h, p] = chi2test (x)

statistics: [h, p, stats] = chi2test (x)

statistics: […] = chi2test (x, name, value)

statistics: […] = chi2test (…, 'Alpha', alpha)

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

For 2-way contingency tables, chi2test performs a chi-squared test for independence or homogeneity, according to the sampling scheme and related question. Independence means that 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 answer 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 returns h, 1 if the null hypothesis is rejected at the significance level alpha and 0 otherwise, the p-value p, and a structure stats with the following fields:

chi2statthe chi^2 statistic of the test.
dfits degrees of freedom.
Othe table tested: x, or for "marginal" the two-way table left after collapsing x.
Ethe expected values under the tested model, in the layout of O.
CohensWCohen’s w, the effect size of the test.
CohensWCIits confidence interval.
CramersVCramer’s V, for a two-way table and for the "joint" and "marginal" models, which test one.
CramersVCIits confidence interval.

Both effect sizes are corrected for bias. With n observations, w = sqrt (max (chi^2 - df, 0) / n), and V = w / sqrt (k) with k one less than the smaller dimension of the two-way table; for "joint" that table has the variable it names as rows and every combination of the other two as columns. The uncorrected sqrt (chi^2 / n) is biased upwards, by df / n in its square. The confidence intervals invert the noncentral chi^2 distribution: the bounds lambda of the noncentrality consistent with the observed chi^2 give sqrt (lambda / n) for w, divided by sqrt (k) for V, whose upper bound is at most 1.

'Alpha' sets the significance level alpha of h and the level 100 (1 - alpha)% of both confidence intervals. It is 0.05 by default.

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 test for mutual independence among the three variables. Alternatively, when called in the form […] = chi2test (x, name, value), it can perform the following tests:

namevalueDescription
"mutual"[]Mutual independence. All variables are independent from each other, (A, B, C). It takes no value; an empty matrix is accepted.
"joint"scalarJoint 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"scalarMarginal 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); the table is collapsed over it and independence is tested in the two-way table that remains.
"conditional"scalarConditional 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). It takes no value; an empty matrix is accepted.

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