# Section 4.2 examples: ################################ # # Section 4.2 R examples # ################################ ##### Chi-square test of probabilities: # Test score data: test.score.data <- matrix(c(6,14,17,9,30,32,17,3), nrow=2, ncol=4, byrow=TRUE, dimnames = list(c("Private", "Public"), c("Low", "Marginal", "Good", "Excellent"))) # Printing the 2 by 4 table: as.table(test.score.data) # A quick way to get expected cell counts and verify large-sample assumption is met: expected.counts <- (apply(test.score.data,1,sum) %o% apply(test.score.data,2,sum))/sum(test.score.data) print(expected.counts) # using the chisq.test() function in R: chisq.test(test.score.data, correct=FALSE) ##### Chi-square test of independence: # Snoring/heart disease data: snore.heart.data <- matrix(c(24,35,51,1355,603,416), nrow=2, ncol=3, byrow=TRUE, dimnames = list(c("Yes", "No"), c("Never", "Occasionally", "~ Every Night"))) # Printing the 2 by 3 table: as.table(snore.heart.data) # A quick way to get expected cell counts and verify large-sample assumption is met: expected.counts <- (apply(snore.heart.data,1,sum) %o% apply(snore.heart.data,2,sum))/sum(snore.heart.data) print(expected.counts) # We test for independence of the two classifications # using the chisq.test() function in R: chisq.test(snore.heart.data, correct=FALSE) ##### Exact test on r-by-c table with fixed marginals: # 3-by-3 bank data: bank.3.data <- matrix(c(0,5,1,2,3,0,2,0,1), nrow=3, ncol=3, byrow=TRUE, dimnames = list(c("White", "Black", "Asian"), c("AcctRep", "Teller", "DataAnalyst"))) # Printing the 3 by 3 table: as.table(bank.3.data) # using the fisher.test() function in R: fisher.test(bank.3.data)