
In this article, I will introduce the fundamental of the chi-square test (χ2), a statistical method to make the inference about the distribution of a variable or to decide whether there is a relationship exists between two variables of a population. The inference relies on the χ2 distribution curve, dependent upon the number of degrees of freedom d.f.
![Figure 1: Chi-square distribution with different degree of freedom [1]](https://towardsdatascience.com/wp-content/uploads/2021/05/1-VtI50wyyTXzOdW2svxR4Q.png)
The χ2 distribution curve is right-skewed and as the number of degrees of freedom becomes larger, the χ2 curve will more similar to the normal distribution.
A: χ2 test of Independence
It is used to decide whether there is a relationship exists between two variables of a population. Useful when analyzing survey results of 2 categorical variables.
-
H₀: The two categorical variables have no relationship H₁: There is a relationship between two categorical variables
-
The number of degrees of freedom of the χ2 independence test Statistics: d.f. = (# rows -1) *(#columns-1)

- If H₀ is true, each cell of the value in the contingency table above will contain a theoretical or expected frequency Eᵢⱼ, as opposed to the observed value Oᵢⱼ for each cell.

- The test statistic: Comparing the differences between Oᵢⱼ & Eᵢⱼ leads to the χ2 statistic, with [(r-1) x (c-1)] degrees of freedom, where

The table below is an exit poll which displays the joint responses to 2 categorical variables: people in categories from consider from 18–29, 30–44, 45–64 and >65 years, and their political affiliation, which is "Conservative", "Socialist" and "Other". Is there any evidence of a relationship between the age group and their political affiliation, at 5% significant level?
![Table 2: Exit poll survey [2]](https://towardsdatascience.com/wp-content/uploads/2021/05/1RhRyvc_638pybH-dqiKRag.png)
According to five steps process of hypothesis testing: H₀: whether age group and their political affiliation are independent, i.e. no relationship H₁: whether age group and their political affiliation are dependent, i.e. ∃ a relationship α = 0.05 Following χ2 independence test statistics:

Conclusion: We have enough evidence that there is an association between age group and their political affiliation, at 5% significance level.
B: χ2 Goodness-Of-Fit Test
It is used to make the inference about the distribution of a variable.
-
H₀: The variable has the specified distribution, normal H₁: The variable does not have the specified distribution, not normal
-
The number of degrees of freedom of the χ2 Goodness-Of-Fit test statistics: d.f. = (# categories -1)
-
It compares the observed frequencies O of a sample with the expected frequencies E. *E = probability of the event total sample size**
The table below displays the more than 44 million people voting result for 2013 German Federal Election. 41.5% of German vote for the Christian Democratic Union (CDU), 25.7% for the Social Democratic Party (SPD) and the remaining 32.8% as Others.
Assume the researcher take a random sample and pick 123 students of FU Berlin about their party affiliation. Out of them 57 vote for CDU, 26 vote for SPD and 40 for Others. These number corresponds to the observed frequencies.
![Table 3: 2013 German Federal Election [3]](https://towardsdatascience.com/wp-content/uploads/2021/05/1PpwdP65Gd9TTC6TY-hByGQ.png)
According to five steps process of hypothesis testing: H₀: The variable has the specified distribution, i.e. the observed and expected frequencies are roughly equal H₁: The variable does not have the specified distribution, not normal α = 0.05 Following χ2 Goodness-Of-Fit test statistics:

Conclusion: We do not have enough evidence that the observed and expected frequencies are not equal, at 5% significance level.
Recommended Reading
Two-Way ANOVA Test, with Python
References
[1] "Chi-Square Tests • SOGA • Department of Earth Sciences." [Online]. Available: https://www.geo.fu-berlin.de/en/v/soga/Basics-of-statistics/Hypothesis-Tests/Chi-Square-Tests/index.html
[2] "The Chi-Square Independence Test • SOGA • Department of Earth Sciences." [Online]. Available: https://www.geo.fu-berlin.de/en/v/soga/Basics-of-statistics/Hypothesis-Tests/Chi-Square-Tests/Chi-Square-Independence-Test/index.html
[3] "Chi-Square Goodness-of-Fit Test • SOGA • Department of Earth Sciences." [Online]. Available: https://www.geo.fu-berlin.de/en/v/soga/Basics-of-statistics/Hypothesis-Tests/Chi-Square-Tests/Chi-Square-Goodness-of-Fit-Test/index.html