The Chi Squared Test
By George Bennett
The chi squared test, sometimes written “chi2”, is a useful statistical test for determining if two variables have a relationship. In this post I will give an introduction to what the chi2 test is, how it works, and how to use it.
As with any statistical test, we should always first establish the null and alternative hypotheses. The null hypothesis is that there is no relationship between the two variables. In other words, If one changes it should not change the other in any predictable way. The alternative hypothesis is that there is a relationship. If one variable changes we should be able to predict some sort of change in the other variable.
The chi2 test works by taking in the expected result if the null hypothesis was true, and the observed records of the data. Allow me to make up an example. Say we have 100 students and we have measured how many books they read over the summer along with whether they passed English class the following semester. The null hypothesis is that there is no relationship between the amount of books they read and whether they passed. The alternative hypothesis states that there is a relationship between the amount of books read over the summer and whether or not they passed. We should also set an alpha value (we’ll use this later) which will be our threshold for the test. A common alpha value in statistics is 0.05, so I will set that as the alpha value. Lets assume the class is designed so that 50% of the students will fail. Below I have drawn a table of data.
# Actual Data Passed | Failed
Read Books 37 23
No Reading 15 25
To compute the chi2 value, first find the expected results if the null hypothesis was true. There are 100 students and 60 read books over the summer. The other 40 did not read any books over the summer. We know that 50% of the students will fail. So the expected amount of students who read and passed would be around 30 if the null hypothesis was true. Likewise the amount of students who didn’t read and failed would be around 20 if the null hypothesis was true. As always sample size and random chance are a factor, so the test will give us the probability of a relationship. Here is a table of the expected data if the null hypothesis was true.
# Expected Data Passed | Failed
Read Books 30 30
No Reading 20 20
Now we take the expected values and subtract the from the observed values. Once we get the result we square it. After that we divide each square by its associated expected value. Then we sum up all the quotients. This will result in our chi2 value.
((37 - 30)**2 / 30) +
((23 - 30)**2 / 30) +
((15 - 20)**2 / 20) +
((25 - 20)**2 / 20)
>>> 5.77
Once we have our chi2 value (in this example it is 5.77) we can take a chi2 table and look up the p-value. The p-value is the probability of there is not a relationship. If the p-value is below the preset alpha value (0.05) then we can reject the null hypothesis and assume there is a relationship. (Here is a link to a chi2 table). To find the right value we must input what is called the “degrees of freedom”. This is the amount of values that are free to vary. So there would be 3 degrees of freedom. Here is a chart of what is called the “chi2 distribution”. In this graph “k” represents degrees of freedom. The p-value is on the y-axis and the chi2 value is on the x-axis.
In our experiment the degrees of freedom were 3. In the table the 0.05 value (equal to our alpha) was 7.815. The chi2 value for our test was 5.77. Since our chi2 value was smaller than that number on the chart we cannot reject the null hypothesis. So in this theoretical example, the fact that a student read books on summer break has no significant relationship with whether or not they pass.