From a Table to a Statistical Conclusion
A two-way table can show an apparent pattern: perhaps the proportion of students who drink coffee daily differs by class year, or perhaps patients in different clinics report different symptoms. A complete analysis must do more than notice that the percentages vary. It must connect how the data were collected to an appropriate question, compare observed counts with counts expected under a null model, assess the overall evidence, and describe the pattern without claiming more than the study supports.
This workshop follows a hypothetical campus survey through those steps. The same calculations appear in tests of independence and tests of homogeneity; the distinction is the data-collection design and the population question being asked.
First Identify the Design
Suppose a campus research team independently selects 30 students at random from each of three class years and asks whether each student drinks coffee daily. Because the team samples separately within class years, the groups are set by design. The natural question is whether the daily-coffee response distribution is the same across the three class years: this is a test of homogeneity.
By contrast, if a team takes one random sample of students from the whole campus and records each student's class year and daily-coffee response, the data arise as paired categorical values on sampled individuals. The question is whether those two variables are associated in the campus population: this is a test of independence. The numerical calculation can be identical when the resulting tables are identical; the design determines which description is appropriate.
Worked Example: Classifying the Question and Hypotheses
A hypothetical campus team takes separate random samples of 30 first-year, 30 second-year, and 30 third-year students. Each student is classified as either a daily coffee drinker or not. Identify the analysis and state its hypotheses.
The sampling plan fixes three class-year groups and samples within each, so this is a test of homogeneity. Let the population proportions of daily coffee drinkers in the three class years be \(p_1\), \(p_2\), and \(p_3\). The null hypothesis says the response distribution is identical across groups. Since the response has two categories, that is equivalent to equal daily-coffee proportions:
The alternative is that the proportions are not all equal:
This is not a claim that class year causes coffee drinking. The survey is observational, and the test concerns whether the distributions differ in the represented populations.
Observed and Expected Counts
Here is a hypothetical set of counts from that survey. The row and column totals are included because they are needed to calculate expected counts.
| Class year | Daily coffee: Yes | Daily coffee: No | Row total |
|---|---|---|---|
| First year | 18 | 12 | 30 |
| Second year | 14 | 16 | 30 |
| Third year | 10 | 20 | 30 |
| Column total | 42 | 48 | 90 |
Under the null hypothesis, each class year has the same response distribution. The expected count in a cell is the count predicted by that null model, calculated as its row total multiplied by its column total, divided by the grand total:
For example, the expected number of first-year daily coffee drinkers is \(30(42)/90=14\). The expected number of first-year students who do not drink coffee daily is \(30(48)/90=16\). Because all three row totals are 30, the expected counts in each row are 14 Yes and 16 No.
Worked Example: Checking the Expected Counts
For the second-year row, calculate both expected counts and check that they sum to the row total.
The expected Yes count is
The expected No count is
The expected counts add to \(14+16=30\), as required by the row total. Across the whole table, the expected Yes counts add to \(14+14+14=42\), and the expected No counts add to \(16+16+16=48\). These checks confirm that the expected table preserves the observed margins.
The Pearson Chi-Square Test
The Pearson chi-square statistic summarizes how far observed counts \(O\) are from their expected counts \(E\), relative to the expected counts:
Large values indicate that the observed table is less compatible with the null model. If the null hypothesis is true and the approximation conditions are met, the statistic is approximately distributed as a chi-square random variable with
degrees of freedom, where \(r\) is the number of rows and \(c\) is the number of columns. The p-value is the probability, under the null model, of obtaining a chi-square statistic at least as large as the observed one.
Worked Example: Completing the Overall Test
Use the hypothetical campus table to calculate the Pearson statistic, degrees of freedom, p-value, and decision at significance level \(\alpha=0.05\).
The six observed counts are \(18,12,14,16,10,20\), and the corresponding expected counts are \(14,16,14,16,14,16\). Substituting cell by cell gives
There are \(r=3\) rows and \(c=2\) columns, so
For a chi-square distribution with 2 degrees of freedom, the upper-tail probability at 4.2857 is approximately \(0.117\). Since \(0.117>0.05\), we fail to reject \(H_0\). These data do not provide sufficient evidence at the 5% significance level that daily-coffee proportions differ among the three class years. This does not prove the proportions are equal; it means the observed differences are not strong enough, under this test, to establish a difference.
The assumptions are reasonable for this hypothetical design: the samples were independently selected within class years, each student contributes one response to one cell, and all expected counts are 14 or 16, above 5. The test's conclusion is therefore based on an appropriate chi-square approximation, subject to the stated sampling design.
Locating the Pattern and Describing Its Size
The overall statistic can be decomposed into cell contributions, \((O-E)^2/E\). In this table, the four nonzero cell contributions are \(16/14=1.143\), \(16/16=1.000\), \(16/14=1.143\), and \(16/16=1.000\); the two second-year contributions are zero. Thus first- and third-year counts account for all of the observed chi-square statistic. The first-year row has more daily coffee drinkers and fewer non-drinkers than expected; the third-year row shows the reverse.
Pearson residuals give the direction and standardized size of each cell's departure:
For example, the first-year Yes residual is \((18-14)/\sqrt{14}=1.069\), while the first-year No residual is \((12-16)/\sqrt{16}=-1.000\). The third-year residuals are \(-1.069\) for Yes and \(1.000\) for No; both second-year residuals are zero. Positive values mean observed counts exceed expected counts, and negative values mean they fall below expectation. Residuals help describe where the overall discrepancy lies, but they do not by themselves establish that any particular cell differs significantly. A formal cell-by-cell follow-up raises multiple-comparison concerns.
For tables larger than \(2\times2\), Cramer's \(V\) is a standardized measure of association:
It ranges from 0 to 1, with values nearer 0 indicating weaker association in the table. It does not give a direction, and interpretation should account for the subject matter and table dimensions rather than relying on a universal cutoff.
Worked Example: Calculating and Reporting Cramer's V
Calculate Cramer's \(V\) for the campus table and explain what it adds to the test result.
Here \(n=90\), \(r=3\), \(c=2\), and \(\min(r-1,c-1)=\min(2,1)=1\). Using the computed chi-square statistic,
The table's Cramer's \(V\) is approximately 0.218, a dimensionless summary of association strength. It describes the size of the observed table pattern; it is not a probability, does not identify which class year has a higher proportion, and does not change the test's p-value. In this example the test does not meet the 0.05 significance threshold, so the estimated association should be presented cautiously rather than as established evidence of a population difference.
A Short Justification for the Degrees of Freedom
The formula \(df=(r-1)(c-1)\) follows from how many cell counts can vary once the margins are fixed. In an \(r\)-by-\(c\) table, the row and column totals constrain the cell counts. One way to see the remaining freedom is to focus on the upper-left \((r-1)\)-by-\((c-1)\) block. Once its counts are chosen, the last column entries in the first \(r-1\) rows are fixed by those row totals. The last row entries in the first \(c-1\) columns are then fixed by their column totals, and the final cell is fixed by the remaining margin. Therefore only \((r-1)(c-1)\) cell counts can vary freely.
For a \(3\times2\) table, the first two rows in the first column can vary freely; the remaining four counts follow from the margins. There are \(2\) free counts, agreeing with \(df=(3-1)(2-1)=2\).
Writing a Concise Contextual Report
A good report names the design, the comparison, the test statistic and degrees of freedom, the p-value, and the conclusion in context. It can include an effect-size estimate and a brief description of the observed pattern. It should not imply that a non-significant result proves there is no difference, and an observational survey does not justify causal language.
For this hypothetical survey, a concise report is: “In independent random samples of 30 students from each of three class years, the proportions reporting daily coffee drinking did not differ significantly, \(\chi^2(2)=4.286\), \(p=0.117\). The observed table had Cramer's \(V=0.218\); first-year students were above their expected daily-coffee count and third-year students were below it. These survey results do not establish that class year causes coffee-drinking behavior.”
Common Pitfall
A frequent error is to decide that a group difference is statistically significant because one row has more observed counts than another, or because a residual is positive. The correction is to compare the complete table with the expected counts, compute the overall chi-square statistic and p-value, and make the inference at the stated significance level. Cell contributions and residuals locate the pattern; they are not substitutes for the overall test or a properly controlled follow-up analysis.
Another important check is arithmetic: the Pearson statistic is the sum of every cell contribution. Here the four nonzero contributions sum to \(1.143+1.000+1.143+1.000=4.286\), which agrees with the reported statistic. Rechecking this sum helps ensure that the p-value and effect-size calculation use the same statistic.
Check Your Understanding
Answer these questions using the definitions and methods in this tutorial.
- A researcher samples students once from the whole campus and records class year and whether they commute. Is the natural analysis a test of independence or homogeneity? Explain using the sampling design.
- In a table with row total 40, column total 25, and grand total 100, calculate the expected count for that cell.
- For a \(4\times3\) table, calculate the chi-square degrees of freedom.
- What does a positive Pearson residual say about the observed count compared with its expected count?
- Why does a non-significant chi-square test not prove that the population distributions are identical?
- What information does Cramer's \(V\) add to a chi-square test result, and what does it not tell you?