Chi-square tests

 
 
Chi-square tests blog post.jpeg
 
 
 

Three kinds of chi-square tests

There are three kinds of χ2\chi^2-tests (chi-squared) we want to look at.

  1. A χ2\chi^2-test for homogeneity

  2. A χ2\chi^2-test for association/independence

  3. χ2\chi^2-test with a contingency table

Krista King Math.jpg

Hi! I'm krista.

I create online courses to help you rock your math class. Read more.

 

In general, χ2\chi^2-tests let you investigate the relationship between categorical variables. For instance the χ2\chi^2-test for homogeneity is a test you can use to determine whether the probability distributions for two different groups are homogeneous. Whereas the χ2\chi^2-test for association lets you determine whether two variables are related in the same group.

We’ll cover all three of these χ2\chi^2-tests in much more detail in this section. But first, we need to remember our conditions for sampling, since we’ll often be using samples to represent populations in χ2\chi^2 problems.


Conditions for inference

Sometimes you’ll have a χ2\chi^2 problem where someone’s taking a sample of a population. When sampling occurs, in order for us to be able to use a χ2\chi^2-test, we need to be able to meet typical sampling conditions:

  1. Random: Any sample we’re using needs to be taking randomly.

  2. Large counts: Each expected value (more on “expected value” later) that we calculate needs to be 55 or greater.

  3. Independent: We should be sampling with replacement, but if we’re not, then the sample we take shouldn’t be larger than 10%10\% of the total population.

If any of these conditions aren’t met, we can’t use a χ2\chi^2 testing method. But assuming we can meet these three conditions, then each of these three χ2\chi^2-tests looks pretty much the same.


χ2\chi^2-test for homogeneity

When we use a χ2\chi^2-test for homogeneity, we’re trying to determine whether the distributions for two variables are similar, or whether they differ from each other.

Let’s use an example. Say you take a sample of male students and a sample of female students and ask all of them whether they prefer cats, dogs, or some other animal as pets.

 
chi square test for homogeneity
 

This is a χ2\chi^2-test for homogeneity because we’re sampling from two different groups (males and females) and comparing their probability distributions.

So in this study you have two variables, gender and pet, and you want to know whether gender affects pet preference, or if pet preference is not affected at all by gender. If pet preference isn’t affected by gender, then the distribution of pet preference for males should be the same or similar to the distribution of pet preference for females. If gender does affect pet preference, then the distributions will be different (they won’t be homogeneous).

To test this, you want to state the null hypothesis, which will be that gender doesn’t affect pet preference.

H0H_0: gender doesn’t affect pet preference

HaH_a: pet preference is affected by gender

The totals in the table margins let you determine the overall probability of being male or female in this study, regardless of pet preference, and the overall probability of preferring cats, dogs, or some other pet, regardless of gender.

If the distributions for pet preference for males and females are homogeneous, then you should be able to use these marginal probabilities to predict the expected number of students who should be in each cell. If the actual value is very different than the predicted value based on the marginal probabilities, that difference tells you that pet preference might be affected by gender, and therefore that the distributions of pet preference for males and females might not be homogeneous.

In this survey, the overall probability of being male is 75/1350.5675/135\approx0.56, and the overall probability of liking cats is 45/1350.3345/135\approx0.33. Which means that if the distributions for males and females are homogeneous, then 33%33\% of 56%56\% of the participants should be male and prefer cats. The value that should be in each cell if the distributions are homogeneous is called the expected value for that cell.

A simple way to find the expected value for each cell is to multiply the row and column totals and then divide by the overall total.

Expected Male-Cats: (7545)/135=25(75\cdot45)/135=25

Expected Male-Dogs: (7562)/13534.4(75\cdot62)/135\approx34.4

Expected Male-Other: (7528)/13515.6(75\cdot28)/135\approx15.6

Expected Female-Cats: (6045)/135=20(60\cdot45)/135=20

Expected Female-Dogs: (6062)/13527.6(60\cdot62)/135\approx27.6

Expected Female-Other: (6028)/13512.4(60\cdot28)/135\approx12.4

Then we can put each actual value, which is the count we got for each category when we surveyed the students, next to the (expected value) in an updated table.

 
expected values in a chi-square test
 

If you’ve done this correctly, the expected values should still sum to the same row and column totals.


The χ2\chi^2 distribution and degrees of freedom

This is where the χ2\chi^2 part comes in. We use the χ2\chi^2 formula to compare the actual and expected value in each cell, and then we sum all those values together to get χ2\chi^2.

χ2=(observedexpected)2expected\chi^2=\sum\frac{(\text{observed}-\text{expected})^2}{\text{expected}}

χ2=(1925.0)225+(3434.4)234.4+(2215.6)215.6\chi^2=\frac{(19-25.0)^2}{25}+\frac{(34-34.4)^2}{34.4}+\frac{(22-15.6)^2}{15.6}

+(2620.0)220.0+(2827.6)227.6+(612.4)212.4+\frac{(26-20.0)^2}{20.0}+\frac{(28-27.6)^2}{27.6}+\frac{(6-12.4)^2}{12.4}

χ2=1.44+0.00+2.63+1.80+0.01+3.30\chi^2=1.44+0.00+2.63+1.80+0.01+3.30

χ2=9.18\chi^2=9.18

In general, the larger the value of χ2\chi^2, the more likely it is that the variables affect each other (not homogeneous).

But to determine whether or not we can reject the null hypothesis, we need to look up the χ2\chi^2 value and the degrees of freedom in the χ2\chi^2-table. Similar to the normal- and tt-distributions, χ2\chi^2 has its own probability distribution that looks like this:

 
chi-square distributions for different degrees of freedom
 

There’s a distinct χ2\chi^2-distribution for each degree of freedom. So you can see the χ2\chi^2-distribution for df=1\text{df}=1 in red, the χ2\chi^2-distribution for df=2\text{df}=2 in yellow, the χ2\chi^2-distribution for df=3\text{df}=3 in light blue, etc.

Of course, like the tt-table for tt-distributions, the χ2\chi^2-table for χ2\chi^2-distributions includes a degrees of freedom value.

The degrees of freedom is always the minimum number of data points you’d need to have all of the information in the table. So for example in a χ2\chi^2-test for homogeneity, degrees of freedom is given by

df=(number of rows1)(number of columns1)\text{df}=(\text{number of rows}-1)(\text{number of columns}-1)

When you calculate degrees of freedom, make sure you only include rows and columns from the body. Don’t include the total column or the total row. So for the table in the example we’ve been working with, we get

df=(21)(31)\text{df}=(2-1)(3-1)

df=(1)(2)\text{df}=(1)(2)

df=2\text{df}=2

It makes sense that df=2\text{df}=2 for this example, because if we have any two pieces of information from the table, we can always figure out the rest of the information.

For instance, if we only know Male-Cats and Male-Dogs, we can find the rest of the missing values.

 
degrees of freedom in a chi square test
 

Taking Cats-Total minus Male-Cats gives Female-Cats; taking Dogs-Total minus Male-Dogs gives Female-Dogs; taking Male-Total minus Male-Cats minus Male-Dogs gives Male-Other, and then once we have Male-Other, taking Other-Total minus Male-Other gives Female-Other.

But back to the example, looking up χ2=9.18\chi^2=9.18 and df=2\text{df}=2 in the χ2\chi^2-table,

 
chi square table
 

puts us between a pp-level of 0.020.02 and 0.010.01, closer to 0.010.01. So we could have picked an alpha value as high as α=0.02\alpha=0.02 and still rejected the null hypothesis, concluding that we’ve found support for the alternative hypothesis that gender affects pet preference (or that pet preference is affected by gender). But if we had picked an alpha value of α=0.01\alpha=0.01, we’d be unable to reject the null hypothesis.

So for this particular example, we can reject the null hypothesis and conclude that there’s a relationship between gender and pet preference (at α=0.02\alpha=0.02).

χ2\chi^2-test for association/independence

In a χ2\chi^2-test for association or independence, we’re sampling from one group (instead of from two, like we did with the homogeneity test), but we’re thinking about two different variables for that same group.

In the example that follows, we’re taking a sample of employees at our company (sampling from one group), and looking at their eye color and handedness (two variables about them), so see whether or not eye color and handedness are associated.

 
 

Using a chi-square test


 
Krista King Math Signup.png
 
Probability & Statistics course.png

Take the course

Want to learn more about Probability & Statistics? I have a step-by-step course for that. :)

 
 

 
 

How to use a chi-square test for independence

Example

You want to know whether eye color and handedness are associated in employees of your 15,00015,000-person company, so you take a random sample of company employees and ask them their eye color, and whether they’re left- or right-handed.

chi square test example

Use a χ2\chi^2-test for independence with α=0.05\alpha=0.05 to say whether or not eye color and handedness are associated.


Start by computing expected values.

Expected Left-Brown: (140532)/1,000=74.48(140\cdot532)/1,000=74.48

Expected Left-Blue: (140251)/1,000=35.14(140\cdot251)/1,000=35.14

Expected Left-Green: (140150)/1,000=21.00(140\cdot150)/1,000=21.00

Expected Left-Hazel: (14067)/1,000=9.38(140\cdot67)/1,000=9.38

Expected Right-Brown: (860532)/1,000=457.52(860\cdot532)/1,000=457.52

Expected Right-Blue: (860251)/1,000=215.86(860\cdot251)/1,000=215.86

Expected Right-Green: (860150)/1,000=129.00(860\cdot150)/1,000=129.00

Expected Right-Hazel: (86067)/1,000=57.62(860\cdot67)/1,000=57.62

Then fill in the table.

adding expected values to your table

Now we’ll check our sampling conditions. The problem told us that we took a random sample, and all of our expected values are at least 55 (Left-Hazel has the smallest expected value at 9.389.38), so we’ve met the random sampling and large counts conditions. And even though we’re sampling without replacement, there are 15,00015,000 employees in our company and we’re sampling less than 10%10\% of them (1,0001,000 is less than 10%10\% of 15,00015,000), so we’ve met the independence condition as well.

We’ll state the null hypothesis.

H0H_0: eye color and handedness are independent (not associated)

HaH_a: eye color and handedness aren’t independent (they’re associated)

Calculate χ2\chi^2.

χ2=(7274.48)274.48+(3635.14)235.14+(2021.00)221.00+(129.38)29.38\chi^2=\frac{(72-74.48)^2}{74.48}+\frac{(36-35.14)^2}{35.14}+\frac{(20-21.00)^2}{21.00}+\frac{(12-9.38)^2}{9.38}

+(460457.52)2457.52+(215215.86)2215.86+(130129.00)2129.00+(5557.62)257.62+\frac{(460-457.52)^2}{457.52}+\frac{(215-215.86)^2}{215.86}+\frac{(130-129.00)^2}{129.00}+\frac{(55-57.62)^2}{57.62}

χ2=0.082578+0.021047+0.047619+0.731812\chi^2=0.082578+0.021047+0.047619+0.731812

+0.013443+0.003426+0.007752+0.119132+0.013443+0.003426+0.007752+0.119132

χ21.03\chi^2\approx1.03

The degrees of freedom are

df=(number of rows1)(number of columns1)\text{df}=(\text{number of rows}-1)(\text{number of columns}-1)

df=(21)(41)\text{df}=(2-1)(4-1)

df=(1)(3)\text{df}=(1)(3)

df=3\text{df}=3

With df=3\text{df}=3 and χ21.03\chi^2\approx1.03, the χ2\chi^2-table gives

chi square table with degrees of freedom

We’re off the chart on the left, which means we will definitely not exceed the alpha level α=0.05\alpha=0.05. Therefore, we fail to reject the null hypothesis, which means we can’t see an association between eye color and handedness, and we therefore assume that those two variables are not associated, which means they’re independent.


Chi-square tests for Probability and Statistics.jpg

When we use a chi-square test for homogeneity, we’re trying to determine whether the distributions for two variables are similar, or whether they differ from each other.

χ2\chi^2-test with contingency tables

In a χ2\chi^2-test for homogeneity, we sample from two different groups and compare whether their probability distributions are similar, and in a χ2\chi^2-test for independence, we sample from one group and try to determine the association between two variables about this one group.

But in a χ2\chi^2-test with a contingency table, we have a data table that gives some value or values, contingent upon some other condition.

For instance, in the example that follows, we’re working with a contingency table where we’re given cupcake sales by month. Which means we’re looking at cupcake sales, contingent upon the month of the year.

We’re not sampling from two different groups so it doesn’t make sense to use a χ2\chi^2-test for homogeneity, and we’re not sampling from one group to look at the relationship between multiple variables of theirs, so it doesn’t make sense to use a χ2\chi^2-test for association, either.

Instead, we’ll use a χ2\chi^2-test for a contingency table.


Example

A small cupcake company wants to know if their sales are affected by month. They’ve recorded actual sales over the past year and collected the data in a table. Use a χ2\chi^2-test with α=0.05\alpha=0.05 to say whether the company’s cupcake sales are affected by month.

Screen Shot 2020-05-24 at 7.12.59 PM.png


The null hypothesis would be that sales aren’t affected by month.

H0H_0: sales aren’t affected by month (the month doesn’t affect sales)

HaH_a: sales are affected by month (the month does affect sales)

If sales aren’t affected by month, then they should be evenly distributed over each month, which means sales each month should be

expected monthly sales=1,020 total sales12 months=85 sales per month\text{expected monthly sales}=\frac{1,020\text{ total sales}}{12\text{ months}}=85\text{ sales per month}

Which means we can expand the table to show actual versus expected sales.

Screen Shot 2020-05-24 at 7.13.10 PM.png

Now that we have actual and expected values, we can calculate χ2\chi^2.

χ2=(6085)285+(8085)285+(6585)285+(7085)285\chi^2=\frac{(60-85)^2}{85}+\frac{(80-85)^2}{85}+\frac{(65-85)^2}{85}+\frac{(70-85)^2}{85}

+(8085)285+(10085)285+(14085)285+(12085)285+\frac{(80-85)^2}{85}+\frac{(100-85)^2}{85}+\frac{(140-85)^2}{85}+\frac{(120-85)^2}{85}

+(9085)285+(9085)285+(6085)285+(6585)285+\frac{(90-85)^2}{85}+\frac{(90-85)^2}{85}+\frac{(60-85)^2}{85}+\frac{(65-85)^2}{85}

χ2=62585+2585+40085+22585\chi^2=\frac{625}{85}+\frac{25}{85}+\frac{400}{85}+\frac{225}{85}

+2585+22585+3,02585+1,22585+\frac{25}{85}+\frac{225}{85}+\frac{3,025}{85}+\frac{1,225}{85}

+22585+22585+62585+40085+\frac{225}{85}+\frac{225}{85}+\frac{625}{85}+\frac{400}{85}

χ2=7,2508585.29\chi^2=\frac{7,250}{85}\approx85.29

In a problem like this one, degrees of freedom is simply given by n1n-1. Because looking at the body of the table, there are 1212 pieces of information in the body (one for each month), and we would need 1111 of these values in order to be able to figure out the 1212th. So we can only be missing one pieces of data, and degrees of freedom is therefore n1=121=11n-1=12-1=11. Look up χ2=85.29\chi^2=\approx85.29 and df=11\text{df}=11 in the χ2\chi^2-table.

chi square table for upper tail probability

With df=11\text{df}=11, the χ2\chi^2 value we found is off the chart. That’s telling us that there’s a massive difference between actual and expected values, which tells us that cupcake sales are very likely affected by month of the year. Which means we can reject the null hypothesis that sales aren’t affected by month of the year, and conclude that cupcake sales are affected by month.


Let’s walk through another example with a contingency table that looks a little different.


Example

Marla owns a dog walking service in which she employees 55 of her friends as dog-walkers. She created a table to record the number of friends she believes are walking dogs for her at any given time.

chi square example

To test her belief, she took a random sample of 100100 times and recorded the number of dog walkers working at that time.

marla's data

Say whether a χ2\chi^2-test can be used to say whether her findings disagree with her belief. If a χ2\chi^2-test is valid, say whether her findings support her belief with 95%95\% confidence.


Since Marla is sampling, we need to meet three conditions if she’s going to use a χ2\chi^2-test.

First, the sample needs to be random, and we were told in the problem that she took a random sample.

Second, every expected value needs to be at least 55. To find the expected values, we multiply her expected percentages by the total number of samples, 100100.

expected values

The smallest of these expected values is 1010, which is greater than 55, so we’ve met the large counts condition.

Third, Marla isn’t sampling with replacement, so the sample can’t be more than 10%10\% of the total population. It’s safe to assume that Marla could continue taking an infinite number of samples at any given time, in theory gathering hundreds or thousands of samples as her business continues, so 100100 samples shouldn’t violate the independence condition.

Therefore, it’s appropriate for her to use a χ2\chi^2-test. She’ll state the null hypothesis that her model for the number of friends walking dogs at any given time is correct (her model matches the actual counts that she collected).

To compute χ2\chi^2, she’ll use the actual and expected values.

χ2=(1515)215+(1620)220+(3730)230+(2420)220+(810)210\chi^2=\frac{(15-15)^2}{15}+\frac{(16-20)^2}{20}+\frac{(37-30)^2}{30}+\frac{(24-20)^2}{20}+\frac{(8-10)^2}{10}

χ2=015+1620+4930+1620+410\chi^2=\frac{0}{15}+\frac{16}{20}+\frac{49}{30}+\frac{16}{20}+\frac{4}{10}

χ2=109303.63\chi^2=\frac{109}{30}\approx3.63

With 55 possibilities for number of walkers, there are 44 degrees of freedom, so look up df=4\text{df}=4 with χ23.63\chi^2\approx3.63.

chi square table with df=4

The value χ23.63\chi^2\approx3.63 is off the chart at df=4\text{df}=4. In order for Marla to reject the null hypothesis at 95%95\% confidence, she would have needed to surpass a value of 9.499.49 to be above the 5%5\% threshold.

chi square table with 4 degrees of freedom

Therefore, Marla cannot reject the null hypothesis, and can’t conclude that her model is correct. So she may want to consider adjusting her model to something that’s more in line with the data she collected, then collecting a new sample of data, and re-running the test to see if the new model seems to be more accurate.

 
Krista King.png
 

Get access to the complete Probability & Statistics course