Tutorials › AP Statistics › Chi-Square Test for Independence: Purpose and Setting

Chi-square tests for categorical data · Tutorial 543 of 1000

Chi-Square Test for Independence: Purpose and Setting

Learn to recognize the one-sample, two-variable setting for a chi-square test of independence and distinguish association between variables from independence among observations.

Intermediate 9 min read

What You'll Learn

  • Recognize when one sample is classified by two categorical variables.
  • State the null and alternative hypotheses for a test of independence.
  • Distinguish independence of variables from independence of observations.
  • Tell a test of independence apart from a test of homogeneity.
  • Connect study design to what an association conclusion can support.
  • Identify the conditions to check before using the chi-square procedure.

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.

Definition: A chi-square test of independence evaluates whether two categorical variables are associated in a population. It uses one sample of individuals, with each individual classified according to both variables, and compares the observed cell counts with counts expected if the variables were independent.

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.

Key distinction: “The variables are independent” is the claim in the null hypothesis. “The observations are independent” is a condition about the way the data were collected and the relationship among sampled individuals.

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.

$$ \text{Expected count}=\frac{(\text{row total})(\text{column total})}{\text{grand total}} $$

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.

FeatureChi-square test of independenceChi-square test of homogeneity
Data collectionOne sample; each individual is classified by two variablesSeparate samples or groups; each individual has one categorical outcome
Main questionAre the two variables associated?Is the outcome distribution the same across groups?
Example questionAre 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 methodParticipatesDoes not participateRow total
Walk303060
Bus324880
Car3466100
Column total96144240

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:

$$ \frac{(60)(96)}{240}=24 $$

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:

$$ \chi^2= \frac{(30-24)^2}{24}+\frac{(30-36)^2}{36} +\frac{(32-32)^2}{32}+\frac{(48-48)^2}{48} +\frac{(34-40)^2}{40}+\frac{(66-60)^2}{60} =1.50+1.00+0+0+0.90+0.60=4.00 $$

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.

NeighborhoodTextEmailPhoneSample size
North503020100
Central403525100
South304030100

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.

Conditions: Use one sample in which each individual is classified by two categorical variables. The data should come from an appropriate random sample, observations should be independent, and each individual should contribute to one cell. For sampling without replacement, check the 10% condition. Under the null model, every expected count must be at least 5.
  • 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.
AP Exam Tip: Name the design before naming the test. Say that one sample was classified by two categorical variables and that the question concerns association. Then state independence as the null hypothesis, check the expected counts and design conditions, and interpret any conclusion in context without claiming causation.

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.

Key takeaway: Use a chi-square test of independence when one sample is classified by two categorical variables and the research question asks whether those variables are associated. Distinguish that null-hypothesis question from the condition that observations be independent.

Check Your Understanding

For each question, focus on the study design and the meaning of independence.

  1. 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?
  2. In a test of independence, what does the null hypothesis say about the two variables?
  3. Explain the difference between independent variables in the null hypothesis and independent observations as a condition.
  4. A researcher takes separate random samples from four towns and compares residents’ preferred emergency-alert method. Is the setting independence or homogeneity? Explain.
  5. A test of independence does not reject its null hypothesis. What is an appropriate conclusion, and what claim would go too far?