BPower & sample size · power.regression
Power analysis — multiple regression
How many observations does a regression F test need?
Sample size, power or smallest detectable effect (Cohen's f²) for the F test of a set of regression coefficients — the whole model or a subset of predictors — from the noncentral F distribution with λ = f²·(u + v + 1) (pwr::pwr.f2.test, statsmodels FTestPower).
A real result
Example “Whole model n”, recorded in both engines. In the app, “Try this example” loads it into Analyze.
N = 77, f² = 0.15, F(3, 73), α = 0.05, 1 − β = 0.802
- Sample size
- 77
- Power
- 0.8018
- Effect size f²
- 0.1500
- R² equivalent
- 0.1304
- Noncentrality λ
- 11.550
Plots
View data
| Power | Effect | Sample size |
|---|---|---|
| 0.0546 | f² = 0.112 | 5 |
| 0.0878 | f² = 0.112 | 9.688 |
| 0.1258 | f² = 0.112 | 14.375 |
| 0.1664 | f² = 0.112 | 19.063 |
| 0.2089 | f² = 0.112 | 23.750 |
| 0.2526 | f² = 0.112 | 28.438 |
| 0.2970 | f² = 0.112 | 33.125 |
| 0.3414 | f² = 0.112 | 37.813 |
| 0.3855 | f² = 0.112 | 42.500 |
| 0.4287 | f² = 0.112 | 47.188 |
| 0.4707 | f² = 0.112 | 51.875 |
| 0.5113 | f² = 0.112 | 56.563 |
| 0.5502 | f² = 0.112 | 61.250 |
| 0.5872 | f² = 0.112 | 65.938 |
| 0.6223 | f² = 0.112 | 70.625 |
| 0.6554 | f² = 0.112 | 75.313 |
| 0.6864 | f² = 0.112 | 80 |
| 0.7153 | f² = 0.112 | 84.688 |
| 0.7422 | f² = 0.112 | 89.375 |
| 0.7671 | f² = 0.112 | 94.063 |
| 0.7901 | f² = 0.112 | 98.750 |
| 0.8112 | f² = 0.112 | 103.44 |
| 0.8305 | f² = 0.112 | 108.13 |
| 0.8481 | f² = 0.112 | 112.81 |
| 0.8642 | f² = 0.112 | 117.50 |
| 0.8788 | f² = 0.112 | 122.19 |
| 0.8920 | f² = 0.112 | 126.88 |
| 0.9040 | f² = 0.112 | 131.56 |
| 0.9147 | f² = 0.112 | 136.25 |
| 0.9244 | f² = 0.112 | 140.94 |
| 0.9331 | f² = 0.112 | 145.63 |
| 0.9409 | f² = 0.112 | 150.31 |
| 0.9478 | f² = 0.112 | 155 |
| 0.9540 | f² = 0.112 | 159.69 |
| 0.9595 | f² = 0.112 | 164.38 |
| 0.9644 | f² = 0.112 | 169.06 |
| 0.9688 | f² = 0.112 | 173.75 |
| 0.9726 | f² = 0.112 | 178.44 |
| 0.9760 | f² = 0.112 | 183.13 |
| 0.9790 | f² = 0.112 | 187.81 |
| 0.9817 | f² = 0.112 | 192.50 |
| 0.0560 | f² = 0.150 | 5 |
| 0.1013 | f² = 0.150 | 9.688 |
| 0.1542 | f² = 0.150 | 14.375 |
| 0.2107 | f² = 0.150 | 19.063 |
| 0.2695 | f² = 0.150 | 23.750 |
| 0.3289 | f² = 0.150 | 28.438 |
| 0.3878 | f² = 0.150 | 33.125 |
| 0.4452 | f² = 0.150 | 37.813 |
| 0.5002 | f² = 0.150 | 42.500 |
| 0.5524 | f² = 0.150 | 47.188 |
| 0.6013 | f² = 0.150 | 51.875 |
| 0.6466 | f² = 0.150 | 56.563 |
| 0.6882 | f² = 0.150 | 61.250 |
| 0.7262 | f² = 0.150 | 65.938 |
| 0.7606 | f² = 0.150 | 70.625 |
| 0.7915 | f² = 0.150 | 75.313 |
| 0.8191 | f² = 0.150 | 80 |
| 0.8436 | f² = 0.150 | 84.688 |
| 0.8652 | f² = 0.150 | 89.375 |
First 60 of 123 rows.
Required sample size by power
| Effect | 70% power | 80% power | 90% power | 95% power |
|---|---|---|---|---|
| f² = 0.112 | 83 | 101 | 130 | 157 |
| f² = 0.150 | 63 | 77 | 99 | 119 |
| f² = 0.188 | 51 | 63 | 80 | 96 |
- Noncentral F with λ = f²·(u + v + 1), v = N − p − 1 (G*Power's λ = f²·N is the same when every predictor is tested). Required sizes are rounded up and the power is recomputed at the rounded size.
N = 77, f² = 0.15, F(3, 73), α = 0.05, 1 − β = 0.802
- Sample size
- 77
- Power
- 0.8018
- Effect size f²
- 0.1500
- R² equivalent
- 0.1304
- Noncentrality λ
- 11.550
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 |
|---|---|---|---|---|
| f² = 0.112 | 83 | 101 | 130 | 157 |
| f² = 0.150 | 63 | 77 | 99 | 119 |
| f² = 0.188 | 51 | 63 | 80 | 96 |
- Noncentral F with λ = f²·(u + v + 1), v = N − p − 1 (G*Power's λ = f²·N is the same when every predictor is tested). 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
- Whole model nNo data needed
- Subset powerNo data needed
- Detectable f2No data needed
In the guides
Where it sits in the decision guides
- Sample-size planning · Statistics Guide · Step 0 — Prepare your data
- Continuous outcome: linear regression · Statistics Guide · D — Predict an outcome
- 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 f²)default 0.15
f² = R² / (1 − R²) for the whole model, or ΔR² / (1 − R²_full) for a subset. 0.02 small, 0.15 medium, 0.35 large.
- Predictors in the model (p)default 3
Number of predictors in the full model.
- Predictors tested (u)default 3
u = p tests the whole model (R²); fewer tests a subset (ΔR²).
- Sample size (N)default 80
Total sample size N 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.
- f2
- Effect size for a regression: the variance a model or term explains relative to what's left unexplained.
- r2
- The share of the outcome's variance explained by the model, from 0 to 1.
- u
- How much independent information is left to estimate variability after fitting the model.
- v
- 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-7). Python uses scipy, codesplash; R uses stats, codesplashr.
Default traps between the two languages
- Noncentral F: scipy uses Boost; R's pf(ncp =) uses AS 226 with an error bound of 1e-9, so power agrees to ~1e-9 and solved sizes/effects to ~1e-8.
The exact script CodeSplash ran for example “Whole model n” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Power analysis — multiple regression
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.regression 1.0.0 · spec a6b482c527851ff0
# 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-F power and Brent solvers (twin of R cs_power_*)
# %% Inputs
solve_for = "n" # n | power | effect
f2 = 0.15 # Cohen's f²
p = 3 # predictors in the model
u = 3 # predictors tested (numerator df)
n_given = 80
target = 0.8
alpha = 0.05
warnings = []
if u > p:
raise ValueError(f"Predictors tested ({u:g}) cannot exceed the predictors in the model ({p:g}).")
lo = p + 2.0 # residual df v = N − p − 1 ≥ 1
def power_at(n, eff):
return pw.f2_power(eff, n, u, p, alpha) # λ = f²·(u + v + 1) (pwr.f2.test, statsmodels FTestPower)
# %% Solve
n_exact = None
target_power = None
if solve_for == "n":
target_power = target
n_exact = pw.solve_increasing(lambda n: power_at(n, f2), target, lo, max(2.0 * lo, 10.0), 1e7)
if math.isnan(n_exact):
warnings.append("The target power is not reached below 10,000,000 observations.")
n = pw.ceil_n(n_exact)
elif solve_for == "power":
n = n_given
if n < lo:
warnings.append(f"N must exceed the number of predictors + 1 ({lo:g}).")
else:
target_power = target
n = n_given
f2 = pw.solve_increasing(lambda e: power_at(n, e), target, 1e-6, 1.0, 1000.0)
if math.isnan(f2):
warnings.append("No effect size up to f² = 1000 reaches the target power with this sample.")
power = power_at(n, f2)
v = n - p - 1.0
lam = f2 * (u + v + 1.0)
crit = float(stats.f.isf(alpha, u, v)) if v > 0 else math.nan
r2 = f2 / (1.0 + f2)
# %% 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 * f2, f2, 1.25 * f2] if math.isfinite(f2) else [0.15]
curve = [(float(x), power_at(float(x), e), f"f² = {e:.3f}") for e in effects for x in xs]
tables = []
if solve_for == "n" and math.isfinite(f2):
rows = [{"effect": f"f² = {e:.3f}", **{f"p{q}": pw.ceil_n(pw.solve_increasing(lambda m: power_at(m, e), q / 100, lo, max(2.0 * lo, 10.0), 1e7)) for q 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{q}", "label": f"{q}% power", "type": "integer"} for q in (70, 80, 90, 95)],
"rows": rows,
})
# %% Result
cs.publish({
"method": "power.regression",
"methodVersion": "1.0.0",
"title": "Power analysis: multiple regression",
"engine": cs.engine_info("scipy"),
"summary": f"Regression F test of {u:g} of {p:g} predictors: N = {n:,.0f}, f² = {f2:.4f}, power = {power:.3f} at α = {alpha}.",
"apa": f"N = {n:,.0f}, f² = {cs.fmt_num(f2)}, F({u:g}, {v:g}), α = {alpha}, 1 − β = {power:.3f}",
"statistics": [
{"key": "n", "label": "Sample size", "value": n, "format": "integer"},
{"key": "power", "label": "Power", "value": power},
{"key": "f2", "label": "Effect size f²", "value": f2},
{"key": "r2", "label": "R² equivalent", "value": r2},
{"key": "lambda", "label": "Noncentrality λ", "value": lam},
],
"tables": tables,
"warnings": warnings,
"notes": ["Noncentral F with λ = f²·(u + v + 1), v = N − p − 1 (G*Power's λ = f²·N is the same when every predictor is tested). 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, "f2": f2, "r2": r2, "u": u, "v": v, "lambda": lam, "crit": crit, "alpha": alpha},
})
The exact script CodeSplash ran for example “Whole model n” · R version 4.5.1 (2025-06-13)
# Power analysis — multiple regression
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.regression 1.0.0 · spec a6b482c527851ff0
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_power_f2() and the Brent solvers (twin of codesplash.stats.power)
# %% Inputs
solve_for <- "n" # n | power | effect
f2 <- 0.15 # Cohen's f^2
p <- 3 # predictors in the model
u <- 3 # predictors tested (numerator df)
n_given <- 80
target <- 0.8
alpha <- 0.05
warnings <- list()
if (u > p) stop(sprintf("Predictors tested (%s) cannot exceed the predictors in the model (%s).", format(u), format(p)))
lo <- p + 2 # residual df v = N - p - 1 >= 1
power_at <- function(n, eff) cs_power_f2(eff, n, u, p, alpha) # lambda = f2 (u + v + 1) (pwr.f2.test)
# %% 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, f2), target, lo, max(2 * lo, 10), 1e7)
if (is.nan(n_exact)) warnings <- c(warnings, "The target power is not reached below 10,000,000 observations.")
n <- cs_power_ceil(n_exact)
} else if (solve_for == "power") {
n <- n_given
if (n < lo) warnings <- c(warnings, sprintf("N must exceed the number of predictors + 1 (%s).", format(lo)))
} else {
target_power <- target
n <- n_given
f2 <- cs_power_solve_increasing(function(e) power_at(n, e), target, 1e-6, 1, 1000)
if (is.nan(f2)) warnings <- c(warnings, "No effect size up to f² = 1000 reaches the target power with this sample.")
}
power <- power_at(n, f2)
v <- n - p - 1
lambda <- f2 * (u + v + 1)
crit <- if (v > 0) stats::qf(alpha, u, v, lower.tail = FALSE) else NaN
r2 <- f2 / (1 + f2)
# %% 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(f2)) c(0.75, 1, 1.25) * f2 else 0.15
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("f² = %.3f", e))
}))
tables <- list()
if (solve_for == "n" && is.finite(f2)) {
rows <- lapply(effects, function(e) {
row <- list(effect = sprintf("f² = %.3f", e))
for (q in c(70, 80, 90, 95)) row[[sprintf("p%d", q)]] <- cs_power_ceil(cs_power_solve_increasing(function(m) power_at(m, e), q / 100, lo, max(2 * lo, 10), 1e7))
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(q) list(key = sprintf("p%d", q), label = sprintf("%d%% power", q), type = "integer"))),
rows = rows
))
}
# %% Result
cs_publish(list(
method = "power.regression",
methodVersion = "1.0.0",
title = "Power analysis: multiple regression",
engine = cs_engine_info("stats"),
summary = sprintf("Regression F test of %s of %s predictors: N = %s, f² = %.4f, power = %.3f at alpha = %s.", format(u), format(p), format(n, big.mark = ","), f2, power, format(alpha)),
apa = sprintf("N = %s, f² = %s, F(%s, %s), α = %s, 1 − β = %.3f", format(n, big.mark = ","), cs_fmt_num(f2), format(u), format(v), format(alpha), power),
statistics = list(
list(key = "n", label = "Sample size", value = n, format = "integer"),
list(key = "power", label = "Power", value = power),
list(key = "f2", label = "Effect size f²", value = f2),
list(key = "r2", label = "R² equivalent", value = r2),
list(key = "lambda", label = "Noncentrality λ", value = lambda)
),
tables = tables,
warnings = warnings,
notes = "Noncentral F with λ = f²·(u + v + 1), v = N − p − 1 (G*Power's λ = f²·N is the same when every predictor is tested). 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, f2 = f2, r2 = r2, u = u, v = v, lambda = lambda, crit = crit, alpha = alpha)
))
Sources
- Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.), chapter 9.
From the CodeSplash method registry · power.regression v1.0.0.