Pearson's chi-squared test or Pearson's <math>\chi^2</math> test is a statistical test applied to sets of categorical data to evaluate how likely it is that any observed difference between the sets arose by chance. It is the most widely used of many chi-squared tests (e.g., Yates, likelihood ratio, portmanteau test in time series, etc.) – statistical procedures whose results are evaluated by reference to the chi-squared distribution. Its properties were first investigated by Karl Pearson in 1900. In contexts where it is important to improve a distinction between the test statistic and its distribution, names similar to Pearson χ-squared test or statistic are used.

It is a p-value test.

A simple example is testing the hypothesis that an ordinary six-sided die is "fair" (i. e., all six outcomes are equally likely to occur). In this case, the observed data is <math>(O_1, O_2, ..., O_6)</math>, the number of times that the die has fallen on each number. The null hypothesis is <math>\mathrm{Multinomial}(N; 1/6, ..., 1/6)</math>, and <math display="inline">\chi^2 := \sum\limits_{i=1}^6 \frac{N} = \sum_{i = 1}^r \frac{O_{i,j{N} </math>

is the fraction of observations of type j ignoring the row attribute (fraction of column totals). The term "frequencies" refers to absolute numbers rather than already normalized values.

The value of the test-statistic is

<math display="block">\begin{align}

\chi^2 &= \sum_{i=1}^r \sum_{j=1}^c \frac \\[1ex]

&= N \sum_{i,j} p_{i\cdot} p_{\cdot j} {\left(\frac{\left(O_{i,j}/N\right) - p_{i\cdot} p_{\cdot j{p_{i\cdot} p_{\cdot j\right)}^2

\end{align}</math>

Note that <math> \chi^2 </math> is 0 if and only if <math> O_{i,j} = E_{i,j} \forall i,j </math>, i.e. only if the expected and true number of observations are equal in all cells.

Fitting the model of "independence" reduces the number of degrees of freedom by . The number of degrees of freedom is equal to the number of cells rc, minus the reduction in degrees of freedom, p, which reduces to&nbsp;.

For the test of independence, also known as the test of homogeneity, a chi-squared probability of less than or equal to 0.05 (or the chi-squared statistic being at or larger than the 0.05 critical point) is commonly interpreted by applied workers as justification for rejecting the null hypothesis that the row variable is independent of the column variable.

The alternative hypothesis corresponds to the variables having an association or relationship where the structure of this relationship is not specified.

Assumptions

The chi-squared test, when used with the standard approximation that a chi-squared distribution is applicable, has the following assumptions:

; Simple random sample: The sample data is a random sampling from a fixed distribution or population where every collection of members of the population of the given sample size has an equal probability of selection. Variants of the test have been developed for complex samples, such as where the data is weighted. Other forms can be used such as purposive sampling.

; Sample size (whole table): A sample with a sufficiently large size is assumed. If a chi squared test is conducted on a sample with a smaller size, then the chi squared test will yield an inaccurate inference. The researcher, by using chi squared test on small samples, might end up committing a Type II error. For small sample sizes the Cash test is preferred.

; Expected cell count: Adequate expected cell counts. Some require 5 or more, and others require 10 or more. A common rule is 5 or more in all cells of a 2-by-2 table, and 5 or more in 80% of cells in larger tables, but no cells with zero expected count. When this assumption is not met, Yates's correction is applied.

; Independence: The observations are always assumed to be independent of each other. This means chi-squared cannot be used to test correlated data (like matched pairs or panel data). In those cases, McNemar's test may be more appropriate.

A test that relies on different assumptions is Fisher's exact test; if its assumption of fixed marginal distributions is met it is substantially more accurate in obtaining a significance level, especially with few observations. In the vast majority of applications this assumption will not be met, and Fisher's exact test will be over conservative and not have correct coverage.

Derivation

The null distribution of the Pearson statistic with j rows and k columns is approximated by the chi-squared distribution with

(k&nbsp;−&nbsp;1)(j&nbsp;−&nbsp;1) degrees of freedom.

This approximation arises as the true distribution, under the null hypothesis, if the expected value is given by a multinomial distribution. For large sample sizes, the central limit theorem says this distribution tends toward a certain multivariate normal distribution.

Two cells

In the special case where there are only two cells in the table, the expected values follow a binomial distribution,

<math display="block">

O \sim \operatorname{Bin}(n,p),

</math>

where

: p = probability, under the null hypothesis,

: n = number of observations in the sample.

In the above example the hypothesised probability of a male observation is 0.5, with 100 samples. Thus we expect to observe 50 males.

If n is sufficiently large, the above binomial distribution may be approximated by a Gaussian (normal) distribution and thus the Pearson test statistic approximates a chi-squared distribution,

<math display="block">

\operatorname{Bin}(n,p) \approx \mathcal{N}\big(np, np(1 - p)\big).

</math>

Let O<sub>1</sub> be the number of observations from the sample that are in the first cell. The Pearson test statistic can be expressed as

<math display="block">

\frac{(O_1 - np)^2}{np} + \frac{\big(n - O_1 - n(1 - p)\big)^2}{n(1 - p)},

</math>

which can in turn be expressed as

<math display="block">

\left(\frac{O_1 - np}{\sqrt{np(1 - p)\right)^2.

</math>

By the normal approximation to a binomial, this is the squared of one standard normal variate, and hence is distributed as chi-squared with 1 degree of freedom. Note that the denominator is one standard deviation of the Gaussian approximation, so can be written

<math display="block">

\frac{(O_1 - \mu)^2}{\sigma^2}.

</math>

So as consistent with the meaning of the chi-squared distribution, we are measuring how probable the observed number of standard deviations away from the mean is under the Gaussian approximation (which is a good approximation for large n).

The chi-squared distribution is then integrated on the right of the statistic value to obtain the p-value, which is equal to the probability of getting a statistic equal or bigger than the observed one, assuming the null hypothesis.

Two-by-two contingency tables

When the test is applied to a contingency table containing two rows and two columns, the test is equivalent to a Z-test of proportions.

Many cells

Broadly similar arguments as above lead to the desired result, though the details are more involved. One may apply an orthogonal change of variables to turn the limiting summands in the test statistic into one fewer squares of i.i.d. standard normal random variables.

Let us now prove that the distribution indeed approaches asymptotically the <math>\chi^2</math> distribution as the number of observations approaches infinity.

Let <math>n</math> be the number of observations, <math>m</math> the number of cells and <math>p_i</math> the probability of an observation to fall in the i-th cell, for <math>1 \le i \le m</math>. We denote by <math>\{k_i\}</math> the configuration where for each i there are <math>k_i</math> observations in the i-th cell. Note that

<math display="block">

\sum_{i=1}^m k_i = n \quad \text{and} \quad \sum_{i=1}^m p_i = 1.

</math>

Let <math>\chi^2_P(\{k_i\}, \{p_i\})</math> be Pearson's cumulative test statistic for such a configuration, and let <math>\chi^2_P(\{p_i\})</math> be the distribution of this statistic. We will show that the latter probability approaches the <math>\chi^2</math> distribution with <math>m - 1</math> degrees of freedom, as <math>n \to \infty.</math>

For any arbitrary value T:

<math display="block">

P\big(\chi^2_P(\{p_i\}) > T\big) = \sum_{\{k_i \mid \chi^2_P(\{k_i\}, \{p_i\}) > T\ \frac{n!}{k_1! \cdots k_m!} \prod_{i=1}^m {p_i}^{k_i}.

</math>

We will use a procedure similar to the approximation in de Moivre–Laplace theorem. Contributions from small <math>k_i</math> are of subleading order in <math>n</math> and thus for large <math>n</math> we may use Stirling's formula for both <math>n!</math> and <math>k_i!</math> to get the following:

<math display="block">

P\big(\chi^2_P(\{p_i\}) > T\big) \sim \sum_{\{k_i \mid \chi^2_P(\{k_i\},\{p_i\}) > T \ \prod_{i=1}^m \left(\frac{np_i}{k_i}\right)^{k_i} \sqrt{\frac{2\pi n}{\prod_{i=1}^m 2\pi k_i.

</math>

By substituting for

<math display="block">

x_i = \frac{k_i - np_i}{\sqrt{n, \quad i = 1, \cdots, m - 1,

</math>

we may approximate for large <math>n</math> the sum over the <math>k_i</math> by an integral over the <math>x_i</math>. Noting that

<math display="block">

k_m = np_m - \sqrt{n} \sum_{i=1}^{m-1} x_i,

</math>

we arrive at

<math display="block"> \begin{align}

P\big(\chi^2_P(\{p_i\}\big) > T)

&\sim \sqrt{\frac{2\pi n}{\prod_{i=1}^m 2\pi k_i \int_\Omega \left[ \prod_{i=1}^{m-1} \sqrt{n} dx_i \right] \times \\

& \qquad \times \left \{\prod_{i=1}^{m-1} \left(1 + \frac{x_i}{\sqrt{n} p_i}\right)^{-(n p_i + \sqrt{n} x_i) } \left(1 - \frac{\sum_{i=1}^{m-1} x_i}{\sqrt{n} p_m}\right)^{-\left(n p_m-\sqrt{n} \sum_{i=1}^{m-1} x_i\right)} \right\} \\

&= \sqrt{\frac{2\pi n}{\prod_{i=1}^m \left(2\pi n p_i + 2\pi \sqrt{n} x_i\right) \int_\Omega \left\{\prod_{i=1}^{m-1} {\sqrt{n} dx_i}\right\} \times \\

& \qquad \times \left\{ \prod_{i=1}^{m-1} \exp\left[-\left(n p_i + \sqrt{n} x_i \right) \ln \left(1 + \frac{x_i}{\sqrt{n} p_i}\right)\right] \exp \left[ -\left(n p_m - \sqrt{n} \sum_{i=1}^{m-1} x_i\right) \ln \left(1 - \frac{\sum_{i=1}^{m-1} x_i}{\sqrt{n} p_m}\right) \right] \right\},

\end{align}</math>

where <math>\Omega</math> is the set defined through <math>\chi^2_P(\{k_i\}, \{p_i\}) = \chi^2_P(\{\sqrt{n} x_i + n p_i\}, \{p_i\}) > T</math>.

By expanding the logarithm and taking the leading terms in <math>n</math>, we get

<math display="block">

P\big(\chi^2_P(\{p_i\}) > T\big) \sim \frac{1}{\sqrt{(2\pi)^{m-1} \prod_{i=1}^m p_i \int_\Omega \left[ \prod_{i=1}^{m-1} dx_i\right] \prod_{i=1}^{m-1} \exp\left[-\frac{1}{2} \sum_{i=1}^{m-1} \frac{x_i^2}{p_i} - \frac{1}{2p_m} \left(\sum_{i=1}^{m-1} x_i\right)^2 \right].

</math>

Pearson's chi, <math>\chi^2_P(\{k_i\}, \{p_i\}) = \chi^2_P(\{\sqrt{n} x_i + n p_i\}, \{p_i\})</math>, is precisely the argument of the exponent (except for the −1/2; note that the final term in the exponent's argument is equal to <math>(k_m - n p_m)^2/(n p_m)</math>).

This argument can be written as

<math display="block">

-\frac{1}{2} \sum_{i,j=1}^{m-1} x_i A_{ij} x_j, \quad A_{ij} = \frac{\delta_{ij{p_i} + \frac{1}{p_m}, \quad i, j = 1, \cdots, m - 1.

</math>

<math>A</math> is a regular symmetric <math>(m - 1) \times (m - 1)</math> matrix, and hence diagonalizable. It is therefore possible to make a linear change of variables in <math>\{x_i\}</math> so as to get <math>m - 1</math> new variables <math>\{y_i\}</math> so that

<math display="block">

\sum_{i,j=1}^{m-1} x_i A_{ij} x_j = \sum_{i=1}^{m-1} y_i^2.

</math>

This linear change of variables merely multiplies the integral by a constant Jacobian, so we get

<math display="block">

P\big(\chi^2_P(\{p_i\}) > T\big) \sim C \int_{\sum_{i=1}^{m-1} y_i^2 > T} \left\{\prod_{i=1}^{m-1} dy_i \right\} \prod_{i=1}^{m-1} \exp\left[-\frac{1}{2} \left(\sum_{i=1}^{m-1} y_i^2 \right)\right],

</math>

where C is a constant.

This is the probability that squared sum of <math>m - 1</math> independent normally distributed variables of zero mean and unit variance will be greater than T, namely that <math>\chi^2</math> with <math>m - 1</math> degrees of freedom is larger than T.

We have thus shown that at the limit where <math>n \to \infty,</math> the distribution of Pearson's chi approaches the chi distribution with <math>m - 1</math> degrees of freedom.

An alternative derivation is on the multinomial distribution page.

Examples

Fairness of dice

A 6-sided die is thrown 60 times. The number of times it lands with 1, 2, 3, 4, 5 and 6 face up is 5, 8, 9, 8, 10 and 20, respectively. Is the die biased, according to the Pearson's chi-squared test at a significance level of 95% and/or 99%?

The null hypothesis is that the die is unbiased, hence each number is expected to occur the same number of times, in this case, = 10. The outcomes can be tabulated as follows:

{| class="wikitable" style="text-align:center;"

|-

! <math>i</math>

! <math>O_i</math>

! <math>E_i</math>

! <math>O_i - E_i</math>

! <math>(O_i - E_i)^2</math>

|-

| 1 || 5 || 10 || &minus;5 || 25

|-

| 2 || 8 || 10 || &minus;2 || 4

|-

| 3 || 9 || 10 || &minus;1 || 1

|-

| 4 || 8 || 10 || &minus;2 || 4

|-

| 5 || 10 || 10 || 0 || 0

|-

| 6 || 20 || 10 || 10 || 100

|-

| colspan="4" |Sum

| 134

|}

We then consult an Upper-tail critical values of chi-square distribution table, the tabular value refers to the sum of the squared variables each divided by the expected outcomes. For the present example, this means

<math display="block">

\chi^2 = \frac{25}{10} + \frac{4}{10} + \frac{1}{10} + \frac{4}{10} + \frac{0}{10} + \frac{100}{10} = 13.4.

</math>

This is the experimental result whose unlikeliness (with a fair die) we wish to estimate.

{| class="wikitable" style="text-align:center"

|-

! rowspan="2"| Degrees<br /> of<br />freedom

! colspan="5"| Probability less than the critical value

|-

! 0.90 || 0.95 || 0.975 || 0.99 || 0.999

|-

| 5 || 9.236 || 11.070 || 12.833 || 15.086 || 20.515

|}

The experimental sum of 13.4 is between the critical values of 97.5% and 99% significance or confidence (p-value). Specifically, getting 20 rolls of 6, when the expectation is only 10 such values, is unlikely with a fair die.

Chi-squared goodness of fit test

In this context, the frequencies of both theoretical and empirical distributions are unnormalised counts, and for a chi-squared test the total sample sizes <math>N</math> of both these distributions (sums of all cells of the corresponding contingency tables) have to be the same.

For example, to test the hypothesis that a random sample of 100 people has been drawn from a population in which men and women are equal in frequency, the observed number of men and women would be compared to the theoretical frequencies of 50 men and 50 women. If there were 44 men in the sample and 56 women, then

<math display="block"> \chi^2 = \frac.</math>

For example, suppose there are two groups of students (Group 1 and Group 2) and three study preferences (Alone, With peers, Tutoring). Let <math>O_{ij}</math> denote the observed frequency in row <math>i</math> (preference) and column <math>j</math> (group).

The observed frequencies are as follows:

{| class="wikitable" style="text-align:center;"

! Preference !! Group 1 !! Group 2 !! Row total

|-

| Alone || 12 || 8 || 20

|-

| With peers || 18 || 22 || 40

|-

| Tutoring || 10 || 30 || 40

|-

! Column total || 40 || 60 || 100

|}

Under the null hypothesis <math>H_0</math>, the rows are proportional across groups.

The expected frequencies are computed using:

<math>E_{ij} = \frac{R_i \cdot C_j}{T},</math>

where <math>R_i</math> is the row total, <math>C_j</math> is the column total, and <math>T</math> is the grand total.

For example, for the first row and first column:

<math>E_{11} = \frac{20 \cdot 40}{100} = 8.</math>

The expected frequencies are:

{| class="wikitable" style="text-align:center;"

! Preference !! Group 1 !! Group 2

|-

| Alone || 8 || 12

|-

| With peers || 16 || 24

|-

| Tutoring || 16 || 24

|}

The Pearson chi-squared statistic is then:

<math>\chi^2_{\text{stat = \sum_{i=1}^{3} \sum_{j=1}^{2} \frac{(O_{ij}-E_{ij})^2}{E_{ij = 7.5.</math>

With <math>(3-1)(2-1) = 2</math> degrees of freedom, this value can be compared to the chi-squared distribution to test the null hypothesis.

Pitfalls of the test

The approximation to the chi-squared distribution breaks down if expected frequencies are too low.

It will normally be acceptable so long as no more than 20% of the events have expected frequencies below 5. The above reasons for the above issues become apparent when the higher order terms are investigated.

See also

  • Chi-squared nomogram
  • Cramér's V – a measure of correlation for the chi-squared test
  • Degrees of freedom (statistics)
  • Deviance (statistics), another measure of the quality of fit
  • Fisher's exact test
  • G-test, test to which chi-squared test is an approximation
  • Lexis ratio, earlier statistic, replaced by chi-squared
  • Mann–Whitney U test
  • Median test
  • Minimum chi-square estimation
  • Reduced chi-squared statistic
  • Two-proportion Z-test

Notes

References