EZ Statistics

Fisher's Exact Test

Calculator

Input Your Values

Variable 1 \ Variable 2Category 1Category 2
Category 1
Category 2

Learn More

Fisher's Exact Test

Definition

Fisher's Exact Test is used to determine whether there is a significant association between two categorical variables in a 2×2 contingency table. It's particularly useful when sample sizes are small or when cell values are less than 5. The test calculates the exact probability of observing the data assuming the null hypothesis of independence is true.

Formula

The probability of observing any particular table:

p=(a+b)!(c+d)!(a+c)!(b+d)!n!a!b!c!d!p = \frac{(a+b)!(c+d)!(a+c)!(b+d)!}{n!a!b!c!d!}

Where:

  • a,b,c,d=a, b, c, d = cell frequencies
  • n=n = total sample size
  • !! denotes factorial

Odds Ratio:

OR=adbcOR = \frac{ad}{bc}

Key Assumptions

Fixed Marginal Totals: Row and column totals are fixed
Independence: Observations are independent
Random Sampling: Data comes from random samples

Fisher's Exact Test: Step-by-Step Example

Step 1: State the Data

Drug effectiveness study results:

GroupTreatedControlTotal
Recovered8210
Not Recovered178
Total9918
Step 2: Calculate Table Probability
p=(8+2)!(1+7)!(8+1)!(2+7)!18!8!2!1!7!=10!8!9!9!18!8!2!1!7!=0.0074p = \frac{(8+2)!(1+7)!(8+1)!(2+7)!}{18!8!2!1!7!} = \frac{10!8!9!9!}{18!8!2!1!7!} = 0.0074

(This is the probability of observing exactly this table configuration)

Step 3: Calculate P-values

For right-sided test (treatment increases recovery), we need all possible tables maintaining margins:

When X=8X = 8:

8210
178
9918

P(X=8)=0.0074P(X=8) = 0.0074

When X=9X = 9:

9110
088
9918

P(X=9)=0.0002P(X=9) = 0.0002

Right-sided p-value calculation:P(X8)=P(X=8)+P(X=9)=0.0074+0.0002=0.0076P(X ≥ 8) = P(X = 8) + P(X = 9) = 0.0074 + 0.0002 = 0.0076

Step 4: Calculate Odds Ratio
OR=adbc=8×72×1=28OR = \frac{ad}{bc} = \frac{8 \times 7}{2 \times 1} = 28
Step 5: Draw Conclusion

Since pp-value (0.00170.0017) <α\lt \alpha (0.050.05), we reject H0H_0. There is strong evidence that the treatment increases recovery rate, with the odds of recovery being 28 times higher in the treatment group.

Note:

  • The initial probability (0.0074) is for our observed table only
  • The p-value (0.0076) includes all tables as or more extreme
  • We maintain row and column totals for all possible tables

Code Examples

R
1# Create a contingency table
2data <- matrix(c(8, 1, 2, 7), nrow = 2,
3               dimnames = list(
4                 c("Recovered", "Not Recovered"),
5                 c("Treatment", "Control")
6               ))
7# or simply
8# data = rbind(c(8,1),c(2,7))
9
10# Perform Fisher's exact test
11result <- fisher.test(data, alternative = "greater")
12
13# Print results
14print(result)
Python
1from scipy.stats import fisher_exact
2import numpy as np
3
4# Create contingency table
5contingency_table = np.array([
6    [8, 2],  # Recovered (Treatment, Control)
7    [1, 7]   # Not Recovered (Treatment, Control)
8])
9
10# Perform Fisher's exact test
11odds_ratio, p_value = fisher_exact(contingency_table, alternative='greater')
12
13print(f'Odds Ratio: {odds_ratio:.4f}')
14print(f'p-value: {p_value:.4f}')

Alternative Tests

Consider these alternatives:

  • Chi-square Test: For larger sample sizes
  • Barnard's Test: When marginal totals aren't fixed

Related Calculators

Contingency Table Calculator

Chi-Square Test of Independence Calculator

Chi-Square Goodness of Fit Calculator

Friedman Test Calculator

Help us improve

Found an error or have a suggestion? Let us know!