How to Do a Chi-Square Test with Examples

Chi-square tests rank among the most widely used statistical methods in research, offering a straightforward way to examine relationships between categorical variables. Whether you are investigating whether a new drug treatment produces different outcomes across patient groups, testing whether survey responses vary by demographic, or checking whether observed frequencies match a theoretical distribution, the chi-square test provides a reliable framework for drawing conclusions from count data.

Unlike tests that compare means or measure correlation between continuous variables, the chi-square test works entirely with frequencies — the number of observations falling into each category. This makes it particularly valuable across fields such as psychology, biology, marketing, and public health, where researchers routinely collect data in discrete groups rather than on continuous scales.

Your time matters more than writer’s block

Let us handle the words

What Is a Chi-Square Test?

A chi-square test is a statistical hypothesis test that evaluates whether observed frequencies in categorical data differ meaningfully from what would be expected under a specific assumption. Rather than comparing averages or measuring the strength of a linear relationship, the chi-square test asks a simpler but powerful question: does the pattern of counts we observed match the pattern we would expect if our assumption were true?

The test produces a single value known as the chi-square statistic (χ²), which quantifies the overall discrepancy between observed and expected frequencies. A small χ² value indicates that the observed data closely matches expectations, while a large value suggests a meaningful departure — one that may be unlikely to occur by chance alone. By comparing the χ² statistic against a critical value from the chi-square distribution, or by calculating a p-value, researchers determine whether to reject the null hypothesis.

Chi-square tests apply exclusively to categorical data — variables that place observations into distinct groups or categories, such as blood type, voting preference, or product rating. They make no assumptions about the underlying distribution of a continuous variable, which distinguishes them from parametric tests such as the t-test or ANOVA. This flexibility makes chi-square tests accessible across a broad range of research contexts where data naturally takes the form of counts or frequencies.

When Should You Use a Chi-Square Test?

Knowing when to apply a chi-square test requires understanding both the type of data you are working with and the specific question you are trying to answer. Chi-square tests are appropriate when your data consists of counts or frequencies distributed across categories, and when your research question concerns the distribution of those categories or the relationship between two categorical variables.

The most common situation calling for a chi-square test arises when you want to determine whether two categorical variables are associated with each other. For example, a researcher might ask whether political affiliation is related to preferred news source, or whether a patient’s treatment group is associated with their recovery outcome. In these cases, the chi-square test of independence is the appropriate choice.

A second situation arises when you want to test whether a single categorical variable follows a specific theoretical distribution. For instance, a geneticist might check whether observed offspring ratios match the proportions predicted by Mendelian inheritance, or a quality control analyst might verify whether product defects are distributed evenly across production shifts. Here, the goodness-of-fit test applies.

Beyond identifying the right question, your data must meet several conditions before a chi-square test is appropriate. Observations must be independent of one another, meaning each participant or item should appear in only one category. Data must be in raw frequency counts rather than percentages or proportions. Additionally, expected frequencies in each cell should generally be five or greater — when this condition is violated, results can become unreliable, and alternative approaches such as Fisher’s exact test may be more suitable.

Key Terms You Need to Know

Observed frequency (O) refers to the actual count of cases recorded in each category or cell of your data. These are the raw numbers collected directly from your sample — for example, the number of survey respondents in each age group who chose a particular product.

Expected frequency (E) is the count you would anticipate in each category if the null hypothesis were true. In a goodness-of-fit test, expected frequencies are derived from a theoretical distribution. In a test of independence, they are calculated from the row and column totals of a contingency table using the formula: E = (row total × column total) / grand total.

The chi-square statistic (χ²) measures the total discrepancy between observed and expected frequencies across all categories. It is calculated by summing the squared difference between observed and expected values, divided by the expected value, for every cell: χ² = Σ[(O − E)² / E]. Larger values indicate greater divergence from what the null hypothesis predicts.

Degrees of freedom (df) determine which chi-square distribution is used to evaluate the test statistic. For a goodness-of-fit test, df = k − 1, where k is the number of categories. For a test of independence, df = (number of rows − 1) × (number of columns − 1).

The p-value represents the probability of obtaining a chi-square statistic as large as the one calculated — or larger — if the null hypothesis were true. A p-value below the chosen significance threshold (commonly 0.05) leads to rejection of the null hypothesis.

A contingency table is a grid used in tests of independence that cross-tabulates two categorical variables, displaying the frequency of observations falling into each combination of categories.

Chi-Square Formula

The chi-square statistic is calculated using a single formula applied consistently across both the goodness-of-fit test and the test of independence:χ2=(OE)2E\chi^2 = \sum \frac{(O – E)^2}{E}

Where:

  • χ² is the chi-square statistic
  • O is the observed frequency in each category or cell
  • E is the expected frequency in each category or cell
  • Σ denotes summation across all categories or cells

The logic behind the formula is straightforward. For each category, the difference between the observed and expected frequency is squared — eliminating negative values that would otherwise cancel out when summed. This squared difference is then divided by the expected frequency, which scales the discrepancy relative to the size of the expectation. A difference of 10 carries more weight when the expected count is 15 than when it is 200. Finally, these scaled values are added together across all categories to produce a single summary statistic.

The resulting χ² value is always zero or positive. A value of zero would mean observed frequencies match expected frequencies exactly across every category — an outcome that rarely occurs in practice. As the divergence between observed and expected counts grows, the χ² value increases. Whether that value is large enough to be statistically significant depends on the degrees of freedom associated with the test, which in turn determines the shape of the chi-square distribution against which the statistic is compared.

It is worth noting that the formula treats each category or cell independently. Every observed-versus-expected comparison contributes additively to the final statistic, meaning that large discrepancies in even one or two cells can drive the overall χ² value substantially higher.

Juggling too much?

Drop your assignment here and focus on your priorities

How to Do a Chi-Square Test

Chi-Square Goodness-of-Fit Test

The scenario: A café owner believes that customer visits are distributed equally across the five working days of the week. Over one week, they record the following visits: Monday 38, Tuesday 52, Wednesday 45, Thursday 61, Sunday 54. They want to test whether the distribution departs significantly from equal proportions.

Step 1: State the hypotheses

  • H₀: Customer visits are equally distributed across all five days
  • H₁: Customer visits are not equally distributed across all five days

Step 2: Calculate expected frequencies

The total number of visits is 38 + 52 + 45 + 61 + 54 = 250. If visits were equally distributed, each day would expect 250 / 5 = 50 visits.

Step 3: Apply the chi-square formulaχ2=(OE)2E\chi^2 = \sum \frac{(O – E)^2}{E}

DayOEO − E(O − E)²(O − E)² / E
Monday3850−121442.88
Tuesday5250240.08
Wednesday4550−5250.50
Thursday6150111212.42
Friday54504160.32

χ2=2.88+0.08+0.50+2.42+0.32=6.20\chi^2 = 2.88 + 0.08 + 0.50 + 2.42 + 0.32 = 6.20

Step 4: Determine degrees of freedom

df = k − 1 = 5 − 1 = 4

Step 5: Find the critical value and interpret

At a significance level of α = 0.05 with 4 degrees of freedom, the critical value from the chi-square distribution table is 9.488. Since χ² = 6.20 is less than 9.488, we fail to reject the null hypothesis. The data does not provide sufficient evidence that customer visits are unevenly distributed across the week.

Chi-Square Test of Independence

The scenario: A researcher wants to know whether exercise frequency is associated with reported stress level. They survey 200 participants and record the results in the following contingency table:

Low StressHigh StressRow Total
Exercises regularly8535120
Does not exercise regularly305080
Column Total11585200

Step 1: State the hypotheses

  • H₀: Exercise frequency and stress level are independent
  • H₁: Exercise frequency and stress level are associated

Step 2: Calculate expected frequencies

Using E = (row total × column total) / grand total:

CellCalculationExpected Frequency
Exercises / Low Stress(120 × 115) / 20069.00
Exercises / High Stress(120 × 85) / 20051.00
Does not exercise / Low Stress(80 × 115) / 20046.00
Does not exercise / High Stress(80 × 85) / 20034.00

Step 3: Apply the chi-square formula

CellOEO − E(O − E)²(O − E)² / E
Exercises / Low Stress8569.0016.00256.003.71
Exercises / High Stress3551.00−16.00256.005.02
Does not exercise / Low Stress3046.00−16.00256.005.57
Does not exercise / High Stress5034.0016.00256.007.53

χ2=3.71+5.02+5.57+7.53=21.83\chi^2 = 3.71 + 5.02 + 5.57 + 7.53 = 21.83

Step 4: Determine degrees of freedom

df = (rows − 1) × (columns − 1) = (2 − 1) × (2 − 1) = 1

Step 5: Find the critical value and interpret

At α = 0.05 with 1 degree of freedom, the critical value is 3.841. Since χ² = 21.83 greatly exceeds 3.841, we reject the null hypothesis. There is statistically significant evidence of an association between exercise frequency and stress level.

How to Interpret Chi-Square Results

Step 1: Compare the Test Statistic to the Critical Value

The first decision point is whether your calculated χ² value exceeds the critical value for your chosen significance level and degrees of freedom. If χ² is greater than the critical value, the result falls in the rejection region and you reject the null hypothesis. If χ² is smaller, you fail to reject the null hypothesis.

Using the test of independence example from the previous section, χ² = 21.83 with 1 degree of freedom. At α = 0.05, the critical value is 3.841. Since 21.83 greatly exceeds 3.841, the result is statistically significant.

Step 2: Interpret the P-Value

The p-value tells you the probability of obtaining a chi-square statistic as large as the one calculated — or larger — purely by chance, assuming the null hypothesis is true. A small p-value indicates that the observed pattern in your data would be very unlikely under the null hypothesis.

  • p < 0.05: Statistically significant at the conventional threshold — reject the null hypothesis
  • p < 0.01: Statistically significant at a stricter threshold — stronger evidence against the null hypothesis
  • p ≥ 0.05: Insufficient evidence to reject the null hypothesis

It is important to remember that the p-value does not tell you the size or practical importance of an effect. A very large sample can produce a statistically significant chi-square result even when the actual association between variables is trivially small.

Step 3: Assess Effect Size with Cramér’s V

Because statistical significance depends heavily on sample size, researchers routinely supplement chi-square results with an effect size measure. The most widely used is Cramér’s V, calculated as:V=χ2n×(k1)V = \sqrt{\frac{\chi^2}{n \times (k – 1)}}

Where:

  • n is the total sample size
  • k is the smaller of the number of rows or columns in the contingency table

Cramér’s V ranges from 0 to 1, with general benchmarks for interpretation:

Cramér’s VEffect Size
0.00 – 0.10Negligible
0.10 – 0.30Small
0.30 – 0.50Moderate
Above 0.50Large

Applying this to the exercise and stress example: V = √(21.83 / (200 × 1)) = √0.1092 = 0.33, indicating a moderate association between exercise frequency and stress level.

Step 4: Examine Residuals to Locate the Source of Significance

When a test of independence returns a significant result, the overall χ² statistic tells you that an association exists but does not identify which specific cells are driving it. Standardised residuals help pinpoint where observed frequencies diverge most substantially from expectations:Standardised Residual=OEE\text{Standardised Residual} = \frac{O – E}{\sqrt{E}}

Standardised residuals with an absolute value greater than 2 indicate cells that contribute disproportionately to the overall chi-square statistic and warrant closer attention in your interpretation.

Step 5: Report Your Results Clearly

A complete chi-square result should include the test statistic, degrees of freedom, sample size, p-value, and effect size. A standard reporting format follows this pattern:

A chi-square test of independence revealed a statistically significant association between exercise frequency and stress level, χ²(1, n = 200) = 21.83, p < 0.001, Cramér’s V = 0.33.

Common Misinterpretations to Avoid

Failing to reject the null hypothesis does not confirm it. A non-significant result means the data did not provide sufficient evidence of an association — it does not prove that no association exists.

Statistical significance is not practical significance. Always pair your p-value with an effect size measure to give readers a complete picture of your findings.

Chi-square does not establish causation. A significant association between two categorical variables indicates a relationship in the data, not that one variable causes the other.

Missing deadlines isn’t an option

Neither is sacrificing your time. We solve both

How to Do a Chi-Square Test in Excel or Software

Microsoft Excel

Excel does not have a dedicated chi-square test dialog, but it provides two functions that handle the core calculations directly.

For the test of independence or goodness-of-fit:

  1. Enter your observed frequencies in one range of cells and your expected frequencies in another
  2. Use the function =CHISQ.TEST(observed_range, expected_range) — this returns the p-value directly
  3. To obtain the chi-square statistic itself, calculate each (O − E)² / E term manually in separate cells and sum them
  4. Use =CHISQ.INV.RT(probability, degrees_of_freedom) to find the critical value for your chosen significance level

Microsoft’s official documentation for CHISQ.TEST provides additional guidance on function syntax and cell range formatting.

R

R handles chi-square tests cleanly with a single built-in function. Using the exercise and stress example from the previous section:

Test of independence:

r

# Enter data as a matrix
data <- matrix(c(85, 35, 30, 50), nrow = 2, byrow = TRUE)

# Run the test
chisq.test(data)

Goodness-of-fit test:

r

observed <- c(38, 52, 45, 61, 54)
expected_proportions <- c(0.2, 0.2, 0.2, 0.2, 0.2)

chisq.test(observed, p = expected_proportions)

Both calls return the χ² statistic, degrees of freedom, and p-value in a single output block. The official R documentation for chisq.test covers additional arguments, including options for continuity correction and simulated p-values.

Python (SciPy)

Python’s SciPy library provides dedicated functions for both test types.

Test of independence:

python

import numpy as np
from scipy.stats import chi2_contingency

data = np.array([[85, 35], [30, 50]])
chi2, p, dof, expected = chi2_contingency(data)

print(f"Chi-square statistic: {chi2:.2f}")
print(f"P-value: {p:.4f}")
print(f"Degrees of freedom: {dof}")
print(f"Expected frequencies:\n{expected}")

Goodness-of-fit test:

python

from scipy.stats import chisquare

observed = [38, 52, 45, 61, 54]
chi2, p = chisquare(observed)

print(f"Chi-square statistic: {chi2:.2f}")
print(f"P-value: {p:.4f}")

Full documentation for chi2_contingency and chisquare is available on the SciPy website.

SPSS

SPSS performs chi-square tests through its Crosstabs procedure, which produces a full output table including the χ² statistic, degrees of freedom, and p-value.

  1. Go to Analyze → Descriptive Statistics → Crosstabs
  2. Assign your row and column variables in the dialog box
  3. Click Statistics and check the Chi-square option
  4. Click Continue, then OK to run the analysis

For the goodness-of-fit test, navigate instead to Analyze → Nonparametric Tests → Legacy Dialogs → Chi-Square, enter your observed variable, and specify expected values or equal proportions. IBM’s SPSS Statistics documentation provides detailed walkthroughs for both procedures.

Stata

Stata offers two straightforward commands for chi-square testing.

Test of independence (using tabulated data):

stata

tabulate exercise_freq stress_level, chi2

Goodness-of-fit test:

stata

csgof observed_var, expperc(20 20 20 20 20)

The tabulate command with the chi2 option produces the statistic, degrees of freedom, and p-value in the output window. Stata’s official documentation covers additional options including exact tests and measures of association.

Advantages and Limitations

Advantages and Limitations of Chi-Square

Chi-Square Test vs Other Statistical Tests

Choosing the right statistical test depends on the type of data you have, the number of groups being compared, and the research question you are asking. The table below situates the chi-square test among the most commonly used alternatives.

TestData TypeTypical Use CaseKey Difference from Chi-Square
Chi-square testCategoricalAssociation between categorical variables; goodness-of-fit
Fisher’s exact testCategoricalAssociation in small samplesExact probabilities rather than chi-square approximation; preferred when expected cell counts fall below 5
McNemar’s testCategorical (paired)Change in categorical response within the same subjectsHandles dependent/matched observations that chi-square cannot
Independent samples t-testContinuousComparing means of two independent groupsTests means rather than frequencies; requires normally distributed continuous outcome
Paired samples t-testContinuous (paired)Comparing means within the same subjects before and afterAccounts for within-subject pairing; continuous outcome variable
ANOVAContinuousComparing means across three or more groupsTests means across multiple groups; continuous outcome, parametric assumptions required
Mann-Whitney U testOrdinal / non-normal continuousComparing two independent groups without normalityRanks-based; used when continuous data violates normality but is not purely categorical
Pearson correlationContinuousLinear relationship between two continuous variablesMeasures strength and direction of relationship; both variables must be continuous
Logistic regressionMixedPredicting a binary categorical outcome from one or more predictorsModels probability of an outcome; accommodates continuous and categorical predictors simultaneously

When to Choose Chi-Square Over Alternatives

Chi-square vs Fisher’s exact test. Both tests evaluate association between two categorical variables, but they differ in how they calculate the p-value. The chi-square test relies on an approximation that works well with larger samples. Fisher’s exact test computes the exact probability of the observed arrangement of frequencies and is the preferred choice when any expected cell count falls below five or when the total sample size is small. Most software will flag when Fisher’s exact test is more appropriate.

Chi-square vs McNemar’s test. The chi-square test of independence assumes that all observations are independent of one another. When the same subjects are measured twice — for example, surveying participants on their product preference before and after an advertising campaign — observations are paired and the independence assumption is violated. McNemar’s test is designed specifically for this situation, analysing change in a binary categorical response within matched pairs.

Chi-square vs t-test and ANOVA. The chi-square test and the t-test answer fundamentally different questions. The chi-square test asks whether the distribution of frequencies across categories differs from expectation. The t-test asks whether the average value of a continuous variable differs between two groups. If your outcome variable is continuous and approximately normally distributed, a t-test or ANOVA will generally be more powerful than converting that variable into categories and running a chi-square test — a process that discards information and reduces statistical power.

Chi-square vs Mann-Whitney U test. When a continuous outcome variable clearly violates normality assumptions, the Mann-Whitney U test offers a non-parametric alternative to the t-test that compares the rank ordering of values across two groups. It is not a substitute for chi-square — it still requires an outcome variable measured on at least an ordinal scale rather than purely nominal categories.

Chi-square vs logistic regression. A 2×2 chi-square test of independence and a simple binary logistic regression applied to the same data will often yield very similar results, and in some configurations they are mathematically equivalent. However, logistic regression offers considerably greater flexibility. It can accommodate multiple predictors simultaneously, control for confounding variables, and include both continuous and categorical predictors in a single model. When a research question involves more than two variables or requires covariate adjustment, logistic regression is the more appropriate choice.

Assignments piling up with no time to tackle them?

We deliver fast, quality work

FAQs

What does 0.05 mean in chi-square?

It is the significance level (α = 0.05).
It means you accept a 5% chance of being wrong when rejecting the null hypothesis.

Which is better, 0.01 or 0.05 significance level?

0.01 is stricter (less chance of false positives).
0.05 is more common and easier to achieve significance.
“Better” depends on how much error risk you can accept.

Is 0.025 a significant p-value?

Yes, if α = 0.05 (since 0.025 < 0.05).
It is considered statistically significant.

Company

Welcome to our writing center! Whether you’re working on a writing assignment or simply need help with a paragraph, we’re here to assist you. Our resources are licensed under a creative commons attribution-noncommercial-sharealike 4.0 international license, so feel free to use them to summarize, revise, or improve your essay writing. Our goal is to help you navigate the transition to college writing and become a confident writer in college. From research process to writing strategies, we can support you with different kinds of writing.

Services Offered

  • Professional custom essay writing service for college students
  • Experienced writers for high-quality academic research papers
  • Affordable thesis and dissertation writing assistance online
  • Best essay editing and proofreading services with quick turnaround
  • Original and plagiarism-free content for academic assignments
  • Expert writers for in-depth literature reviews and case studies

Services Offered

  • Professional custom essay writing service for college students
  • Experienced writers for high-quality academic research papers
  • Affordable thesis and dissertation writing assistance online
  • Best essay editing and proofreading services with quick turnaround
  • Original and plagiarism-free content for academic assignments
  • Expert writers for in-depth literature reviews and case studies