Mostly Harmless StatisticsXYZ Homework Edition

⇩ Download ▾

10.3 Test for Independence

Use the chi-square test for independence to assess association between two categorical variables. Count each sampled individual in a contingency-table cell. For example, a table may cross-classify a child’s ASD diagnosis and reported duration of breastfeeding. The null hypothesis states that the variables are independent; the alternative states that they are associated. Association concerns their joint distribution. This observational test alone does not establish that one variable causes the other.

Finding the test statistic involves several steps. First, the data is collected, counted, and then organized into a contingency table. These values are known as the observed frequencies, and the symbol for an observed frequency is OO. Total each row and column.

The null hypothesis is that the two variables are independent. If two events are independent then P(B)=P(B|A)P(B) = P(B | A) and we can use the multiplication rule for independent events, to calculate the probability that variable AA and BB as the P(AandB)=P(A)·P(B)P(A\, \text{and}\, B) = P(A) \cdot P(B). Remember in a hypothesis test, you assume that H0H_{0} is true, the two variables are assumed to be independent.

P ( A and B ) = P ( A ) · P ( B )  if  A  and  B  are independent =  Number of ways A can happen   Total number of individuals  ·  Number of ways B can happen   Total number of individuals  =  Row Total  n ·  Column Total  n \begin{aligned} P(A\, \text{and}\, B) &= P(A) \cdot P(B) \text{ if } A \text{ and } B \text{ are independent} \\[4pt] &=\dfrac{\text { Number of ways A can happen }}{\text { Total number of individuals }} \cdot \dfrac{\text { Number of ways B can happen }}{\text { Total number of individuals }} \\[4pt] &=\dfrac{\text { Row Total }}{n} \cdot \dfrac{\text { Column Total }}{n} \end{aligned}

General two-way table layout with Variable 1 columns B and Not B and Variable 2 rows A and Not A; the cells hold P(A and B), P(A and Not B), P(Not A and B), and P(Not A and Not B), with row totals for A and Not A, column totals for B and Not B, and grand total n.

Now you want to find out how many individuals you expect to be in a certain cell. To find the expected frequencies, you just need to multiply the probability of that cell times the total number of individuals. Do not round the expected frequencies.

Expected frequency (cell A and B) = E ( A  and  B ) = n (  Row Total  n ·  Column Total  n ) =  Row Total  ·  Column Total  n \begin{aligned} \text{Expected frequency (cell A and B)} &= E(A \text{ and } B) \\ &= n \left(\dfrac{\text { Row Total }}{n} \cdot \dfrac{\text { Column Total }}{n}\right) = \dfrac{\text { Row Total } \cdot \text { Column Total }}{n} \end{aligned}

If the variables are independent, the observed frequencies should be reasonably close to the expected frequencies, allowing for random sampling variation.

The statistic sums (O−E)2/E(O-E)^2/E over the cells. Larger values indicate stronger departures from the fitted counts under independence. The right-tail p-value measures how unusual this statistic or a larger one would be under the null model. A small p-value is evidence against independence; it does not prove dependence with certainty or establish causation.

How do you find the test statistic? First, compute the differences between the observed and expected frequencies. Because some of these differences will be positive and some will be negative, you need to square these differences. These squares could be large just because the frequencies are large, so you need to divide by the expected frequencies to scale them. Then finally add up all of these fractional values. This sum is Pearson’s chi-square statistic, rather than a sample variance. Under the null model and the count assumptions, use a chi-square reference distribution to find the critical value or p-value. Hence, sometimes this test is called a chi-square test.

It will be helpful to make a table for the expected counts and another one for each of the (O−E)2E\frac{(O-E)^{2}}{E} values to aid in computing the test statistic.

Three stacked tables for the ASD and breastfeeding test: the observed counts; the expected counts computed as row total times column total over 934, giving 228.585, 195.304, 167.278, 226.833 for Yes and 32.415, 27.696, 23.722, 32.167 for No; and the (O−E)²/E values 0.6743, 0.0372, 0.0642, 0.6173 and 4.7552, 0.2624, 0.4531, 4.3529, which sum to the test statistic 11.217.

The test statistic is the sum of all eight (O−E)2E\frac{(O-E)^{2}}{E} values: χ2=∑(O−E)2E=11.217\chi^{2}=\sum \frac{(O-E)^{2}}{E} = 11.217.

The critical value for a right-tailed χ2\chi^{2}-test with degrees of freedom df=(R−1)(C−1)=(2−1)(4−1)=3\text{df} = (R-1)(C-1) = (2-1)(4-1) = 3 is found using a χ2\chi^{2} -distribution α=0.01\alpha=0.01 right-tail area. The critical value is χ2\chi^{2} = CHISQ.INV.RT(0.01,3) = 11.3449. See Figure 10-5.

Right-skewed chi-square curve with the critical value 11.3449 marked in green on the axis and the test statistic 11.217 marked in blue just to its left, outside the small shaded rejection region in the right tail.
Figure 10-5: Values of χ2\chi^{2} and χα2\chi_{\alpha}^{2} on a χ2\chi^{2}-test with df=3\text{df}=3 and α=0.01\alpha=0.01.

Alternatively, use the online calculator: https://homepage.divms.uiowa.edu/~mbognar/applets/chisq.html.

Online chi-square distribution calculator titled Chi-Square Distribution with inputs df = 3, x = 11.34487, and P(X > x) = .01, above a plotted right-skewed chi-square curve on an axis from 0 to 12.

Since the test statistic χ2=11.217\chi^{2} = 11.217 is not in the rejection area, our decision is to fail to reject H0H_{0}.

There is not enough evidence to show a relationship between autism spectrum disorder and breastfeeding.

TI-84 calculator screen showing a χ²cdf command with lower bound 11.217 returning .0106085228.

If we were asked to find the p-value, you would just find the area to right of the test statistic (always a right-tailed test) using your calculator or Excel. This gives a p-value = 0.0106, which is more than α=0.01\alpha=0.01; therefore, we do not reject H0.

You can also use the χ2\chi^{2}-Test shortcut keys on your calculator to get a p-value, see directions below.

TI-84: Press the [2nd] then [MATRX] key. Arrow over to the EDIT menu and 1:[A] should be highlighted, press the [ENTER] key. For a m×nm \times n contingency table, type in the number of rows (m)(m) and the number of columns (n)(n) at the top of the screen so that it looks like this: MATRIX[A] m×nm \times n. For a 2×42 \times 4 contingency table, the top of the screen would look like this: MATRIX[A] 2×42 \times 4. As you hit [ENTER], the table will automatically widen to the size you put in. Now enter all of the observed values in their proper positions. Then press the [STAT] key, arrow over to the [TESTS] menu, arrow down to the option [C: χ2\chi^{2} -Test] and press the [ENTER] key. Leave the default as Observed:[A] and Expected:[B], arrow down to [Calculate] and press the [ENTER] key.

The calculator returns the χ2\chi^{2}-test statistic and the p-value.

If you go back to the matrix menu [2nd] then [MATRX] key, arrow over to EDIT and choose 2:[B], you will see all of the expected values.

Five TI-84 screens for the matrix χ²-Test: the matrix EDIT menu, matrix [A] entered as a 2 by 4 table of observed counts ending with cell 2,4 = 44, the TESTS menu with C:χ²-Test selected, the inputs Observed:[A] and Expected:[B], and the output χ²=11.21668801, p=.0106100514, df=3.

TI-89: First you need to create the matrix for the observed values: Press [Home] to return to the Home screen, press [Apps] and select Data/Matrix Editor. A menu is displayed, select 3:New. The New dialog box is displayed. Press the right arrow key to highlight 2:Matrix, and press [ENTER] to choose Matrix type. Press the down arrow key to highlight 1:Main, and press [ENTER], to choose main folder. Press the down arrow key, and then enter the letter oo for the name in the Variable field. Enter 2 for Row dimension and 4 for Column dimension. Press [ENTER] to display the matrix editor. Enter the observed value (do not include total row or column). Important: Next time you use this test instead of option 3:New, choose 2: Open. The open dialog box is displayed. Press the right arrow key to highlight 2:Matrix, and press [ENTER] to choose Matrix type. Press the down arrow key to make sure you are in the Main folder and that your variable says oo. Press [Apps], and then select Stats/List Editor. To display the Chi-square 2-Way dialog box, press 2nd then F6 [Tests], then select 8: Chi-2 2-way. Enter in in the Observed Mat: o; set Store Expected to: statvars\expmat; Store CompMat to: statvars\compmat. This will store the expected values in the matrix folder statvars with the name expmat, and the (o−e)2/e(o-e)^{2}/e values in the matrix compmat. Press the [ENTER] key to calculate. The calculator returns the χ2\chi^{2}-test statistic and the p-value. If you go back to the matrix menu, you will see some of the expected and (o−e)2/e(o-e)^{2}/e values.

Sequence of TI-89 screens for the two-way chi-square test: creating a new 2 by 4 matrix named o in the Data/Matrix Editor, entering the observed counts, then in the Stats/List Editor choosing Tests and Chi2 2-way with Observed Mat o and stored expmat and compmat matrices, ending with results Chi-2 = 11.2167, P Value = .01061, df = 3.

To see all the expected values, select [APPS] and select Data/Matrix Editor. Select 2:Open, change the Type to Matrix, change the Folder to statvars, and change the Variable to expmat.

Four TI-89 screens opening the stored expected matrix: the Apps screen with Data/Matrix Editor selected, the 2:Open option, the Open dialog with Type Matrix, Folder statvars, and Variable expmat, and the matrix of expected values with row 1 starting 228.58, 195.3, 167.28 and row 2 starting 32.415, 27.696, 23.722.

To see all the (o−e)2/e(o-e)^{2}/e values, select [APPS] and select Data/Matrix Editor. Select 2:Open, change the Type to Matrix, change the Folder to statvars, and change the Variable to compmat.

Four TI-89 screens opening the stored comp matrix: the Apps screen with Data/Matrix Editor selected, the 2:Open option, the Open dialog with Folder statvars and Variable compmat, and the matrix of (O−E)²/E values with row 1 starting .67434, .03721, .06425 and row 2 starting 4.7552, .26242, .45308.

If you need to delete a row or column, move the cursor to the row or column that you want to delete, then select F6 Util, then 2:Delete, then choose row or column, then enter. To add a row or column, just arrow over to the new row or column and type in the observed values.

Two TI-89 Data/Matrix Editor screens of the Util menu, with options including 1:Insert, 2:Delete, Sort Column, and 6:Resize Matrix; the Delete submenu offers choices including row and column.

Adapted from Mostly Harmless Statistics by Rachel Webb (Portland State University, https://mostlyharmlessstat.wixsite.com/webpage), © Rachel Webb, licensed under CC BY-SA 4.0. Changes were made. License: CC-BY-SA-4.0.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or any other accessibility standard. Every page is built against an automated accessibility gate, and our target for the published editions is WCAG 2.2 Level AA. If something is unusable, please tell us.