Much thesis data is not numbers on a scale but counts in categories: how many respondents chose each option, how many males and females prefer online classes, how many patients recovered under each treatment. The chi-square (\( \chi^2 \)) test is the standard tool for such data. It compares the counts you observed with the counts you would expect if your null hypothesis were true.
The One Formula Behind Every Chi-Square Test
Both versions of the test use the same statistic:
$$ \chi^2 = \sum \frac{(O - E)^2}{E} $$
where \( O \) is an observed count and \( E \) is the expected count for the same cell. Each cell contributes a little "surprise" value; adding them up gives the total discrepancy. If observed and expected counts match closely, \( \chi^2 \) is small. If they differ a lot, \( \chi^2 \) is large and the p-value is small.
The test was introduced by Karl Pearson in 1900, making it one of the oldest hypothesis tests still in everyday use.
Assumptions and Conditions
- Data are counts (frequencies), not percentages or means. Convert percentages back to counts before testing.
- Each participant appears in only one cell. The categories must be mutually exclusive.
- Observations are independent. Do not use chi-square for pre/post data on the same people; McNemar's test handles that.
- Expected counts are large enough. A common rule is that every expected count should be at least 5 (some texts allow up to 20% of cells between 1 and 5). For small 2×2 tables, use Fisher's exact test instead.
Test 1: Chi-Square Goodness of Fit
This test checks whether the distribution of a single categorical variable matches a hypothesised distribution.
Example. A university wants to know whether 120 first-year students are equally interested in four elective courses: Data Science, Accounting, Psychology and Urdu Literature. The observed choices are 30, 22, 28 and 40.
Step 1: Hypotheses. \( H_0 \): all four electives are equally popular. \( H_1 \): they are not.
Step 2: Expected counts. Under \( H_0 \), each elective gets \( 120/4 = 30 \) students.
Step 3: Cell contributions.
- Data Science: \( (30-30)^2/30 = 0 \)
- Accounting: \( (22-30)^2/30 = 64/30 = 2.133 \)
- Psychology: \( (28-30)^2/30 = 4/30 = 0.133 \)
- Urdu Literature: \( (40-30)^2/30 = 100/30 = 3.333 \)
Step 4: Total.
$$ \chi^2 = 0 + 2.133 + 0.133 + 3.333 = 5.60 $$
Step 5: Degrees of freedom and decision. For goodness of fit, \( df = k - 1 = 3 \). The critical value at \( \alpha = .05 \) is 7.815. Since 5.60 < 7.815, we fail to reject \( H_0 \); the exact p-value is .133.
APA report: A chi-square goodness-of-fit test indicated that elective choices did not differ significantly from an equal distribution, χ²(3, N = 120) = 5.60, p = .133.
The Urdu Literature count looks high, but with only 120 students the departure from equality could easily be chance.
Test 2: Chi-Square Test of Independence
This test checks whether two categorical variables are related, using a contingency (cross-tabulation) table.
Example. A researcher surveys 120 university students in Islamabad about whether they prefer online or in-person classes, and records gender.
| Gender | Online | In-person | Row total |
|---|---|---|---|
| Male | 40 | 20 | 60 |
| Female | 25 | 35 | 60 |
| Column total | 65 | 55 | 120 |
Step 1: Hypotheses. \( H_0 \): class preference is independent of gender. \( H_1 \): preference is related to gender.
Step 2: Expected counts. For each cell:
$$ E = \frac{\text{row total} \times \text{column total}}{N} $$
- Male, Online: \( 60 \times 65/120 = 32.5 \)
- Male, In-person: \( 60 \times 55/120 = 27.5 \)
- Female, Online: \( 60 \times 65/120 = 32.5 \)
- Female, In-person: \( 60 \times 55/120 = 27.5 \)
All expected counts exceed 5, so the test is appropriate.
Step 3: Cell contributions. Every cell differs from its expected count by 7.5.
- Male, Online: \( 7.5^2/32.5 = 56.25/32.5 = 1.731 \)
- Male, In-person: \( 56.25/27.5 = 2.045 \)
- Female, Online: \( 56.25/32.5 = 1.731 \)
- Female, In-person: \( 56.25/27.5 = 2.045 \)
Step 4: Total.
$$ \chi^2 = 1.731 + 2.045 + 1.731 + 2.045 = 7.55 $$
Step 5: Degrees of freedom and decision.
$$ df = (r-1)(c-1) = (2-1)(2-1) = 1 $$
The critical value for 1 df is 3.841. Since 7.55 > 3.841, we reject \( H_0 \); the exact p-value is .006.
Step 6: Effect size. Cramér's V measures the strength of association:
$$ V = \sqrt{\frac{\chi^2}{N(m-1)}} = \sqrt{\frac{7.55}{120 \times 1}} = \sqrt{0.0629} = 0.25 $$
where \( m \) is the smaller of the number of rows and columns. For a 2×2 table, V of about .1 is small, .3 medium and .5 large, so this is a small-to-medium association.
Interpretation. About 67% of males (40 of 60) preferred online classes, compared with about 42% of females (25 of 60).
APA report: A chi-square test of independence showed a significant association between gender and class-mode preference, χ²(1, N = 120) = 7.55, p = .006, Cramér's V = .25. Male students were more likely than female students to prefer online classes (67% vs 42%).
A Note on Yates' Correction
For 2×2 tables, SPSS also prints a "Continuity Correction" row (Yates' correction), which subtracts 0.5 from each \( |O - E| \) before squaring. It makes the test more conservative. Many modern statisticians prefer the uncorrected Pearson value for reasonable sample sizes, or Fisher's exact test for small ones. Whichever you choose, state it in your methodology.
Running Chi-Square in Software
- SPSS: Analyze → Descriptive Statistics → Crosstabs; under Statistics tick Chi-square and Phi and Cramér's V; under Cells tick Expected.
- Excel:
CHISQ.TEST(observed_range, expected_range)returns the p-value. - Stata:
tabulate gender preference, chi2 V expected. - Python:
python
from scipy.stats import chi2_contingency
chi2, p, df, expected = chi2_contingency([[40, 20], [25, 35]], correction=False)
Common Mistakes
- Running the test on percentages instead of counts.
- Ignoring cells with tiny expected counts.
- Treating a significant result as proof that one variable causes the other.
- Forgetting to describe the direction of the association using row or column percentages.
Did You Know?
- Pearson's 1900 paper on chi-square also helped launch modern hypothesis testing, decades before p-values became routine.
- R. A. Fisher later corrected Pearson's degrees of freedom for contingency tables, sparking a long-running dispute between the two statisticians.
- For a 2×2 table, the chi-square statistic equals \( N \) times the square of the phi coefficient.
Key Takeaways
- Use goodness of fit for one categorical variable and the test of independence for two.
- \( \chi^2 = \sum (O-E)^2/E \); expected counts come from the null hypothesis or from row and column totals.
- Check that expected counts are at least 5, or switch to Fisher's exact test.
- Report χ², df, N, p and Cramér's V, plus percentages showing the direction of the effect.
Have survey data that needs chi-square analysis, or a statistics assignment due soon? Amathify's experts and PhD scholars provide complete step-by-step solutions; reach us on WhatsApp or through the Ask a Question form.
Stuck on a problem or assignment?
Our experts and PhD scholars solve maths, physics and chemistry — step by step.