Skip to content

BPower & sample size · power.chisq

Power analysis — χ² tests

How many observations does a χ² test (independence or goodness of fit) need?

Sample size, power or smallest detectable effect (Cohen's w) for χ² tests of independence or goodness of fit with a given number of degrees of freedom, from the noncentral χ² distribution (λ = w²·N).

Verified in Python and R Python and R agree to 6 decimals

A real result

Example “Df1 n”, recorded in both engines. In the app, “Try this example” loads it into Analyze.

Power analysis: χ² testRecorded · Python and R agreeRecorded 2026-11-09 · 9 values compared
R

N = 88, w = 0.30, df = 1, α = 0.05, 1 − β = 0.804

Sample size
88
Power
0.8035
Effect size w
0.3000
Noncentrality λ
7.920

Plots

Power by sample sizeHow does power grow with sample size (or effect size), and where does it reach the target?
View data
PowerEffectSample size
0.0558w = 0.221
0.0883w = 0.226.475
0.1217w = 0.2211.950
0.1556w = 0.2217.425
0.1897w = 0.2222.900
0.2240w = 0.2228.375
0.2581w = 0.2233.850
0.2919w = 0.2239.325
0.3252w = 0.2244.800
0.3579w = 0.2250.275
0.3899w = 0.2255.750
0.4211w = 0.2261.225
0.4514w = 0.2266.700
0.4807w = 0.2272.175
0.5091w = 0.2277.650
0.5365w = 0.2283.125
0.5628w = 0.2288.600
0.5880w = 0.2294.075
0.6122w = 0.2299.550
0.6353w = 0.22105.03
0.6573w = 0.22110.50
0.6784w = 0.22115.97
0.6983w = 0.22121.45
0.7173w = 0.22126.92
0.7353w = 0.22132.40
0.7524w = 0.22137.88
0.7685w = 0.22143.35
0.7837w = 0.22148.82
0.7981w = 0.22154.30
0.8117w = 0.22159.78
0.8244w = 0.22165.25
0.8364w = 0.22170.72
0.8477w = 0.22176.20
0.8583w = 0.22181.68
0.8682w = 0.22187.15
0.8775w = 0.22192.63
0.8863w = 0.22198.10
0.8944w = 0.22203.57
0.9020w = 0.22209.05
0.9091w = 0.22214.53
0.9158w = 0.22220
0.0604w = 0.301
0.1190w = 0.306.475
0.1794w = 0.3011.950
0.2402w = 0.3017.425
0.3004w = 0.3022.900
0.3589w = 0.3028.375
0.4152w = 0.3033.850
0.4687w = 0.3039.325
0.5192w = 0.3044.800
0.5664w = 0.3050.275
0.6103w = 0.3055.750
0.6508w = 0.3061.225
0.6880w = 0.3066.700
0.7220w = 0.3072.175
0.7529w = 0.3077.650
0.7809w = 0.3083.125
0.8062w = 0.3088.600
0.8289w = 0.3094.075
0.8493w = 0.3099.550

First 60 of 123 rows.

Required sample size by power

Effect70% power80% power90% power95% power
w = 0.22122156208257
w = 0.306988117145
w = 0.3844567593
  • Noncentral χ² with λ = w²·N. Required sizes are rounded up and the power is recomputed at the rounded size.
Python

N = 88, w = 0.30, df = 1, α = 0.05, 1 − β = 0.804

Sample size
88
Power
0.8035
Effect size w
0.3000
Noncentrality λ
7.920

To keep this page light, the plots are shown once, under the R tab.

Required sample size by power

Effect70% power80% power90% power95% power
w = 0.22122156208257
w = 0.306988117145
w = 0.3844567593
  • Noncentral χ² with λ = w²·N. Required sizes are rounded up and the power is recomputed at the rounded size.

Do it in CodeSplash Run it on your own data, in Python or R, with the code to prove it.

Worked examples in the app

  • Df1 nNo data needed
  • Df6 powerNo data needed
  • Detectable wNo data needed

In the guides

Where it sits in the decision guides

  • Sample-size planning · Statistics Guide · Step 0 — Prepare your data
  • Categorical–categorical: χ², Fisher, Cramér's V · Statistics Guide · C — Test association
  • More than two groups: χ² independence · Statistics Guide · E — Compare proportions / frequencies
  • Goodness of fit · Statistics Guide · E — Compare proportions / frequencies
  • Sample size and power · Statistics Guide · Additional considerations

Choices you make

hypothesis

Solve fordefault n

Which quantity to compute from the others: the sample size, the power, or the smallest effect you could detect.

  • Sample size(default) — Finds the sample size needed for your power and effect; the usual question before a study.
  • Power — Finds the chance of detecting your effect at this sample; pick it when the sample size is fixed.
  • Smallest detectable effect — Finds the smallest effect this sample can detect; pick it when the sample size is fixed.
Effect size (Cohen's w)default 0.3

0.1 small, 0.3 medium, 0.5 large; for an r × c table w = Cramér's V · √(min(r, c) − 1).

Degrees of freedomdefault 1

(rows − 1)(columns − 1) for independence; categories − 1 for goodness of fit.

Total sample sizedefault 100

Total sample size to assume (ignored when you solve for it).

Target power (1 − β)default 0.8

Chance of detecting the effect if it is real. 0.8 is common; 0.9 is stricter and needs more data.

Significance level (α)default 0.05

The false-alarm rate you accept: the chance of a significant result when there is truly no effect. 0.05 is conventional.

What it reports

n
How many independent units the analysis used; it sets precision and power.
n_exact
How many independent units the analysis used; it sets precision and power.
power
The chance a test detects an effect of a given size, if that effect is real.
target_power
The chance a test detects an effect of a given size, if that effect is real.
w
Effect size for chi-square tests. It measures how far the true cell shares sit from the null's.
df
How much independent information is left to estimate variability after fitting the model.
lambda
Shrinks coefficients towards zero to reduce overfitting; lasso can drop predictors entirely.
crit
The value a statistic must pass for the test to reject at the chosen significance level.
alpha
The false-positive rate you accept before looking at the data, usually 0.05.

Python and R

Python and R agree to 6 decimals (relative tolerance 1e-8). Python uses scipy, codesplash; R uses stats, codesplashr.

Default traps between the two languages

  • Noncentral χ²: scipy uses Boost, R's pchisq(ncp =) its own series (error bound 1e-12); solved sizes/effects from Brent root finding on identical brackets.
Python

The exact script CodeSplash ran for example “Df1 n” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0

# Power analysis — χ² tests
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.chisq 1.0.0 · spec 742905e427c33b11
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.

# %% Setup
import math

from scipy import stats

import codesplash as cs
from codesplash.stats import power as pw  # noncentral-χ² power and Brent solvers (twin of R cs_power_*)

# %% Inputs
solve_for = "n"  # n | power | effect
w = 0.3  # Cohen's w
df = 1
n_given = 100
target = 0.8
alpha = 0.05
warnings = []
lo = 1.0


def power_at(n, eff):
    return pw.chisq_power(eff, n, df, alpha)  # λ = w²·N (statsmodels GofChisquarePower)


# %% Solve
n_exact = None
target_power = None
if solve_for == "n":
    target_power = target
    n_exact = pw.solve_increasing(lambda n: power_at(n, w), target, lo, 10.0, 1e8)
    if math.isnan(n_exact):
        warnings.append("The target power is not reached below 100,000,000 observations.")
    n = pw.ceil_n(n_exact)
elif solve_for == "power":
    n = n_given
else:
    target_power = target
    n = n_given
    w = pw.solve_increasing(lambda e: power_at(n, e), target, 1e-6, 1.0, 100.0)
    if math.isnan(w):
        warnings.append("No effect size up to w = 100 reaches the target power with this sample.")
power = power_at(n, w)
lam = w * w * n
crit = float(stats.chi2.isf(alpha, df))

# %% Curves and sensitivity
n_ref = n if math.isfinite(n) else 100.0
xs = pw.grid(lo, max(2.5 * n_ref, lo + 10.0))
effects = [0.75 * w, w, 1.25 * w] if math.isfinite(w) else [0.3]
curve = [(float(x), power_at(float(x), e), f"w = {e:.2f}") for e in effects for x in xs]
tables = []
if solve_for == "n" and math.isfinite(w):
    rows = [{"effect": f"w = {e:.2f}", **{f"p{p}": pw.ceil_n(pw.solve_increasing(lambda m: power_at(m, e), p / 100, lo, 10.0, 1e8)) for p in (70, 80, 90, 95)}} for e in effects]
    tables.append({
        "key": "sensitivity", "title": "Required sample size by power",
        "columns": [{"key": "effect", "label": "Effect", "type": "string"}] + [{"key": f"p{p}", "label": f"{p}% power", "type": "integer"} for p in (70, 80, 90, 95)],
        "rows": rows,
    })

# %% Result
cs.publish({
    "method": "power.chisq",
    "methodVersion": "1.0.0",
    "title": "Power analysis: χ² test",
    "engine": cs.engine_info("scipy"),
    "summary": f"χ² test with df = {df:g}: n = {n:,.0f}, w = {w:.3f}, power = {power:.3f} at α = {alpha}.",
    "apa": f"N = {n:,.0f}, w = {cs.fmt_num(w)}, df = {df:g}, α = {alpha}, 1 − β = {power:.3f}",
    "statistics": [
        {"key": "n", "label": "Sample size", "value": n, "format": "integer"},
        {"key": "power", "label": "Power", "value": power},
        {"key": "w", "label": "Effect size w", "value": w},
        {"key": "lambda", "label": "Noncentrality λ", "value": lam},
    ],
    "tables": tables,
    "warnings": warnings,
    "notes": ["Noncentral χ² with λ = w²·N. Required sizes are rounded up and the power is recomputed at the rounded size."],
    "charts": [cs.chart(
        "power", "power-curve", "Power by sample size",
        {"x": cs.col("Sample size", [c[0] for c in curve]), "power": cs.col("Power", [c[1] for c in curve]),
         "series": cs.col("Effect", [c[2] for c in curve])},
        controls={"target": target if target_power is not None else 0.8},
    )],
    "values": {"n": n, "n_exact": n_exact, "power": power, "target_power": target_power, "w": w, "df": df, "lambda": lam, "crit": crit, "alpha": alpha},
})
R

The exact script CodeSplash ran for example “Df1 n” · R version 4.5.1 (2025-06-13)

# Power analysis — χ² tests
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.chisq 1.0.0 · spec 742905e427c33b11
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.

# %% Setup
library(codesplashr) # cs_power_chisq() and the Brent solvers (twin of codesplash.stats.power)

# %% Inputs
solve_for <- "n" # n | power | effect
w <- 0.3 # Cohen's w
df <- 1
n_given <- 100
target <- 0.8
alpha <- 0.05
warnings <- list()
lo <- 1

power_at <- function(n, eff) cs_power_chisq(eff, n, df, alpha) # lambda = w^2 N

# %% Solve
n_exact <- NA
target_power <- NA
if (solve_for == "n") {
  target_power <- target
  n_exact <- cs_power_solve_increasing(function(n) power_at(n, w), target, lo, 10, 1e8)
  if (is.nan(n_exact)) warnings <- c(warnings, "The target power is not reached below 100,000,000 observations.")
  n <- cs_power_ceil(n_exact)
} else if (solve_for == "power") {
  n <- n_given
} else {
  target_power <- target
  n <- n_given
  w <- cs_power_solve_increasing(function(e) power_at(n, e), target, 1e-6, 1, 100)
  if (is.nan(w)) warnings <- c(warnings, "No effect size up to w = 100 reaches the target power with this sample.")
}
power <- power_at(n, w)
lambda <- w * w * n
crit <- stats::qchisq(alpha, df, lower.tail = FALSE)

# %% Curves and sensitivity
n_ref <- if (is.finite(n)) n else 100
xs <- cs_power_grid(lo, max(2.5 * n_ref, lo + 10))
effects <- if (is.finite(w)) c(0.75, 1, 1.25) * w else 0.3
curve <- do.call(rbind, lapply(effects, function(e) {
  data.frame(x = xs, power = vapply(xs, function(x) power_at(x, e), numeric(1)), series = sprintf("w = %.2f", e))
}))
tables <- list()
if (solve_for == "n" && is.finite(w)) {
  rows <- lapply(effects, function(e) {
    row <- list(effect = sprintf("w = %.2f", e))
    for (p in c(70, 80, 90, 95)) row[[sprintf("p%d", p)]] <- cs_power_ceil(cs_power_solve_increasing(function(m) power_at(m, e), p / 100, lo, 10, 1e8))
    row
  })
  tables <- list(list(
    key = "sensitivity", title = "Required sample size by power",
    columns = c(list(list(key = "effect", label = "Effect", type = "string")),
                lapply(c(70, 80, 90, 95), function(p) list(key = sprintf("p%d", p), label = sprintf("%d%% power", p), type = "integer"))),
    rows = rows
  ))
}

# %% Result
cs_publish(list(
  method = "power.chisq",
  methodVersion = "1.0.0",
  title = "Power analysis: χ² test",
  engine = cs_engine_info("stats"),
  summary = sprintf("χ² test with df = %s: n = %s, w = %.3f, power = %.3f at alpha = %s.", format(df), format(n, big.mark = ","), w, power, format(alpha)),
  apa = sprintf("N = %s, w = %s, df = %s, α = %s, 1 − β = %.3f", format(n, big.mark = ","), cs_fmt_num(w), format(df), format(alpha), power),
  statistics = list(
    list(key = "n", label = "Sample size", value = n, format = "integer"),
    list(key = "power", label = "Power", value = power),
    list(key = "w", label = "Effect size w", value = w),
    list(key = "lambda", label = "Noncentrality λ", value = lambda)
  ),
  tables = tables,
  warnings = warnings,
  notes = "Noncentral χ² with λ = w²·N. Required sizes are rounded up and the power is recomputed at the rounded size.",
  charts = list(cs_chart(
    "power", "power-curve", "Power by sample size",
    list(x = cs_col("Sample size", curve$x), power = cs_col("Power", curve$power), series = cs_col("Effect", curve$series)),
    controls = list(target = if (is.na(target_power)) 0.8 else target)
  )),
  values = list(n = n, n_exact = n_exact, power = power, target_power = target_power, w = w, df = df, lambda = lambda, crit = crit, alpha = alpha)
))

Sources

  • Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.), chapter 7.

From the CodeSplash method registry · power.chisq v1.0.0.