Hypothesis tests

Chi-Square Test of Independence Calculator

Assess whether two categorical variables are independent.

Calculator

Enter the contingency table (rows separated by line breaks, columns by commas or spaces).

Result pending…
Expected frequencies pending…

Explanation

The chi-square test of independence lets you evaluate whether there is an association between two categorical variables based on the counts observed in a contingency table. The question it answers is: are the data compatible with both variables being distributed independently, or is there evidence that one variable influences the other?

The core idea is to compare what was observed with what would be expected if the variables were truly independent. If the differences between observed and expected frequencies are too large to attribute to chance, the hypothesis of independence is rejected and it is concluded that an association exists.

For example, a company wants to know whether customer satisfaction (high, medium, low) depends on the purchase channel (physical store, website, app). It collects data from a sample and builds a contingency table. If the test is significant, there is evidence that channel and satisfaction are associated; if it is not, the data are compatible with both variables being independent.

The test statistic aggregates the discrepancies across all cells of the table and follows a chi-square distribution under the null hypothesis. The degrees of freedom depend on the size of the table: \((r-1)(c-1)\), where \(r\) is the number of rows and \(c\) is the number of columns.

Hypotheses and test statistic

\(H_0\): the variables are independent

\(H_1\): there is an association between the variables

\(\chi^2 = \sum_{i,j}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}\),   with \(E_{ij} = \frac{n_{i\cdot} \cdot n_{\cdot j}}{n}\)

Quick check

The chi-square statistic sums, for each cell of the table, the squared difference between observed and expected frequency, divided by the expected frequency. Under the null hypothesis of independence, this statistic approximately follows a \(\chi^2\) distribution with \((r-1)(c-1)\) degrees of freedom:

\(\chi^2 = \sum_{i=1}^{r}\sum_{j=1}^{c} \frac{(O_{ij} - E_{ij})^2}{E_{ij}} \sim \chi^2_{(r-1)(c-1)}\)

Expected frequencies are obtained from the marginal totals of the table. For the cell in row \(i\) and column \(j\):

\(E_{ij} = \frac{n_{i\cdot} \cdot n_{\cdot j}}{n}\)

where \(n_{i\cdot}\) is the total of row \(i\), \(n_{\cdot j}\) is the total of column \(j\), and \(n\) is the overall total of the table.

For the chi-square approximation to be reliable, most expected frequencies should be greater than or equal to 5. If the table is 2×2 with small frequencies, consider the Fisher's exact test.

Worked example

A study looks at the possible relationship between smoking habits and lung disease in a sample of 200 people. The observed contingency table is:

Non-smokers: with disease = 10, without disease = 90 (total 100). Smokers: with disease = 25, without disease = 75 (total 100). Overall totals: with disease = 35, without disease = 165, \(N = 200\).

Expected frequencies under independence are computed as \(E_{ij} = (\text{row total}_i \times \text{column total}_j) / N\). For example, for non-smokers with disease: \(E_{11} = 100 \cdot 35 / 200 = 17.5\). For smokers with disease: \(E_{21} = 100 \cdot 35 / 200 = 17.5\). For non-smokers without disease: \(E_{12} = 100 \cdot 165 / 200 = 82.5\). For smokers without disease: \(E_{22} = 82.5\).

The chi-square statistic accumulates the discrepancies between observed and expected frequencies:

\( \chi^2 = \sum \dfrac{(O_{ij}-E_{ij})^2}{E_{ij}} = \dfrac{(10-17.5)^2}{17.5}+\dfrac{(90-82.5)^2}{82.5}+\dfrac{(25-17.5)^2}{17.5}+\dfrac{(75-82.5)^2}{82.5} \approx 7.79 \)

The degrees of freedom are \(df = (2-1)(2-1) = 1\). The critical value for \(\alpha = 0.05\) is \(\chi^2_{0.05,\,1} \approx 3.84\). Since \(7.79 > 3.84\), \(H_0\) is rejected. The p-value is \(p \approx 0.0052 < 0.05\).

Conclusion: independence is rejected. There is a statistically significant association between smoking habits and the presence of lung disease: smokers show a clearly higher proportion of disease than expected under independence.

How to interpret the result

Rejecting \(H_0\) (p-value < \(\alpha\)) means there is statistical evidence that the two categorical variables are not independent, i.e., that the distribution of one changes depending on the category of the other. However, the chi-square test does not indicate either the direction or the magnitude of the association: it only detects its existence. To quantify the strength, compute Cramér's V \(V = \sqrt{\chi^2 / (n \cdot \min(r-1, c-1))}\), which ranges between 0 (independence) and 1 (perfect association). Values of \(V\) around 0.1, 0.3, and 0.5 are usually interpreted as weak, moderate, and strong association, respectively.

Failing to reject \(H_0\) (p-value ≥ \(\alpha\)) does not prove independence; it only indicates that the data are compatible with it at the chosen level. With large tables or cells with small expected frequencies, power may be insufficient. Check that no expected frequency \(E_{ij} = n \cdot p_{i\cdot} \cdot p_{\cdot j}\) is below 5; if some are, consider merging categories or using Fisher's exact test (for 2×2 tables).

The chi-square statistic is computed by summing the weighted squared discrepancies of all cells: \(\chi^2 = \sum_{ij}(O_{ij}-E_{ij})^2/E_{ij}\). Cells with standardized residuals \((O_{ij}-E_{ij})/\sqrt{E_{ij}}\) whose absolute value exceeds 2 contribute the most and point to where the association is concentrated. In the chart, the green zone is the non-rejection region and the red zone is the right critical tail; the amber line shows the observed statistic.

Frequently asked questions

  • When is the chi-square test of independence used? When you want to know whether two categorical variables are associated based on the counts in a contingency table.
  • How many degrees of freedom does the statistic have? The degrees of freedom are \((r-1)(c-1)\), where \(r\) is the number of rows and \(c\) is the number of columns of the table.
  • What condition must the expected frequencies meet? The approximation is adequate when most expected frequencies are ≥ 5. If there are cells with very low expected frequencies, it's best to use the Fisher's exact test or group categories.
  • What's the difference from the chi-square goodness-of-fit test? The goodness-of-fit test checks whether a variable follows a specific theoretical distribution. The independence test checks whether two variables are associated with each other, without assuming any specific distribution.
  • Can there be association without causation? Yes. The test detects statistical association, but it does not allow you to conclude that one variable causes the other. Causation requires experimental design or additional argumentation.
  • How does sample size affect the result? With very large samples, the test can detect very small associations that are not practically relevant. In those cases it is a good idea to complement the p-value with an effect size measure such as Cramér's V.

Reference: Chi-squared test of independence — Wikipedia