## 1 die <- c(87,96,108,89,122,98) probs.null <- c(1/6,1/6,1/6,1/6,1/6,1/6) expec.counts <- sum(die)*probs.null; print (expec.counts) chisq.test(die, p=probs.null) ############################################### Chi-squared test for given probabilities data: die X-squared = 8.58, df = 5, p-value = 0.127 ############################################### # 2 Oj <- c(0,6,8,12) expect.counts <- c(26/4,26/4,26/4,26/4) # Test statistic: test.stat <- sum(Oj^2 / expect.counts) - sum(Oj); print(test.stat) p.val <- pchisq(test.stat, df=3, lower=F); print(p.val) ############################## > test.stat <- sum(Oj^2 / expect.counts) - sum(Oj); print(test.stat) [1] 11.53846 > > p.val <- pchisq(test.stat, df=3, lower=F); print(p.val) [1] 0.009143631 ############################## # 3 prop.test(matrix(c(23,7,27,3),nrow=2,ncol=2,byrow=TRUE), alternative="less", correct=FALSE) ################################################# data: matrix(c(23, 7, 27, 3), nrow = 2, ncol = 2, byrow = TRUE) X-squared = 1.92, df = 1, p-value = 0.08293 alternative hypothesis: less ################################################# # 4 fisher.test(matrix(c(10,11,14,49),nrow=2,ncol=2,byrow=TRUE), alternative="greater") ####################### data: matrix(c(10, 11, 14, 49), nrow = 2, ncol = 2, byrow = TRUE) p-value = 0.02774 ######################## # 5 # chi-square test for independence horse.data <- matrix(c(8,6,8,16,3,6,5,28), nrow=2, ncol=4, byrow=TRUE, dimnames = list(c("1-4", "5-9"), c("1", "2", "3", "Other"))) as.table(horse.data) # A quick way to get expected cell counts and verify large-sample assumption is met: expected.counts <- (apply(horse.data,1,sum) %o% apply(horse.data,2,sum))/sum(horse.data) print(expected.counts) chisq.test(horse.data, correct=FALSE) #################################################### > print(expected.counts) 1 2 3 Other 1-4 5.225 5.7 6.175 20.9 5-9 5.775 6.3 6.825 23.1 Pearson's Chi-squared test data: horse.data X-squared = 6.0529, df = 3, p-value = 0.1091 #################################################### ## 6 drill.2c <- matrix(c(11,8,8,15,9,14,12,7), nrow=2, ncol=4, byrow=TRUE, dimnames = list(c("> GM", "<= GM"), c("A", "B", "C", "D"))) # Printing the 2 by c table: as.table(drill.2c) # A quick way to get expected cell counts: expected.counts <- (apply(drill.2c,1,sum) %o% apply(drill.2c,2,sum))/sum(drill.2c) print(expected.counts) # Row sums: > apply(drill.2c,1,sum) > GM <= GM 42 42 #Column sums: > apply(drill.2c,2,sum) A B C D 20 22 20 22 # Test statistic: test.stat <- ((84^2)/(42*42))*(11^2 /20 + 8^2/22 + 8^2/20 + 15^2/22) - (84*42/42); print(test.stat) # P-value: pchisq(5.55,df=3,lower=F) ################################## > test.stat <- ((84^2)/(42*42))*(11^2 /20 + 8^2/22 + 8^2/20 + 15^2/22) - (84*42/42); print(test.stat) [1] 5.545455 > > # P-value: > > pchisq(5.55,df=3,lower=F) [1] 0.1356785 ##################################