# Handout for GLM by Yu-Ling Tseng # Fisher exact test # Ex1: Fisher's tea taster, p.46, Table 2.8 > n.table<-array(data = c(3, 1, 1, 3), dim = c(2,2), + dimnames=list(Actual = c("Pour Milk", "Pour Tea"), + Guess = c("Pour Milk", "Pour Tea"))) > n.table Guess Actual Pour Milk Pour Tea Pour Milk 3 1 Pour Tea 1 3 > > > #P(3) > dhyper(3, 4, 4, 4) [1] 0.2285714 > > #P(0),...,P(4) > dhyper(0:4, 4, 4, 4) ## Column 1 of Table 2.9, p. 47 [1] 0.01428571 0.22857143 0.51428571 0.22857143 0.01428571 > > > fisher.test(x = n.table) Fisher's Exact Test for Count Data data: n.table p-value = 0.4857 ## c.f. Line 1, p. 48 alternative hypothesis: true odds ratio is not equal to 1 95 percent confidence interval: 0.2117329 621.9337505 ## c.f. Line -3, p. 48. sample estimates: odds ratio 6.408309 > fisher.test(x = n.table, alternative = "greater") Fisher's Exact Test for Count Data data: n.table p-value = 0.2429 ## c.f. Line 9, p. 47. Note: it= two-sided P-value/2, see Line 13, p. 48 alternative hypothesis: true odds ratio is greater than 1 95 percent confidence interval: 0.3135693 Inf sample estimates: odds ratio 6.408309 # EX2: example used in class > n.table<-array(data = c(2, 1, 3, 1), dim = c(2,2), + dimnames=list(Actual = c("Pour Milk", "Pour Tea"), + Guess = c("Pour Milk", "Pour Tea"))) > n.table Guess Actual Pour Milk Pour Tea Pour Milk 2 3 Pour Tea 1 1 > > > > > #P(1),...,P(3) > dhyper(1:3, 3, 4, 5) [1] 0.1428571 0.5714286 0.2857143 > > > fisher.test(x = n.table) Fisher's Exact Test for Count Data data: n.table p-value = 1 alternative hypothesis: true odds ratio is not equal to 1 95 percent confidence interval: 0.006400778 78.183984029 sample estimates: odds ratio 0.7071212 > fisher.test(x = n.table, alternative = "greater") Fisher's Exact Test for Count Data data: n.table p-value = 0.8571 alternative hypothesis: true odds ratio is greater than 1 95 percent confidence interval: 0.01307242 Inf sample estimates: odds ratio 0.7071212 > > # Create a cross-classified table out of Table 2.10 > vic.race<-c("white","black") > def.race<-vic.race > death.penalty<-c("yes", "no") > datalabel<-list(defendant=def.race,death=death.penalty,victim=vic.race) > table.2.6<- expand.grid(defendant=def.race,death=death.penalty,victim=vic.race) > Data<-c(53, 11, 414, 37, 0, 4, 16, 139) > table.2.6<-cbind(table.2.6,count=Data) > table.2.6 defendant death victim count 1 white yes white 53 2 black yes white 11 3 white no white 414 4 black no white 37 5 white yes black 0 6 black yes black 4 7 white no black 16 8 black no black 139 > temp<-xtabs(count~defendant+death+victim ,data=table.2.6) > temp , , victim = white death defendant yes no white 53 414 black 11 37 , , victim = black death defendant yes no white 0 16 black 4 139