A survey of students records where each one lives and whether the usual trip to school uses public transport. In one neighborhood 60 percent of the sampled students use it, and in the other 40 percent. Is that a difference between the populations, or a gap that chance alone often produces in samples of 50? This lesson computes the counts a table would have if the two variables were unrelated, measures how far the observed table is from them, and shows that the same calculation answers two different questions depending on how the data were collected.
Key terms
Expected count
Definition A contingency table with rows and columns counts units classified by two categorical variables. Let be the observed count in row and column , the total of row , the total of column and the grand total. When every row and column total is positive, the expected count of the cell is
It is the count the cell would have if every row had the column proportions of the whole table, and it estimates the mean of under the null hypothesis of independence or of homogeneity. The table of expected counts has the same row totals and the same column totals as the observed table, and its entries need not be whole numbers. The chi-square approximation in the tests below needs expected counts that are not too small. A common guideline asks for all of them to be at least 5, but such cutoffs are rules of thumb, not theorems. For a sparse table, an exact method such as Fisher’s exact test or a simulation is the better choice.
In plain words Take the overall split of the columns and apply it to every row. Under the null hypothesis the rows differ only in size, not in pattern. That means equal conditional proportions in every row, not equal counts in every cell, and the column proportions come from the observed table, not from an assumption that the categories are equally likely.
Example A constructed survey gives this table.
| Neighborhood | Public transport | Other | Total |
|---|---|---|---|
| North | 30 | 20 | 50 |
| South | 20 | 30 | 50 |
| Total | 50 | 50 | 100 |
Every expected count is . If the column totals had been 80 and 20 instead, a row with total 50 would have expected counts and .
Chi-square test of independence
Definition Suppose units are sampled independently from one population, for example by simple random sampling from a large population, and each unit is classified by a row variable with categories and a column variable with categories, so that it lands in exactly one cell. The test of independence has null hypothesis
for every and , and alternative that the variables are associated. When every category of has positive probability, the null hypothesis says that the conditional distribution of given is the same for every . The statistic is
If holds and the expected counts are large, has approximately the chi-square distribution with
degrees of freedom, and the p-value is the upper tail at the observed value . The count is the number of interior cells that can be chosen freely once the margins are fixed. Clustered observations, weighted survey samples and paired outcomes fall outside these conditions and need other methods.
In plain words The test asks whether the observed table is farther from its expected table than chance usually takes it when the two variables are unrelated in the population. A small p-value is evidence of association. It does not say which variable influences the other, whether a third variable explains the pattern, or how strong the association is. The independence in concerns the two variables. Independence of the observations is a separate condition of the design, and it fails, for example, when the same student is counted several times.
Example Suppose the table above comes from a random sample of 100 students from one city, each classified by neighborhood and by transport. Each difference is or , so every cell contributes and . With the p-value is , so at a significance level of 0.05 chosen in advance the test rejects independence. This is the uncorrected Pearson statistic. Some software applies the Yates continuity correction to a 2 by 2 table by default, which gives 3.24 and a p-value of about 0.072 for the same counts, so a report should name the procedure it used.
Chi-square test of homogeneity
Definition Suppose independent random samples of fixed sizes are drawn from populations, and each sampled unit falls into exactly one of categories. Let be the probability of category in population . The test of homogeneity has null hypothesis
for every , so that all the populations share one category distribution, and alternative that at least two populations differ in at least one category. It uses the same expected counts , the same statistic and the same approximate chi-square reference with degrees of freedom as the test of independence. With two populations and two categories, equals the square of the pooled two-proportion statistic.
In plain words The arithmetic is the same, but the design and the question are different. In a test of independence there is one sample, two variables are measured on each unit and the row totals are random. In a test of homogeneity the row totals are set by the design, with one sample per population, and the question is whether the populations share a distribution. The conclusion should be worded for the design that produced the data, and the table alone does not show which design that was.
Example Now suppose the same table comes from independent random samples of 50 students at each of two schools, North and South. The sample proportions using public transport are 0.60 and 0.40, and the pooled proportion is 0.50. The pooled standard error is , so and , with the same p-value of about 0.0455. At the 0.05 level this is evidence that the proportion of students using public transport differs between the two school populations. It does not show that the school causes the difference. Students who live farther away, for example, may be more likely both to attend one school and to take the bus.
Pearson residual
Definition The Pearson residual of the cell in row and column is
Its square is the contribution of the cell to , so the squared residuals add to . A positive residual marks a cell with more units than the null hypothesis predicts, and a negative residual a cell with fewer. Under with large counts, Pearson residuals have variances below 1 and are correlated with each other, so they are not independent standard normal values. An adjusted residual divides by an estimate of its own standard error instead, and it is approximately standard normal under when the counts are large. Scanning many cells for the largest residual is a multiple comparison problem, so a formal claim about a single cell needs a follow-up procedure planned for that purpose.
In plain words The chi-square statistic says how far the table is from the null hypothesis, and the residuals say where. Each one measures a cell’s excess or shortage in units of the square root of its expected count, a rough yardstick for chance variation in that cell, and the cell with the largest squared residual makes the largest contribution to . Read their pattern as a description of the association, not as a set of separate tests.
Example In the neighborhood table, the cells North with public transport and South with other transport each have residual , and the other two cells have . Each cell contributes to . The signs show the pattern, more public transport in North and less in South than the null hypothesis predicts. The adjusted residuals here are and .
Cramér’s V
Definition For a table with rows and columns and every row and column total positive, Cramér’s V is
It satisfies . It equals exactly when , that is, when every observed count equals its expected count, and it equals exactly when the category of one variable determines the category of the other, as in a 2 by 2 table with all its counts on one diagonal. Multiplying every count by the same positive factor multiplies by that factor and leaves unchanged.
In plain words Cramér’s V puts the strength of association on a scale from 0 to 1. Unlike , it does not grow when the same pattern is observed in more data. It has no sign, so the table and the residuals still have to show which way the association goes. There are no universal cutoffs for a small or a large value, and what counts as large depends on the subject.
Example For the neighborhood table, . Doubling every count keeps the percentages at 60 and 40 and doubles to 8, which lowers the p-value to about 0.0047, but stays 0.2. More data give stronger evidence of the same association, not a larger association.
Check yourself
- A constructed table has the counts 12 and 18 in its first row and 8 and 12 in its second. Find the row and column totals, the expected counts and the chi-square statistic. Does the result prove that the two variables are independent in the population?
Show the answer
The row totals are 30 and 20, the column totals are 20 and 30, and the grand total is 50. The expected counts are and in the first row, and and in the second. They equal the observed counts, so and the p-value is 1.
The sample fits independence exactly, but that does not prove independence in the population. Populations with a weak association can also produce such a table, and a sample of 50 has limited power to detect a weak association. The conclusion is that these data give no evidence of association.
- For each design, say which chi-square test fits, if either, and give its degrees of freedom. In design (a), a random sample of 200 adults from a town is classified by age group, with three groups, and by main news source, with four sources. In design (b), independent random samples of 80 customers are taken at each of three stores, and each customer’s payment method is recorded as cash, card or phone. In design (c), sixty students answer the same yes or no question before and after a workshop, and the 120 answers are arranged in a table with rows for before and after and columns for yes and no.
Show the answer
Design (a) is one sample classified by two variables, so it calls for the test of independence, with degrees of freedom. Design (b) has row totals fixed at 80 by the design, with one sample per store, so it calls for the test of homogeneity, with degrees of freedom. In both cases the expected counts should be checked before relying on the chi-square approximation.
Design (c) fits neither test. Each student appears in both rows, so the rows are not independent samples and the 120 answers are not 120 independent units. Paired binary data need a method built on the pairs, such as McNemar’s test, which uses the students whose answers changed.
- Independent random samples of 50 students at two schools record the usual way of getting to school. At school 1, 21 students walk or bike, 18 take the bus and 11 come by car. At school 2 the counts are 9, 22 and 19. Find the expected counts, the chi-square statistic, the degrees of freedom and the approximate p-value. Which cells contribute most, and what is Cramér’s V?
Show the answer
Both row totals are 50, the column totals are 30, 40 and 30, and the grand total is 100. Each row therefore has expected counts , and 15. In each row the contributions are for walking or biking, for the bus and for the car, so
There are degrees of freedom, and for 2 degrees of freedom the upper tail is exactly , so the p-value is . At the 0.05 level this is evidence that the distributions of travel mode differ between the two school populations. Every expected count is at least 15, so the approximation is reasonable.
The two walk or bike cells contribute most, 4.8 of the 7.33, with residuals at school 1 and at school 2. School 1 has 42 percent of its sample walking or biking against 18 percent at school 2. Cramér’s V is . This describes a difference between the populations, not its cause, since the schools may differ in other ways, such as how far students live from them.