Skip to content

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).

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

A real result

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

Power analysis: multiple regressionRecorded · Python and R agreeRecorded 2026-11-09 · 11 values compared · largest relative difference 2.5e-10
R

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

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.0546f² = 0.1125
0.0878f² = 0.1129.688
0.1258f² = 0.11214.375
0.1664f² = 0.11219.063
0.2089f² = 0.11223.750
0.2526f² = 0.11228.438
0.2970f² = 0.11233.125
0.3414f² = 0.11237.813
0.3855f² = 0.11242.500
0.4287f² = 0.11247.188
0.4707f² = 0.11251.875
0.5113f² = 0.11256.563
0.5502f² = 0.11261.250
0.5872f² = 0.11265.938
0.6223f² = 0.11270.625
0.6554f² = 0.11275.313
0.6864f² = 0.11280
0.7153f² = 0.11284.688
0.7422f² = 0.11289.375
0.7671f² = 0.11294.063
0.7901f² = 0.11298.750
0.8112f² = 0.112103.44
0.8305f² = 0.112108.13
0.8481f² = 0.112112.81
0.8642f² = 0.112117.50
0.8788f² = 0.112122.19
0.8920f² = 0.112126.88
0.9040f² = 0.112131.56
0.9147f² = 0.112136.25
0.9244f² = 0.112140.94
0.9331f² = 0.112145.63
0.9409f² = 0.112150.31
0.9478f² = 0.112155
0.9540f² = 0.112159.69
0.9595f² = 0.112164.38
0.9644f² = 0.112169.06
0.9688f² = 0.112173.75
0.9726f² = 0.112178.44
0.9760f² = 0.112183.13
0.9790f² = 0.112187.81
0.9817f² = 0.112192.50
0.0560f² = 0.1505
0.1013f² = 0.1509.688
0.1542f² = 0.15014.375
0.2107f² = 0.15019.063
0.2695f² = 0.15023.750
0.3289f² = 0.15028.438
0.3878f² = 0.15033.125
0.4452f² = 0.15037.813
0.5002f² = 0.15042.500
0.5524f² = 0.15047.188
0.6013f² = 0.15051.875
0.6466f² = 0.15056.563
0.6882f² = 0.15061.250
0.7262f² = 0.15065.938
0.7606f² = 0.15070.625
0.7915f² = 0.15075.313
0.8191f² = 0.15080
0.8436f² = 0.15084.688
0.8652f² = 0.15089.375

First 60 of 123 rows.

Required sample size by power

Effect70% power80% power90% power95% power
f² = 0.11283101130157
f² = 0.150637799119
f² = 0.18851638096
  • 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.
Python

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

Effect70% power80% power90% power95% power
f² = 0.11283101130157
f² = 0.150637799119
f² = 0.18851638096
  • 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.
Python

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},
})
R

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.