Skip to content

Type I and II errors, and power

Two ways to be wrong, and how sample size trades them off. Plan a study so that it can actually answer your question.

Introductory ~12 min8 steps

What runs in CodeSplash

You will be able to

  • Define Type I error, Type II error and power
  • Say how effect size, sample size and α change power
  • Plan a sample size for a stated effect, and avoid "observed power"

Read first

01

Two ways to be wrong

Read

Every yes-or-no test has four possible outcomes.

No real effectA real effect
Test says "significant"Type I errorType I errorRejecting H₀ when it is actually true — a false alarm. (rate α)Correct
Test says "not significant"CorrectType II errorType II errorMissing a real effect — failing to reject H₀ when it is false. (rate β)

PowerStatistical powerThe chance a test detects an effect of a given size, if that effect is real. is 1−β1 - \beta: the chance of detecting an effect of a given size, if it's really there. You choose α directly. Power you get by design.

02

What raises power most?

Predict

Predict first

A two-group study has moderate power. Which change raises power the most?

Doubling the sample sizeDoubling the true effectMoving α from .05 to .10
Reveal

Doubling the effect. The signal-to-noise ratio grows in proportion to the effect, but only with the square root of nn. Doubling the effect is worth as much as four times the sample, which is why measuring well and choosing a strong contrast matter so much.

03

Explore power

Explore

Guess the power first. Then try the challenge: find the smallest n per group that gives 80% power for d=0.5d = 0.5. Raise Outcomes tested (Bonferroni) and see what testing many outcomes costs.

BExplorable · Power explorer

How likely is my study to detect an effect that is really there?

Guess the power: the chance this study rejects H0 if the effect is real.

0.50
How sure?
0.50

Standardized difference between the group means.

30

α

Alternative

1

Each outcome is tested at α ÷ this number.

E[t] = 1.94H₁

Solid: t under H₀. Dashed: t if the effect is real. Shaded orange: power, the alternative's area inside the hatched rejection region.

80%

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

04

Plan the number

Read

With d = 0.5, α = 0.05 and a target of 0.8, you need 64 per group, or 128 in total.

The hard input is dd. Use the smallest effect that matters to you, rather than an optimistic guess or a pilot's noisy estimate.

05

Many studies, one rule

Explore

Low power hurts more than missed effects. When few tested ideas are true, and power is low, many significant results are false alarms. Change Power and Share of studies with a real effect, and watch False discoveries.

BExplorable · Truth and decision: the four quadrants

Out of many studies, how many significant results are real?

Of the significant results, what share will be false alarms?

0.50
How sure?
0.10
0.50
0.050
1000
  • Real effect, significant: 50
  • No effect, significant (false alarm): 45
  • Real effect, missed: 50
  • No effect, not significant: 855

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

06

Check your reading

Checkpoint

Check yourself

A new drug truly works, but your trial finds p = 0.30. What kind of error is that?

Show the answer

Answer: b

A real effect went undetected. You can't tell this happened from the trial alone, which is why power is planned in advance.

Check yourself

How many subjects per group does a two-sided t test need for 80% power at d = 0.5 and α = .05?

Show the answer

Answer: 64

The exact answer is a little under 64, and you round up to whole subjects.

Check yourself

You planned for d = 0.5. Your budget changes, and you want to plan for d = 0.25 with the same power. About how many subjects will you need?

Show the answer

Answer: c

Sample size grows with one over the square of the effect. Half the effect takes four times the data.

07

Plan it in the app

In the app

Analyze has power analyses for t tests, proportions, ANOVA, correlations, regressions and survival. Use Solve for to get the sample size, the power or the detectable effect.

Power analysis: two-sample t-testRecorded · Python and R agreeRecorded 2026-11-09 · 11 values compared
R

n = 64 + 64 (N = 128), d = 0.50, α = 0.05, 1 − β = 0.801

Group 1 / pairs / participants
64
Group 2
64
Total
128
Power
0.8015
Effect size d
0.5000
df
126
Noncentrality δ
2.828

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 (group 1)
0.0565d = 0.382
0.0902d = 0.385.950
0.1240d = 0.389.900
0.1581d = 0.3813.850
0.1925d = 0.3817.800
0.2268d = 0.3821.750
0.2610d = 0.3825.700
0.2949d = 0.3829.650
0.3282d = 0.3833.600
0.3609d = 0.3837.550
0.3929d = 0.3841.500
0.4241d = 0.3845.450
0.4544d = 0.3849.400
0.4837d = 0.3853.350
0.5121d = 0.3857.300
0.5394d = 0.3861.250
0.5656d = 0.3865.200
0.5908d = 0.3869.150
0.6149d = 0.3873.100
0.6379d = 0.3877.050
0.6599d = 0.3881
0.6808d = 0.3884.950
0.7007d = 0.3888.900
0.7196d = 0.3892.850
0.7376d = 0.3896.800
0.7545d = 0.38100.75
0.7706d = 0.38104.70
0.7857d = 0.38108.65
0.8000d = 0.38112.60
0.8135d = 0.38116.55
0.8262d = 0.38120.50
0.8381d = 0.38124.45
0.8493d = 0.38128.40
0.8598d = 0.38132.35
0.8697d = 0.38136.30
0.8789d = 0.38140.25
0.8875d = 0.38144.20
0.8956d = 0.38148.15
0.9032d = 0.38152.10
0.9102d = 0.38156.05
0.9168d = 0.38160
0.0615d = 0.502
0.1223d = 0.505.950
0.1835d = 0.509.900
0.2448d = 0.5013.850
0.3051d = 0.5017.800
0.3636d = 0.5021.750
0.4199d = 0.5025.700
0.4733d = 0.5029.650
0.5236d = 0.5033.600
0.5707d = 0.5037.550
0.6143d = 0.5041.500
0.6546d = 0.5045.450
0.6916d = 0.5049.400
0.7253d = 0.5053.350
0.7560d = 0.5057.300
0.7837d = 0.5061.250
0.8088d = 0.5065.200
0.8313d = 0.5069.150
0.8515d = 0.5073.100

First 60 of 123 rows.

Required size of group 1 (or pairs / participants) by power

Effect70% power80% power90% power95% power
d = 0.3889113151186
d = 0.50516486105
d = 0.6233425568
  • Noncentral t; two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.
  • Group 2 = ceiling(1 × group 1).
The exact R script · R R version 4.5.1 (2025-06-13)
# Power analysis — t tests
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.ttest 1.0.0 · spec 91cdd5784bea1eda
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.

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

# %% Inputs
solve_for <- "n" # n | power | effect
design <- "two-sample" # two-sample | paired | one-sample
alpha <- 0.05
alternative <- "two-sided" # two-sided adds both rejection tails (power.t.test(strict = TRUE))
two <- design == "two-sample"
ratio <- if (two) 1 else NA # n2 / n1
d <- 0.5
n_given <- 30
target <- 0.8
warnings <- list()
lo <- if (two) max(2, 2 / ratio) else 2 # smallest size with a defined test (n2 >= 2 too)

power_at <- function(n1, eff, n2 = NULL) {
  # Power with n1 in group 1 (n2 = ratio * n1 unless given) - noncentral t
  if (two) cs_power_t(eff, n1, if (is.null(n2)) ratio * n1 else n2, alpha, alternative)
  else cs_power_t(eff, n1, NULL, alpha, alternative)
}

# Equivalent idiom for equal groups: power.t.test(delta = d, sd = 1, sig.level = alpha, power = target,
# type = "two.sample", strict = TRUE) - solved here with tighter tolerances so R and Python agree.
# %% 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, d), 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 participants.")
  n1 <- cs_power_ceil(n_exact)
} else if (solve_for == "power") {
  n1 <- n_given
} else {
  target_power <- target
  n1 <- n_given
  d <- cs_power_solve_increasing(function(e) power_at(n1, e), target, 1e-6, 1, 1000)
  if (is.nan(d)) warnings <- c(warnings, "No effect size up to d = 1000 reaches the target power with this sample.")
}
n2 <- if (two) cs_power_ceil(ratio * n1) else NA
power <- power_at(n1, d, if (two) n2 else NULL)
n_total <- if (two) n1 + n2 else n1
df <- if (two) n1 + n2 - 2 else n1 - 1
ncp <- if (two) abs(d) * sqrt(n1 * n2 / (n1 + n2)) else abs(d) * sqrt(n1)
crit <- stats::qt(alpha / (if (alternative == "two-sided") 2 else 1), df, lower.tail = FALSE)
if (is.finite(n1) && (n1 < 2 || (two && n2 < 2))) warnings <- c(warnings, "Each group needs at least two participants.")

# %% Curves and sensitivity
labels <- c("two-sample" = "two-sample t-test", "paired" = "paired t-test", "one-sample" = "one-sample t-test")
n_ref <- if (is.finite(n1)) n1 else 100
xs <- cs_power_grid(lo, max(2.5 * n_ref, lo + 10))
effects <- if (is.finite(d)) c(0.75 * d, d, 1.25 * d) else 0.5
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("d = %.2f", e))
}))
tables <- list()
if (solve_for == "n" && is.finite(d)) {
  rows <- lapply(effects, function(e) {
    row <- list(effect = sprintf("d = %.2f", e))
    for (tp in c(0.7, 0.8, 0.9, 0.95)) {
      row[[sprintf("p%d", round(tp * 100))]] <-
        cs_power_ceil(cs_power_solve_increasing(function(n) power_at(n, e), tp, lo, max(2 * lo, 10), 1e7))
    }
    row
  })
  tables <- list(list(
    key = "sensitivity", title = "Required size of group 1 (or pairs / participants) 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
size_text <- if (two) sprintf("%s + %s (N = %s)", format(n1, big.mark = ","), format(n2, big.mark = ","), format(n_total, big.mark = ",")) else format(n1, big.mark = ",")
stats_list <- list(
  list(key = "n", label = if (two) "Group 1 / pairs / participants" else if (design == "paired") "Pairs" else "Participants", value = n1, format = "integer")
)
if (two) {
  stats_list <- c(stats_list, list(list(key = "n2", label = "Group 2", value = n2, format = "integer"),
                                   list(key = "n_total", label = "Total", value = n_total, format = "integer")))
}
stats_list <- c(stats_list, list(
  list(key = "power", label = "Power", value = power),
  list(key = "d", label = "Effect size d", value = d),
  list(key = "df", label = "df", value = df),
  list(key = "ncp", label = "Noncentrality δ", value = ncp)
))
cs_publish(list(
  method = "power.ttest",
  methodVersion = "1.0.0",
  title = sprintf("Power analysis: %s", labels[[design]]),
  engine = cs_engine_info("stats"),
  summary = sprintf("%s: n = %s, d = %.3f, power = %.3f at %s alpha = %s.", labels[[design]], size_text, d, power, alternative, format(alpha)),
  apa = sprintf("n = %s, d = %s, α = %s, 1 − β = %.3f", size_text, cs_fmt_num(d), format(alpha), power),
  statistics = stats_list,
  tables = tables,
  warnings = warnings,
  notes = c("Noncentral t; two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.",
            if (two) sprintf("Group 2 = ceiling(%s × group 1).", format(ratio))),
  charts = list(cs_chart(
    "power", "power-curve", "Power by sample size",
    list(x = cs_col(if (two) "Sample size (group 1)" else "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 = n1, n2 = n2, n_total = n_total, n_exact = n_exact, power = power, target_power = target_power,
    d = d, df = df, ncp = ncp, crit = crit, alpha = alpha
  )
))
Python

n = 64 + 64 (N = 128), d = 0.50, α = 0.05, 1 − β = 0.801

Group 1 / pairs / participants
64
Group 2
64
Total
128
Power
0.8015
Effect size d
0.5000
df
126
Noncentrality δ
2.828

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

Required size of group 1 (or pairs / participants) by power

Effect70% power80% power90% power95% power
d = 0.3889113151186
d = 0.50516486105
d = 0.6233425568
  • Noncentral t; two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.
  • Group 2 = ⌈1 × group 1⌉.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Power analysis — t tests
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.ttest 1.0.0 · spec 91cdd5784bea1eda
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.

# %% Setup
import math

import numpy as np
from scipy import stats

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

# %% Inputs
solve_for = "n"  # n | power | effect
design = "two-sample"  # two-sample | paired | one-sample
alpha = 0.05
alternative = "two-sided"  # two-sided adds both rejection tails (power.t.test(strict = TRUE))
ratio = 1 if design == "two-sample" else None  # n2 / n1
d = 0.5
n_given = 30
target = 0.8
warnings = []
two = design == "two-sample"
lo = max(2.0, 2.0 / ratio) if two else 2.0  # smallest size with a defined test (n2 ≥ 2 too)


def power_at(n1, eff, n2=None):
    """Power with n1 in group 1 (n2 = ratio · n1 unless given) — noncentral t."""
    if two:
        return pw.t_power(eff, n1, ratio * n1 if n2 is None else n2, alpha, alternative)
    return pw.t_power(eff, n1, None, alpha, alternative)


# Equivalent idiom: statsmodels.stats.power.TTestIndPower().solve_power(effect_size=d, alpha=alpha,
# power=target, ratio=ratio) — solved here with tighter tolerances so Python and R agree.
# %% Solve
n_exact = None
target_power = None
if solve_for == "n":
    target_power = target
    n_exact = pw.solve_increasing(lambda n: power_at(n, d), 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 participants.")
    n1 = pw.ceil_n(n_exact)
elif solve_for == "power":
    n1 = n_given
else:
    target_power = target
    n1 = n_given
    d = pw.solve_increasing(lambda e: power_at(n1, e), target, 1e-6, 1.0, 1000.0)
    if math.isnan(d):
        warnings.append("No effect size up to d = 1000 reaches the target power with this sample.")
n2 = pw.ceil_n(ratio * n1) if two else None
power = power_at(n1, d, n2)
n_total = n1 + n2 if two else n1
df = n1 + n2 - 2.0 if two else n1 - 1.0
ncp = abs(d) * math.sqrt(n1 * n2 / (n1 + n2)) if two else abs(d) * math.sqrt(n1)
crit = float(stats.t.isf(alpha / (2 if alternative == "two-sided" else 1), df))
if n1 < 2 or (two and n2 < 2):
    warnings.append("Each group needs at least two participants.")

# %% Curves and sensitivity
labels = {"two-sample": "two-sample t-test", "paired": "paired t-test", "one-sample": "one-sample t-test"}
n_ref = n1 if math.isfinite(n1) else 100.0
xs = pw.grid(lo, max(2.5 * n_ref, lo + 10.0))
effects = [0.75 * d, d, 1.25 * d] if math.isfinite(d) else [0.5]
curve_x, curve_power, curve_series = [], [], []
for e in effects:
    for x in xs:
        curve_x.append(float(x))
        curve_power.append(power_at(float(x), e))
        curve_series.append(f"d = {e:.2f}")
tables = []
if solve_for == "n" and math.isfinite(d):
    rows = []
    for e in effects:
        row = {"effect": f"d = {e:.2f}"}
        for tp in (0.7, 0.8, 0.9, 0.95):
            row[f"p{round(tp * 100)}"] = pw.ceil_n(pw.solve_increasing(lambda n: power_at(n, e), tp, lo, max(2.0 * lo, 10.0), 1e7))
        rows.append(row)
    tables.append({
        "key": "sensitivity", "title": "Required size of group 1 (or pairs / participants) 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
size_text = f"{n1:,.0f} + {n2:,.0f} (N = {n_total:,.0f})" if two else f"{n1:,.0f}"
summary = f"{labels[design].capitalize()}: n = {size_text}, d = {d:.3f}, power = {power:.3f} at {alternative} α = {alpha}."
cs.publish({
    "method": "power.ttest",
    "methodVersion": "1.0.0",
    "title": f"Power analysis: {labels[design]}",
    "engine": cs.engine_info("numpy", "scipy"),
    "summary": summary,
    "apa": f"n = {size_text}, d = {cs.fmt_num(d)}, α = {alpha}, 1 − β = {power:.3f}",
    "statistics": [
        {"key": "n", "label": "Group 1 / pairs / participants" if two else ("Pairs" if design == "paired" else "Participants"), "value": n1, "format": "integer"},
        *([{"key": "n2", "label": "Group 2", "value": n2, "format": "integer"}, {"key": "n_total", "label": "Total", "value": n_total, "format": "integer"}] if two else []),
        {"key": "power", "label": "Power", "value": power},
        {"key": "d", "label": "Effect size d", "value": d},
        {"key": "df", "label": "df", "value": df},
        {"key": "ncp", "label": "Noncentrality δ", "value": ncp},
    ],
    "tables": tables,
    "warnings": warnings,
    "notes": [
        "Noncentral t; two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.",
        *([f"Group 2 = ⌈{ratio:g} × group 1⌉."] if two else []),
    ],
    "charts": [cs.chart(
        "power", "power-curve", "Power by sample size",
        {"x": cs.col("Sample size (group 1)" if two else "Sample size", curve_x),
         "power": cs.col("Power", curve_power),
         "series": cs.col("Effect", curve_series)},
        controls={"target": target if target_power is not None else 0.8},
    )],
    "values": {
        "n": n1, "n2": n2, "n_total": n_total, "n_exact": n_exact, "power": power, "target_power": target_power,
        "d": d, "df": df, "ncp": ncp, "crit": crit, "alpha": alpha,
    },
})
Python
from statsmodels.stats.power import TTestIndPower

n = TTestIndPower().solve_power(effect_size=0.5, alpha=0.05, power=0.8)
print(n)
R
power.t.test(delta = 0.5, sd = 1, sig.level = 0.05, power = 0.8)
08

Recap

Recap
  • Type I: a false alarm, at rate α. Type II: a missed effect, at rate β. Power is 1−β1 - \beta.
  • Power rises with the effect, the sample size and α, and falls as noise grows.
  • Half the effect takes four times the data.
  • Plan power before the study, for an effect that matters. "Observed power" adds nothing to the p-value.

Concepts in this lesson