1 1 Lecture 4 Chi-Square Test 2 This lecture covers ? C rosstabulation ? C hi-Square Test 2 3 Crosstabulation ? C rosstabulations are also called contingency tables or two-way frequency tables. In their simplest form they are the count of the categories of one variable for each category of another variable. ? F or example, we might like to examine a crosstabulation o f age of woman with whether they have ever had a child. 4 3 56 ? W e call this a table because it has 7 rows and 2 columns. A table with r rows and c columns is an table . The table shows the relationship between two catego rical va riables . The explanatory variable is the treatment (the drugs). The respon se variabl e is su ccess (no relapse ) or failure (relapse). The two-way table gives the counts for all 6 combinations of values of these variables. Each of the count s o ccu pies a cell of the table. rc × 72 × 4 7 ? W e can also calculate row or column percentages. The following table shows column percentages. It presents the percentage age distribution for each category of WCEB. We can see straight away that the NO category is younger than the YES catego ry. 8 5-year age group * whether have any child Crosstabulation % withi n whether have any chi l d 51. 9% .0 % 10. 3% 36. 0% 7. 3% 13. 0% 7. 9% 22. 1% 19. 3% 1. 5% 22. 4% 18. 3% 1. 3% 14. 4% 11. 8% .6 % 18. 2% 14. 7% .9 % 15. 5% 12. 6% 100. 0% 100. 0% 100. 0% 15-1920-2425-2930-3435-3940-4445-49 5-yearagegroupTotal no yes whether have any child Total 5 9 ? W hen creating crosstabulations it is standard practice to use the dependent variable as the rows and the independent variable as the columns. ? W e can create a crosstabulation w ith three variables. For example, we may want to see the age distribution for WCEB for urban and rural area separately. This is shown in the following table. 10 Rural and urban age distribution for WCEB 5 - ye a r a g e gr oup * whe t he r ha ve a n y child * pla c e of r e side nce C r o sst abulat io n % wit h in wh et h e r h a v e an y ch ild 57. 3% .1 % 10. 7% 33. 9% 8. 4% 13. 2% 5. 0% 22. 9% 19. 6% 1. 4% 22. 8% 18. 8% 1. 4% 12. 9% 10. 8% .3 % 17. 9% 14. 6% .8 % 15. 0% 12. 3% 100. 0% 100. 0% 100. 0% 37. 4% 8. 9% 41. 5% 3. 3% 12. 4% 15. 6% 19. 3% 18. 4% 1. 9% 21. 1% 16. 5% 1. 1% 19. 9% 15. 4% 1. 5% 19. 1% 14. 9% 1. 1% 17. 3% 13. 5% 100. 0% 100. 0% 100. 0% 15- 19 20- 24 25- 29 30- 34 35- 39 40- 44 45- 49 5- ye ar agegrou p To tal 15- 19 20- 24 25- 29 30- 34 35- 39 40- 44 45- 49 5- ye ar agegrou p To tal place of res iden c e ruralurb a n no yes w h e t he r have any chi ld To tal 5- year ag e g r o u p * whet her hav e any child * p l ace o f resid e nce C r o sst abulat io n % wi thi n 5-year age gro u p 99. 5% .5 % 100. 0% 48. 0% 52. 0% 100. 0% 4. 8% 95. 2% 100. 0% 1. 4% 98. 6% 100. 0% 2. 4% 97. 6% 100. 0% .4 % 99. 6% 100. 0% 1. 3% 98. 7% 100. 0% 18. 7% 81. 3% 100. 0% 100. 0% 100. 0% 79. 4% 20. 6% 100. 0% 20. 1% 79. 9% 100. 0% 2. 7% 97. 3% 100. 0% 1. 7% 98. 3% 100. 0% 2. 4% 97. 6% 100. 0% 2. 0% 98. 0% 100. 0% 23. 8% 76. 2% 100. 0% 15- 19 20- 24 25- 29 30- 34 35- 39 40- 44 45- 49 5- ye ar agegrou p To tal 15- 19 20- 24 25- 29 30- 34 35- 39 40- 44 45- 49 5- ye ar agegrou p To tal place of res iden c e ruralurb a n no yes wh et h e r h a v e an y chi ld To tal 6 11 ? T he question is: Is there a significant relationship between woman’s age and having ever had a child?5 - y e ar ag e g r o u p 45- 49 40- 4 4 35 - 3 9 30- 34 25- 2 9 20 - 2 4 15- 19 M e a n w h e t he r ha v e a n y c h i l d 1. 2 1. 0.8.6.4.2 0. 0 ag e 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 M e an w h e t h e r h a v e an y c h i l d 1. 2 1. 0.8.6.4.2 0. 0 Please examine this question by your own after class. I will discuss some other examples. 12 Example 1: Treating cocaine addiction ? T his is a three-year study on medication to help cocaine addicts stay off cocaine: D, L, and P. Each treatment was randomly assigned with 24 subjects. The counts and proportions who avoided relapse into cocaine use during the study: 7 13 0.167 4 24 P 3 0.250 6 24 L 2 0.583 14 24 D 1 Proportion No relaps e Subjects Treatment Group 14 ? T he sample proportions of subjects who stayed off cocaine are quite different. Are these data good evidence that the proportions of successes for the three treatments differ in the population of all cocaine addicts? Does su cce ss di ffer signi ficantly between the treatments?Is there a significant relationship between treatment and success? 8 15 Here is the two-way table of the cocaine addiction data: 16 ? W e want to test the null hypothesis that there are no differences among the proportions of success es for addicts given the three treatments: ? T he alternative hypothesis is that there is some difference, that not all three proportions are equal: 01 2 3 : Hp p p == 11 2 3 : not a l l of , , a n d a r e e qua l Hp p p 9 17 ? T o test , we compare the observed counts in a two-way table with the expected counts, the counts we would expect if were true. If the observed counts are far from the expected counts, that is evidence against . 0 H 0 H 0 H 18 Expected counts ? T he expected count in any cell of a two- way table when is true is 0 H r o w tota l c ol um n tota l e xpe c t e d c ount ta bl e tota l × = 10 19 ? I n more formal language, if we have n independent tries and the probability of a success on each try is p, we expect npsuccesses. If we draw an SRS of n individuals from a population in which the proportion of successes is p, we expect np succe sses in the sample. That’s t h e fact behind the formula for expected counts in a two-way table. 20 ? L et’s ap ply this f a ct to th e co caine stud y. The two-way table with row and column totals is 11 21 ? W e will find the expected count for the cell in row 1 and column 1. The proportion of all 72 subjects who succeed in avoiding a relapse is count of suc c e sses c o l u m n 1 total 2 4 1 ta bl e tota l t a b l e tota l 7 2 3 == = Think of this as p, the overall proportion of successes. If is true, we expect this same proportion of successes in all three groups. 0 H 22 ? S o the expected count of successes among the 24 subjects who took D is 1 24 8 3 np =× = This expected count has the form: row 1 total c ol um n 1 total 2 4 2 4 ta bl e tota l 7 2 ×× = 12 23 Observed versus expected counts 24 ? B ecause 1/3 of all subjects succeed, we expect 1/3 of the 24 subjects in each group to avoid a relapse if there are no differences among the treatments. In fact, D has more successes (14) and fewer failures (10) than expected. The P has fewer su cce sse s (4) and more rel a pse s (20). D does much better than P, with L in between. 13 25 The Chi-Square Test ? T he statistical test that tells us whether those differences are statistically significant compares the observed and expe cte d count s . The te st st atist i c that makes the comparison is the chi-square statistic. 26 Chi-square statistic ? T he chi-square statistic is a measure of how far the observed counts in a two-way table are from the expected counts. The formula for the statistic is 2 2 (obser v e d count e xpected count) expe cted count χ ? =∑ 14 27 ? T he chi-square statistic is a sum of terms, one for each cell in the table. In the cocaine example, 14 of the D group succeeded in avoiding a relapse. The expected count for this cell is 8. So the component of the chi-square statistic from this cell is 2 2 (obse r v e d c ount expe c t e d c ount) e xpec t e d count (1 4 8 ) 3 6 4.5 88 ? ? == = 28 ? T hink of the chi-square statistic as a measure of the distance of the observed counts from the expected counts. Like any distance , it is always zero or positive, and it is zero only when the observed counts are exactly equal to the expected counts. Large values of are evidence against because they say that the observed counts are far from what we would expect if were true. 2 χ 2 χ 0 H 0 H 15 29 The chi-square distribution ? T he chi-square distributions are a family of distributions that take onl y po sitive values and are skewed to the right. A specific chi-square distribution is specified by giving its degrees of freedom. ? T he chi-square test for a two-way table with r ro ws and c colum n s u s es critical values from the chi-square distribution with (r-1 )(c-1 ) degree s of fre e d om. Th e P-valu e is the area to the right of under the chi-square density curve. 2 χ 30 Figure 1 shows the density curves for three members of the chi-square family of distributions. 16 31 There are three major properties of a chi-square distribution? C hi-square is either 0 or positive, never negative. ? A chi-square distribution in not symmetrical. Its skewness is positive. As the number of degree s of freed o m incre a se s, ch i-squar e approaches a symmetric distribution. ? T here is a particular distribution for each degree of freedom. 32 Table E gives cri t ical values for chi- squa re distributions. Use Table E to find P-value for a chi- squa re test. 17 33 ? W e use the formula to calculate chi-square statistic: 2 2 22 2 22 2 (o bs erv e d cou n t e x p ect ed co un t ) ex p ect ed co u n t ( 1 4 8 ) ( 10 16) ( 6 8 ) 81 6 8 ( 1 8 16) ( 4 8 ) ( 2 0 16) 16 8 1 6 4.50 2.25 0.500 0 .25 2 .00 1 .00 10.50 χ ? =∑ ?? ? =+ + ?? ? ++ + =+ + + + + = 34 ? T he two-way table has 3 rows and 2 columns. That is, r=3, c=2. The chi-square statistic therefore has degrees of freedom (r-1)(c-1)=(3 -1 )(2-1)=(2)(1 )=2. ? L ook in the df=2 row of Table E. The chi- square statistic =10.5 falls between the 0.01 and 0.05 critical values. Remember that the chi-square test is always one-sided. So the P-value of =10.5 is between 0.01 and 0.05. The P-value is equal to 0.005 when rounded to three decimal places. 2 χ 2 χ 18 35 Using SPSS we can ea sily find the P-valu e. 36 ? If we want our significance level to be < 0.05, the critical value is 5.99. To reject the null hypothesis at the 0.05 level the value of chi-square needs to be greater than 5.99. If it were less than 5.99 the null hypothesis would be accepted. ? I n this example, the value of chi-square is 10.5, which is greater than 5.99, so we reject the null hypothesis in this case. We can conclude that there is a statistically significant difference in effects of the treatments (drugs). 19 37 Using Crosstabs in SPSS ? C alculating the expected counts and then the chi-square statistic by hand is a bit time-consuming. We can avoid this trouble by using SPSS’s crosstabs. But you need to arrange the data in the following format: 38 20 3940 21 41 Example 2: Abortion and educational level ? T he 1997 survey data: educational level * abortion C r osstabulation Count 229 765 994 374 942 1316 465 715 1180 223 191 414 61 48 109 1352 2661 4013 illiterateprimaryj unior high senior highc o llege+ e d uc ational levelTota l yes no abortion Tota l 42 22 43 ? O ne of the most useful properties of chi- square is that it tests the hypothesis “the row and column variables are not related to each other” i n a two-way table. ? I n this example, the null hypothesis: : there is no relationship between education and ever having had an abortion. 0 H 44 Using SPSS 23 4546 Chi-Square Tests 183.743 a 4 .000 180.879 4 .000 175.137 1 .000 4013 Pearson Chi-SquareLikelihood RatioLinear-by-LinearAssociationN of Valid Cases Value df Asymp. Sig. (2-sided) 0 cells (.0%) have expected count less than 5. Theminimum expected count is 36.72. a. 24 47 Uses of the chi-square test Use the chi-square test to test the null hypothesis : there is no relationship between two categorical variables when you have a two-way table from one of these situations: 0 H 48 ? A single SRS, with each individual classified according to both of two categorical variables. ? I ndependent SRSs from each of several populations, with each individual classified according to one categorical variable. (The other va riable sa ys which sample the individual come s from. ) 25 49 Cell counts required for the chi- square test ? Y ou can safely use the chi-square test with critical values from the chi-square distribution when no more than 20% of the expected counts are less than 5 and all individual expected counts are 1 or greater. In particular, all four expected counts in a table should be 5 or greater. 22 × 50 Example 3: SRB ? E ffect of place of residence on SRB. dummy for urban * PAR_OC Crosstabulation 3474 3055 6529 53.2% 46.8% 100.0% 613 526 1139 53.8% 46.2% 100.0% 4087 3581 7668 53.3% 46.7% 100.0% Count% wit h in dummy for u r ba n Count% wit h in dummy for u r ba n Count% wit h in dummy for u r ba n .001.00 d u mmy f o r urbanTotal mal e fe m a le PAR_OC Total 26 51 Chi-Square Tests .145 b 1 .703 .122 1 .727 .145 1 .703 .723 .364 .145 1 .703 7668 Pearson Chi-SquareContinuity Correction a Likelihood RatioFisher's Exa c t T e st L i ne ar-by-L i ne ar Associa t ion N of Valid Cases Value df Asymp. Sig. (2-sided) Exact Sig. (2-sided) Exact Sig. (1-sided) Computed only for a 2x2 table a. 0 cells (.0%) ha ve ex pect ed count less t h a n 5. T h e m i nim u m ex pect ed count is 531.92. b. 52 ? E ffect of parity on SRB: PAR_NUM * PAR_OC Cr osstabulation 1958 1815 3773 51.9% 48.1% 100.0% 1127 903 2030 55.5% 44.5% 100.0% 3085 2718 5803 53.2% 46.8% 100.0% Co unt % within P A R_NUM Co unt % within P A R_NUM Co unt % within P A R_NUM 12 PA R_N U M To t a l male fema le PA R_O C To t a l Chi-Square Tests 6.955 b 1 .008 6.810 1 .009 6.963 1 .008 .009 .005 6.953 1 .008 5803 P e a r so n Chi - Squa re Co nti nui ty Co rrec t i o n a Likelih ood Rat i o Fisher's Exact TestLin e ar -by-Lin e ar AssociationN of Valid Cases Valu e df Asymp. Sig. (2-sided) Ex act Sig. (2-sided) Ex act Sig. (1-sided) Comput ed only for a 2x2 t a ble a. 0 cells (.0%) h ave ex pect ed cou n t less t h an 5. T h e min i mu m ex pect ed cou n t is 950.81. b.