StatsCalculators.com

Fisher's Exact Test

Created:November 2, 2024
Last Updated:January 15, 2025

This calculator helps you determine whether there is a significant relationship between two categorical variables, especially when sample sizes are small (n < 20) or when cell values are less than 5. Unlike the Chi-Square test, Fisher's Exact Test calculates the exact probability of observing a particular distribution of frequencies in a 2×2 contingency table, making it ideal when expected cell counts are low. This test is widely used in fields like genetics, clinical trials, and social sciences to analyze small datasets. Click here to populate the sample data for a quick example.

Calculator

1. Load Your Data

2. Select Columns & Options

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 \geq 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
# Create a contingency table
data <- matrix(c(8, 1, 2, 7), nrow = 2,
               dimnames = list(
                 c("Recovered", "Not Recovered"),
                 c("Treatment", "Control")
               ))
# or simply
# data = rbind(c(8,1),c(2,7))

# Perform Fisher's exact test
result <- fisher.test(data, alternative = "greater")

# Print results
print(result)
Python
from scipy.stats import fisher_exact
import numpy as np

# Create contingency table
contingency_table = np.array([
    [8, 2],  # Recovered (Treatment, Control)
    [1, 7]   # Not Recovered (Treatment, Control)
])

# Perform Fisher's exact test
odds_ratio, p_value = fisher_exact(contingency_table, alternative='greater')

print(f'Odds Ratio: {odds_ratio:.4f}')
print(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

Verification

Related Calculators