Sample size

Sample size calculator for chi-square test of independence

Calculate the sample size needed for a chi-square test of independence at a target power, using Cohen's \(w\) and the exact noncentral chi-square distribution.

This calculator determines the sample size needed for a chi-square test of independence to detect an association between two categorical variables with the target power, based on Cohen's \(w\) and the dimensions of the table.

Calculator

Enter Cohen's w, the table's degrees of freedom, the significance level and the desired power.

Result pending…
Wizard for 2×2 tables: calculate w from p₁ and p₂

Enter the proportions of the two groups to automatically calculate w and update it in the field above.

Explanation

The chi-square test of independence checks whether two categorical variables are independent in an \(r \times c\) contingency table. Under \(H_0\) the \(\chi^2\) statistic follows a chi-square distribution with \(df = (r-1)(c-1)\) degrees of freedom.

Under the alternative, \(\chi^2\) follows a noncentral chi-square distribution with noncentrality parameter \(\lambda = N \cdot w^2\), where \(N\) is the total sample size and \(w\) is Cohen's \(w\).

Noncentrality parameter and power

\( \lambda = N \cdot w^2 \)

\( \text{Power} = 1 - F_{\chi^2_{df,\,\lambda}}\!\left(\chi^2_{1-\alpha,\,df}\right) \)

The noncentral chi-square distribution is evaluated exactly via the Poisson mixture of central chi-squares:

\( F_{\chi^2_{df,\lambda}}(x) = \sum_{k=0}^{\infty} \frac{e^{-\lambda/2}(\lambda/2)^k}{k!} \cdot F_{\chi^2_{df+2k}}(x) \)

Cohen's w for 2×2 tables

With \(p_1\) and \(p_2\) the proportions in the two groups and \(\bar{p} = (p_1+p_2)/2\):

\( w = \frac{|p_1 - p_2|}{2\sqrt{\bar{p}(1-\bar{p})}} \)

Rough benchmarks according to Cohen (1988): \(w = 0.1\) (small effect), \(w = 0.3\) (medium), \(w = 0.5\) (large).

Quick setup

  • w: 0.1 for subtle effects, 0.3 for moderate effects (the most common), 0.5 for large effects. For 2×2 tables use the wizard.
  • df: (number of rows − 1) × (number of columns − 1). For 2×2 tables, df=1.
  • α: 0.05 is the standard.
  • Power: 0.80 as a common minimum; 0.90 for confirmatory studies.

Worked example

A researcher wants to study whether there is an association between gender (2 categories) and preference for three types of product (3 categories), i.e., a 2×3 table. They expect a medium effect (\(w = 0.3\)) with \(\alpha = 0.05\) and 80% power. The degrees of freedom are \(df = (2-1)(3-1) = 2\).

The calculator returns approximately \(N = 107\) total observations. With that sample, if the real association has an effect size \(w \geq 0.3\), the test will detect it 80% of the time.

2×2 table with the wizard: if p₁ = 0.40 and p₂ = 0.60, then \(\bar{p} = 0.50\) and \(w = |0.40-0.60|/(2\sqrt{0.5 \times 0.5}) = 0.20/1.00 = 0.20\). With df=1, α=0.05 and power=0.80, N ≈ 197 observations are needed.

Model assumptions

  • Random sample of independent observations.
  • Two categorical variables crossed in a contingency table; each unit contributes to a single cell.
  • Expected frequencies \(\ge 5\) in most cells.
  • Effect measured with Cohen's \(w\) and \((\text{rows}-1)(\text{columns}-1)\) degrees of freedom on the noncentral chi-square.

How to interpret the result

The value \(N\) is the total number of observations (sum of all cells in the contingency table) needed for the chi-square test of independence to detect the specified association with the desired power and significance level. Always round up. In a design where the size of each row is fixed (e.g., treatment and control groups), divide \(N\) by the number of rows to get the size of each group; in unrestricted sampling, \(N\) is simply the total number of subjects. Add a margin for dropout by dividing by \((1 - \text{dropout rate})\).

Cohen's effect size \(w\) is calculated from the expected frequencies under \(H_1\): \(w = \sqrt{\sum_{ij}(p_{1,ij} - p_{0,ij})^2 / p_{0,ij}}\), where \(p_{0,ij} = p_{i\cdot} \times p_{\cdot j}\) are the proportions under independence. Reference values are \(w = 0.10\) (small effect), \(w = 0.30\) (medium) and \(w = 0.50\) (large). The most important applicability condition is that the expected frequencies in each cell under \(H_0\) — computed as \(N \times p_{0,ij}\) — are all \(\geq 5\); otherwise, the chi-square statistic may not follow the chi-square distribution and the p-value would be unreliable. When several cells have low expected frequencies, merge substantively meaningful categories and reduce the degrees of freedom \((r-1)(c-1)\) of the test.

If the resulting \(N\) is impractical, check whether the specified effect size is realistic: a very weak association may require thousands of observations. Consider whether the range of variation of one of the categorical variables could be widened, or whether some categories could be merged to concentrate the effect. When the table is \(2 \times 2\) and \(N\) is small, Fisher's exact test is more appropriate than chi-square; in that case, use the sample size calculator for Fisher's exact test. Once the data has been collected, run the analysis with the chi-square test of independence calculator and interpret the standardized residuals of each cell to locate the source of the association.

References

  • Cohen, J. (1988). Statistical Power Analysis for the Behavioral Sciences (2nd ed.). Lawrence Erlbaum Associates.
  • Agresti, A. (2013). Categorical Data Analysis (3rd ed.). Wiley.