Chi-square test of independence

The relationship between two categorical variables: a crosstabulation with percentages, expected frequencies and standardized residuals, Pearson's chi-square, the likelihood ratio, Yates's correction and Fisher's exact test for a 2×2 table, and the strength of the relationship – φ, Cramér's V and the odds ratio with a confidence interval. From raw data or from a ready-made frequency table.

Calculator

Loading the data

Drag an Excel file or an SPSS file (.sav) here, or click to choose
In the site's standard template: row = participant, column = variable, first row = headers. CSV is also accepted.
If the file includes a "legend" sheet – the value labels (1 = male) will appear in the crosstabulation and in the syntax.
Just want to try it out? Download a sample data file (320 residents: gender, years in the community, type of locality, participation in community activity): English example
or

Paste columns directly from the sheet (including the header row) – select them in Excel, copy, and paste here:

A
B
C
D
E
F
You can also just paste – the data will be loaded and displayed automatically.
This is how a table from a paper or a book is entered – for example Field's table (2024, Table 19.1): "Training \ Dance", "Danced", "Did not dance" in the header row; "Food 28 10"; "Affection 48 114".

Variable selection

Guide

When is the chi-square test of independence used?

When both variables are categorical and you want to know whether the distribution of one depends on the other: does the rate of participants in community activity differ between long-time and new residents? Is the choice of a study track related to background? With a categorical variable there is no mean – "the mean of a categorical variable is meaningless" (Field, 2024, Section 19.3) – so frequencies are analyzed: how many participants fell in each combination of categories. Pallant (2020, chapter 16) phrases the same question in three ways: is there an association between gender and smoking? Are males more likely to be smokers than females? Is the proportion of smokers the same for both genders? – it is the same test. If one of the variables is continuous, this is not the test: to compare means between groups – a t-test or ANOVA; to predict a categorical variable from continuous predictors – logistic regression (Field, Section 19.2). A table of three or more categorical variables – loglinear analysis (Field, Section 19.4), which is not on the site.

How the test works

The model is "no relationship". If there is no relationship, the expected frequency in each cell derives from the margins of the table alone: E = (row total × column total) / N (Field, 2024, Equation 19.3). The test sums the discrepancy between observed and expected over all cells, standardized by the expected – χ² = Σ (O − E)²/E (Equation 19.2; Gravetter & Wallnau, 2017, Section 17.3) – and the statistic has an approximate chi-square distribution with df = (r − 1)(c − 1) degrees of freedom: the number of cells that can be "freely chosen" once the row and column totals are given (Gravetter & Wallnau, Equation 17.5). Field's example (Table 19.1): 200 cats were trained to dance in return for food or for affection; 28 of the 38 that received food danced, compared with 48 of the 162 that received affection – χ²(1) = 25.36, p < .001. You can paste this table into the page in "ready-made frequency table" mode and get all the numbers in the chapter.

The four rows in the tests table – and what is reported

Pearson's chi-square is the test; the other rows answer the question of what happens when the sample is small. Fisher's exact test (Field, Section 19.3.3) is not a different test but an exact calculation of the probability instead of the approximation to the chi-square distribution – the approximation is good when the expected frequencies are greater than 5, and breaks down when they are small; so Fisher is intended for a 2×2 table with a small sample, and SPSS prints it automatically for every 2×2 table. The likelihood ratio (G²; Section 19.3.4) is based on maximum likelihood, is distributed like Pearson, and in a large sample is almost identical to it; Field prefers it in small samples. Yates's continuity correction (section 19.3.5) subtracts 0.5 from each absolute deviation before squaring, because in a 2×2 table Pearson tends to give a p that is too small – and here the two books part ways: Field writes that there is "a fair bit of evidence" that the correction overcorrects and produces χ² values that are too small (Howell, 2012, as cited in Field), and concludes "it's worth knowing about, but probably best ignored"; Pallant (2020, chapter 16) instructs reporting the Continuity Correction row specifically in a 2×2 table, and demonstrates this in her report sentence. The page shows both, and marks for reporting according to the site's rule: Pearson; and in a 2×2 table that violates the expected frequencies condition – Fisher. Those whose supervisor asks for Yates – the numbers are in Table 3, and the report follows Pallant's wording (the reporting section below).

Strength of the relationship: φ, Cramér's V and the odds ratio

Significance says that there is a relationship; it does not say how strong it is, and in a large sample even a small difference in percentages comes out significant (Field, 2024, Section 19.5.3). Three measures correct χ² for sample size and the table's dimensions to obtain a number between 0 and 1 (Field, Section 19.3.6): φ – √(χ²/N), accurate for a 2×2 table and in it equal to the Pearson correlation between the two dichotomous variables (so it has a sign; Gravetter & Wallnau, 2017, equation 17.8); Pearson's contingency coefficient, which seldom reaches 1; and Cramér's V – √(χ²/(N·df*)) where df* = min(r − 1, c − 1) (Gravetter & Wallnau, Equation 17.9) – which can reach 1 even in a large table, and is therefore "the most useful" (Field). In a 2×2 table φ and V are identical. The cutoffs: to interpret φ the correlation cutoffs are used – .10 small, .30 medium, .50 large (Cohen, 1988, as cited in Pallant, 2020, and in Gravetter & Wallnau, 2017); for V in a larger table the cutoffs are lower, according to df* (Gravetter & Wallnau, Table 17.10): for df* = 2 – .07 / .21 / .35; for df* = 3 – .06 / .17 / .29. The page interprets according to this table. The relation between the two: Cohen defines w as V·√df* with the cutoffs .10 / .30 / .50 – exactly the same cutoffs, on a different scale.

The odds ratio (OR) is, in Field's view (section 19.8.4), the more common and more useful effect size measure for a 2×2 table – "the categorical equivalent of a focused comparison". Odds are the number of cases that did divided by the number of cases that did not: the odds of dancing after food 28/10 = 2.8, after affection 48/114 = .42, and the ratio between them 6.65 – "if a cat was trained with food the odds of their dancing were 6.65 times higher than if they had been trained with affection" (equations 19.40-19.42). The page adds a 95% confidence interval on the log scale (like Risk Estimate in SPSS): an interval that does not include 1 – the relationship is significant. Note the direction: the odds for the first column category, in the first row compared with the second – the order of the categories in the table determines whether you get 6.65 or 1/6.65 = .15, and both are the same finding.

Figure 1. The same percentages, a larger sample: χ² grows, φ stays

The table of Gravetter and Wallnau (2017, example 17.6): 5/10 vs. 10/5 – two-thirds on each side. Horizontal axis – the table multiplied by 1 to 8 (N = 30 to 240); left axis – χ²; the horizontal line – φ (right axis, 0-1)

N = 30N = 60N = 90N = 120N = 150N = 180N = 210N = 240 07142128 0.25.5.751.0 χ²crit = 3.84 (df = 1, α = .05) χ² (left axis) φ = .33 (right axis)

Note. Computed from the formulas (Gravetter & Wallnau, 2017, Equations 17.8-17.9), not simulated: in the original table (N = 30) χ² = 3.33 – not significant – and φ = .333; when each cell is doubled (N = 60) χ² = 6.66 – significant – and φ is still .333. "The larger sample provides more convincing evidence and, therefore, is more likely to produce a significant result", but the strength of the relationship has not changed. This is why both the significance and the effect size measure are reported (and also the percentages – Field, 2024, section 19.5.3).

How to read the output?

Table 1 – the percentages. Field (2024, section 19.8) reads the cats' crosstabulation through the percentages within each type of training: of those trained with food 73.7% danced, of those trained with affection 29.6% – "when food was used as a reward most cats danced, but when affection was used most cats refused to dance". Pallant (2020, chapter 16) does this with "% within sex": 17.9% of the males smoke compared with 20.6% of the females, and reminds to check that the two percentages in each row add up to 100. This is the main reading of the result; the test only says whether the difference between the rows is larger than chance can explain.

Table 2 – expected values and residuals. First check the minimum expected frequency (the condition – in the assumptions section). Then, in a table larger than 2×2, the residuals tell which cells drive the relationship: the standardized residual (O − E)/√E is a z-score (Field, Section 19.8.1) – outside ±1.96 the cell deviates significantly from the expected at .05, ±2.58 at .01, ±3.29 at .001, and the sign says whether more or fewer than expected; for the cats the two food cells are significant (3.6 and −2.8) and the two affection cells are not (−1.7 and 1.4): "the association between the type of reward and dancing is mainly driven by food". Pallant uses the adjusted standardized residual (Adjusted Residual), which also corrects for the size of the row and column: above 2 – more than expected, below −2 – fewer, "a kind of post hoc test". The page shows both, and highlights in yellow cells whose adjusted residual is outside ±1.96.

Table 3 – the tests. Read Pearson (or the marked row); in a 2×2 table note that Fisher's exact value is printed in the p column without a χ² value. Table 4 – strength of the relationship. V (or φ) with the label according to the cutoffs, and in a 2×2 table the odds ratio with its confidence interval. The chart shows Table 1 visually: if the bars in each group are in the same proportion – there is no relationship.

Assumptions and warnings

Independence of observations. The test does not rely on the assumptions of the linear model (normality and the like – a categorical variable cannot be normally distributed), but it has two of its own (Field, 2024, Section 19.5). The first: each participant, item or entity contributes to one cell only (Section 19.5.1; Gravetter & Wallnau, 2017, Section 17.4). Therefore chi-square cannot be run on repeated measures – the same people before and after – nor on a participant who was marked in several categories. For repeated measures in two dichotomous variables – McNemar's test (Pallant, 2020, Chapter 16); for more complex designs Field refers to generalized mixed models.

The expected frequencies. In a 2×2 table no cell should be below 5; in a larger table – all cells above 1 and no more than 20% below 5 (Field, Section 19.5.2). Pallant adds that some authors recommend at least 10 in a 2×2 table. A violation "results in a radical reduction in test power – so dramatic, in fact, that it may not be worth bothering" (Field). The remedies: in a 2×2 table – Fisher's exact; in a large table – collapsing categories (only when there is a substantive logic for it: spring, summer and "autumn/winter"), or more data. Adding a constant to all cells solves nothing. Note: the condition is on the expected frequency, not the observed one – an empty observed cell is not a problem in itself.

A large sample. "Relatively small differences in cell frequencies can result in a significant association if the sample is large enough" (Field, Section 19.5.3) – so percentages and an effect size are read, not only p (Figure 1).

What the page does not compute. The contingency coefficient and Goodman and Kruskal's λ (Field, tip 19.1); a z-test comparing column proportions with a Bonferroni correction (Compare column proportions – Field, section 19.8: the letters next to the frequencies in the SPSS output); a Bayes factor for the table (section 19.8.3); loglinear analysis for three or more variables (sections 19.4, 19.9-19.11); McNemar's test and the chi-square goodness-of-fit test (Gravetter & Wallnau, Sections 17.1-17.2). Field's interval for the standardized residuals (±1.96) and Pallant's cutoff (±2) – the same idea with different rounding; the page uses 1.96.

And how do you do it in SPSS?

Analyze › Descriptive Statistics › Crosstabs… – one variable to Row(s), the other to Column(s) (Pallant, 2020, Chapter 16; Field, 2024, Section 19.7.3). Under the Statistics… button: Chi-square, Phi and Cramer's V, and for a 2×2 table also Risk (the odds ratio). Under the Cells… button: Observed, Expected (to check the condition), Row under percentages, and under residuals Standardized (Field) and/or Adjusted standardized (Pallant). Fisher's test is printed automatically for a 2×2 table; for a larger table – the Exact… button. Pallant's syntax, with the addition of the cells and measures that the page shows:

CROSSTABS
  /TABLES=training BY dance
  /FORMAT=AVALUE TABLES
  /STATISTICS=CHISQ PHI RISK
  /CELLS=COUNT ROW EXPECTED SRESID ASRESID
  /COUNT ROUND CELL.

A ready-made frequency table (Field, Section 19.7.2): type a row for each cell – the row code, the column code and the frequency – and weight: Data › Weight Cases… › Weight cases by the frequency variable (WEIGHT BY freq.). This is what the page's syntax produces when the input is a ready-made table, including the value labels. Reading the output: Crosstabulation – Count and % within = Table 1, Expected Count, Std. Residual and Adjusted Residual = Table 2; Chi-Square Tests – Pearson Chi-Square, Continuity Correction (Yates), Likelihood Ratio, Fisher's Exact Test in Exact Sig. (2-sided) and (1-sided) = Table 3, and footnote "a" – how many cells are below 5 and the minimum expected count; Symmetric Measures – Phi, Cramer's V = Table 4; Risk Estimate – Odds Ratio with its confidence interval = Table 4. The Linear-by-Linear Association in the output – a test for a linear trend between two ordinal variables, is not on the page.

And how do you do it in R?

Everything in base R: table for the table, chisq.test with correct = FALSE for Pearson (R's default is Yates's correction in a 2×2 table – the opposite of SPSS), $expected, $residuals (standardized) and $stdres (adjusted), fisher.test. The script was run as is on the sample file and gave the table values (in the comments). The site's rule for Hebrew in R: the file is saved in UTF-8, every comment line starts with an English word, and Hebrew names require a UTF-8 locale (on Windows: R 4.2 or later, or Sys.setlocale('LC_ALL', 'Hebrew')).

# Chi-square test of independence – Professor Stat. This file is saved in UTF-8; every comment line starts with an English word
library(readxl)   # reading Excel

# Loading: the standard template – row = participant, column = variable; the sheet "data"
d <- read_excel("C:/Rdata/DATA_chi_example_eng.xlsx", sheet = "data")

# The crosstabulation: rows = years in community, columns = participation (participants with a missing value in either are dropped)
tab <- table(d$"tenure", d$"participation")
tab
prop.table(tab, 1)                       # percentages of the row

# Pearson's chi-square test without continuity correction (like Pearson Chi-Square in SPSS)
ct <- chisq.test(tab, correct = FALSE)
ct                                       # χ² = 17.79, df = 2, p < .001
ct$expected                              # expected frequencies – checking the condition (no cell below 5)
ct$residuals                             # standardized residuals (O − E)/√E
ct$stdres                                # adjusted standardized residuals (Adjusted Residual in SPSS)

# Cramér's V: √(χ² / (N · min(r − 1, c − 1)))
sqrt(ct$statistic / (sum(tab) * (min(dim(tab)) - 1)))   # V = .242

# A 2×2 table: gender × participation – Yates's correction, Fisher's exact and the odds ratio
tab2 <- table(d$"gender", d$"participation")
chisq.test(tab2, correct = FALSE)$statistic     # Pearson = .023
chisq.test(tab2, correct = TRUE)$statistic      # Yates's correction = .001
fisher.test(tab2)$p.value                       # Fisher two-sided = .907
(tab2[1, 1] * tab2[2, 2]) / (tab2[1, 2] * tab2[2, 1])   # sample odds ratio = .965 (fisher.test prints a conditional estimate, slightly different)

Reporting the results

Report the statistic with the degrees of freedom and N in parentheses, p and the effect size; according to APA (2020, section 6.36) φ and V – which do not exceed 1 – without a leading zero, and χ² itself with two decimals. Field (2024, section 19.8.5): "There was a significant association between the type of training and whether or not cats would dance, χ²(1) = 25.36, p < .001. This seems to represent the fact that, based on the odds ratio, the odds of cats dancing were 6.65 times higher if they were trained with food" – and he recommends also including the crosstabulation. Gravetter and Wallnau (2017, section 17.4) add N and φ: χ²(1, n = 200) = 6.381, p < .05, φ = 0.179, followed by a sentence on the direction. Pallant (2020, chapter 16), with Yates: "A chi-square test for independence (with Yates' Continuity Correction) indicated no significant association between gender and smoking status, χ²(1, n = 436) = .34, p = .56, phi = −.03". The page's report sentences follow Field's approach with N in parentheses like Gravetter and Wallnau; the contrast sentence ("among X 72% were…") is the "direction" that both books require after the numbers. When you report Fisher – write "Fisher's exact test, p = …" without χ².

Common mistakes

Reading the frequencies instead of the percentages. 48 cats that danced after affection is more than 28 after food – and yet food works better, because 162 received affection and only 38 food. Field (section 19.5.3): the percentages reflect the pattern, the frequencies depend on the group sizes.

Checking the condition on the observed frequencies. The condition is on the expected frequency. An observed cell with 2 participants can be perfectly fine; an expected cell of 2.4 – not.

Chi-square on repeated measures. The same 100 people before and after are 100 observations, not 200. McNemar (Pallant, Chapter 16).

Reporting p without percentages and without an effect size. In a sample of 2,000 even a difference of three percentage points is significant; V = .04 tells the reader what the p hides (Figure 1).

Confusing the direction of the odds ratio. OR = 6.65 and OR = .15 are the same finding in opposite directions; say in words whose odds and of what, as in the sentence the page produces.

Exploiting the four rows. Choosing between Pearson, Yates, the likelihood ratio and Fisher according to which gives the smaller p. Decide in advance – the site's rule, or the supervisor's instruction – and report the same row in all the tables in the study.

Collapsing categories after seeing the result. Collapsing is allowed when it has a substantive logic and is documented in advance (Field, Section 19.5.2); collapsing chosen because it makes the test significant is p-hacking.

References

American Psychological Association. (2020). Publication manual of the American Psychological Association (7th ed.). https://doi.org/10.1037/0000165-000

Field, A. (2024). Discovering statistics using IBM SPSS statistics (6th ed.). SAGE.

Gravetter, F. J., & Wallnau, L. B. (2017). Statistics for the behavioral sciences (10th ed.). Cengage Learning.

Pallant, J. (2020). SPSS survival manual: A step by step guide to data analysis using IBM SPSS (7th ed.). Routledge.

Pearson (1900), Fisher (1922), Yates and Howell (2012) – as cited in Field (2024); Cohen (1988) – as cited in Gravetter and Wallnau (2017) and in Pallant (2020). According to the site's rule, the list of sources includes only sources that were read in the original. A note for the reader who compares with the books: Pallant prints .01 as the "small" cutoff of V for two categories – a typo for .10 (the value in Gravetter & Wallnau, Table 17.10, and in Cohen); Gravetter and Wallnau print χ² = 6.381 for example 17.4 – the exact computation gives 6.380 (they rounded the four terms before summing).