1 / 44

Chi-Square Tests 3/14/12

Chi-Square Tests 3/14/12. Testing the distribution of a single categorical variable :  2 goodness of fit Testing for an association between two categorical variables:  2 test for association. Section 7.1, 7.2. Professor Kari Lock Morgan Duke University.

sugar
Download Presentation

Chi-Square Tests 3/14/12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chi-Square Tests • 3/14/12 • Testing the distribution of a single categorical variable : 2 goodness of fit • Testing for an association between two categorical variables: 2 test for association • Section 7.1, 7.2 • Professor Kari Lock Morgan • Duke University

  2. For more information and to sign up, go to www.stat.duke.edu/datafest

  3. Office Hours This Week • NOTE: This week only, my Friday office hours will be from 3:30 – 5:30 pm (NOT 1-3pm as usual)

  4. To Do • Homework 6 (due Monday, 3/19) • Anonymous Midterm Evaluation (due Monday, 3/19, 5pm) • Project 1 (due Thursday, 3/22, 5pm)

  5. Multiple Categories • So far, we’ve learned how to do inference for categorical variables with only two categories • Today, we’ll learn how to do hypothesis tests for categorical variables with multiple categories

  6. Rock-Paper-Scissors (Roshambo) • Play a game! Can we use statistics to help us win?

  7. Rock-Paper-Scissors Which did you throw? • Rock • Paper • Scissors

  8. Rock-Paper-Scissors How would we test whether all of these categories are equally likely?

  9. Hypothesis Testing • State Hypotheses • Calculate a statistic, based on your sample data • Create a distribution of this statistic, as it would be observed if the null hypothesis were true • Measure how extreme your test statistic from (2) is, as compared to the distribution generated in (3) test statistic

  10. Hypotheses Let pidenote the proportion in the ithcategory. • H0 : All pis are the same • Ha : At least one pidiffers from the others • OR • H0 : Every pi= 1/3 • Ha : At least one pi≠ 1/3

  11. Test Statistic Why can’t we use the familiar formula to get the test statistic? • More than one sample statistic • More than one null value • We need something a bit more complicated…

  12. Observed Counts • The observed countsare the actual counts observed in the study

  13. Expected Counts • The expected countsare the expected counts if the null hypothesis were true • For each cell, the expected count is the sample size (n) times the null proportion, pi

  14. Chi-Square Statistic • A test statistic is one number, computed from the data, which we can use to assess the null hypothesis • The chi-square statisticis a test statistic for categorical variables:

  15. Rock-Paper-Scissors

  16. What Next? • We have a test statistic. What else do we need to perform the hypothesis test? • A distribution of the test statistic assuming H0 is true • How do we get this? Two options: • Simulation • Distributional Theory

  17. Simulation • Let’s see what kind of statistics we would get if rock, paper, and scissors are all equally likely. • 1) Take 3 scraps of paper and label them Rock, Paper, Scissors. Fold or crumple them so they are indistinguishable. Choose one at random. • 2) What did you get? • (a) Rock (b) Paper (c) Scissors • 3) Calculate the 2 statistic. • 4) Form a distribution. • 5) How extreme is the actual test statistic?

  18. RStudio Chi-Square Distribution

  19. Chi-Square Distribution • If each of the expected counts are at least 5, AND if the null hypothesis is true, then the 2 statistic follows a 2 –distribution, with degrees of freedom equal to • df = number of categories – 1 • Rock-Paper-Scissors: • df = 3 – 1 = 2

  20. Chi-Square Distribution

  21. Chi-Square p-values • Applet: • http://surfstat.anu.edu.au/surfstat-home/tables/chi.php • RStudio: • pchisq(ts,df,lower.tail=FALSE) • TI-83: • 2nd DISTR  7:2cdf(  • lower bound, upper bound, df • Because the 2 is always positive, the p-value (the area as extreme or more extreme) is always the upper tail

  22. t-distribution p-values and t* • Applet: • http://surfstat.anu.edu.au/surfstat-home/tables/t.php • RStudio: • p-value: pt(ts,df) (use lower.tail=TRUE or FALSE, and double if two-tailed) • t* for 95% CI: qt(0.975,df) • TI-83: • p-value: 2nd DISTR  5: tcdf(  • lower bound, upper bound, df • t*: Approximate with a normal (invnorm)

  23. Goodness of Fit • A 2 test for goodness of fit tests whether the distribution of a categorical variable is the same as some null hypothesized distribution • The null hypothesized proportions for each category do not have to be the same

  24. Chi-Square Test for Goodness of Fit • State null hypothesized proportions for each category, pi. Alternative is that at least one of the proportions is different than specified in the null. • Calculate the expected counts for each cell as npi. Make sure they are all greater than 5 to proceed. • Calculate the 2 statistic: • Compute the p-value as the area in the tail above the 2 statistic, for a 2 distribution with df = (# of categories – 1) • Interpret the p-value in context.

  25. Mendel’s Pea Experiment • In 1866, Gregor Mendel, the “father of genetics” published the results of his experiments on peas • He found that his experimental distribution of peas closely matched the theoretical distribution predicted by his theory of genetics (involving alleles, and dominant and recessive genes) Source: Mendel, Gregor. (1866). Versuche über Pflanzen-Hybriden. Verh. Naturforsch. Ver. Brünn 4: 3–47 (in English in 1901, Experiments in Plant Hybridization, J. R. Hortic. Soc. 26: 1–32)

  26. Mendel’s Pea Experiment • Mate SSYY with ssyy: • 1st Generation: all Ss Yy • Mate 1st Generation: •  2nd Generation •  Second Generation S, Y: Dominant s, y: Recessive

  27. Mendel’s Pea Experiment Let’s test this data against the null hypothesis of each piequal to the theoretical value, based on genetics

  28. Mendel’s Pea Experiment The 2 statistic is made up of 4 components (because there are 4 categories). What is the contribution of the first cell (315)? (a) 0.016 (b) 1.05 (c) 5.21 (d) 107.2

  29. Mendel’s Pea Experiment

  30. Mendel’s Pea Experiment • Compare to a 2 distribution with 4 – 1 = 3 degrees of freedom • http://surfstat.anu.edu.au/surfstat-home/tables/chi.php p-value = 0.9254

  31. Mendel’s Pea Experiment p-value = 0.9254 Does this prove Mendel’s theory of genetics? Or at least prove that his theoretical proportions for pea phenotypes were correct? (a) Yes (b) No

  32. Two Categorical Variables • The statistics behind a 2 test easily extends to two categorical variables • A 2 test for association (often called a 2 test for independence) tests for an association between two categorical variables

  33. Rock-Paper-Scissors • Did reading the example on Rock-Paper-Scissors in the textbook influence the way you play the game? • Did you read the Rock-Paper-Scissors example in Section 6.3 of the textbook? • Yes • No

  34. Rock-Paper-Scissors H0 : Whether or not a person read the example in the textbook is not associated with how a person plays rock-paper-scissors Ha : Whether or not a person read the example in the textbook is associated with how a person plays rock-paper-scissors

  35. Expected Counts Maintains row and column totals, but redistributes the counts as if there were no association between the variables

  36. Chi-Square

  37. Chi-Square Statistic

  38. Rock-Paper-Scissors • The smallest expected count is 5.96, which is greater than 5, so it is okay to use the chi-square distribution to find the p-value. p-value = 0.0005 We have strong evidence that there is an association between reading the RPS example in the textbook and the first RPS throw. (This means you are paying attention and using what you learn in the textbook! )

  39. Chi-Square Test for Association • H0 : The two variables are not associated • Ha : The two variables are associated • Calculate the expected counts for each cell: • Make sure they are all greater than 5 to proceed. • Calculate the 2 statistic: • Compute the p-value as the area in the tail above the 2 statistic, for a 2 distribution with df = (r – 1)  (c – 1) • Interpret the p-value in context.

  40. Metal Tags and Penguins Let’s return to the question of whether metal tags are detrimental to penguins. Source: Saraux, et. al. (2011). “Reliability of flipper-banded penguins as indicators of climate change,” Nature, 469, 203-206. Is there an association between type of tag and survival? (a) Yes (b) No

  41. Metal Tags and Penguins • Calculate expected counts (shown in parentheses). First cell: • Calculate the chi-square statistic: • p-value: p-value = 0.0007 df = (2 – 1) × (2 – 1) = 1 There is strong evidence that metal tags are detrimental to penguins.

  42. Metal Tags and Penguins Difference in proportions test: z = -3.387 (we got something slightly different due to rounding) p-value = 0.0007 (two-sided) Chi-Square test: 2 = 11.47 p-value = 0.0007 Note: (-3.387)2 = 11.47

  43. Two Categorical Variables with Two Categories Each • When testing two categorical variables with two categories each, the 2 statistic is exactly the square of the z-statistic • The 2 distribution with 1 df is exactly the square of the normal distribution • Doing a two-sided test for difference in proportions or doing a 2 test will produce the exact same p-value.

  44. Summary • The 2 test for goodness of fit tests whether one categorical variable differs from a hypothesized distribution • The 2 test for association tests whether two categorical variables are associated • For both, you calculate expected counts for each cell, compute the 2 statistic as • and find the p-value as the area above this statistic on a 2 distribution

More Related