Contingency Tables
University of Amsterdam
11 November 2026
In this lecture we aim to:
Reading: Chapter 16 (§16.1–16.10)
Relation between categorical variables
A “chi-squared test”, also written as \(\chi^2\) test, is any statistical hypothesis test wherein the sampling distribution of the test statistic is a chi-squared distribution when the null hypothesis is true. Without other qualification, ‘chi-squared test’ often is used as short for Pearson’s chi-squared test.
Chi-squared tests are often constructed from a Lack-of-fit sum of squared errors. A chi-squared test can be used to attempt rejection of the null hypothesis that the data are independent.
Source: wikipedia
\(\chi^2 = \sum \frac{(\text{observed}_{ij} - \text{prediction}_{ij})^2}{\text{prediction}_{ij}}\)
\(\text{observed}_{ij} = \begin{pmatrix} o_{11} & o_{12} & \cdots & o_{1j} \\ o_{21} & o_{22} & \cdots & o_{2j} \\ \vdots & \vdots & \ddots & \vdots \\ o_{i1} & o_{i2} & \cdots & o_{ij} \end{pmatrix}\)
\(\text{prediction}_{ij} = \begin{pmatrix} p_{11} & p_{12} & \cdots & p_{1j} \\ p_{21} & p_{22} & \cdots & p_{2j} \\ \vdots & \vdots & \ddots & \vdots \\ p_{i1} & p_{i2} & \cdots & p_{ij} \end{pmatrix}\)
The \(\chi^2\) distribution describes the test statistic under the assumption of \(H_0\), given the degrees of freedom.
\(df = (r - 1) (c - 1)\) where \(r\) is the number of rows and \(c\) the number of columns.

Personality
Lecture Extrovert Introvert
Digital 13 10
Live 15 26
\(\text{observed}_{ij} = \begin{pmatrix} 13 & 10 \\ 15 & 26 \\ \end{pmatrix}\)
\(\text{prediction}_{ij} = \text{expected}_{ij} = \frac{\text{row total}_i \times \text{column total}_j}{n }\)
pDigital <- totDig / n # P(digital), row total / n
pExtrovert <- totExt / n # P(extrovert), column total / n
pDigital * pExtrovert * n # predicted number of digital extrovertsDigital
10.0625
\(\small \text{prediction}_{\text{digital, extrovert}} = \underbrace{\dfrac{23}{64}}_{P(\text{digital})} \times \underbrace{\dfrac{28}{64}}_{P(\text{extrovert})} \times \underbrace{\;64\;}_{n} = \dfrac{\text{row total} \times \text{column total}}{n} = 10.06\)
\(\text{prediction}_{ij} = \text{expected}_{ij} = \frac{\text{row total}_i \times \text{column total}_j}{n }\)
Personality
Lecture Extrovert Introvert
Digital 10.0625 12.9375
Live 17.9375 23.0625
\(\text{prediction}_{ij} = \begin{pmatrix} 10.0625 & 12.9375 \\ 17.9375 & 23.0625 \\ \end{pmatrix}\)
\(\chi^2 = \sum \frac{(\text{observed}_{ij} - \text{prediction}_{ij})^2}{\text{prediction}_{ij}}\)
\(df = (r - 1) (c - 1)\)
Personality
Lecture Extrovert Introvert
Digital 10.0625 12.9375
Live 17.9375 23.0625
Calculates the exact probability of the \(\chi^2\) statistic for small samples (at least one cell of the table has an expected count smaller than 5), when the \(\chi^2\) approximation does not yet suffice.
Calculate all possible permutations.
\(\text{standardized residuals} = \frac{ \text{observed}_{ij} - \text{prediction}_{ij} }{ \sqrt{ \text{prediction}_{ij} } }\)
Odds ratio based on the observed values
Personality
Lecture Extrovert Introvert
Digital 13 10
Live 15 26
The extrovert/introvert ratio for digital and live audiences:
In the digital responses, there are +- 1.30 times as many extroverts as introverts. In the live responses, there are +- 0.58 times as many extroverts as introverts.
Is the ratio of these odds.
\(OR = \dfrac{\text{odds}_{\text{digital}}}{\text{odds}_{\text{live}}} = \dfrac{13 / 10}{15 / 26} = \dfrac{1.30}{0.58} = 2.25\)
Personality
Lecture Extrovert Introvert
Digital 13 10
Live 15 26
Alternatively, we can look at the ratios of digital/live for extroverts and introverts:
For the extroverts, there are +- 0.87 times as many digital viewers as live viewers. For the introverts, there are +- 0.38 times as many digital viewers as live viewers.
\(OR = \dfrac{\text{odds}_{\text{extrovert}}}{\text{odds}_{\text{introvert}}} = \dfrac{13 / 15}{10 / 26} = \dfrac{0.87}{0.38} = 2.25\)
\(\dfrac{\text{odds}_{\text{digital}}}{\text{odds}_{\text{live}}} = \dfrac{\text{odds}_{\text{extrovert}}}{\text{odds}_{\text{introvert}}} = 2.25\)
For these data, the odds of watching digitally were approximately 2.25 times higher for extroverts than for introverts. The odds ratio also accounts for the scores in both conditions—watching digitally and watching live—by comparing the odds of watching digitally to live viewing across both personality types.
\(\begin{pmatrix} a & b \\ c & d \\ \end{pmatrix} = \begin{pmatrix} 13 & 10 \\ 15 & 26 \\ \end{pmatrix}\)
\(OR = \dfrac{a / b}{c / d} = \dfrac{a / c}{b / d} = \dfrac{a \times d}{b \times c} = \dfrac{13 \times 26}{10 \times 15} = 2.25\)
# row total x column total / n, for every cell
catsModel <- outer(rowSums(cats), colSums(cats)) / sum(cats)
catsChi <- sum((cats - catsModel)^2 / catsModel)
catsChi[1] 25.35569
Accidents in the driving simulator from lecture 17 (alcohol \(\times\) speed)
| Alcohol / speed | none | some | much |
|---|---|---|---|
| none | 20 | 20 | 20 |
| some | 20 | 20 | 20 |
| much | 20 | 20 | 20 |
Daniels (2012): The impact of sexualized images on women’s self-evaluations
Is the type of image viewed related to self-objectification?
picture: performance vs. sexualized athlete imagestheme_present: self-objectification theme present or absentself_evaluation: countsBeckham (1929): Is the Black American happy?
Do yes/no answers differ between professions? One test for each of the three questions.
profession: the respondent’s jobresponse: yes or nohappy, you_happy, should_be_happy: counts for each questionData (.jasp) · Results (.jasp) · Results (.html)
Note
This study asked three simple questions, yet the data are fascinating. Pioneering school psychologist Albert Sidney Beckham (1897–1964) raised further hypotheses that could be tested, it unearthed very different views in different professions, and it illuminated very important social and psychological issues.

Scientific & Statistical Reasoning