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).
A real result
Example “Df1 n”, recorded in both engines. In the app, “Try this example” loads it into Analyze.
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
View data
| Power | Effect | Sample size |
|---|---|---|
| 0.0558 | w = 0.22 | 1 |
| 0.0883 | w = 0.22 | 6.475 |
| 0.1217 | w = 0.22 | 11.950 |
| 0.1556 | w = 0.22 | 17.425 |
| 0.1897 | w = 0.22 | 22.900 |
| 0.2240 | w = 0.22 | 28.375 |
| 0.2581 | w = 0.22 | 33.850 |
| 0.2919 | w = 0.22 | 39.325 |
| 0.3252 | w = 0.22 | 44.800 |
| 0.3579 | w = 0.22 | 50.275 |
| 0.3899 | w = 0.22 | 55.750 |
| 0.4211 | w = 0.22 | 61.225 |
| 0.4514 | w = 0.22 | 66.700 |
| 0.4807 | w = 0.22 | 72.175 |
| 0.5091 | w = 0.22 | 77.650 |
| 0.5365 | w = 0.22 | 83.125 |
| 0.5628 | w = 0.22 | 88.600 |
| 0.5880 | w = 0.22 | 94.075 |
| 0.6122 | w = 0.22 | 99.550 |
| 0.6353 | w = 0.22 | 105.03 |
| 0.6573 | w = 0.22 | 110.50 |
| 0.6784 | w = 0.22 | 115.97 |
| 0.6983 | w = 0.22 | 121.45 |
| 0.7173 | w = 0.22 | 126.92 |
| 0.7353 | w = 0.22 | 132.40 |
| 0.7524 | w = 0.22 | 137.88 |
| 0.7685 | w = 0.22 | 143.35 |
| 0.7837 | w = 0.22 | 148.82 |
| 0.7981 | w = 0.22 | 154.30 |
| 0.8117 | w = 0.22 | 159.78 |
| 0.8244 | w = 0.22 | 165.25 |
| 0.8364 | w = 0.22 | 170.72 |
| 0.8477 | w = 0.22 | 176.20 |
| 0.8583 | w = 0.22 | 181.68 |
| 0.8682 | w = 0.22 | 187.15 |
| 0.8775 | w = 0.22 | 192.63 |
| 0.8863 | w = 0.22 | 198.10 |
| 0.8944 | w = 0.22 | 203.57 |
| 0.9020 | w = 0.22 | 209.05 |
| 0.9091 | w = 0.22 | 214.53 |
| 0.9158 | w = 0.22 | 220 |
| 0.0604 | w = 0.30 | 1 |
| 0.1190 | w = 0.30 | 6.475 |
| 0.1794 | w = 0.30 | 11.950 |
| 0.2402 | w = 0.30 | 17.425 |
| 0.3004 | w = 0.30 | 22.900 |
| 0.3589 | w = 0.30 | 28.375 |
| 0.4152 | w = 0.30 | 33.850 |
| 0.4687 | w = 0.30 | 39.325 |
| 0.5192 | w = 0.30 | 44.800 |
| 0.5664 | w = 0.30 | 50.275 |
| 0.6103 | w = 0.30 | 55.750 |
| 0.6508 | w = 0.30 | 61.225 |
| 0.6880 | w = 0.30 | 66.700 |
| 0.7220 | w = 0.30 | 72.175 |
| 0.7529 | w = 0.30 | 77.650 |
| 0.7809 | w = 0.30 | 83.125 |
| 0.8062 | w = 0.30 | 88.600 |
| 0.8289 | w = 0.30 | 94.075 |
| 0.8493 | w = 0.30 | 99.550 |
First 60 of 123 rows.
Required sample size by power
| Effect | 70% power | 80% power | 90% power | 95% power |
|---|---|---|---|---|
| w = 0.22 | 122 | 156 | 208 | 257 |
| w = 0.30 | 69 | 88 | 117 | 145 |
| w = 0.38 | 44 | 56 | 75 | 93 |
- Noncentral χ² with λ = w²·N. Required sizes are rounded up and the power is recomputed at the rounded size.
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
| Effect | 70% power | 80% power | 90% power | 95% power |
|---|---|---|---|---|
| w = 0.22 | 122 | 156 | 208 | 257 |
| w = 0.30 | 69 | 88 | 117 | 145 |
| w = 0.38 | 44 | 56 | 75 | 93 |
- 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.
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},
})
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.