The chi squared test
test is a statistical hypothesis test in which the distribution of the test statistic calculated on the data is a
distribution (see page above) under null hypothesis. The assumption is that data is normally distributed and independent so the
test can also be used to reject the hypothesis that data are independent.
It is used with categorical data to see if the number of individuals in each category is consistent with the expected values. In practice, the test is used to determine if there is a significant difference between the expected frequencies and the observed frequencies of the outcomes of an experiment in one or more categories, that is, if the observed differences are due to chance. The idea is: is the number of individuals falling into each category significantly different from the number you would expect under the null hypothesis? Is this difference between expected and observed data due to sampling or is it real?
is defined as
is the observed value and
the null hypothesis value.
has to be compared to table values for the
distribution at the chosen level of significance and for given degrees of freedom one has in order to decide if the null hypothesis can be rejected or not, using the
-value (see page).
Let's say that we have a (6-faces) dice and we want to know if it is fair, that is, if each of the faces is equiprobable or if there is any bias towards a face. We throw the dice 60 times: in the case of a fair dice we would have each face appearing 10 times (60/6 where 6 is the number of possible results). This will be the null hypothesis.
We build a table containing the actual counts we get for each face, and said null hypothesis:
gets calculated as
The number of degrees of freedom is the number of terms minus 1 , so 6-1=5. Looking up for the values of the
distribution at this number of degrees of freedom and for a confidence level of 95% we get a value of 11.070. Because our calculated
exceeds the table value, this means that the
-value associated to it is smaller than 0.05, so we can discard the null hypothesis at that significance level.
Nevertheless, note that if we choose a confidence level of 99% instead, so want to be safer, we cannot discard the null hypothesis as the table value for the
at that level is 15.086, bigger than our calculated one, hence the
-value does pass the required threshold of 0.01.
test is widely used to determine how good a fit is, that is, how well a statistical model describes (fits) the observational points. The number of degrees of freedom to be used to retrieve the comparison with table values is the total number of observations minus the number of fit parameters.
Let's say we have
data points and we bin them into
bins, the expected occurrence frequency of each bin (the number per bin expected) would be, given that the distribution is uniform,
being the index of the bins.
test statistic is
is the observed number of data points in the bin.
In that case the hypothesis values have to be computed from the hypothesis distribution.