Sb16, SH1, SH2 - Testing contingency tables for association

Sb16, SH1, SH2 - Testing contingency tables for association

A contingency table records two categorical variables for the same individuals. You will calculate the counts expected under independence, carry out a chi-squared test for association, and interpret both the overall result and the individual contributions.

Read counts without confusing them with proportions

Could a test reach a different verdict after collecting ten times as much data, even if every percentage stayed the same? The chi-squared test measures how convincing a departure from independence is, so both the pattern and the sample size matter.

You already know

A hypothesis test compares sample evidence with a population null hypothesis. A small pp-value can provide evidence against that hypothesis; failure to reject it does not prove it true. Random sampling provides a probability basis for population inference.

Start by asking what one observation represents. Here each randomly selected pupil supplies one pair: school site and usual main travel method. Each pupil appears in exactly one cell.

School siteBusOtherTotal
North1212882020
South323248488080
Total44445656100100

More South pupils use the bus in this sample, but the South group is also larger. The bus proportions are 12/20=0.6012/20=0.60 at North and 32/80=0.4032/80=0.40 at South. Raw counts and within-group proportions answer different questions.

Marginal totals can also recover missing observed cells: the North Other count is 20−12=820-12=8. Use the matching row or column total, keeping every observation counted once.

Contingency table

A contingency table records frequencies classified by two categorical variables. Each cell counts observations in one row category and one column category. Row totals and column totals are called marginal totals.

Association means that the distribution of one variable depends on the category of the other. Independence means that it does not.

Categories need not have a meaningful numerical order: a payment method cannot sensibly be averaged. Numerical measurements can also be put into categories, such as journey times below or above a threshold, but this loses the exact times. An association test then concerns the categories, not every detail of the original measurements.

Key idea

Compare relevant proportions to describe a table, then use a test to judge whether the sample provides evidence of association in the population. A visible difference alone is not a completed inference.

No assumption that the categorical observations are Normally distributed is needed. The chi-squared reference distribution belongs to the test statistic under the null hypothesis, not to the category labels.

Build the table expected under independence

Under independence, the same column proportions apply in every row. If half the observations belong to one row, that row should contain half of each column total in the expected table.

Expected frequency under independence

For a cell in a contingency table,

E=(row total)(column total)NE=\frac{(\text{row total})(\text{column total})}{N}

where NN is the grand total. This is the expected frequency under the null hypothesis of independence.

Not in the formula booklet: learn it.

For example, multiplying a row total by the overall column proportion gives the same expression. Expected frequencies may be fractional, even though actual observed counts are integers.

Chi-squared test statistic

For observed frequencies OO and corresponding positive expected frequencies EE,

X2=∑cells(O−E)2EX^2=\sum_{\text{cells}}\frac{(O-E)^2}{E}

Each summand is a cell's contribution. Sum over the interior cells, not the marginal totals.

In the formula booklet.

The square stops positive and negative deviations cancelling. Dividing by EE judges a deviation relative to its expected count. Use unrounded expected values in calculations, even if a displayed table is rounded.

For the usual approximation used here, expected frequencies should be at least 55. Check expected counts, not observed counts, and use any grouping specified in the question. Also check that the sampling model is appropriate: one observation per individual and independent observations, rather than repeated replies treated as different people.

Worked example 1: expectations and contributions

Question. A random sample of pupils gives the following frequencies for site and main travel method. Calculate the expected table under independence and X2X^2.

SiteBusWalkCycleTotal
East1818121210104040
West1212181810104040
Total3030303020208080

Plan. Use each row and column total for EE, then add the six contributions.

EEast,Bus=40(30)80=15,EEast,Cycle=40(20)80=10E_{\mathrm{East,Bus}}=\frac{40(30)}{80}=15,\qquad E_{\mathrm{East,Cycle}}=\frac{40(20)}{80}=10

Both expected rows are (15,15,10)(15,15,10). Thus

X2=2((18−15)215+(12−15)215+(10−10)210)=2(0.6+0.6+0)=2.4\begin{aligned} X^2&=2\left(\frac{(18-15)^2}{15} +\frac{(12-15)^2}{15} +\frac{(10-10)^2}{10}\right)\\ &=2(0.6+0.6+0)=2.4 \end{aligned}

Why this step? The observed table has different bus and walking proportions, but the expected table imposes the same proportions at both sites.

Answer. Both expected rows are (15,15,10)(15,15,10), and X2=2.4X^2=2.4.

Check. Expected row totals are 4040, expected column totals are 30,30,2030,30,20, and all expected frequencies exceed 55.

Change only the allocation of bus and walking counts between the sites, keeping every marginal total fixed. The expected table should stay unchanged.

Worked example 2: a stronger departure with the same margins

Question. Find the expected table and X2X^2 when the observed East row is (24,6,10)(24,6,10) and West row is (6,24,10)(6,24,10), with column order Bus, Walk, Cycle.

Plan. Retain the expectations determined by the margins and recalculate the contributions.

EBus=40(30)80=15,EWalk=15,ECycle=10X2=2(9215+(−9)215+0)=2(5.4+5.4)=21.6\begin{aligned} E_{\mathrm{Bus}}&=\frac{40(30)}{80}=15,\qquad E_{\mathrm{Walk}}=15,\qquad E_{\mathrm{Cycle}}=10\\ X^2&=2\left(\frac{9^2}{15}+\frac{(-9)^2}{15}+0\right)\\ &=2(5.4+5.4)=21.6 \end{aligned}

Why this step? Expected counts depend on the marginal totals, so changing interior counts alone does not necessarily change the expectations.

Answer. Both expected rows remain (15,15,10)(15,15,10); X2=21.6X^2=21.6.

Check. Each non-zero deviation is three times its size in example 1, giving nine times the contribution. Indeed 21.6=9(2.4)21.6=9(2.4).

The marginal totals are a useful built-in check. They must agree in the observed and expected tables.

Watch out

Wrong: round expected counts to whole people, or include row and column totals as extra cells in X2X^2.

Right: retain fractional expectations and sum contributions over exactly the classified observations' cells.

The next table changes those same interior allocations once more.

Turn the statistic into a population conclusion

Large discrepancies count against independence. State the hypotheses about the population factors, calculate the reference degrees of freedom, and use the upper tail of the chi-squared distribution.

Degrees of freedom for association

For an rr by cc table, excluding marginal totals,

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

Under the null hypothesis, X2X^2 has approximately the distribution χν2\chi^2_\nu when the test's conditions hold.

Not in the formula booklet: learn it.

Once the margins are fixed, the first r−1r-1 rows and c−1c-1 columns determine the remaining entries, explaining the degrees-of-freedom count. The booklet supplies the chi-squared percentage-point table. Its 5%5\% column gives an upper-tail area of 0.050.05; do not split the level between two tails.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account