Why Work Through a Complete Test?
A two-way table can show how two categorical variables occur together in a sample. For example, a survey might record whether a person uses a health information app and whether that person prefers appointment reminders by text or email. A test of independence asks whether the observed differences in the table provide evidence that the variables are associated in the population from which the sample was drawn.
The test compares observed counts with counts expected under independence. To make the comparison meaningful, the sampling and count conditions must be reasonable, the hypotheses must match the study, and the p-value must be interpreted in context. This tutorial works through the complete process for 2x2 tables.
Set Up the Hypotheses and Check Conditions
Let the two categorical variables define the rows and columns of a 2x2 table. The hypotheses concern the population relationship between these variables:
- \(H_0\): The two categorical variables are independent in the population.
- \(H_a\): The two categorical variables are associated in the population.
These are statements about the population, not claims that the variables in the observed sample are exactly unrelated or associated. The sample table will almost always show some difference from the pattern expected under independence, even when the null hypothesis is true. The test assesses whether the difference is large relative to the variation expected from sampling.
Before using the Pearson chi-square approximation, check the conditions for chi-square inference. The sample should come from a suitable random process, and the observations should be independent. For sampling without replacement, the sample should be a small fraction of the population; a common check is that the sample is less than 10% of that population. Finally, expected counts should be sufficiently large for the chi-square approximation to be appropriate. A common guideline is that every expected count is at least 5. If these conditions are not met, the usual Pearson chi-square p-value may not be reliable.
For a survey of individuals, independence of observations means that each sampled person contributes to one cell only and that one person's response does not determine another's. A large sample does not compensate for a biased sampling method or strongly dependent responses.
Calculate Expected Counts and the Test Statistic
The expected count in row \(i\), column \(j\), is calculated using the table margins:
Here, \(E_{ij}\) is the count expected in that cell if the null hypothesis of independence holds, and \(n\) is the grand total. Expected counts need not be whole numbers. They describe the null model, not additional observed individuals.
The Pearson chi-square statistic is:
In this expression, \(O_{ij}\) is the observed count in a cell. Each term measures the squared difference between observed and expected counts relative to the expected count. The terms are nonnegative, so a larger value of \(\chi^2\) indicates a greater overall discrepancy from independence.
For a table with \(r\) rows and \(c\) columns, the degrees of freedom are \((r-1)(c-1)\). In a 2x2 table, \(df=(2-1)(2-1)=1\). One way to see why there is one degree of freedom is to imagine that the row and column totals are fixed: once one of the four cell counts is set, the margins determine the other three. Thus only one count can vary freely.
The test uses the right tail because larger chi-square values mean greater disagreement between the observed table and the counts expected under independence. A small p-value is evidence against \(H_0\). Choose a significance level \(\alpha\) before making the decision: reject \(H_0\) when the p-value is at most \(\alpha\); otherwise, do not reject \(H_0\).
Worked Test: Reminder Preference and App Use
Worked Example: Testing for Association in a Survey
Suppose a hypothetical random survey of 120 adults records whether each respondent uses a health information app and whether the respondent prefers appointment reminders by text or email. The table gives the observed counts.
| Prefers text | Prefers email | Total | |
|---|---|---|---|
| Uses app | 42 | 18 | 60 |
| Does not use app | 28 | 32 | 60 |
| Total | 70 | 50 | 120 |
Conditions. Treat the survey as a random sample, assume each adult responded once, and suppose the 120 respondents are less than 10% of the population of interest. These design assumptions make independent observations plausible. All four expected counts, calculated below, are at least 5, so the expected-count condition is met.
Hypotheses. \(H_0\): app use and reminder preference are independent among adults in the population. \(H_a\): app use and reminder preference are associated in that population.
Expected counts. For app users who prefer text, the expected count is:
The other expected counts are \((60)(50)/120=25\), \((60)(70)/120=35\), and \((60)(50)/120=25\), in the same cell order as the table. Each expected count is at least 5.
Test statistic. Substituting each observed and expected count gives:
The arithmetic is \(49/35+49/25+49/35+49/25=1.400+1.960+1.400+1.960=6.720\). The degrees of freedom are \(df=1\). The right-tail probability for \(\chi^2=6.720\) with \(df=1\) is approximately \(0.0095\).
Conclusion. At \(\alpha=0.05\), \(0.0095<0.05\), so reject \(H_0\). The hypothetical survey provides evidence that app use and reminder preference are associated among adults in the population of interest. The result does not establish that using an app causes a person to prefer text reminders.
Why the Comparison Uses the Right Tail
The statistic adds up squared differences, so it cannot be negative. If observed counts are close to the null expected counts, each cell contributes little and the total is small. If several observed counts depart substantially from their expected values, the total becomes larger. Therefore, evidence against independence lies in unusually large values of \(\chi^2\), not unusually small ones.
The p-value is not the probability that the null hypothesis is true. It is the probability, assuming independence and the test conditions, of obtaining a statistic at least as large as the one calculated from the sample. This interpretation keeps the conclusion tied to the data and the model.
Worked Test: A Small Difference from Independence
Worked Example: A Nonsignificant Result
Imagine a second hypothetical random survey of 60 adults. It records whether a respondent uses a particular service and whether the respondent recommends that service.
| Recommends | Does not recommend | Total | |
|---|---|---|---|
| Uses service | 18 | 12 | 30 |
| Does not use service | 12 | 18 | 30 |
| Total | 30 | 30 | 60 |
Conditions and hypotheses. Assume the survey is a random sample, each person contributes one response, and the sample is less than 10% of the population. The hypotheses are that service use and recommending the service are independent (\(H_0\)) versus associated (\(H_a\)). Each expected count is \((30)(30)/60=15\), so all expected counts exceed 5.
Statistic and p-value. The four expected counts are 15. Therefore:
With \(df=1\), the right-tail p-value is approximately \(0.1213\). At \(\alpha=0.05\), \(0.1213>0.05\), so do not reject \(H_0\). This survey does not provide sufficient evidence of an association between service use and recommending the service. It does not prove that the variables are independent; a different sample could provide stronger or weaker evidence.
Worked Test: A Very Small Statistic
Worked Example: Interpreting a Weak Departure
Consider a third hypothetical random survey of 199,100 adults. It records two Yes/No responses, and the observed table is:
| Yes to question B | No to question B | Total | |
|---|---|---|---|
| Yes to question A | 49,851 | 49,699 | 99,550 |
| No to question A | 49,699 | 49,851 | 99,550 |
| Total | 99,550 | 99,550 | 199,100 |
Conditions and expected counts. Suppose the survey was randomly conducted, each adult responded once, and the sample is less than 10% of the population. Every expected count is \((99{,}550)(99{,}550)/199{,}100=49{,}775\), which is well above 5. The hypotheses are independence of the two responses versus association.
Statistic. Each observed count differs from its expected count by 76. Thus:
For \(df=1\), the right-tail p-value is approximately \(0.4957\) (or \(0.496\) to three decimal places). At \(\alpha=0.05\), do not reject \(H_0\). The sample provides little evidence against independence between the responses. The p-value does not mean there is a 49.57% probability that the null hypothesis is true.
A Compact Workflow
A complete test is easier to check when the reasoning follows the same order each time. The flow below keeps the design assumptions, calculations, and conclusion connected.
Name the population relationship under \(H_0\) (independence) and under \(H_a\) (association).
Consider random sampling, independent observations, the 10% guideline when sampling without replacement, and the expected counts.
Use row total times column total divided by \(n\) for each expected count, then sum \((O-E)^2/E\) across the cells.
For a 2x2 table use \(df=1\), compare the p-value with \(\alpha\), and describe the evidence in the population context.
Common Mistakes
- Using observed counts as expected counts. Expected counts come from the margins under the independence model; they are calculated, not copied from the table.
- Using the wrong tail. A small chi-square statistic indicates little discrepancy. The p-value is the right-tail probability for a statistic at least as large as the observed one.
- Reporting only a decision. “Reject” or “do not reject” is incomplete without identifying the variables and describing the evidence in context.
- Treating a nonsignificant result as proof of independence. Not rejecting \(H_0\) means the sample does not provide sufficiently strong evidence against it at the chosen \(\alpha\); it does not establish that the variables are independent.
- Claiming causation from an association test. A survey test can provide evidence of association, but by itself it does not show that one variable causes the other.
Check Your Understanding
For each question, explain your reasoning rather than giving only a number or decision.
- In a test of independence, what does the null hypothesis say about the two population variables?
- A cell has row total 40, column total 30, and the grand total is 100. What is its expected count under independence?
- Why is the p-value for a Pearson chi-square test found in the right tail?
- For a 2x2 table, what are the degrees of freedom, and why is there one freely varying cell count when the margins are fixed?
- A test has p-value \(0.08\) and uses \(\alpha=0.05\). State the decision and explain what it does, and does not, establish.
- Why should a conclusion from a survey test of independence avoid saying that one variable causes the other?