费舍尔检验帮助我们了解分类变量之间是否存在显着的非随机关系。它用于列联表,因为这些表用于表示分类变量的频率,我们可以将其应用于矩阵,并且矩阵具有类似的形式。在R中,我们可以使用fisher.test函数执行fisher测试。
M1<-matrix(1:9,ncol=3) M1
输出结果
[,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9
fisher.test(M1)
Fisher's Exact Test for Count Data data: M1 p-value = 0.9888 alternative hypothesis: two.sided
M2<-matrix(1:16,ncol=4) M2
输出结果
[,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16
fisher.test(M2)
Fisher's Exact Test for Count Data data: M2 p-value = 0.9993 alternative hypothesis: two.sided
M3<-matrix(sample(0:4,9,replace=TRUE),nrow=3) M3
输出结果
[,1] [,2] [,3] [1,] 0 0 4 [2,] 4 0 4 [3,] 1 2 3
fisher.test(M3)
Fisher's Exact Test for Count Data data: M3 p-value = 0.5567 alternative hypothesis: two.sided
M4<-matrix(c(14,27,15,24,27,17,39,19,24),nrow=3) M4
输出结果
[,1] [,2] [,3] [1,] 14 24 39 [2,] 27 27 19 [3,] 15 17 24
fisher.test(M4)
Fisher's Exact Test for Count Data data: M4 p-value = 0.02126 alternative hypothesis: two.sided
fisher.test(M4,alternative =“ greater”)
Fisher's Exact Test for Count Data data: M4 p-value = 0.02126 alternative hypothesis: greater
fisher.test(M4,alternative =“ less”)
Fisher's Exact Test for Count Data data: M4 p-value = 0.02126 alternative hypothesis: less
M5<-matrix(sample(c(545,501,576),4,replace=TRUE),nrow=2) M5
输出结果
[,1] [,2] [1,] 545 545 [2,] 545 545
fisher.test(M5)
Fisher's Exact Test for Count Data data: M5 p-value = 1 alternative hypothesis: true odds ratio is not equal to 1 95 percent confidence interval: 0.8391933 1.1916205 sample estimates: odds ratio 1
fisher.test(M5,alternative =“ greater”)
Fisher's Exact Test for Count Data data: M5 p-value = 0.5175 alternative hypothesis: true odds ratio is greater than 1 95 percent confidence interval: 0.8626582 Inf sample estimates: odds ratio 1
fisher.test(M5,alternative =“ less”)
Fisher's Exact Test for Count Data data: M5 p-value = 0.5175 alternative hypothesis: true odds ratio is less than 1 95 percent confidence interval: 0.000000 1.159208 sample estimates: odds ratio 1