Chi-Square Test Explained: When and How to Use It, with Real Data

Home › Research Methodology › Chi-Square Test Explained

Quick answerThe chi-square (χ²) test checks whether two categorical variables are related, by comparing the counts you observed with the counts you would expect if there were no relationship. A large chi-square value and a small p-value (below 0.05) mean the variables are probably related.

Below, we explain the chi-square test step by step and run it live on real data from ScholarsColab.

Chi-square test at a glance

Item Plain meaning
Used for Two categorical variables, such as gender and course choice
Data Counts in a table (a contingency table)
Null hypothesis The two variables are independent (not related)
Observed counts What actually happened
Expected counts What would happen if there were no relationship
Result χ² value, degrees of freedom and p-value; Cramér’s V for strength

Worked example: do research fields prefer journals or conferences?

We run a complete chi-square test, notebook-style, on real publication data. Every result is calculated live, and you can run the same code yourself.

The data: 41,792 papers, mostly published between 1998 and 2026, by 967 ScholarsColab mentors (data updated 15 October 2026). The file you will load is anonymised: no paper titles, journal names or researcher identities.

Step 1: Load the data

In [1]:
import numpy as np
import pandas as pd
from datetime import date

# The ScholarsColab Research Methods Index (papers level): anonymised, updated daily
DATA = "https://scholarscolab.in/wp-json/scholarscolab/v1/research-methods-data?level=papers"
papers = pd.read_csv(DATA, comment="#")   # lines starting with # hold the citation

def p_text(p):
    return "< 0.001" if p < 0.001 else f"= {p:.3f}"

print(papers.shape)
papers.head()
Out [1]:
(41792, 15)
yearcitationsvenue_typevenue_grouptitle_wordstitle_colonreviewsystematicmachine_learningdeep_learningiotblockchaincovidsustainabilitydiscipline
02023.031journalOther journals12000000000Medicine & health
12024.024journalOther journals13000110000Medicine & health
22024.012journalOther journals16000000000Medicine & health
32024.09journalOther journals20010000000Medicine & health
42024.09journalGeneric "International Journal of"9000000000Medicine & health

Step 2: Build the contingency table

Our two categorical variables are the research discipline and the type of venue (journal or conference). A contingency table counts the papers in every combination.

In [2]:
jc = papers[papers["venue_type"].isin(["journal", "conference"])]
table = pd.crosstab(jc["discipline"], jc["venue_type"])
table
Out [2]:
conferencejournal
Engineering & computer science594312841
Life & environmental sciences2354298
Management, economics & finance1971750
Medicine & health7515194
Other or unclassified4482425
Physical sciences & mathematics1892454
Social sciences & education40592

Step 3: Run the test and compare observed with expected counts

The test asks: if discipline and venue type were unrelated, how many papers would we expect in each cell? Those expected counts are shown below the result.

In [3]:
from scipy import stats
chi2, p, dof, expected = stats.chi2_contingency(table)
n = table.to_numpy().sum()
cramers_v = np.sqrt(chi2 / (n * (min(table.shape) - 1)))
print(f"chi2 = {chi2:.1f}, df = {dof}, p {p_text(p)}, Cramér's V = {cramers_v:.2f}")
pd.DataFrame(expected, index=table.index, columns=table.columns).round(0)
Out [3]:
chi2 = 2803.7, df = 6, p < 0.001, Cramér's V = 0.27
conferencejournal
Engineering & computer science3924.014860.0
Life & environmental sciences947.03586.0
Management, economics & finance407.01540.0
Medicine & health1242.04703.0
Other or unclassified600.02273.0
Physical sciences & mathematics552.02091.0
Social sciences & education132.0500.0
How to read it: Engineering & computer science has more conference papers than expected, and Life & environmental sciences has fewer than expected. These gaps add up to a large chi-square value and a very small p-value, so the type of venue clearly depends on the discipline. Cramér’s V is 0.27, a weak to moderate association.

Step 4: Show the pattern in a chart

In [4]:
import matplotlib.pyplot as plt
share = (table["conference"] / table.sum(axis=1) * 100).sort_values()
overall = table["conference"].sum() / n * 100

fig, ax = plt.subplots(figsize=(8, 4))
ax.barh(share.index, share.values, color="lightgreen", edgecolor="green")
ax.axvline(overall, color="green", linestyle="--", label=f"All disciplines: {overall:.1f}%")
ax.set_xlabel("% of papers published at conferences")
ax.legend()
plt.tight_layout()
plt.show()
Out [4]:
Share of papers published at conferencesEngineering & computer science31.6%18,784 papersOther or unclassified15.6%2,873 papersMedicine & health12.6%5,945 papersManagement, economics & finance10.1%1,947 papersPhysical sciences & mathematics7.2%2,643 papersSocial sciences & education6.3%632 papersLife & environmental sciences5.2%4,533 papers
Share of each discipline’s journal and conference papers that were conference papers. Across all disciplines: 20.9%.
The chart makes the result easy to explain: in Engineering & computer science, 31.6% of these papers are conference papers; in Life & environmental sciences, only 5.2%. Across all disciplines the figure is 20.9%. That matters when you choose where to submit your own work.
Run this analysis yourself. Download the notebook and open it in Jupyter or Google Colab (File → Upload notebook). It loads the same anonymised ScholarsColab data, so your results will match this page.Download notebook (.ipynb) →Data files: papers.csv · mentors.csv

Types of chi-square test

Test Question Example
Test of independence Are two categorical variables related? Is discipline related to using machine learning?
Goodness-of-fit test Does one variable match an expected distribution? Are births equally spread across the days of the week?

How to run a chi-square test

  1. Arrange the counts in a table with one variable in rows and the other in columns.
  2. Check the assumptions: counts (not percentages), independent observations, and expected counts of at least 5 in most cells.
  3. Run the test in SPSS, R, Python or Excel (CHISQ.TEST).
  4. Report χ², degrees of freedom, sample size and p-value, for example, from the test above: χ²(6, N = 37,357) = 2,803.7, p < 0.001.
  5. Add an effect size such as Cramér’s V, and look at which cells differ most from expected.

Common mistakes reviewers point out

  • Running the test on percentages instead of counts.
  • Too many small expected counts; use Fisher’s exact test for small tables.
  • Stopping at “significant” without saying which categories differ or how strongly.
  • Counting the same person more than once.

Frequently asked questions

What does a chi-square test tell you?

It tells you whether the pattern of counts in a table differs from what you would expect if two categorical variables were unrelated.

When should I use a chi-square test?

Use it when both variables are categorical, such as yes/no or groups, and your data are counts of independent observations.

What are degrees of freedom in a chi-square test?

For a test of independence, degrees of freedom equal (number of rows − 1) × (number of columns − 1).

What is Cramér’s V?

Cramér’s V measures the strength of the relationship found by a chi-square test, from 0 (no relationship) to 1 (perfect relationship).

What if expected counts are below 5?

Combine small categories if it makes sense, or use Fisher’s exact test, which works with small counts.

Use this data in your own work

Use this data in your own research. Download the aggregated figures behind these charts (counts, medians and shares by type of paper, period, method and discipline) as a CSV file. Please cite it as:ScholarsColab (2026). ScholarsColab Research Methods Index [Data set]. Vidhilekha Soft Solutions Pvt Ltd. Retrieved 15 October 2026, from https://scholarscolab.in/research-methodology/. Licensed under CC BY 4.0.Download the data (CSV) →

More research methodology guides

About the data on this page. The ScholarsColab Research Methods Index is built from the Google Scholar publication lists of PhD researchers who mentor on ScholarsColab, and is updated daily as mentors and publications are added. Methods, designs and paper types are identified from titles, so papers whose titles do not mention them are not counted in those groups. The mentors are not a random sample of all researchers (engineering and computer science are strongly represented), so the figures describe this group of researchers and may not apply to research as a whole.