How to perform fisher test in R?

R ProgrammingServer Side ProgrammingProgramming

The fisher test helps us to understand whether there exists a significant non-random relationship among categorical variables or not. It is applied on contingency tables because these tables are used to represent the frequency for categorical variables and we can apply it on a matrix as well as matrices have the similar form. In R, we can use fisher.test function to perform the fisher test.

Example

 Live Demo

M1<-matrix(1:9,ncol=3)
M1

Output

   [,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

Example

 Live Demo

M2<-matrix(1:16,ncol=4)
M2

Output

     [,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

Example

 Live Demo

M3<-matrix(sample(0:4,9,replace=TRUE),nrow=3)
M3

Output

   [,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

Example

 Live Demo

M4<-matrix(c(14,27,15,24,27,17,39,19,24),nrow=3)
M4

Output

    [,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

Example

 Live Demo

M5<-matrix(sample(c(545,501,576),4,replace=TRUE),nrow=2)
M5

Output

    [,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
raja
Published on 19-Oct-2020 18:12:02
Advertisements