GAgreement & reliability · agreement.bland_altman
Bland–Altman limits of agreement
Can one measurement method replace another — how far apart are their readings?
Compares two methods measuring the same quantity: the mean difference (bias) with its confidence interval and t test, the limits of agreement within which most differences fall, exact (Carkeet 2015) or approximate confidence intervals for those limits, and a test of proportional bias (differences changing with the level).
A real result
Example “Pefr exact”, recorded in both engines. In the app, “Try this example” loads it into Analyze.
bias = −2.12, 95% CI [−22.05, 17.81]; limits of agreement −78.10 to 73.86
- Bias (A − B)
- −2.118
- p (bias = 0)
- .825
- 95% limits of agreement
- [−78.096, 73.861]
- SD of differences
- 38.765
Plots
View data
| wright − mini wright | Mean of wright and mini wright |
|---|---|
| −18 | 503 |
| −35 | 412.50 |
| −4 | 518 |
| 6 | 431 |
| −24 | 488 |
| −43 | 578.50 |
| 49 | 388.50 |
| 62 | 411 |
| −8 | 654 |
| −12 | 439 |
| −15 | 424.50 |
| 30 | 641 |
| 7 | 263.50 |
| 1 | 477.50 |
| −81 | 218.50 |
| 73 | 386.50 |
| −24 | 439 |
Assumptions
- Normal differences — Shapiro–Wilk on A − B: p = .593.
- No proportional bias — Slope of differences on means = 0.029, p = .749.
- Sample size — 17 pairs — the limits of agreement are imprecise (wide intervals).
Bias and limits of agreement
| Estimate | 95% CI low | 95% CI high | |
|---|---|---|---|
| Bias (mean difference) | −2.118 | −22.049 | 17.814 |
| Lower limit (bias − 1.96 SD) | −78.096 | −124.16 | −53.095 |
| Upper limit (bias + 1.96 SD) | 73.861 | 48.860 | 119.93 |
Intervals for the limits: exact (Carkeet 2015).
- Differences are wright − mini wright; means are their average. Whether the limits are acceptable is a clinical judgment.
bias = −2.12, 95% CI [−22.05, 17.81]; limits of agreement −78.10 to 73.86
- Bias (A − B)
- −2.118
- p (bias = 0)
- .825
- 95% limits of agreement
- [−78.096, 73.861]
- SD of differences
- 38.765
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Normal differences — Shapiro–Wilk on A − B: p = .593.
- No proportional bias — Slope of differences on means = 0.029, p = .749.
- Sample size — 17 pairs — the limits of agreement are imprecise (wide intervals).
Bias and limits of agreement
| Estimate | 95% CI low | 95% CI high | |
|---|---|---|---|
| Bias (mean difference) | −2.118 | −22.049 | 17.814 |
| Lower limit (bias − 1.96 SD) | −78.096 | −124.16 | −53.095 |
| Upper limit (bias + 1.96 SD) | 73.861 | 48.860 | 119.93 |
Intervals for the limits: exact (Carkeet 2015).
- Differences are wright − mini wright; means are their average. Whether the limits are acceptable is a clinical judgment.
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
- Pefr exactpefr · method_a: wright · method_b: mini wright
- Pefr approximatepefr · method_a: wright · method_b: mini wright
- Scoresratings_sim · method_a: score_1 · method_b: score_3
- Degenerate infpefr · method_a: wright · method_b: mini wright
- Degenerate emptypefr · method_a: wright · method_b: mini wright
- Degenerate n2pefr · method_a: wright · method_b: mini wright
In the guides
Where it sits in the decision guides
- Repeated measurements: Bland–Altman · Statistics Guide · G — Agreement / reliability
- Concordance: CCC, Deming, Passing–Bablok · Statistics Guide · G — Agreement / reliability
What you need
- Method Anumeric · 1 column
- Readings by the first method.
- Method Bnumeric · 1 column
- Readings by the second method on the same subjects. Differences are A − B.
Choices you make
hypothesis
- Limits of agreement coverdefault 0.95
Share of differences the limits should contain (0.95 → bias ± 1.96 SD).
- Confidence leveldefault 0.95
For the intervals around the bias and the limits.
statistics
- Interval for the limitsdefault exact
How intervals around the limits of agreement are computed: exact (noncentral t) or the Bland–Altman approximation.
- Exact (Carkeet 2015, noncentral t)(default) — Exact limits from the noncentral t (Carkeet 2015); the better choice, especially in small samples.
- Approximate (Bland & Altman 1999) — The original Bland & Altman (1999) formula; matches older papers, but runs a little narrow in small samples.
assumptions
- Test proportional biasdefault on
Regress the differences on the means.
Checks and remedies
CodeSplash checks each testable assumption on every run and shows the result on the card; premises the data can’t test are marked. If a check fails, these are the ways forward.
Normal differences
The limits assume roughly normal differences (Shapiro–Wilk on A − B).
No proportional bias
Differences should not change with the size of the measurement (slope of differences on means).
Sample size
Limits of agreement are imprecise with fewer than about 50 pairs.
What it reports
- Tests: Mean difference (bias) is zero
- Tests: No proportional bias
- n
- How many independent units the analysis used; it sets precision and power.
- bias
- The range expected to hold 95% of differences between two measurement methods (bias ± 1.96 SD).
- bias_low
- A range of effect values compatible with your data at the chosen confidence level.
- bias_high
- A range of effect values compatible with your data at the chosen confidence level.
- sd_diff
- The typical distance of values from their mean, in the data's own units.
- loa_low
- The range expected to hold 95% of differences between two measurement methods (bias ± 1.96 SD).
- loa_high
- The range expected to hold 95% of differences between two measurement methods (bias ± 1.96 SD).
- loa_low_ci_low
- A range of effect values compatible with your data at the chosen confidence level.
- loa_low_ci_high
- A range of effect values compatible with your data at the chosen confidence level.
- loa_high_ci_low
- A range of effect values compatible with your data at the chosen confidence level.
- loa_high_ci_high
- A range of effect values compatible with your data at the chosen confidence level.
- t
- An estimate divided by its standard error, compared with a t distribution.
- df
- How much independent information is left to estimate variability after fitting the model.
- p
- How surprising your data would be if the null hypothesis were true, as a probability.
- slope
- A rank-based line through two methods' readings (median of pairwise slopes) that allows for error in both and resists outliers.
- slope_low
- A range of effect values compatible with your data at the chosen confidence level.
- slope_high
- A range of effect values compatible with your data at the chosen confidence level.
- slope_p
- How surprising your data would be if the null hypothesis were true, as a probability.
- intercept
- A rank-based line through two methods' readings (median of pairwise slopes) that allows for error in both and resists outliers.
- sw_p
- A test of normality; a small p-value says the data are unlikely to come from a normal distribution.
- mean_a
- The sum divided by the count; the balance point of the data, pulled by extreme values.
- mean_b
- The sum divided by the count; the balance point of the data, pulled by extreme values.
- z_loa
- An estimate in standard-error units, compared with the standard normal distribution.
Python and R
Python and R agree to 6 decimals (relative tolerance 1e-10). Python uses pandas, numpy, scipy, codesplash; R uses stats, nanoparquet, codesplashr.
Default traps between the two languages
- Exact limits invert the noncentral t (scipy's Boost nct.ppf ↔ R's qt(ncp =)), which agree to about 1e-9.
The exact script CodeSplash ran for example “Pefr exact” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Bland–Altman limits of agreement
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method agreement.bland_altman 1.0.1 · spec 2a6596a4d95bbfd6
# Dataset: pefr (version 1)
# 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
import pandas as pd
from scipy import stats
import codesplash as cs
# %% Data
df = pd.read_parquet("tests/parity/fixtures/pefr.parquet", columns=["wright", "mini wright"])
n_input = len(df)
df = df.dropna(subset=["wright", "mini wright"]) # listwise deletion over the analysis columns
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["wright", "mini wright"]) # infinite values stop the run (registry/errors.yaml)
# %% Differences
a_name, b_name = "wright", "mini wright"
a = df[a_name].to_numpy(dtype=float)
b = df[b_name].to_numpy(dtype=float)
d = a - b # differences A − B
m = (a + b) / 2
n = len(d)
cs.check_rows(n, 3, "Bland–Altman analysis") # needs at least three pairs
loa_level = 0.95
conf = 0.95
gam = 1 - conf
zp = stats.norm.ppf(1 - (1 - loa_level) / 2) # 1.96 for 95% limits
# %% Bias and limits of agreement
bias = float(d.mean())
sd = float(d.std(ddof=1))
tq = stats.t.ppf(1 - gam / 2, n - 1)
se_bias = sd / math.sqrt(n)
t_stat = bias / se_bias
p = float(2 * stats.t.sf(abs(t_stat), n - 1))
loa_low, loa_high = bias - zp * sd, bias + zp * sd
if "exact" == "exact":
# Carkeet (2015): the limits are normal quantiles; their CIs invert the noncentral t (ncp = z√n)
lo_q = stats.nct.ppf(gam / 2, n - 1, zp * math.sqrt(n))
hi_q = stats.nct.ppf(1 - gam / 2, n - 1, zp * math.sqrt(n))
up_ci = (bias + sd * lo_q / math.sqrt(n), bias + sd * hi_q / math.sqrt(n))
low_ci = (bias - sd * hi_q / math.sqrt(n), bias - sd * lo_q / math.sqrt(n))
ci_label = "exact (Carkeet 2015)"
else:
se_loa = sd * math.sqrt(1 / n + zp**2 / (2 * (n - 1))) # Bland & Altman (1999)
up_ci = (loa_high - tq * se_loa, loa_high + tq * se_loa)
low_ci = (loa_low - tq * se_loa, loa_low + tq * se_loa)
ci_label = "approximate (Bland & Altman 1999)"
# %% Proportional bias: regression of the differences on the means
slope = slope_lo = slope_hi = slope_p = intercept = None
if True:
reg = stats.linregress(m, d)
tq2 = stats.t.ppf(1 - gam / 2, n - 2)
slope, intercept, slope_p = float(reg.slope), float(reg.intercept), float(reg.pvalue)
slope_lo, slope_hi = slope - tq2 * reg.stderr, slope + tq2 * reg.stderr
sw_p = float(stats.shapiro(d).pvalue) if 3 <= n <= 5000 else None
values = {"n": float(n), "bias": bias, "bias_low": bias - tq * se_bias, "bias_high": bias + tq * se_bias, "sd_diff": sd,
"loa_low": loa_low, "loa_high": loa_high, "loa_low_ci_low": low_ci[0], "loa_low_ci_high": low_ci[1],
"loa_high_ci_low": up_ci[0], "loa_high_ci_high": up_ci[1], "t": t_stat, "df": float(n - 1), "p": p,
"slope": slope, "slope_low": slope_lo, "slope_high": slope_hi, "slope_p": slope_p, "intercept": intercept,
"sw_p": sw_p, "mean_a": float(a.mean()), "mean_b": float(b.mean()), "z_loa": float(zp)}
# %% Assumptions
assumptions = [
{"key": "normality", "label": "Normal differences", "status": "info" if sw_p is None else ("warn" if sw_p < 0.05 else "pass"), "p": sw_p,
"detail": "Too many pairs for Shapiro–Wilk; inspect the histogram of differences." if sw_p is None else
f"Shapiro–Wilk on A − B: {cs.fmt_p(sw_p)}" + (" — consider log-transformed (ratio) differences or nonparametric limits." if sw_p < 0.05 else ".")},
{"key": "proportional_bias", "label": "No proportional bias",
"status": "info" if slope is None else ("warn" if slope_p < 0.05 else "pass"), "p": slope_p,
"detail": "Not tested." if slope is None else f"Slope of differences on means = {slope:.3f}, {cs.fmt_p(slope_p)}"
+ (" — the bias changes with the measurement level; consider regression-based limits." if slope_p < 0.05 else ".")},
{"key": "sample_size", "label": "Sample size", "status": "warn" if n < 50 else "pass",
"detail": f"{n} pairs" + (" — the limits of agreement are imprecise (wide intervals)." if n < 50 else ".")},
]
# %% Result
pct, cover = f"{conf:.0%}", f"{loa_level:.0%}"
take = np.arange(n) if n <= 5000 else np.unique(np.round(np.linspace(0, n - 1, 5000)).astype(int))
controls = {"bias": bias, "lower": loa_low, "upper": loa_high, "biasLow": values["bias_low"], "biasHigh": values["bias_high"],
"lowerLow": low_ci[0], "lowerHigh": low_ci[1], "upperLow": up_ci[0], "upperHigh": up_ci[1]}
if slope is not None and slope_p < 0.05:
controls.update({"slope": slope, "intercept": intercept})
cs.publish({
"method": "agreement.bland_altman",
"methodVersion": "1.0.1",
"title": f"Bland–Altman: {a_name} vs {b_name}",
"engine": cs.engine_info("numpy", "scipy"),
"n": int(n),
"summary": f"Bias (A − B) = {bias:.3g}, {pct} CI [{values['bias_low']:.3g}, {values['bias_high']:.3g}]; {cover} limits of agreement {loa_low:.3g} to {loa_high:.3g}.",
"apa": f"bias = {cs.fmt_num(bias)}, {pct} CI [{cs.fmt_num(values['bias_low'])}, {cs.fmt_num(values['bias_high'])}]; limits of agreement {cs.fmt_num(loa_low)} to {cs.fmt_num(loa_high)}",
"statistics": [{"key": "bias", "label": "Bias (A − B)", "value": bias}, {"key": "p", "label": "p (bias = 0)", "value": p, "format": "p"},
{"key": "loa", "label": f"{cover} limits of agreement", "value": [loa_low, loa_high]}, {"key": "sd_diff", "label": "SD of differences", "value": sd}],
"tables": [{"key": "agreement", "title": "Bias and limits of agreement", "columns": [
{"key": "quantity", "label": "", "type": "string"}, {"key": "estimate", "label": "Estimate", "type": "number"},
{"key": "lower", "label": f"{pct} CI low", "type": "number"}, {"key": "upper", "label": f"{pct} CI high", "type": "number"},
], "rows": [
{"quantity": "Bias (mean difference)", "estimate": bias, "lower": values["bias_low"], "upper": values["bias_high"]},
{"quantity": f"Lower limit (bias − {zp:.2f} SD)", "estimate": loa_low, "lower": low_ci[0], "upper": low_ci[1]},
{"quantity": f"Upper limit (bias + {zp:.2f} SD)", "estimate": loa_high, "lower": up_ci[0], "upper": up_ci[1]},
], "note": f"Intervals for the limits: {ci_label}."}],
"assumptions": assumptions,
"warnings": warnings,
"notes": [f"Differences are {a_name} − {b_name}; means are their average. Whether the limits are acceptable is a clinical judgment."],
"charts": [cs.chart("ba", "bland-altman", "Bland–Altman plot", {
"mean": cs.col(f"Mean of {a_name} and {b_name}", m[take], "numeric"),
"diff": cs.col(f"{a_name} − {b_name}", d[take], "numeric"),
}, controls=controls)],
"values": values,
})
The exact script CodeSplash ran for example “Pefr exact” · R version 4.5.1 (2025-06-13)
# Bland–Altman limits of agreement
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method agreement.bland_altman 1.0.1 · spec 2a6596a4d95bbfd6
# Dataset: pefr (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/pefr.parquet", col_select = c("wright", "mini wright")))
n_input <- nrow(df)
df <- df[stats::complete.cases(df[c("wright", "mini wright")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("wright", "mini wright")) # infinite values stop the run (registry/errors.yaml)
# %% Differences
a_name <- "wright"
b_name <- "mini wright"
a <- as.numeric(df[[a_name]])
b <- as.numeric(df[[b_name]])
d <- a - b # differences A − B
m <- (a + b) / 2
n <- length(d)
cs_check_rows(n, 3, "Bland–Altman analysis") # needs at least three pairs
loa_level <- 0.95
conf <- 0.95
gam <- 1 - conf
zp <- stats::qnorm(1 - (1 - loa_level) / 2) # 1.96 for 95% limits
# %% Bias and limits of agreement
tt <- t.test(d, conf.level = conf)
bias <- mean(d)
sd_d <- sd(d)
tq <- stats::qt(1 - gam / 2, n - 1)
se_bias <- sd_d / sqrt(n)
loa_low <- bias - zp * sd_d
loa_high <- bias + zp * sd_d
if ("exact" == "exact") {
# Carkeet (2015): the limits are normal quantiles; their CIs invert the noncentral t (ncp = z√n)
lo_q <- stats::qt(gam / 2, n - 1, ncp = zp * sqrt(n))
hi_q <- stats::qt(1 - gam / 2, n - 1, ncp = zp * sqrt(n))
up_ci <- c(bias + sd_d * lo_q / sqrt(n), bias + sd_d * hi_q / sqrt(n))
low_ci <- c(bias - sd_d * hi_q / sqrt(n), bias - sd_d * lo_q / sqrt(n))
ci_label <- "exact (Carkeet 2015)"
} else {
se_loa <- sd_d * sqrt(1 / n + zp^2 / (2 * (n - 1))) # Bland & Altman (1999)
up_ci <- c(loa_high - tq * se_loa, loa_high + tq * se_loa)
low_ci <- c(loa_low - tq * se_loa, loa_low + tq * se_loa)
ci_label <- "approximate (Bland & Altman 1999)"
}
# %% Proportional bias: regression of the differences on the means
slope <- slope_lo <- slope_hi <- slope_p <- intercept <- NA
if (TRUE) {
fit <- lm(d ~ m)
cf <- summary(fit)$coefficients
ci <- confint(fit, level = conf)
slope <- cf[2, 1]; intercept <- cf[1, 1]; slope_p <- cf[2, 4]
slope_lo <- ci[2, 1]; slope_hi <- ci[2, 2]
}
sw_p <- if (n >= 3 && n <= 5000) shapiro.test(d)$p.value else NA
values <- list(n = n, bias = bias, bias_low = tt$conf.int[1], bias_high = tt$conf.int[2], sd_diff = sd_d,
loa_low = loa_low, loa_high = loa_high, loa_low_ci_low = low_ci[1], loa_low_ci_high = low_ci[2],
loa_high_ci_low = up_ci[1], loa_high_ci_high = up_ci[2], t = unname(tt$statistic), df = n - 1, p = tt$p.value,
slope = slope, slope_low = slope_lo, slope_high = slope_hi, slope_p = slope_p, intercept = intercept,
sw_p = sw_p, mean_a = mean(a), mean_b = mean(b), z_loa = zp)
# %% Assumptions
assumptions <- list(
list(key = "normality", label = "Normal differences", status = if (is.na(sw_p)) "info" else if (sw_p < 0.05) "warn" else "pass", p = sw_p,
detail = if (is.na(sw_p)) "Too many pairs for Shapiro–Wilk; inspect the histogram of differences."
else paste0(sprintf("Shapiro–Wilk on A − B: %s", cs_fmt_p(sw_p)),
if (sw_p < 0.05) " — consider log-transformed (ratio) differences or nonparametric limits." else ".")),
list(key = "proportional_bias", label = "No proportional bias", status = if (is.na(slope)) "info" else if (slope_p < 0.05) "warn" else "pass",
p = slope_p,
detail = if (is.na(slope)) "Not tested." else paste0(sprintf("Slope of differences on means = %.3f, %s", slope, cs_fmt_p(slope_p)),
if (slope_p < 0.05) " — the bias changes with the measurement level; consider regression-based limits." else ".")),
list(key = "sample_size", label = "Sample size", status = if (n < 50) "warn" else "pass",
detail = paste0(sprintf("%d pairs", n), if (n < 50) " — the limits of agreement are imprecise (wide intervals)." else "."))
)
# %% Result
pct <- sprintf("%.0f%%", conf * 100)
cover <- sprintf("%.0f%%", loa_level * 100)
take <- if (n <= 5000) seq_len(n) else unique(round(seq(1, n, length.out = 5000)))
controls <- list(bias = bias, lower = loa_low, upper = loa_high, biasLow = values$bias_low, biasHigh = values$bias_high,
lowerLow = low_ci[1], lowerHigh = low_ci[2], upperLow = up_ci[1], upperHigh = up_ci[2])
if (!is.na(slope) && slope_p < 0.05) controls <- c(controls, list(slope = slope, intercept = intercept))
cs_publish(list(
method = "agreement.bland_altman",
methodVersion = "1.0.1",
title = sprintf("Bland–Altman: %s vs %s", a_name, b_name),
engine = cs_engine_info("stats"),
n = n,
summary = sprintf("Bias (A − B) = %s, %s CI [%s, %s]; %s limits of agreement %s to %s.", format(signif(bias, 3)), pct,
format(signif(values$bias_low, 3)), format(signif(values$bias_high, 3)), cover, format(signif(loa_low, 3)), format(signif(loa_high, 3))),
apa = sprintf("bias = %s, %s CI [%s, %s]; limits of agreement %s to %s", cs_fmt_num(bias), pct, cs_fmt_num(values$bias_low),
cs_fmt_num(values$bias_high), cs_fmt_num(loa_low), cs_fmt_num(loa_high)),
statistics = list(list(key = "bias", label = "Bias (A − B)", value = bias), list(key = "p", label = "p (bias = 0)", value = values$p, format = "p"),
list(key = "loa", label = paste(cover, "limits of agreement"), value = c(loa_low, loa_high)),
list(key = "sd_diff", label = "SD of differences", value = sd_d)),
tables = list(list(key = "agreement", title = "Bias and limits of agreement", columns = list(
list(key = "quantity", label = "", type = "string"), list(key = "estimate", label = "Estimate", type = "number"),
list(key = "lower", label = paste(pct, "CI low"), type = "number"), list(key = "upper", label = paste(pct, "CI high"), type = "number")
), rows = list(
list(quantity = "Bias (mean difference)", estimate = bias, lower = values$bias_low, upper = values$bias_high),
list(quantity = sprintf("Lower limit (bias − %.2f SD)", zp), estimate = loa_low, lower = low_ci[1], upper = low_ci[2]),
list(quantity = sprintf("Upper limit (bias + %.2f SD)", zp), estimate = loa_high, lower = up_ci[1], upper = up_ci[2])
), note = sprintf("Intervals for the limits: %s.", ci_label))),
assumptions = assumptions,
warnings = warnings,
notes = list(sprintf("Differences are %s − %s; means are their average. Whether the limits are acceptable is a clinical judgment.", a_name, b_name)),
charts = list(cs_chart("ba", "bland-altman", "Bland–Altman plot", list(
mean = cs_col(sprintf("Mean of %s and %s", a_name, b_name), m[take], "numeric"),
diff = cs_col(sprintf("%s − %s", a_name, b_name), d[take], "numeric")
), controls = controls)),
values = values
))
Sources
- Bland, J. M., & Altman, D. G. (1986). Statistical methods for assessing agreement between two methods of clinical measurement. The Lancet, 327, 307–310.
- Bland, J. M., & Altman, D. G. (1999). Measuring agreement in method comparison studies. Statistical Methods in Medical Research, 8, 135–160.
- Carkeet, A. (2015). Exact parametric confidence intervals for Bland–Altman limits of agreement. Optometry and Vision Science, 92, e71–e80.
From the CodeSplash method registry · agreement.bland_altman v1.0.1.