hello ML
Computer Science and programming articles. We do not sell courses.
Machine Learning
Data Structures
Algorithms
Problem Solving
C Programming
Python Programming
Web Dev
A chi-square statistic is a test that measures how we can compare a model’s predicted data to the actual observed data . These tests are often used in hypothesis testing. The chi-square statistic is what compares the size of the difference between the expected and observed data, given the sample size and the number of variables in the relationship.
For performing a chi-square test, the number of degrees of freedom is used to find if a particular null hypothesis can be rejected or not based on the number of samples and variables we have. Just like any other statistic, the bigger the sample size, the more reliable our results will be.
Before you continue with this article, we recommend that you check out another similar concept called the z-test .
The two common chi-square tests are the chi-square goodness of fit test and the chi-square test of independence . What do these two tests measure exactly?
The chi-square test for goodness of fit tests claims about the population proportion .
The chi-square test for independence evaluates the relationship between two variables in the dataset.
More on these below.
As mentioned above, there are two types of chi-square tests – the test of independence, which asks a relationship question like “Is there a link between one’s gender and colour preference?” and the goodness-of-fit test, which asks questions like “How well does the coin in my hand resemble a theoretically fair coin?”
A chi-square test for independence might be employed when evaluating gender and colour preference. To conduct this study, the researcher would gather data on the two variables of interest (gender and colour preference), and then compare the frequencies with which males and females choose from the available colours using the method above and a chi-square statistical table.
If gender and colour selection are unrelated (i.e. if they are independent), the actual frequencies at which male and female genders choose each offered course should be roughly equal , or the proportion of male and female genders in any selected course should be roughly equal to the proportion of males and females in the sample. A chi-square test for independence can tell us how likely it is that a random chance can explain any observed difference between actual frequencies in our data and these theoretical expectations.
The Chi-square test allows us to see how well a data sample fits the (known or presumed) features of the population it is supposed to represent. We wouldn’t want to utilise this sample to make inferences about the broader population if the sample data didn’t match the predicted features of the population we are interested in.
For instance, consider an imaginary coin with exactly a 50/50 chance of landing heads or tails and a real coin that you toss 100 times. If this real coin is fair, then it will also have an equal probability of landing on either side, and the expected result of tossing the coin 100 times is that heads will come up 50 times and tails will come up 50 times.
In this case, the chi-square test can tell us how well the actual results of 100 coin flips compare to the theoretical model that a fair coin will give 50/50 results. The actual toss could come up 50/50, or 70/30, or even 80/20. The farther away the actual results of the 100 tosses is from 50/50, the less good the fit of this set of tosses is to the theoretical expectation of 50/50 and the more likely we might conclude that this coin is not actually a fair coin.
The chi-square test allows us to compare observed and expected frequencies objectively as it’s not always possible to tell by just looking at them if they’re different enough to be considered statistically significant.
The chi-square test is the statistical test to compare observed results with the expected results . The purpose of a chi-square test is to find if the difference between observed and expected data is by chance or if it is because of a relationship between the variables we’re studying.
The advantages of this test is that they are robust with respect to the given data.
Degrees of freedom.
Observed values.
Expected values.
The test can be done only if our data contains categorical values.
The sampling method should be simple and random .
The data must be expressed in original units. It shouldn’t be expressed in any percentage or ratio.
The total sample size should be atleast 50.
Type
Chi-Square Test For Goodness of Fit Test
Chi-Square Test for Independence
Number of categorical variables
One
Two
Purpose of test
Decide if one variable is likely to come from a given distribution or not
Decide if two variables might be related or not
Example
Decide if bags of candy have the same number of pieces of each flavour or not
Decide if movie goers’ decision to buy snacks is related to the type of movie they plan to watch
Hypotheses in example
Ho: proportion of flavours of candy are the same, Ha/H1: proportions of flavours are not the same
Ho: proportion of people who buy snacks is independent of the movie type, Ha/H1: proportion of people who buy snacks is different for different types of movies
The theoretical distribution used in the test
Chi-Square
Degrees of freedom
A number of categories minus 1. In our example, the number of flavours of candy minus 1
A number of categories for the first variable minus 1, multiplied by a number of categories for the second variable minus 1. In our example, number of movie categories minus 1, multiplied by 1 (as snack purchase is a Yes/No variable and 2-1 = 1)
In the 2000 U.S Census, the ages of individuals in a small town were recorded. This data is shown in the table below.
Less than 18
18-35
Greater than 35
In 2010, 500 (n) individuals were sampled. The results are as stated in the table below.
121
288
Using alpha i.e. the significance level of 5%, would you conclude that the population distribution of ages has changed in the last 10 years?
We have our expected and observed values:
Observed
Expected
500*0.20=100
500*0.30=150
500*0.50=250
The data shouldn’t be expressed in percentages or ratios but must be expressed in original units as shown in the table here. Now, we perform our hypothesis test.
Null hypothesis – H0: The sample data meets the expected distribution i.e, the ages of the 2010 population are the same as those expected based on the 2000 population.
Alternate hypothesis – H1: The data doesn’t meet the expected distribution i.e, the ages of the 2010 population are different than those expected based on the 2000 population.
We generally use a value of 0.05 or 5% significance.
The degrees of freedom is calculated by this formula: degrees of freedom = k - 1 . Here, k is the number of groups we have i.e. 3. Therefore, df=3-1=2.
We, now use the degrees of freedom value, the alpha value to find the p-value from the chi-square distribution table.
If the chi-square we calculate turns out to be greater than 5.99, then we reject the null hypothesis.
By using the chi-square formula stated earlier, we get the following value.
As the chi-square value is much greater than 5.99, we reject the null hypothesis .
Thus, the ages of the 2010 population are different than those expected based on the 2000 population.
Say, 500 elementary school boys and girls are asked which is their favourite colour – blue, green or pink? Results are as shown in the table below.
Blue
Green
Pink
Boys
100
150
300
Girls
180
200
120
130
N=500
Using alpha i.e. the significance level as 5%, would you conclude that there is a relationship between gender and favourite colour?
Null hypothesis- H0: For the population of elementary school students, gender and favourite colour aren’t related.
Alternate hypothesis- H1: For the population of elementary school students, gender and favourite colour are related.
The degrees of freedom is calculated by: degrees of freedom = (rows-1)*(columns-1). Note that this is different from the previous case. We get df=(2-1)(3-1 , therefore df=(1)(2)=2 . We, now use the degrees of freedom value, the alpha value to find the p-value from the chi-square distribution table.
If the chi-square value is greater than 5.99, then we’ll reject the null hypothesis.
We’ll be using the same chi-square formula except for the calculation of frequencies is a little more complex now.
Ei=(Ec-Er)/n where Ec-Frequency in column, Er-Frequency in row and n-total number of subjects we have.
Say we need to find the expected number of boys who chose blue.
(Boys, Blue)=(column total of blue*row total of boys)/N=500=(120*300)/500=72
By doing so for all our rows and columns, we get the table shown below.
108
Now, by applying the chi-square formula,
As the chi-square value is greater than 5.99, we reject the null hypothesis.
In the population, there is a relationship between gender and the favourite colour.
Hope you enjoyed this article. For more such amazing articles check out hello ML . If you would like to improve this article or report something incorrect, please do let us know in the comments below.
If you have any copyright infringement claims, kindly send a mail to [email protected] . We deal with plagiarism very strictly and if our authors are found to be involved in plagiarism, we will take appropriate action against them after reviewing the claim.
I’m pursuing my Bachelor of Technology in Computer Science Engineering at Dayananda Sagar University. I find Data Science highly intriguing and I love writing on concepts I’ve once struggled to grasp an understanding of.
An Introduction to the Chi-Squared Test (with examples)
Understanding Z Test With Examples
A Step-by-step Approach to Principal Component Analysis (PCA)
Click to share on LinkedIn (Opens in new window)
Click to share on Twitter (Opens in new window)
Click to share on WhatsApp (Opens in new window)
Click to share on Telegram (Opens in new window)
Click to share on Facebook (Opens in new window)
Click to share on Reddit (Opens in new window)
Internship Guidelines at hello ML
Privacy Policy