Chi-squared Test

In practice, there are two possible Chi-squared tests:

  • A chi-square goodness of fit test determines if a sample data matches a population.
  • A chi-square test for independence compares two variables in a contingency table to see if they are related.

In general, we consider the Chi-squared test to see whether distributions of categorical variables differ from each another.

1. Defintion: Chi-squared Distribution

If Z1,...,Zk are independent, standard normal random variables, then the sum of their squares,

Q =ki=1Z2i,

is distributed according to the chi-squared distribution with k degrees of freedom. This is usually denoted as

Q  χ2(k)

The chi-squared distribution has one parameter: a positive integer k that specifies the number of degrees of freedom (the number of Zi’s).

chi_square

The term ‘chi square’ (pronounced with a hard ‘ch’) is used because the Greek letter χ is used to define this distribution. It will be seen that the elements on which this distribution is based are squared, so that the symbol χ2 is used to denote the distribution.

2. Chi-squared Statistics

For both the goodness of fit test and the test of independence, the Chi-squared statistic is the same. For both of these tests, all the categories into which the data have been divided are used. The data obtained from the sample are referred to as the observed numbers of cases. These are the frequencies of occurrence for each category into which the data have been grouped. In the Chi-squared tests, the null hypothesis makes a statement concerning how many cases are to be expected in each category if this hypothesis is correct. The Chi-squared test is based on the difference between the observed and the expected values for each category. The chi square statistic is defined as

χ2=(OiEi)2Ei

where Oi is the observed number of cases in category i, and Ei is the expected number of cases in category i. This Chi-squared statistic is obtained by calculating the difference between the observed number of cases and the expected number of cases in each category. This difference is squared and divided by the expected number of cases in that category. These values are then added for all the categories, and the total is referred to as the Chi-squared statistic.

3. Approximation

According to the work of Pearson and many afterwards, under the circumstance of the null hypothesis being correct (i.e. the observed are part of the large population satisfying the expected statistical behavior), as n, the limiting distribution of the quantity given below is the χ2-distribution. In practice, the condition in which the Chi-square statistic is approximated by the theoretical Chi-squared distribution, is that the sample size is reasonably large and the rough rule of thumb concerning sample size is that there should be at least 5 expected cases per category.

4. Examples

Example 1. There are four blood types (ABO system). It is believed that 34%, 15%, 23% and 28% of people have blood type A, B, AB and O respectively. The blood samples of 100 students were collected: A(12), B(56), AB(2), O(30). Test if the collected blood sample contradicts the previous belief. Solution. The null hypothesis would be

H0:pA=0.34,pB=0.15,pAB=0.23,pO=0.28.

We compute the Chi-squared test statistic as follows:

χ2=(1234)2/34+(5615)2/15+(223)2/23+(3028)2/28=145.6187

Since the corresponding P-value is almost 0, we reject H0.

Example 2. The number of accidents X per week at an intersection was checked for 50 weeks: 32weeks with no accident, 12 weeks with 1 accident, 6 weeks with 2 accidents. Test if X follows a Poission distribution.

Solution. The null hypothesis is given by

H0:P(X=x)=λxeλ/x!

for all x=0,1,2,. Since λ unknown, we estimate it with MLE.

ˆλ=ˉx=(12+2×6)/50=0.48.

Define three categories by X=0 , X=1, and X2. The expected probabilities are estimated to give ˆp1=0.619, ˆp2=0.297, and ˆp3=1ˆp1ˆp2=0.084. Then the test statistic χ2χ21 (the additional degree of freedom is lost due to the estimation of λ). χ2=1.354 and the P-value would be 0.25. We do not reject H0.

4. Conclusion

  • A very small chi square test statistic means that your observed data fits your expected data extremely well. In other words, there is a relationship.

  • A very large chi square test statistic means that the data does not fit very well. In other words, there isn’t a relationship.

5. Application

For an interesting application of Chi-squared test in relation to Benford’s law and checking authenticity of any numerical data like presidential votes and company revenue, please see my jupyter notebook.