# Math 322 Biostatistics # Lecture, March 18, 2009 # R examples for Chapter 10.1-2 (2x2 Contingency tables) # Geir Arne Hjelle (hjelle@math.wustl.edu) # Consider the example from the lecture looking at the relationship # between gender and handedness # # | Boys Girls | Total # --------------+---------------------+--------- # Left-handed | 6 12 | 18 # Right-handed | 28 24 | 52 # --------------+---------------------+--------- # Total | 34 36 | 70 # # To use R to test for whether the proportion of left-handed boys is # equal to the proportion of left-handed girls, we can use chisq.test(). # # First construct a 2x2 matrix with the observed data observed <- matrix( c(6, 28, 12, 24), ncol = 2 ) # Then test the data chisq.test(observed) # We can also perform the test without the continuity correction # (see the paragraph at the bottom of page 398 and top of page 399 # in the book for a discussion about the Yates correction). chisq.test(observed, correct = FALSE) # If the data are given to us in a "full format", we can either use table # to make a summary, or use chisq.test() directly on the data. # # Assume we have two paired vectors with all 70 observations above. gender <- rep(c("Boy", "Girl"), c(34, 36)) handedness <- rep(c("Left", "Right", "Left", "Right"), c(6, 28, 12, 24)) data <- data.frame(gender, handedness) # Using table to make a frequency matrix table(data) chisq.test(table(data)) # Sending the vectors directly to chisq.test() chisq.test(gender, handedness) # As a final remark, note that because of the symmetry of the problem and # of the calculations, it does not matter which vector we specify first, # and which we specify second. chisq.test(handedness, gender) # Similarly, we can transpose the 2x2 contingency table (matrix) and # still get the same results t(observed) chisq.test(t(observed))