Inferential Statistics • Lesson 17

Chi-Square Tests

Learn how chi-square tests help analyze categorical data, compare observed and expected frequencies, and determine whether categorical variables are associated.

Categorical DataGoodness-of-FitIndependenceHypothesis Testing

What You Will Learn

✓What a chi-square test is
✓When to use categorical data
✓Observed vs expected frequencies
✓Chi-square goodness-of-fit test
✓Chi-square test of independence
✓Degrees of freedom and interpretation

1. What Is a Chi-Square Test?

A chi-square test is a statistical test commonly used with categorical data.

It compares observed frequencies with frequencies that would be expected under a particular hypothesis.

Observed

What happened

↔

Expected

What H₀ predicts

The test asks whether the difference between observed and expected counts is larger than we would reasonably expect from random variation under the null hypothesis.

2. Categorical Data

Categorical variables place observations into groups or categories.

Payment Method

Cash, Card, UPI

Customer Type

New, Returning

Product Category

Laptop, Phone, Tablet

Chi-square methods work with counts or frequenciesrather than directly comparing numerical means.

3. Observed vs Expected Frequency

The observed frequency is the actual number of observations in a category.

The expected frequency is the number we would expect if the null hypothesis were true.

CategoryObservedExpected
A4050
B5550
C4550
D6050

Key idea:

A chi-square statistic becomes larger when the observed frequencies differ substantially from the expected frequencies, relative to the expected counts.

4. Chi-Square Statistic

χ² = Σ (O − E)² / E

O

Observed frequency

E

Expected frequency

Σ

Add the contribution from every category

Worked Example: Customer Preference

Suppose a company expects customers to choose four payment methods equally. From 200 customers, the observed counts are:

Payment MethodObserved (O)Expected (E)
Cash4050
Card6050
UPI7050
Wallet3050

Expected frequency:

200 ÷ 4 = 50 per category

Cash: (40 − 50)² / 50 = 2

Card: (60 − 50)² / 50 = 2

UPI: (70 − 50)² / 50 = 8

Wallet: (30 − 50)² / 50 = 8

χ² = 2 + 2 + 8 + 8 = 20

5. Chi-Square Goodness-of-Fit Test

A goodness-of-fit test examines whether observed categorical frequencies are consistent with a specified distribution or set of category proportions.

H₀

The population category proportions follow the specified distribution.

Hₐ

The population category proportions do not follow the specified distribution.

Example questions

  • • Are four payment methods equally popular?
  • • Does a product category distribution match the company's target?
  • • Do customer choices follow an expected market distribution?

6. Chi-Square Test of Independence

A chi-square test of independence examines whether two categorical variables are associated in a population.

Variable 1

Device Type

Mobile, Desktop, Tablet

Variable 2

Purchase

Yes, No

Business question:

Is purchase behavior associated with the customer's device type?

H₀: Independent

The two categorical variables are independent in the population.

Hₐ: Associated

The two categorical variables are associated in the population.

7. Contingency Tables

Independence tests commonly use a contingency table showing counts for combinations of two categorical variables.

DevicePurchasedDid Not PurchaseTotal
Mobile80120200
Desktop6090150
Tablet203050

8. Expected Frequency in an Independence Test

For a contingency table, the expected count for a cell is calculated using the corresponding row and column totals.

Expected Count = (Row Total × Column Total) / Grand Total

Example:

If a row total is 200, a column total is 140, and the grand total is 400:

Expected = (200 × 140) / 400 = 70

9. Degrees of Freedom

Goodness-of-Fit

df = k − 1

k is the number of categories.

Independence

df = (r − 1)(c − 1)

r is the number of rows and c is the number of columns.

Example:

A 3 × 2 contingency table has:

df = (3 − 1)(2 − 1) = 2

10. Interpreting the Result

Like other hypothesis tests, the chi-square test can be interpreted using a p-value and a chosen significance level α.

If p ≤ α

Reject H₀ because the observed data provide sufficient evidence against the null hypothesis under the chosen procedure.

If p > α

Fail to reject H₀ because the evidence is insufficient to reject the null hypothesis at that significance level.

11. Important Conditions

•

The data should be categorical counts or frequencies.

•

Observations should be appropriately independent for the study design.

•

Expected cell counts should generally be sufficiently large for the usual chi-square approximation.

•

Very small expected counts can make the chi-square approximation unreliable.

•

The test should match the research question: goodness-of-fit or independence.

12. Which Chi-Square Test?

FeatureGoodness-of-FitIndependence
Main questionDoes one categorical distribution match a specified pattern?Are two categorical variables associated?
Data structureOne categorical variableTwo categorical variables
ExampleDo four products have equal preference?Is purchase associated with device type?

Interactive Practice

Test your understanding of chi-square tests.

CHECK YOUR UNDERSTANDING

Chi-square tests are commonly used with which type of data?

CHECK YOUR UNDERSTANDING

What does the observed frequency represent?

CHECK YOUR UNDERSTANDING

Which formula represents the chi-square statistic?

CHECK YOUR UNDERSTANDING

Which test checks whether observed category frequencies match a specified distribution?

CHECK YOUR UNDERSTANDING

A chi-square test of independence is used to investigate:

CHECK YOUR UNDERSTANDING

For a 4 × 3 contingency table, what are the degrees of freedom?

Fill in the Blanks

Fill in the Blank

A chi-square test commonly works with categorical ______.

Fill in the Blank

The expected frequency represents what the null hypothesis ______.

Fill in the Blank

For a goodness-of-fit test with k categories, df = k − ______.

Fill in the Blank

For an r × c contingency table, df = (r − 1)(c − ______).

Data Analytics Challenge

Customer Support Channel

A company expects four support channels to be used equally. It records 400 support requests:

Phone

80

Email

120

Chat

140

Social

60

Expected frequency:

400 ÷ 4 = 100

Chi-square statistic:

(80 − 100)² / 100 = 4

(120 − 100)² / 100 = 4

(140 − 100)² / 100 = 16

(60 − 100)² / 100 = 16

χ² = 40

Degrees of freedom:

df = 4 − 1 = 3

Final Challenge

Device Type and Purchase

An online store wants to know whether device type is associated with whether a visitor makes a purchase.

DevicePurchasedNo Purchase
Mobile80120
Desktop6090
Tablet2030

Your task:

  1. Identify the appropriate chi-square test.
  2. Write H₀ and Hₐ.
  3. Calculate the expected count for Mobile + Purchased.
  4. Calculate the degrees of freedom.

Test: Chi-square test of independence

H₀: Device type and purchase are independent.

Hₐ: Device type and purchase are associated.

Mobile row total = 200

Purchased column total = 160

Grand total = 400

Expected Mobile + Purchased = (200 × 160) / 400

Expected count = 80

df = (3 − 1)(2 − 1)

df = 2

Lesson Summary

✓ Chi-square tests are commonly used with categorical counts.

✓ Observed counts come from the actual data.

✓ Expected counts are calculated under H₀.

✓ χ² = Σ(O − E)² / E.

✓ Goodness-of-fit tests compare one distribution with a specified pattern.

✓ Independence tests examine association between two categorical variables.

✓ For independence, df = (r − 1)(c − 1).

✓ Use the p-value and chosen α to make the statistical decision.