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.
Counts and composition
Counts, relative frequencies, and percentages in each category; bar charts and pie charts.
Key question: What is the denominator?
Contingency tables and conditional proportions
Cross-tabulated counts; row percentages and column percentages; comparisons of group composition.
Key question: Which group is the condition?
χ² 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.
Read categorical data: counts first, then denominators
Count O
The observed number of people in a category.
Relative frequency f
The number of people in a category divided by the total number of people.
Percentage
Relative frequency multiplied by 100%, for easier comparison.
Two variables: cross-tabulated counts form a contingency table
| Year of study | Have used AI | Have not used AI | Total |
|---|---|---|---|
| First-year students | 62 | 38 | 100 |
| Third-year students | 128 | 32 | 160 |
| Grand total | 190 | 70 | 260 |
This sample example, used again in Lessons 02–03, examines year of study and AI tool use.
First-year students: 62 ÷ 100 = 62%; third-year students: 128 ÷ 160 = 80%.
Of the 190 students who have used AI, third-year students account for 128 ÷ 190 ≈ 67.4%.
Two χ² tests: the same statistic, different research questions
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.
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.
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.
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:
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).
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
Use a frequency table, proportions, and suitable charts. There is no need to perform a test just to include one.
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.
Consider a chi-square goodness-of-fit test. Specify the hypothesized proportions and null hypothesis first.
Conditions for χ² tests and limits of interpretation
- Independent observations: Records should meet the independence requirement; assess this using the sampling and survey design.
- 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.
- 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.
- 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.
Lesson map: 01–04
Counts, relative frequencies, percentages, bar charts, and pie charts; comparing the composition of two groups.
Cross-tabulated counts, row percentages, column percentages, and conditional proportions.
Independence hypothesis, marginal totals, expected counts for each cell, contributions to χ², df, and the right-tail p-value.
Prespecified hypothesized proportions, E=np₀, contributions from each category, df, and how degrees of freedom affect the curve's shape.