Chi-Square Test of Independence
Calculator
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:
Where:
- = observed frequency in cell (,)
- = expected frequency in cell (,)
Modified Formula with Yates Continuity Correction (for 2x2 tables):
Key Assumptions
Practical Example
Step 1: State the Data
Contingency table of Gender and Product Preference:
Like | Dislike | Total | |
---|---|---|---|
Male | 40 | 30 | 70 |
Female | 30 | 50 | 80 |
Total | 70 | 80 | 150 |
Step 2: State Hypotheses
- : Gender and Preference are independent
- : Gender and Preference are not independent
Step 3: Calculate Expected Frequencies
- Male, Like:
- Male, Dislike:
- Female, Like:
- Female, Dislike:
Step 4: Calculate Chi-Square Statistic
Step 5: Calculate Degrees of Freedom
Step 6: Draw Conclusion
At with , the critical value is . Since , we reject . There is sufficient evidence to conclude that Gender and Product Preference are not independent (-value ).
Effect Size
Cramer's V measures the strength of association:
For our example:
For tables:
- Small effect:
- Medium effect:
- Large effect:
With , this indicates a small effect size, suggesting a weak association between Gender and Product Preference in our sample.
Code Examples
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)
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!