Chapter 13 — Chi-Square Procedures

Tests for Categorical Data

Today: We use chi-square distributions to test claims aboutcategorical variables — whether a distribution fits a model, and whether two variables are associated.

Where Does Chi‑Square Fit in Statistical Inference?

Big picture map: today’s topic is part of our broader inference toolkit.

One Mean (z/t)Two Means (independent/paired)Proportions (one/two)Chi‑Square (goodness‑of‑fit • independence • homogeneity)Simple Linear RegressionANOVA

Same inference workflow: state \(H_0/H_a\), check conditions, compute statistic, get p‑value, conclude in context. Chi‑square is different only in that it works with counts and a right‑tailed \(\chi^2\) model.

We’re Seeking a Supplemental Instruction (SI) Leader

Who should consider applying?

  • Performing strongly in MATH 1320 (comfortable with material so far).
  • Confident with RStudio and willing to model good habits.
  • Genuine interest in teaching/mentoring; patient with questions.
  • Bring compassion and empathy for classmates who are catching up.

What an SI leader typically does

  • Attends class sessions regularly.
  • Holds weekly, structured review sessions.
  • Helps think through examples, R code, and problem‑solving strategies.
  • Works with the university SI/Student Success team.
RStudioMentorStudy SkillsCommunity

Interested? Email Prof. Vinay with your full name, student ID, and section (in‑person/online).

Learning Goals

  • Recognize when chi-square procedures are appropriate for categorical data.
  • Describe the shape and key properties of the chi-square distribution.
  • Carry out a chi-square goodness-of-fit test for a single categorical variable.
  • Use contingency tables to examine association between two categorical variables.
  • Perform chi-square tests of independence and homogeneity and interpret the results in context.

The Chi-Square Distribution

The chi-square distribution with k degrees of freedom($\chi^2_k$) is a family of right-skewed curves used fortests involving counts in categories.

  • Defined only for $x \ge 0$.
  • Shape depends on degrees of freedom $k$.
  • As $k$ increases, the curve becomes more symmetric and bell-shaped.
  • Area under the curve is 1 (a probability model).

Sketch of $\chi^2_k$ for a few values of $k$. Larger $k$ shifts the peak to the right.

Chi-Square Distribution: Key Features

For a chi-square distribution with $k$ degrees of freedom:

  • Support: $X \ge 0$. There is no left tail into negative values.
  • Right-tailed tests: All chi-square tests use the right tail area as the p-value.
  • Center: Mean is $E(X) = k$.
  • Spread: Variance is $\text{Var}(X) = 2k$.
  • Shape: Strongly right-skewed for small $k$; becomes more bell-shaped for large $k$.

Most technology (software, calculators, tables) will give you right-tail areas $P(\chi^2_k \ge x)$ directly.

Observed vs Expected Counts

Chi-square procedures compare observed counts $O$ in categories toexpected counts $E$ predicted by a model or null hypothesis.

$$\chi^2 = \sum \frac{(O - E)^2}{E}$$

Each term $\dfrac{(O-E)^2}{E}$ measures the standardized squared distance between what we saw and what the null hypothesis expects. Large chi-square values signal that the data do not fit the null model well.

Chi-Square Goodness-of-Fit Test

A goodness-of-fit test asks whether the distribution of a single categorical variable in a population matches a claimed or known model.

Example Scenario (Conceptual)

A college claims that the proportions of smartphone operating systems among students are 40% Android, 40% iOS, and 20% other. You take a random sample and record the counts in each category.

One categorical variablek categoriesCompare data to a model

This is not yet about association between variables; we are just judging how well one distribution fits a model.

Goodness-of-Fit: Conditions

Before using a chi-square goodness-of-fit test, check:

  • Randomness: Data come from a random sample or randomized experiment.
  • Independence: Individual observations are (approximately) independent.If sampling without replacement, population should be at least 10 times the sample size.
  • Large enough expected counts: For each category,$$E = n \times p_{\text{model}} \ge 5$$so that the chi-square approximation is reliable.

When some expected counts are less than 5, you may need to combine categories or use a different method.

Goodness-of-Fit: Worked Example (Outline)

Example: Campus Snack Preferences

A campus cafeteria believes that 30% of students prefer chips, 40% prefer fruit, and 30% prefer cookies.In a random sample of 150 students, we observe the following preferences.

SnackObserved count $O$Expected count $E$
Chips38$150 \times 0.30 = 45$
Fruit70$150 \times 0.40 = 60$
Cookies42$150 \times 0.30 = 45$
  1. State $H_0$: The population snack preferences follow the claimed distribution (30%, 40%, 30%).
  2. State $H_a$: The population snack preferences do not follow that distribution.
  3. Check conditions (random sample, independence, expected counts ≥ 5).

Computing $\chi^2$ for Goodness-of-Fit

$$\chi^2 = \sum \frac{(O - E)^2}{E}$$

For the snack example:

  • Chips: $\dfrac{(38 - 45)^2}{45}$
  • Fruit: $\dfrac{(70 - 60)^2}{60}$
  • Cookies: $\dfrac{(42 - 45)^2}{45}$

Add these contributions to obtain the test statistic $\chi^2$. Then find the p-value from a chi-square distribution with $k - 1$ degrees of freedom, where $k$ is the number of categories.

In practice, you will usually have technology compute $\chi^2$ and the p-value once you supply the table of observed and expected counts.

Contingency Tables & Association

A contingency table (two-way table) displays counts for two categorical variables at once,showing how the variables are related in a sample.

Example Variables

  • Class level (First-year, Sophomore, Junior, Senior)
  • Preferred study method (Alone, Small group, Online)

A two-way table would cross these variables to show, for example, how many first-year students prefer group study,how many seniors prefer online study, and so on.

Chi-square tests of independence and homogeneity are both based on contingency tables.

How to Build a Contingency Table (Illustration)

Raw responses (pairs)

Choose two categorical variables, then tally.

  • (Year = A1, Preference = B1) × 7
  • (Year = A1, Preference = B2) × 3
  • (Year = A2, Preference = B1) × 4
  • (Year = A2, Preference = B2) × 6

Now convert to a 2×2 table of counts.

B1B2Row total ri
A17310
A24610
Col total cj11920grand N

Always include row totals, column totals, and the grand total.

  1. Pick row and column variables and list their categories.
  2. Tally each response into the correct cell (counts only, not %).
  3. Compute row/column totals and the grand total.

Expected Counts from Totals (Visual)

Under independence, the expected count in cell \((i,j)\) is:

$$ E_{ij} = \frac{(\text{row total})_i \times (\text{column total})_j}{\text{grand total}} $$

For cell \(A_1, B_1\): \(E_{11} = \dfrac{\color{#2e7d32}{10} \times \color{#1e88e5}{11}}{\color{#6a1b9a}{20}} = 5.5\).Similarly, \(E_{12}=4.5\), \(E_{21}=5.5\), \(E_{22}=4.5\).

B1B2Row total ri
A1O=7
\(E_{11}=5.5\)
O=3
\(E_{12}=4.5\)
10
A2O=4
\(E_{21}=5.5\)
O=6
\(E_{22}=4.5\)
10
Col total cj11920grand N

Check the rule: expected counts should be \(\ge 5\) (or most \(\ge 5\) with none too small).

Association Between Variables

Two categorical variables in a population are said to be associated if theconditional distributions of one variable differ across levels of the other variable.

If the conditional distributions are the same across all levels of the other variable,then the variables are not associated (we say they are independent).

We will use chi-square tests to decide, based on sample data, whether we have enough evidence to claim an association in the population.

Chi-Square Test of Independence

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

We start with a contingency table of observed counts $O_{ij}$ for row $i$ and column $j$.

Under $H_0$ (independence), the expected count in each cell is:

$$E_{ij} = \frac{(\text{row total})_i \times (\text{column total})_j}{\text{grand total}}$$

Hypotheses

  • $H_0$: The two variables are independent in the population.
  • $H_a$: The two variables are associated in the population.

“Independent” means knowing one variable does not help predict the other.

Independence Test: Conditions

Before using a chi-square test of independence, check:

  • Randomness: Data come from a single random sample of individuals from the population.
  • Independence: Observations are approximately independent (10% condition if sampling without replacement).
  • Expected counts: All expected counts $E_{ij}$ are at least 5 (or “most” are ≥ 5 with none too small).

If these conditions are not met, the chi-square approximation may not be accurate and a different method may be needed.

Interactive: Chi-Square from a 2×2 Table

Adjust the four cell counts in this 2×2 table. We will automatically compute the expected counts and the chi-square statistic.

Category B₁Category B₂Row total
Category A₁50
Category A₂50
Col total5545100

Think of A as one variable (e.g., Gender) and B as another (e.g., Preference).

Live Output

$\chi^2 =$ 0.000 withdf = 1.

Expected counts under independence:

  • $E_{11}$ = 0.0,$E_{12}$ = 0.0
  • $E_{21}$ = 0.0,$E_{22}$ = 0.0

Adjust the counts to see how the chi-square statistic reacts to stronger or weaker associations.

Large $\chi^2$ (far from 0) suggests the data are inconsistent with independence —the variables may be associated. A very small $\chi^2$ means the observed table is close to what we’d expect if the variables were independent.

Chi-Square Test of Homogeneity

A chi-square test of homogeneity compares the distribution of a categorical variable across two or more populations or treatments.

Example: Are the grade-level distributions of preferred study method the same across three different schools?

We take one random sample from each population (or treatment group),record counts for the categorical outcome, and place them in a contingency table.

Independence vs. Homogeneity

  • Independence: One population, two variables.
  • Homogeneity: One variable, several populations (or treatments).

The calculations for the chi-square statistic and expected counts are the same.The main difference lies in the research question and study design.

Chi-Square Procedures: Quick Reference

All chi-square procedures use

$$\chi^2 = \sum \frac{(O - E)^2}{E}$$

and compare the statistic to a chi-square distribution with$\text{df} = (\text{rows} - 1)(\text{columns} - 1)$ (or $k - 1$ for goodness-of-fit).

  1. State the parameter(s) and null & alternative hypotheses clearly.
  2. Check conditions: randomness, independence, and sufficiently large expected counts.
  3. Compute all expected counts and the chi-square statistic $\chi^2$.
  4. Determine the p-value from the appropriate chi-square distribution (right-tail area).
  5. Make a decision (reject / fail to reject $H_0$) and conclude in context.

R Basics for Chi‑Square (Copy/Paste)

Goodness‑of‑Fit (model given)

k = 5 categories ⇒ df = 4. Use counts (not percentages). If some expected counts are small, use the simulated p‑value.

# Goodness-of-fit (k = 5 ⇒ df = 4)
counts <- c(12, 3, 6, 20, 9)                # observed
probs  <- c(0.2, 0.1, 0.1, 0.3, 0.3)        # stated distribution (sums to 1)
chisq.test(counts, p = probs, correct = FALSE)

# If any expected count < 5, prefer a simulated p-value:
chisq.test(counts, p = probs, simulate.p.value = TRUE, B = 10000)

Tip: If rounding makes sum(probs) ≠ 1, call chisq.test(counts, p = probs, rescale.p = TRUE).

Independence / Homogeneity (r × c table)

Same code for both tests; supply an r×c table of counts. df = (rows − 1)(cols − 1).

# Independence / Homogeneity (2×2 example)
tab <- matrix(c(30, 20,
                25, 25),
              nrow = 2, byrow = TRUE)
chisq.test(tab, correct = FALSE)  # Yates off for clarity in class

# Larger tables work the same way; enter counts row by row.
# Always interpret in context and check expected counts ≥ 5.

Heads‑up: If any expected count < 5, be cautious about the p‑value. Combine sensible categories or collect more data; for 2×2, an exact method exists (concept only).

Common Mistakes & Tips

Watch Out For:

  • Using chi-square with percentages instead of counts (always work with counts).
  • Forgetting to check expected counts ≥ 5 before trusting the test.
  • Using the wrong degrees of freedom in the chi-square distribution.
  • Interpreting a large p-value as evidence that the null hypothesis is “true” (it just means the data are not surprising under $H_0$).
  • Claiming causation from chi-square tests when the data come from an observational study.

Good practice: Sketch the table, label variables clearly, and write a complete sentence answer that mentions the direction of any association (who has higher proportions where?).