如何在 R 中执行 Fisher 检验?
Fisher 检验帮助我们了解分类变量之间是否存在显著的非随机关系。它应用于列联表,因为这些表用于表示分类变量的频率,并且我们既可以将其应用于矩阵,也可以将其应用于行矩阵,因为它们具有类似的形式。在 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
广告