FAQ: Hypothesis Testing - Chi Square Test

This community-built FAQ covers the “Chi Square Test” exercise from the lesson “Hypothesis Testing”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

FAQs on the exercise Chi Square Test

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Can someone explain me what exactly is the null-hypotesis in this exercise please?

1 Like

As I got it, Null hepothesis always is a statment, that any difference between sets of data is accidental. So, if a result of test is less then 0.05 (5%), you can reject Null hepothesis, and state, that there is a difference.

I don’t understand. How can p be so big (= 0.155082308077) for this table:

# Contingency table
#         harvester |  leaf cutter
# ----+------------------+------------
# 1st gr | 30       |  10
# 2nd gr | 35       |  5
# 3rd gr | 28       |  12

when there are visibly extreme differences between harvester and leaf cutter, while at the same time it’s so small (p = 0.00281283455955) for this table:

# Contingency table
#         harvester |  leaf cutter
# ----+------------------+------------
# 1st gr | 30       |  10
# 2nd gr | 35       |  5
# 3rd gr | 28       |  12
# 4th gr | 20       |  20

after we added an equal amount of ants to both columns, which should even things out and therefore increase the probability of there not being a major difference between these sets.


This is a very good point. However, I think that the answer to your questions can be found in the way you read and interprete the tables; you look at the tables only in an “horizontal” way (harvester vs. leaf cutter) but you should look at them in a “vertical” way as well (harvester & leaf cutter in relation to each grade). Below is what I mean.

In the first place, I will tell you that it was easier for me to read the table and interprete the statistical results if I gave some discrete attributes to the grades of students. For example consider that the 1st grade come from USA, the 2nd from Canada and the 3rd from France. Now, recall what you are looking for: is species-ants preference related to the country of origin of the students? The Null Hypothesis is that there is no association between country and ants-preferences.

Before you make the statistical calculations, you can make a guess by looking at the first table: For each county individually, the majority of students show an obvious preference to harvester ; this makes you guess that the country of origin does not matter; whichever of the three countries students come from, most of them prefer harvester. After the calculation of the chi-square test and the result of the p-value, it seems that you were right. P-value is quite high , you can not reject the Ho, meaning that there is no association.
Now, a fourth group of students visit the VeryAnts ant store, they come from Spain. They are again 40 in total, but something different happens now. You notice a different attitude, the majority of them don’t show a preference towards harvester as the previous 3 groups did; their preferences are equally shared (20-20). You start to be suspicious that maybe the country does matter to the preferences of ants. The calculation of chi-square test and p-value proves that. Low p-value -> rejection of Ho -> there seems to be actually an association.

To feel more confident with the above results, I did the following: I added another group of “extreme” values to the contigency table. For instance, a 5th group of students, coming let’say from Japan, visit the store. They are again 40 in total. 39 buy harvester and only 1 leaf cutter! One may be very surprised by that overwhelming preference to harvester and may wonder why. Well, this might be a part of another survey. In the current survey, now we feel much more confident that country does matter. We may guess what the p-value will be , extemely low, the Ho is rejected .


Would you explain the logic of Chi Square Test in comparisson of ants:
why, before the adding the data of 4th grade, we don’t have significant differencies between 2 categories (harvester and leaf cutter), and after the adding - we do.

In my opinion, adding to both categories 2 equal quantities of ants, on the contrary, should equalize 2 samples (make them equal to each other, and consequantly, p-value should increace). But in this case, p-value decreased.

Thank you.

It’s not worded super clearly, but here’s what I think is going on here. Rather than comparing the popularity of the two types of ants, we are interested in whether there is a difference in the relative popularity of the two types of ants among different grade levels. For 1st, 2nd, and 3rd graders, Harvester Ants are far more popular than Leaf Cutters. For 4th graders, however, the two types of ants are equally popular. That’s what is different about the 4th grade data from the other 3 groups, which is why including it lowered the p-value.

1 Like

That’s pretty much it. If you add up the columns horizontally you get totals for each grade, from which you can get the proportion of all students represented by each grade.

Likewise, adding vertically, you can get the proportion of total ants represented by each species.

Putting those two together, you can get an “expected” value for each square: the number of ants sold to that group if everything was consistent with the proportions just calculated.

The chi square takes those expected proportions as the null hypothesis, and yields a p value giving the probability that the measured values would be seen if the null hypothesis is true.