EZ Statistics

Chi-Square Test of Independence

Calculator

2. Select Columns & Options

Learn More

Chi-Square Test of Independence

Definition

Chi-Square Test of Independence examines whether there is a significant association between two categorical variables. It tests whether the observed frequencies in a contingency table differ significantly from the frequencies we would expect if there were no relationship between the variables.

Formula

Test Statistic:

χ2=(OijEij)2Eij\chi^2 = \sum\frac{(O_{ij} - E_{ij})^2}{E_{ij}}

Where:

  • OijO_{ij} = observed frequency in cell (ii,jj)
  • EijE_{ij} = expected frequency in cell (ii,jj)
  • Eij=(row i total)(column j total)grand totalE_{ij} = \frac{(\text{row }i\text{ total})(\text{column }j\text{ total})}{\text{grand total}}

Modified Formula with Yates Continuity Correction (for 2x2 tables):

χ2=(OijEij0.5)2Eij\chi^2 = \sum\frac{(|O_{ij} - E_{ij}| - 0.5)^2}{E_{ij}}

Key Assumptions

Random Sampling: Data must be randomly sampled
Independence: Observations must be independent
Sample Size: Expected frequenciesshould be 5\geq 5
Mutual Exclusivity: Categories must be mutually exclusive

Practical Example

Step 1: State the Data

Contingency table of Gender and Product Preference:

LikeDislikeTotal
Male403070
Female305080
Total7080150
Step 2: State Hypotheses
  • H0H_0: Gender and Preference are independent
  • HaH_a: Gender and Preference are not independent
  • α=0.05\alpha = 0.05
Step 3: Calculate Expected Frequencies
  • Male, Like: E11=70×70150=32.67E_{11} = \frac{70 \times 70}{150} = 32.67
  • Male, Dislike: E12=70×80150=37.33E_{12} = \frac{70 \times 80}{150} = 37.33
  • Female, Like: E21=80×70150=37.33E_{21} = \frac{80 \times 70}{150} = 37.33
  • Female, Dislike: E22=80×80150=42.67E_{22} = \frac{80 \times 80}{150} = 42.67
Step 4: Calculate Chi-Square Statistic
χ2=(4032.670.5)232.67+(3037.330.5)237.33+(3037.330.5)237.33+(5042.670.5)242.67=5.02\chi^2 = \frac{(|40-32.67|-0.5)^2}{32.67} + \frac{(|30-37.33|-0.5)^2}{37.33} + \frac{(|30-37.33|-0.5)^2}{37.33} + \frac{(|50-42.67|-0.5)^2}{42.67} = 5.02
Step 5: Calculate Degrees of Freedom

df=(r1)(c1)=(21)(21)=1df = (r-1)(c-1) = (2-1)(2-1) = 1

Step 6: Draw Conclusion

At α=0.05\alpha = 0.05 with df=1df = 1, the critical value is 3.8413.841. Since χ2=5.02>3.841\chi^2 = 5.02 \gt 3.841, we reject H0H_0. There is sufficient evidence to conclude that Gender and Product Preference are not independent (pp-value =0.008= 0.008).

Effect Size

Cramer's V measures the strength of association:

V=χ2n(min(r,c)1)V = \sqrt{\frac{\chi^2}{n(\min(r,c)-1)}}

For our example:

V=5.02150(1)=0.18V = \sqrt{\frac{5.02}{150(1)}} = 0.18

For 2×22\times 2 tables:

  • Small effect: V0.10V \approx 0.10
  • Medium effect: V0.30V \approx 0.30
  • Large effect: V0.50V \approx 0.50

With V=0.18V = 0.18, this indicates a small effect size, suggesting a weak association between Gender and Product Preference in our sample.

Code Examples

R
1# Create contingency table
2data <- matrix(c(40, 30, 30, 50), 
3               nrow = 2, 
4               dimnames = list(
5                 Gender = c("Male", "Female"),
6                 Preference = c("Like", "Dislike")
7               ))
8
9# Perform chi-square test
10result <- chisq.test(data)
11print(result)
12
13# View expected frequencies
14print(result$expected)
Python
1import numpy as np
2from scipy.stats import chi2_contingency
3
4# Create contingency table
5observed = np.array([
6    [40, 30],  # Male (Like, Dislike)
7    [30, 50]   # Female (Like, Dislike)
8])
9
10# Perform chi-square test
11chi2, pvalue, dof, expected = chi2_contingency(observed)
12
13print(f'Chi-square statistic: {chi2:.4f}')
14print(f'p-value: {pvalue:.4f}')
15print(f'Degrees of freedom: {dof}')
16print(f'Expected frequencies: {expected}')

Alternative Tests

Consider these alternatives:

  • Fisher's Exact Test: For small expected frequencies
  • G-test: Alternative to chi-square, using likelihood ratios

Related Calculators

Chi-Square Goodness of Fit Test Calculator

One-Sample T-Test Calculator

Two-Sample T-Test Calculator

One-Way ANOVA Calculator

Help us improve

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