Topic contents

Lesson 00

Categorical data analysis: from counts to statistical association

First describe the category distribution correctly, then assess whether differences exceed sampling variability. Distinguish observing a difference from performing a statistical test.

What questions does categorical data analysis answer?

Categorical data place the units being studied into different categories. The first step is to describe how many units are in each category and what proportion they represent. With two categorical variables, we can also compare the composition of different groups. To generalize sample differences to the population, we need statistical inference, provided its conditions are met.

Describe one variable

Counts and composition

Counts, relative frequencies, and percentages in each category; bar charts and pie charts.

Key question: What is the denominator?

→
Examine two variables together

Contingency tables and conditional proportions

Cross-tabulated counts; row percentages and column percentages; comparisons of group composition.

Key question: Which group is the condition?

→
Make inferences from a sample

χ² tests

Are the observed counts O too far from the counts E expected under the null hypothesis?

Key question: What is H₀?

One categorical variable

A school surveys students about their main learning device. First describe the shares using computers, tablets, and phones. If there are prespecified hypothesized proportions, a chi-square goodness-of-fit test can also be used.

Two categorical variables

Survey year of study and AI tool use. First compare usage proportions across years, then use a chi-square test of independence to assess whether the two variables are statistically associated in the population.

The question running through this module: How do we describe category differences observed in a sample? Do these differences provide enough evidence against a clearly stated population hypothesis?

Read categorical data: counts first, then denominators

Count O

The observed number of people in a category.

Oi=observed count in category i{O}_{i}=\text{observed count in category i}

Relative frequency f

The number of people in a category divided by the total number of people.

fi=Oin{f}_{i}=\frac{{O}_{i}}{n}

Percentage

Relative frequency multiplied by 100%, for easier comparison.

Percentage=fi×100%\text{Percentage}={f}_{i}\times 100\%

Two variables: cross-tabulated counts form a contingency table

Year of studyHave used AIHave not used AITotal
First-year students6238100
Third-year students12832160
Grand total19070260

This sample example, used again in Lessons 02–03, examines year of study and AI tool use.

1
Compare years of study: row percentages

First-year students: 62 ÷ 100 = 62%; third-year students: 128 ÷ 160 = 80%.

2
Compare by AI use: column percentages

Of the 190 students who have used AI, third-year students account for 128 ÷ 190 ≈ 67.4%.

Row and column percentages use different denominators and answer different questions. Different sample proportions in two groups do not establish that the two variables are associated in the population.

Two χ² tests: the same statistic, different research questions

χ2=∑(O−E)2E{\chi }^{2}=\sum \frac{{(O-E)}^{2}}{E}
Shared logic: under the null hypothesis, larger deviations of observed counts O from expected counts E generally produce a larger χ² statistic. Use the appropriate degrees of freedom to find the right-tail p-value.
03 · Test of independence

Are two categorical variables independent?

H₀: The two categorical variables are independent in the population.

Expected counts: Calculated from the marginal totals of the contingency table.

Eij=row i total×column j totaln{E}_{ij}=\frac{\text{row i total}\times \text{column j total}}{n}

Degrees of freedom: df = (r−1)(c−1)

This example is a 2×2 contingency table with df=1. Add the four cell contributions to obtain one χ² statistic for the entire table, rather than performing a separate test for each year of study.

04 · Goodness-of-fit test

Does one variable follow a hypothesized distribution?

H₀: The population category proportions equal the prespecified hypothesized proportions.

Expected counts: Calculated from the sample size and hypothesized proportions.

Ei=n×pi0{E}_{i}=n\times {p}_{i0}

Degrees of freedom: df = k−1

Here the hypothesized proportions are prespecified, with no distribution parameters estimated from the same data. If parameters are estimated, the degrees of freedom may need adjustment.

Test of independence: substitute values from an example

Sample contingency table: first-year students (62,38), third-year students (128,32).

Expected count for the first-year “Have used AI” cell:

E=100×190260≈73.077E=\frac{100\times 190}{260}\approx 73.077

Sum of the four cell contributions: χ² ≈ 10.134; df=1; P≈0.0015.

At α=0.05, reject independence. There is evidence of statistical association between the two categorical variables in the population; this does not establish causation.

Goodness-of-fit test: substitute values from an example

Hypothesized proportions (50%,30%,20%), n=100, observed counts (56,27,17).

Expected counts: 100×(0.5,0.3,0.2) = (50,30,20).

χ2=(56−50)250+(27−30)230+(17−20)220=1.470{\chi }^{2}=\frac{{(56-50)}^{2}}{50}+\frac{{(27-30)}^{2}}{30}+\frac{{(17-20)}^{2}}{20}=1.470

df=2; P≈0.4795; fail to reject H₀ at α=0.05.

Failing to reject does not prove that the hypothesized proportions are exactly correct.

In practice: choose the right question, then check the conditions

Choose a method for your research purpose

1
Only describing the composition of one variable?

Use a frequency table, proportions, and suitable charts. There is no need to perform a test just to include one.

2
Need to compare two categorical variables?

First construct a contingency table and compare conditional proportions. To infer whether the variables are independent in the population, consider a chi-square test of independence.

3
Have one categorical variable and prespecified proportions?

Consider a chi-square goodness-of-fit test. Specify the hypothesized proportions and null hypothesis first.

Conditions for χ² tests and limits of interpretation

  1. Independent observations: Records should meet the independence requirement; assess this using the sampling and survey design.
  2. Sample size involves more than the total count: There is no universal “n≥30” threshold for all χ² tests. Pay closer attention to the expected counts in each category or cell.
  3. Expected counts should not be too small: For teaching purposes, this module uses E≥5 in every cell or category as an easy-to-use, conservative rule of thumb for both the 2×2 test of independence and the goodness-of-fit test. If it is not met, consider an exact test, appropriately combining categories, or simulation methods; avoid a mechanical response.
  4. Appropriate categories and hypotheses: Categories should be mutually exclusive and cover the scope of the study. Hypothesized proportions for a goodness-of-fit test should be specified in advance.

E≥5 is the simplified, conservative teaching rule used in this course, rather than the only mathematically necessary condition in every situation. Complex sampling, weighted data, or dependent observations require methods suited to the design.

Essential limits of statistical interpretation: A p-value is the probability, under the null hypothesis, of a statistic at least as extreme as the observed one; it is not the probability that the null hypothesis is true. “Significant association” does not mean “causation,” and “failing to reject H₀” does not mean “proving H₀ correct.”

Lesson map: 01–04

01 How do we describe categorical data?

Counts, relative frequencies, percentages, bar charts, and pie charts; comparing the composition of two groups.

02 Contingency tables: are two categorical variables related?

Cross-tabulated counts, row percentages, column percentages, and conditional proportions.

03 Chi-square test of independence for contingency tables

Independence hypothesis, marginal totals, expected counts for each cell, contributions to χ², df, and the right-tail p-value.

04 Chi-square goodness-of-fit test

Prespecified hypothesized proportions, E=np₀, contributions from each category, df, and how degrees of freedom affect the curve's shape.