Unit 7, lesson 54

Chi-Square Independence and Homogeneity

How expected counts from the margins of a contingency table give the chi-square tests of independence and homogeneity, how Pearson residuals locate a discrepancy, and how Cramér's V measures its size.

  • Video 54
  • Chi-Square and Exact Tests

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 rr rows and cc columns counts units classified by two categorical variables. Let OijO_{ij} be the observed count in row ii and column jj, Oi+O_{i+} the total of row ii, O+jO_{+j} the total of column jj and nn the grand total. When every row and column total is positive, the expected count of the cell is

Eij=Oi+ O+jn.E_{ij} = \frac{O_{i+}\, O_{+j}}{n} .

It is the count the cell would have if every row had the column proportions O+j/nO_{+j}/n of the whole table, and it estimates the mean of OijO_{ij} 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.

NeighborhoodPublic transportOtherTotal
North302050
South203050
Total5050100

Every expected count is 50⋅50/100=2550 \cdot 50/100 = 25. If the column totals had been 80 and 20 instead, a row with total 50 would have expected counts 50⋅80/100=4050 \cdot 80/100 = 40 and 50⋅20/100=1050 \cdot 20/100 = 10.

Chi-square test of independence

Definition Suppose nn 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 GG with rr categories and a column variable YY with cc categories, so that it lands in exactly one cell. The test of independence has null hypothesis

H0 ⁣:P(G=i,Y=j)=P(G=i) P(Y=j)\begin{aligned} &H_0\colon P(G = i, Y = j) \\[0.4em] &\quad = P(G = i)\, P(Y = j) \end{aligned}

for every ii and jj, and alternative H1H_1 that the variables are associated. When every category of GG has positive probability, the null hypothesis says that the conditional distribution of YY given G=iG = i is the same for every ii. The statistic is

X2=∑i=1r∑j=1c(Oij−Eij)2Eij.X^2 = \sum_{i=1}^{r} \sum_{j=1}^{c} \frac{(O_{ij} - E_{ij})^2}{E_{ij}} .

If H0H_0 holds and the expected counts are large, X2X^2 has approximately the chi-square distribution with

ν=(r−1)(c−1)\nu = (r - 1)(c - 1)

degrees of freedom, and the p-value is the upper tail P(χν2≥x)P(\chi^2_{\nu} \ge x) at the observed value xx. The count (r−1)(c−1)(r - 1)(c - 1) 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 H0H_0 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 Oij−EijO_{ij} - E_{ij} is 55 or −5-5, so every cell contributes 25/25=125/25 = 1 and X2=4X^2 = 4. With ν=1\nu = 1 the p-value is P(χ12≥4)≈0.0455P(\chi^2_1 \ge 4) \approx 0.0455, 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 n1,…,nrn_1, \dots, n_r are drawn from rr populations, and each sampled unit falls into exactly one of cc categories. Let pijp_{ij} be the probability of category jj in population ii. The test of homogeneity has null hypothesis

H0 ⁣:p1j=p2j=⋯=prjH_0\colon p_{1j} = p_{2j} = \dots = p_{rj}

for every jj, so that all the populations share one category distribution, and alternative H1H_1 that at least two populations differ in at least one category. It uses the same expected counts EijE_{ij}, the same statistic X2X^2 and the same approximate chi-square reference with (r−1)(c−1)(r - 1)(c - 1) degrees of freedom as the test of independence. With two populations and two categories, X2X^2 equals the square of the pooled two-proportion zz 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 0.5⋅0.5⋅(1/50+1/50)=0.10\sqrt{0.5 \cdot 0.5 \cdot (1/50 + 1/50)} = 0.10, so z=0.20/0.10=2z = 0.20/0.10 = 2 and z2=4=X2z^2 = 4 = X^2, 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 ii and column jj is

rij=Oij−EijEij.r_{ij} = \frac{O_{ij} - E_{ij}}{\sqrt{E_{ij}}} .

Its square is the contribution of the cell to X2X^2, so the squared residuals add to X2X^2. A positive residual marks a cell with more units than the null hypothesis predicts, and a negative residual a cell with fewer. Under H0H_0 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 Oij−EijO_{ij} - E_{ij} by an estimate of its own standard error instead, and it is approximately standard normal under H0H_0 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 X2X^2. 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 5/25=15/\sqrt{25} = 1, and the other two cells have −1-1. Each cell contributes 12=11^2 = 1 to X2=4X^2 = 4. The signs show the pattern, more public transport in North and less in South than the null hypothesis predicts. The adjusted residuals here are 22 and −2-2.

Cramér’s V

Definition For a table with r≥2r \ge 2 rows and c≥2c \ge 2 columns and every row and column total positive, Cramér’s V is

V=X2nmin⁡(r−1,c−1).V = \sqrt{\frac{X^2}{n \min(r - 1, c - 1)}} .

It satisfies 0≤V≤10 \le V \le 1. It equals 00 exactly when X2=0X^2 = 0, that is, when every observed count equals its expected count, and it equals 11 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 X2X^2 by that factor and leaves VV unchanged.

In plain words Cramér’s V puts the strength of association on a scale from 0 to 1. Unlike X2X^2, 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, V=4/(100⋅1)=0.2V = \sqrt{4/(100 \cdot 1)} = 0.2. Doubling every count keeps the percentages at 60 and 40 and doubles X2X^2 to 8, which lowers the p-value to about 0.0047, but VV stays 0.2. More data give stronger evidence of the same association, not a larger association.

Check yourself

  1. 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 30⋅20/50=1230 \cdot 20/50 = 12 and 30⋅30/50=1830 \cdot 30/50 = 18 in the first row, and 20⋅20/50=820 \cdot 20/50 = 8 and 20⋅30/50=1220 \cdot 30/50 = 12 in the second. They equal the observed counts, so X2=0X^2 = 0 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.

  1. 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 (3−1)(4−1)=6(3 - 1)(4 - 1) = 6 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 (3−1)(3−1)=4(3 - 1)(3 - 1) = 4 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.

  1. 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 50⋅30/100=1550 \cdot 30/100 = 15, 50⋅40/100=2050 \cdot 40/100 = 20 and 15. In each row the contributions are 62/15=2.46^2/15 = 2.4 for walking or biking, 22/20=0.22^2/20 = 0.2 for the bus and 42/15≈1.074^2/15 \approx 1.07 for the car, so

X2=2(2.4+0.2+1615)=223≈7.33.\begin{aligned} X^2 &= 2\left(2.4 + 0.2 + \tfrac{16}{15}\right) \\[0.4em] &= \frac{22}{3} \approx 7.33 . \end{aligned}

There are (2−1)(3−1)=2(2 - 1)(3 - 1) = 2 degrees of freedom, and for 2 degrees of freedom the upper tail is exactly e−x/2e^{-x/2}, so the p-value is e−11/3≈0.026e^{-11/3} \approx 0.026. 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 6/15≈1.556/\sqrt{15} \approx 1.55 at school 1 and −1.55-1.55 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 (22/3)/(100⋅1)≈0.27\sqrt{(22/3)/(100 \cdot 1)} \approx 0.27. 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.

How this lesson connects