๐Ÿ“Š Chi-Square Test: Definition, Types, and Examples

Statistics isnโ€™t just about averages โ€” itโ€™s also about testing relationships between variables. One of the most commonly used statistical tools for this purpose is the Chi-Square (ฯ‡ยฒ) Test.

Whether youโ€™re analyzing survey data, market preferences, or categorical outcomes, the Chi-Square test helps you determine if an observed difference or relationship is real or just due to chance.


๐Ÿ”น What is a Chi-Square Test?

Welcome back, data enthusiasts! So far, we’ve explored how to measure spread in numerical data. But what happens when your data isn’t numbers, but categories? For instance, does gender influence political party preference? Is there a link between a marketing campaign and a customer’s purchase decision?

To answer these questions, we need a different toolโ€”one designed for counts and categories. Enter the Chi-Square Test (pronounced “Kai-Square”).

The Chi-Square Test (ฯ‡ยฒ) is a non-parametric test used to determine whether there is a significant association between categorical variables or whether the observed frequencies differ from expected frequencies. It compares the actual data you observed with the data you would expect if there were no relationship between the variables.

At its heart, the Chi-Square test checks if there’s a significant relationship between two categorical variables or if the distribution of one categorical variable fits a specific expectation. It does this by comparing what we observe in the real world to what we would expect to see if there were no relationship (i.e., if they were independent).

The Core Idea: Observed vs. Expected

The entire test hinges on a simple comparison:

  • Observed Frequency (O): The actual counts you recorded in your data.
  • Expected Frequency (E): The counts you would expect if there was no relationship between the variables.

If the differences between Observed (O) and Expected (E) counts are large, it suggests a relationship exists. If the differences are small, it’s likely due to random chance.


๐Ÿ”น Formula

Where:

  • (O) = Observed frequency
  • (E) = Expected frequency

If the calculated ฯ‡ยฒ value is greater than the critical value (from the Chi-square table for given degrees of freedom and significance level), we reject the null hypothesis.


๐Ÿ”น Types of Chi-Square Tests

TypePurposeExample
1. Chi-Square Goodness of Fit TestTests if a sample data matches a population distribution.Checking if dice is fair.
2. Chi-Square Test of IndependenceTests if two categorical variables are related.Checking if gender and product preference are related.
3. Chi-Square Test for HomogeneityTests if distributions are the same across multiple populations.Comparing age groups and their choice of a streaming service.

๐Ÿ”น 1. Chi-Square Goodness of Fit Test Example

๐Ÿงฎ Problems:

Example 1

A die is rolled 60 times, and the results are:

Face123456
Observed (O)8910121110

Is the die fair at 5% significance level?

โœ… Step 1: Hypotheses

  • Hโ‚€: The die is fair (all faces have equal probability).
  • Hโ‚: The die is not fair.

โœ… Step 2: Expected Frequency

If the die is fair:

โœ… Step 3: Compute ฯ‡ยฒ

FaceOE(Oโˆ’E)ยฒ/E
18100.4
29100.1
310100.0
412100.4
511100.1
610100.0


ฯ‡2 = 0.4 + 0.1 + 0.0 + 0.4 + 0.1 + 0.0 = 1.0

โœ… Step 4: Decision

Degrees of freedom (df) = 6 โ€“ 1 = 5
Critical ฯ‡ยฒ value at 5% = 11.07

Since 1.0 < 11.07,
โœ… Fail to reject Hโ‚€. The die is fair.

Example 2

The Colored Candy Problem

A candy company claims that its “Rainbow Mix” is distributed as follows: 30% Red, 20% Yellow, 25% Green, and 25% Blue. You buy a large bag containing 400 candies to test their claim. Your observed counts are:

  • Red: 130
  • Yellow: 80
  • Green: 90
  • Blue: 100

Does your sample support the company’s claim?

Step 1: State the Hypotheses

  • Null Hypothesis (Hโ‚€): The distribution of candy colors matches the company’s claim.
  • Alternative Hypothesis (Hโ‚): The distribution of candy colors does not match the claim.

Step 2: Calculate the Expected Frequencies (E)

This is easier. We simply apply the claimed percentages to our total sample size.

  • Expected Red: 30% of 400 = 120
  • Expected Yellow: 20% of 400 = 80
  • Expected Green: 25% of 400 = 100
  • Expected Blue: 25% of 400 = 100

Step 3: Calculate the Chi-Square Statistic (ฯ‡ยฒ)

We use the same formula: ฯ‡ยฒ = ฮฃ [ (O – E)ยฒ / E ]

ColorOEO – E(O – E)ยฒ(O – E)ยฒ / E
Red130120101000.833
Yellow8080000.000
Green90100-101001.000
Blue100100000.000
Total (ฯ‡ยฒ)1.833

Our calculated Chi-Square test statistic is ฯ‡ยฒ = 1.833.

Step 4: Find the Critical Value and Make a Decision

  • Significance Level (ฮฑ): 0.05
  • Degrees of Freedom (df): For goodness of fit, df = (number of categories – 1). We have 4 colors, so df = 3.

The critical value from the Chi-Square table for df=3 and ฮฑ=0.05 is 7.815.

Decision:

  • Our ฯ‡ยฒ (1.833) < Critical Value (7.815)

Step 5: Conclusion

We fail to reject the null hypothesis. There is not enough evidence to say that the candy color distribution is different from what the company claims. Your bag’s contents are consistent with their advertised proportions.


๐Ÿ”น 2. Chi-Square Test of Independence Example

๐Ÿงฎ Problem:

Example 1

A company wants to know if gender and product preference are related.

GenderProduct AProduct BTotal
Male301040
Female202040
Total503080

โœ… Step 1: Hypotheses

  • Hโ‚€: Gender and product preference are independent.
  • Hโ‚: They are related.

โœ… Step 2: Compute Expected Frequencies

CellFormulaE
Male, A(40ร—50)/8025
Male, B(40ร—30)/8015
Female, A(40ร—50)/8025
Female, B(40ร—30)/8015

โœ… Step 3: Compute ฯ‡ยฒ

CellOE(Oโˆ’E)ยฒ/E
Male, A30251.0
Male, B10151.67
Female, A20251.0
Female, B20151.67

โœ… Step 4: Decision

df = (2โˆ’1)(2โˆ’1) = 1
Critical ฯ‡ยฒ value at 5% = 3.84

Since 5.34 > 3.84,
โŒ Reject Hโ‚€.
โœ… Gender and product preference are related.

Example 2

Ice Cream Flavor & Gender

A local ice cream shop wants to know if gender is independent of favorite ice cream flavor. They survey 200 customers and get the following results:

Observed Frequencies (O)

FlavorMenWomenRow Total
Chocolate304070
Vanilla203050
Strawberry255580
Column Total75125200 (Grand Total)

At a glance, it seems women might prefer Strawberry more. But is this a real pattern or just a random fluke? The Chi-Square test can tell us.

Step 1: State the Hypotheses

  • Null Hypothesis (Hโ‚€): There is no relationship between gender and ice cream preference. The variables are independent.
  • Alternative Hypothesis (Hโ‚): There is a relationship between gender and ice cream preference.

Step 2: Calculate the Expected Frequencies (E)

The expected count for each cell is calculated as:
E = (Row Total ร— Column Total) / Grand Total

This is the core formula! It represents the count we’d expect if flavor preference was distributed proportionally across genders.

  • Expected Men who like Chocolate: (70 ร— 75) / 200 = 52.5 / 200 = 26.25
  • Expected Women who like Chocolate: (70 ร— 125) / 200 = 8750 / 200 = 43.75
  • Expected Men who like Vanilla: (50 ร— 75) / 200 = 3750 / 200 = 18.75
  • Expected Women who like Vanilla: (50 ร— 125) / 200 = 6250 / 200 = 31.25
  • Expected Men who like Strawberry: (80 ร— 75) / 200 = 6000 / 200 = 30
  • Expected Women who like Strawberry: (80 ร— 125) / 200 = 10000 / 200 = 50

Expected Frequencies (E) Table

FlavorMenWomen
Chocolate26.2543.75
Vanilla18.7531.25
Strawberry30.0050.00

Step 3: Calculate the Chi-Square Statistic (ฯ‡ยฒ)

The formula for the test statistic is:
ฯ‡ยฒ = ฮฃ [ (O – E)ยฒ / E ]

Where ฮฃ means “sum of” and we calculate (O-E)ยฒ/E for every single cell in the table.

Let’s calculate it for each cell:

CellOEO – E(O – E)ยฒ(O – E)ยฒ / E
Men, Chocolate3026.253.7514.060.536
Women, Chocolate4043.75-3.7514.060.321
Men, Vanilla2018.751.251.560.083
Women, Vanilla3031.25-1.251.560.050
Men, Strawberry2530.00-5.0025.000.833
Women, Strawberry5550.005.0025.000.500
Total (ฯ‡ยฒ)2.323

Our calculated Chi-Square test statistic is ฯ‡ยฒ = 2.323.

Step 4: Find the Critical Value and Make a Decision

We need to compare our statistic to a critical value from a Chi-Square distribution table. To do this, we need:

  • Significance Level (ฮฑ): Typically 0.05 (5%).
  • Degrees of Freedom (df): For a test of independence, df = (number of rows – 1) ร— (number of columns – 1). In our case, (3-1) ร— (2-1) = 2.

Looking up the critical value for df=2 and ฮฑ=0.05, we find it is 5.991.

Decision Rule: If our ฯ‡ยฒ statistic is greater than the critical value, we reject the null hypothesis.

  • Our ฯ‡ยฒ (2.323) < Critical Value (5.991)

Step 5: Conclusion

We fail to reject the null hypothesis. There is not enough evidence to conclude a significant relationship between gender and ice cream preference at the 5% significance level. The differences we observed in our sample are likely due to random chance.


๐Ÿ”น When to Use the Chi-Square Test

SituationUse
Checking fairness of dice, coin, or survey distributionGoodness of Fit
Testing relationship between variables (like age and satisfaction)Test of Independence
Comparing multiple populationsTest for Homogeneity

๐Ÿ”น Assumptions of the Chi-Square Test

Before you run off to chi-square everything, remember these crucial rules:

  • Data should be in frequency (count) form, not percentages.
  • Categorical Data: Your data must be in categories (counts or frequencies). Categories should be mutually exclusive.
  • Independence: Observations must be independent of each other.
  • Sample Size: The expected frequency (E) for any cell should not be less than 5. (Some relax this to 80% of cells should have E > 5). If this assumption is violated, the test may not be valid.

๐Ÿ”น Interpretation Guidelines

ฯ‡ยฒ valuep-valueInterpretation
Small> 0.05No significant difference (accept Hโ‚€)
Large< 0.05Significant difference (reject Hโ‚€)

๐Ÿง  Quick Summary

  • Chi-Square (ฯ‡ยฒ) compares observed vs expected frequencies.
  • Used for categorical (nominal) data.
  • Two main tests: Goodness of Fit and Independence.
  • Higher ฯ‡ยฒ โ†’ stronger evidence against null hypothesis.

The Chi-Square test is a powerful, fundamental tool for unlocking stories hidden in categorical data.

  • Use the Test of Independence to explore relationships between two variables (e.g., “Is smoking linked to lung cancer?”).
  • Use the Goodness of Fit Test to see if your data matches a theoretical distribution (e.g., “Does the ratio of plant phenotypes match Mendelian genetics?”).

By comparing what you see to what you’d expect by chance, you can move beyond guesswork and make robust, data-driven conclusions about the world of categories.


๐Ÿ“š Further Reading


Leave a comment

It’s time2analytics

Welcome to time2analytics.com, your one-stop destination for exploring the fascinating world of analytics, technology, and statistical techniques. Whether you’re a data enthusiast, professional, or curious learner, this blog offers practical insights, trends, and tools to simplify complex concepts and turn data into actionable knowledge. Join us to stay ahead in the ever-evolving landscape of analytics and technology, where every post empowers you to think critically, act decisively, and innovate confidently. The future of decision-making starts hereโ€”letโ€™s embrace it together!

Let’s connect