# 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:

## 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 () 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.

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

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.

1 Like

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.

9 Likes

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 .

22 Likes

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.

6 Likes

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.

3 Likes

patrickd314 and softpower, thank you so much, but I can’t understand it anyway.

As far as i’m aware, the null-hypothesis is that 2 popuation of ants (‘harvester’ and ‘leaf cutter’) don’t have significant differencies.

We find this out by sequential comparison of their characteristics (values in row1, values in row2 ets).

What proportions should we calculate: grade_proportions or population_proportions (see table below)?

initial_values harvester leaf cutter
# 1st gr 30 10
# 2nd gr 35 5
# 3rd gr 28 12
# 1st gr 0,32 0,37
# 2nd gr 0,38 0,19
# 3rd gr 0,30 0,44
population_proportions harvester leaf cutter
# 1st gr 0,75 0,25
# 2nd gr 0,875 0,125
# 3rd gr 0,7 0,3

How can it help us to calculate p-value?
Thank you.

Begin with the totals:

``````Grade	Harvester	Leaf	Total
1	       30	     10	     40
2	       35	      5	     40
3	       28	     12	     40
Total	   93	     27	    120

``````

So, 120 students made 1 selection each, or 120 selections.

Of all 120 ant selections, harvester comprised 93, or 77.5%

Of all 120 students, 40 (33%) are in 1st grade

Therefore, we would expect from all of the 120 ant selections, 0.333 * 0.775 * 120 = 31 would be in the first grade:

This can be done in each block to create an “Expected” table:

``````Grade	Harvester	Leaf	Total
1	       31	     9	     40
2	       31	     9	     40
3	       31	     9	     40
Total	   93	     27	    120
``````

Note that the totals remain the same, and that in this particular case, numbers are the same for each grade, since there are the same number of students in each grade, and our default (null) assumption is that there is no difference in the number of ants selected between grades 1, 2 and 3.

Now, chi-square is simply sum((Observed - Expected)**2/ Expected)

For instance for grade 1, Observed - Expected is -1; that squared is 1, and dividing by Expected gives 1/31, or 0.03226. The similarly calculated values for the other five blocks are (0.51613, 0.29032, 0.11111, 1.77778 and 1.0). The sum of those 6 is 3.7276, which, as you will note, compares nicely with the “chi2” value yielded in the exercise.

I do not know the details of getting there to the p-value, mainly because nearly every reference says either “look it up in a table” or “use an online p-value calculator.” You need to know the “degrees of freedom” which is one less than the number of categories, in our case 2. So you take chi-square of 3.7276 and 2 degrees of freedom to a p-value calculator to get: 0.1551 Check!

3 Likes

patrickd314, thank you very much. I understood practiculy everithing:

1. I was surpised that the null hypothesis: ‘there is no difference between grades’, but not ‘between ants_populations’. In our case, p-value is much than 0,05. But what counclusion we would do if it was less than 0,05? That some pair of grades has significant difference? If yes, how to know: between whatabouts?..

2. And how to know, is there a difference between ants population - via binomial test?

3. sum((Observed - Expected)^2/ Expected) - you probably mean ** instead ^. ‘^’ in Python “XOR” operator

4. it’s interesting, that degrees of freedom-value is one less than number of categories.

2 Likes

Thanks for the comment.- I have edited for clarity.

Remember that the ant population was determined by the students. They selected what types of ants that they wanted to purchase.

Yes, edited.

It is actually the (number of rows - 1) * (number of columns - 1) for chi-square, which in our case with two columns, is number of rows-1. It represents the number of data points that you must have to fill out the remainder of the blocks, given the totals.

``````Grade	Harvester	Leaf	Total
1	       31	     ?	     40
2	       ?	     9	     40
3	       ?	     ?	     40
Total	   93	     27	    120
``````

For instance, given only the two values 31 and 9, above, I could recreate the original table, since I know the totals.

2 Likes

Thank you very much for comments.
Initial task in the exercise is to know:

if Leaf Cutter and the Harvester vary in popularity between 1st, 2nd, and 3rd graders.

According to this, I see, that null-hypothesis is:

there is no difference between 2 population of ants in the grades 1, 2 and 3
i.e. 1 and 1, 2 and 2, 3 and 3.

null-hypothesis:

there is no difference in the number of ants selected between grades 1, 2 and 3

I understand as:
there is no significant difference between grades 1 and 2, 1 and 3, 2 and 3…
If so, why should we take into account ants populations? Couldn’t we simply compare sums of both populations between each pair of 3 grades (40 vs 40, 40 vs 40, 40 vs 40)?

1. All in all, If p-value < 0,05, what conclusion would we do ?

I would express the null hypothesis in this experiment as:

Given that every student has expressed a preference for a particular type of ant, there is no difference in ant preference that can be correlated with the student’s grade level. That is, whether we are looking at grade 1, grade 2 or grade 3, we will find that students selected harvester vs leaf ants in the same ratio.

The numbers total 40 for each grade only because there are 40 students in each grade, and each student has one ant preference. (The exercise would have been more interesting had there been a different number of students for each grade.)

The p-value is the probability that the null hypothesis is true, so if the p value were less than 0.05, we would conclude that the null hypothesis is probably false, and that, yes, there is a difference in ant preferences that depends upon the student’s grade level. If we were selling ants, we might market differently to different grades.

Perhaps what is going on would be more clear if we were asking different questions:

Do men and women have different preferences in pets, re: dogs and cats?

``````          dogs       cats
men
women
``````

Null hypothesis: men and women prefer dogs vs cats in the same proportion.

Do residents of various countries prefer different sports?

``````              Baseball         Eur. Football
NL
USA
UK
FR
CAN
MEX
JAP
``````

Null hypothesis: The country of residence has no influence on the relative preference of its citizens on baseball vs. European Football.

Do men and women have different political preferences?

``````          Support Trump      Do Not Support Trump
Men
Women
``````

Null hypotheses: Gender has no influence on whether an individual supports Trump.

In each case, you could do a survey, fill out the table, and do the calculation I outlined earlier.

2 Likes

patrickd314
Thank you so much. Now, null-hypothesis has become crystall clear.
But one question is still unanswered.
E.g we have 4 types of respondents: schoolkids, teenagers, students, adults. And the results of their surveys: 'What do you like the most: Audi, MB, BMW or Ferrari.

We will find, that choice is connected with age_group (i.e. reject Null hipothesis that choice doesn’t depend on age_groups). What’s next? How will we find, on whoom to target our advertisement company?

1 Like

Well, I know nothing of advertising. Perhaps we would target the group that least likes our brand to better familiarize the group with it. Or perhaps we target the group that most likes our brand to encourage them to buy even more…???

There are no doubt more statistical tests available to help with the decision.

1 Like

Why are you so great. Top grade explanation. Greatly appreciated your helping us.

The null hypothesis is that the grade you are in doesn’t affect which ant is more popular. For the first three grades, generally the harvester is preferred - all the grades act the same way. Then for the fourth grade, suddenly harvesters and leaf cutters are equally popular - the grades don’t all act the same way, so the grade you are in makes a difference (and you reject the null hypothesis).