One Sample, Two Categorical Variables
In “Categorical Variables and Two-Way Tables,” you learned to organize counts for combinations of two categorical variables. The chi-square test of independence uses such a table to answer a particular question: In the population represented by one sample, is there an association between the two variables?
For example, a researcher might select a random sample of students and record both each student’s preferred way of commuting and whether the student participates in a school club. The sample is classified according to two variables, and each student contributes one count to one cell of the two-way table. The research question concerns whether club participation is associated with commute preference.
The null hypothesis says that the two variables are independent in the population: knowing an individual’s category for one variable does not help predict the individual’s category for the other. The alternative hypothesis says that the variables are associated. “Associated” means that the distribution of one variable differs across categories of the other variable.
The variables must be categorical, such as commute method and club participation, or device type and reason for visiting a library. The test uses counts in the cells of a two-way table. It is not a test for a relationship between two quantitative variables, and it is not designed for data where the same individual contributes multiple responses to the table.
Two Different Meanings of Independence
The word independence appears in two parts of this procedure, and the meanings are different. The null hypothesis concerns whether the two variables are independent in the population. A condition for using the procedure concerns whether the sampled observations are independent of one another.
For instance, the null hypothesis might say that commute preference and club participation are independent as variables. Separately, the observations condition asks whether the sampled students’ responses can reasonably be treated as independent. A random sample of individual students, with each student counted once, can support that condition. The null hypothesis being tested does not itself establish that the sample observations are independent.
As in “Why Chi-Square Is Needed Beyond Two Proportions,” the chi-square statistic compares observed counts with expected counts under a null model. For a test of independence, the expected count in a cell is based on the assumption that the two variables are independent. The expected-count calculation is the same one used for a chi-square test of homogeneity.
The procedure’s setting comes from the study design and question, not just the appearance of the table. A table with two categorical variables does not, by itself, tell you whether independence or homogeneity is the appropriate name for the test.
Independence or Homogeneity?
A chi-square test of independence and a chi-square test of homogeneity use the same expected-count formula, the same chi-square statistic, and the same degrees-of-freedom calculation. Their distinction is the design and the question being asked.
For a test of independence, researchers take one sample and record two categorical variables for each sampled individual. They ask whether those variables are associated in the population. For a test of homogeneity, researchers take separate samples from different populations or groups, or assign individuals to different groups, and compare the distribution of one categorical outcome across those groups. The next tutorial focuses on the homogeneity setting.
| Feature | Chi-square test of independence | Chi-square test of homogeneity |
|---|---|---|
| Data collection | One sample; each individual is classified by two variables | Separate samples or groups; each individual has one categorical outcome |
| Main question | Are the two variables associated? | Is the outcome distribution the same across groups? |
| Example question | Are device type and visit purpose associated among library users? | Does preferred device vary among users sampled from three libraries? |
The same kinds of categories can appear in either setting. For example, researchers might record library location and preferred device. If they take one sample of people and record each person’s library location and device preference, the question can be framed as whether those variables are associated. If they select separate samples from each of three libraries and compare the device-preference distributions, the design is a test of homogeneity.
Worked Example: Commute Preference and Club Participation
Worked Example: Commute Preference and Club Participation
A fictional school surveys a random sample of 240 enrolled students. Each student reports one usual commute method and whether they participate in at least one school club. The school has 4,000 students. The counts are:
| Commute method | Participates | Does not participate | Row total |
|---|---|---|---|
| Walk | 30 | 30 | 60 |
| Bus | 32 | 48 | 80 |
| Car | 34 | 66 | 100 |
| Column total | 96 | 144 | 240 |
State. Let the two variables be commute method and club participation status for students at this school. The null hypothesis is that commute method and club participation are independent in the school’s student population. The alternative hypothesis is that the variables are associated.
Plan. A chi-square test of independence is appropriate because the school took one sample and classified each student by two categorical variables. The students were randomly sampled. The sample size, 240, is less than 10% of the school’s 4,000 students, supporting the 10% condition for sampling without replacement. Each student contributes to exactly one cell, and the individual students can reasonably be treated as independent observations. We must also check that every expected count is at least 5.
Do. Under the null hypothesis of independence, the expected number of sampled students who walk and participate in a club is:
Applying the same calculation to all six cells gives expected counts of 24 and 36 for the Walk row, 32 and 48 for the Bus row, and 40 and 60 for the Car row. Each expected count is at least 5. The chi-square statistic is:
There are 3 commute categories and 2 participation categories, so the degrees of freedom are \((3-1)(2-1)=2\). For \(\chi^2=4.00\) with 2 degrees of freedom, the right-tail p-value is approximately \(0.1353\), rounded.
Conclude. At the \(0.05\) significance level, fail to reject the null hypothesis because \(0.1353>0.05\). The sample does not provide convincing evidence of an association between commute method and club participation among students at this school. This is not proof that the variables are independent; the data simply do not provide convincing evidence of an association.
What an Association Conclusion Can—and Cannot—Say
The interpretation of a test result depends on the study design as well as the p-value. In the example, a random sample from the school supports generalizing the conclusion to the population of students at that school. Because the school only observed students’ existing commute methods and club participation, the study does not show that commute method causes club participation or that club participation causes a commute choice.
In general, random sampling can support generalization to the population represented by the sample. It does not establish cause and effect in an observational study. Random assignment can support a causal conclusion about assigned treatments, but a chi-square test of independence is most naturally introduced with one sample and two measured categorical variables. Always describe what the design supports rather than treating a small p-value as proof of causation.
A small p-value for a test of independence would be evidence against the claim that the variables are independent. It would support the conclusion that an association exists in the population represented by the study, subject to the design and conditions. It would not, by itself, show which categories are responsible for the association, how large or important the association is, or that one variable causes the other.
Worked Example: Device Type and Library Visit Purpose
Worked Example: Device Type and Library Visit Purpose
A fictional library system randomly selects 300 visitors from its visitor records. For each selected visitor, staff record the primary device used during the visit—phone, laptop, or tablet—and the main purpose of the visit—study, research, or printing. The research question is whether device type and visit purpose are associated among visitors to this library system.
Identify the setting. This is one sample of visitors, and each person is classified according to two categorical variables. The research question is about association between those variables, so a chi-square test of independence is the appropriate type of test. It is not a test of homogeneity because the design does not begin with separate samples from different device groups or libraries and compare one outcome across them.
State the hypotheses. The null hypothesis is that device type and visit purpose are independent among visitors to this library system. The alternative hypothesis is that device type and visit purpose are associated among those visitors.
Check the design information. The random selection supports inference to the library-system visitor population represented by the records, assuming the records and sampling process are appropriate. Each visitor should contribute one observation and one cell count. Before carrying out the test, the analyst would also need to verify that the sample is no more than 10% of the visitor population if sampling without replacement, that observations are independent, and that every expected count is at least 5. The prompt gives no cell counts, so expected counts and the chi-square statistic cannot be calculated from the information provided.
Interpret the scope. If the eventual test provided convincing evidence of an association, that would indicate that the distribution of visit purposes differs across device categories in the represented visitor population. It would not establish that using a particular device causes a person to visit for a particular purpose.
Worked Example: When the Design Calls for Homogeneity
Worked Example: When the Design Calls for Homogeneity
A fictional community organization selects separate random samples of 100 residents from each of three neighborhoods. Each resident is asked whether they prefer text, email, or phone reminders. The organization wants to know whether reminder preferences have the same distribution in all three neighborhoods.
| Neighborhood | Text | Phone | Sample size | |
|---|---|---|---|---|
| North | 50 | 30 | 20 | 100 |
| Central | 40 | 35 | 25 | 100 |
| South | 30 | 40 | 30 | 100 |
Identify the setting. Although the data can be placed in a two-way table, this is not the one-sample, two-variable setup for a test of independence. The organization selected separate samples from three neighborhoods and compared one categorical outcome, reminder preference, across those groups. The appropriate overall procedure is a chi-square test of homogeneity.
State the question. The null hypothesis for homogeneity is that the distribution of reminder preferences is the same in all three neighborhoods. The alternative is that at least one neighborhood has a different preference distribution. The same table structure and expected-count formula are used, but the design and the hypotheses reflect a comparison of populations.
Explain what must be checked. Before performing the test, verify that each neighborhood’s sample was randomly selected, that observations are independent within and across the samples, and that the 10% condition holds for each sample if sampling without replacement. Then calculate every expected count and check that each is at least 5. The displayed observed counts alone do not establish the expected-count condition.
Explain the conclusion’s scope. A small p-value would provide evidence that the reminder-preference distributions are not all the same across the three neighborhoods. It would not establish that every pair of neighborhoods differs. Random samples could support generalizing to the represented neighborhood populations, but the test itself does not establish a cause for any differences.
Conditions and Common Mistakes
The chi-square test of independence requires attention to how the data were collected and to the expected counts under the null model. The conditions are similar to those introduced for the chi-square test of homogeneity, but the sampling description must fit the independence setting: one sample, two categorical variables, and one cell assignment per individual.
- Confusing variable independence with observation independence: The null hypothesis is about association between the two variables. The observations condition is about how sampled individuals relate to one another.
- Choosing the test from the table alone: The same two-way table format can arise from different designs. Identify whether there was one sample classified by two variables or separate samples compared on one outcome.
- Calling a non-significant result proof of independence: “Fail to reject” means the data do not provide convincing evidence of an association. It does not prove that the variables are independent.
- Claiming causation from an observational association: An observed association does not establish that one variable causes the other. Describe the relationship and respect what the study design permits.
- Checking observed counts instead of expected counts: The chi-square condition applies to every expected count calculated under the null model, not to the observed counts.
Key Takeaway
A chi-square test of independence is for one sample in which each individual is classified according to two categorical variables. It evaluates whether those variables are associated in the population. The test’s name comes from the question about the variables; the independence condition comes separately from the relationship among the sampled observations.
Check Your Understanding
For each question, focus on the study design and the meaning of independence.
- A random sample of 500 customers is classified by payment method and whether the purchase was returned. What is the population question a chi-square test of independence would address?
- In a test of independence, what does the null hypothesis say about the two variables?
- Explain the difference between independent variables in the null hypothesis and independent observations as a condition.
- A researcher takes separate random samples from four towns and compares residents’ preferred emergency-alert method. Is the setting independence or homogeneity? Explain.
- A test of independence does not reject its null hypothesis. What is an appropriate conclusion, and what claim would go too far?