Comparing a new method to a reference
Both instruments have error, so ordinary regression is biased here. Use Deming or Passing–Bablok, and read the intercept and slope as constant and proportional bias.
What runs in CodeSplash
- Method comparison regression (Passing–Bablok, Deming) Verified in Python and R
You will be able to
- Explain why ordinary least squares misleads when both methods have error
- Read constant and proportional bias from the intercept and slope intervals
- Choose among Passing–Bablok, Deming and weighted Deming
Read first
Error on both sides
ReadOrdinary regression assumes is measured perfectly. In a method comparison, neither instrument is perfect. Error in pulls the OLS slope toward zero, so you can find "proportional bias" where there's none.
Two methods avoid this:
- Deming regressionDeming regressionA straight line fitted allowing for measurement error in both methods, given the ratio of their error variances (1 = equally precise). allows error in both, given the ratio of their error variances, .
- Passing–BablokPassing–Bablok regressionA rank-based line through two methods' readings (median of pairwise slopes) that allows for error in both and resists outliers. uses the median of all pairwise slopes. It needs no distribution and resists outliers.
Read the line
ReadIf the methods agree, the line is : slope 1, intercept 0.
- Intercept interval excludes 0: constant biasConstant and proportional differenceA constant difference shifts every reading by the same amount (intercept ≠ 0); a proportional one grows with the level (slope ≠ 1)..
- Slope interval excludes 1: proportional bias.
Predict the verdict
PredictPredict first
A new assay is compared with a reference on 80 samples. Passing–Bablok gives intercept 0.37 (95% interval 0.12 to 0.63), and slope 0.987 (0.966 to 1.007).
What do these intervals show?
Reveal
Constant bias only. The intercept's interval excludes 0, so the new assay reads a fixed amount high. The slope's interval includes 1, so these data are compatible with no proportional bias. Decide whether that offset matters in practice. A fixed offset can often be calibrated out.
Check linearity first
In the appStraight-line methods assume a straight relation. The CUSUM test checks this for Passing–Bablok.
For the first assay, p = .576: no sign of curvature. For a second version that really is curved, p < .001. Its slope of 1.47 doesn't describe anything well.
Passing–Bablok regression: slope = 1.467, 90% CI [1.397, 1.542]; intercept = −1.687, 90% CI [−2.272, −1.240]
- Slope
- 1.467
- Intercept
- −1.687
- Linearity p (CUSUM)
- < .001
Plots
View data
| new_curved | reference_cv |
|---|---|
| 21.929 | 15.939 |
| 12.000 | 10.789 |
| 2.392 | 3.015 |
| 4.634 | 4.358 |
| 13.216 | 10.436 |
| 2.607 | 2.309 |
| 9.054 | 7.941 |
| 40.311 | 25.756 |
| 4.874 | 4.241 |
| 14.185 | 12.147 |
| 2.332 | 1.965 |
| 21.021 | 16.388 |
| 4.447 | 3.899 |
| 37.746 | 25.077 |
| 4.218 | 3.126 |
| 6.429 | 6.006 |
| 12.536 | 10.491 |
| 1.543 | 2.094 |
| 5.573 | 5.403 |
| 2.498 | 2.288 |
| 6.242 | 5.580 |
| 6.327 | 5.028 |
| 3.814 | 3.927 |
| 2.716 | 2.329 |
| 7.621 | 6.798 |
| 3.065 | 3.647 |
| 8.030 | 6.689 |
| 3.234 | 2.945 |
| 1.822 | 2.073 |
| 15.185 | 12.471 |
| 48.672 | 30.342 |
| 4.442 | 3.438 |
| 23.327 | 18.235 |
| 2.318 | 2.130 |
| 14.166 | 11.312 |
| 7.716 | 7.374 |
| 4.172 | 3.416 |
| 9.587 | 8.559 |
| 27.165 | 19.500 |
| 8.349 | 8.253 |
| 7.355 | 6.145 |
| 42.821 | 27.609 |
| 8.011 | 7.705 |
| 8.518 | 7.695 |
| 2.324 | 2.556 |
| 1.517 | 2.217 |
| 53.935 | 32.481 |
| 5.928 | 4.627 |
| 11.326 | 9.943 |
| 3.822 | 3.973 |
| 63.993 | 36.325 |
| 16.447 | 13.669 |
| 60.459 | 34.736 |
| 45.002 | 28.340 |
| 11.502 | 10.174 |
| 22.885 | 16.645 |
| 25.588 | 18.225 |
| 2.828 | 2.433 |
| 60.268 | 34.766 |
| 3.871 | 3.573 |
First 60 of 80 rows.
View data
| reference_cv | new_curved − (-1.69 + 1.47·reference_cv) |
|---|---|
| 15.939 | 0.2370 |
| 10.789 | −2.139 |
| 3.015 | −0.3441 |
| 4.358 | −0.0710 |
| 10.436 | −0.4039 |
| 2.309 | 0.9074 |
| 7.941 | −0.9068 |
| 25.756 | 4.220 |
| 4.241 | 0.3402 |
| 12.147 | −1.946 |
| 1.965 | 1.136 |
| 16.388 | −1.329 |
| 3.899 | 0.4150 |
| 25.077 | 2.651 |
| 3.126 | 1.320 |
| 6.006 | −0.6943 |
| 10.491 | −1.165 |
| 2.094 | 0.1590 |
| 5.403 | −0.6653 |
| 2.288 | 0.8275 |
| 5.580 | −0.2558 |
| 5.028 | 0.6380 |
| 3.927 | −0.2598 |
| 2.329 | 0.9865 |
| 6.798 | −0.6630 |
| 3.647 | −0.5974 |
| 6.689 | −0.0938 |
| 2.945 | 0.6016 |
| 2.073 | 0.4681 |
| 12.471 | −1.421 |
| 30.342 | 5.854 |
| 3.438 | 1.086 |
| 18.235 | −1.733 |
| 2.130 | 0.8804 |
| 11.312 | −0.7401 |
| 7.374 | −1.413 |
| 3.416 | 0.8473 |
| 8.559 | −1.281 |
| 19.500 | 0.2501 |
| 8.253 | −2.069 |
| 6.145 | 0.0286 |
| 27.609 | 4.011 |
| 7.705 | −1.603 |
| 7.695 | −1.083 |
| 2.556 | 0.2610 |
| 2.217 | −0.0481 |
| 32.481 | 7.979 |
| 4.627 | 0.8288 |
| 9.943 | −1.571 |
| 3.973 | −0.3186 |
| 36.325 | 12.399 |
| 13.669 | −1.916 |
| 34.736 | 11.196 |
| 28.340 | 5.120 |
| 10.174 | −1.735 |
| 16.645 | 0.1566 |
| 18.225 | 0.5424 |
| 2.433 | 0.9470 |
| 34.766 | 10.960 |
| 3.573 | 0.3171 |
First 60 of 80 rows.
Assumptions
- Strong positive relation between the methods — Pearson r = 0.992; Passing–Bablok assumes highly correlated, linearly related readings.
- Linear relation (CUSUM test) — H = 2.499, p = 7.54e-06 (1.36 is the 5 % critical value).
- Enough samples (≥ 40 recommended) — 80 pairs.
Passing–Bablok regression of new_curved on reference_cv
| Estimate | SE (jackknife) | 90% CI low | 90% CI high | Identity value inside the CI? | |
|---|---|---|---|---|---|
| Slope | 1.467 | — | 1.397 | 1.542 | no (proportional difference) |
| Intercept | −1.687 | — | −2.272 | −1.240 | no (constant difference) |
Confidence limits from the ordered pairwise slopes (Passing & Bablok 1983).
- The methods agree when the slope interval contains 1 and the intercept interval contains 0. Ordinary least squares is not used: it assumes reference_cv is measured without error and biases the slope towards 0.
- Passing–Bablok is robust to outliers and needs no error-variance ratio; Deming assumes normal errors with a known ratio; weighted Deming suits errors that grow with the level (constant CV).
The exact R script · R R version 4.5.1 (2025-06-13)
# Method comparison regression (Passing–Bablok, Deming)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method agreement.method_comparison 1.0.2 · spec 8a2bf1f0f7f909ef
# Dataset: c3_methcomp (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_passing_bablok, cs_cusum_linearity, cs_deming (twins of codesplash.stats.method_comparison)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/c3_methcomp.parquet", col_select = c("reference_cv", "new_curved")))
n_input <- nrow(df)
df <- df[stats::complete.cases(df[c("reference_cv", "new_curved")]), , 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("reference_cv", "new_curved")) # infinite values stop the run (registry/errors.yaml)
# %% Regression
xr <- "reference_cv"
yr <- "new_curved"
x <- as.numeric(df[[xr]])
y <- as.numeric(df[[yr]])
n <- length(x)
regression <- "passing_bablok"
lam <- 1
conf <- 0.9
linearity <- TRUE
cs_check_rows(n, 3, "Method comparison") # needs at least three pairs of readings
# A method with one reading for every sample leaves the slope undefined (no variation to regress on).
cs_check_not_constant(x, xr)
cs_check_not_constant(y, yr)
if (regression == "passing_bablok") {
fit <- cs_passing_bablok(x, y, conf)
label <- "Passing–Bablok"
} else if (regression == "weighted_deming") {
if (any(x <= 0) || any(y <= 0)) stop("Weighted Deming regression (constant CV) needs positive readings from both methods")
fit <- cs_deming(x, y, lam, weighted = TRUE, conf = conf)
label <- "Weighted Deming"
} else {
fit <- cs_deming(x, y, lam, weighted = FALSE, conf = conf)
label <- "Deming"
}
a <- fit$intercept
b <- fit$slope
cus <- if (linearity && regression == "passing_bablok") cs_cusum_linearity(x, y, a, b) else NULL
num_or_null <- function(v) if (is.null(v) || !is.finite(v)) NULL else v
constant_bias <- is.finite(fit$intercept_low) && is.finite(fit$intercept_high) && (fit$intercept_low > 0 || fit$intercept_high < 0)
proportional_bias <- is.finite(fit$slope_low) && is.finite(fit$slope_high) && (fit$slope_low > 1 || fit$slope_high < 1)
pct <- sprintf("%.0f%%", conf * 100)
# %% Assumptions
assumptions <- list()
if (regression == "passing_bablok") {
rr <- stats::cor(x, y)
assumptions[[length(assumptions) + 1]] <- list(key = "correlation", label = "Strong positive relation between the methods",
status = if (rr > 0.5) "pass" else "warn",
detail = sprintf("Pearson r = %.3f; Passing–Bablok assumes highly correlated, linearly related readings.", rr))
if (!is.null(cus) && !is.finite(cus$p)) {
# The CUSUM test needs residuals on both sides of the line (and a nonzero slope).
assumptions[[length(assumptions) + 1]] <- list(key = "linearity", label = "Linear relation (CUSUM test)", status = "info",
detail = "The CUSUM test needs points above and below the fitted line; it can't be computed here.")
} else if (!is.null(cus)) {
assumptions[[length(assumptions) + 1]] <- list(key = "linearity", label = "Linear relation (CUSUM test)", status = if (cus$p < 0.05) "fail" else "pass",
detail = sprintf("H = %.3f, p = %.3g (1.36 is the 5 %% critical value).", cus$h, cus$p))
}
} else {
assumptions[[length(assumptions) + 1]] <- list(key = "error_ratio", label = "Error-variance ratio known", status = "info",
detail = sprintf("λ = Var(error in %s) / Var(error in %s) = %s; estimate it from replicate measurements.", yr, xr, format(lam)))
}
assumptions[[length(assumptions) + 1]] <- list(key = "sample_size", label = "Enough samples (≥ 40 recommended)", status = if (n < 40) "warn" else "pass",
detail = sprintf("%d pairs%s", n, if (n < 40) "; intervals are wide and the linearity test has little power." else "."))
# %% Result
values <- list(
n = n, regression = regression, slope = b, slope_low = num_or_null(fit$slope_low), slope_high = num_or_null(fit$slope_high),
intercept = a, intercept_low = num_or_null(fit$intercept_low), intercept_high = num_or_null(fit$intercept_high),
slope_se = num_or_null(fit$slope_se), intercept_se = num_or_null(fit$intercept_se),
error_ratio = if (regression != "passing_bablok") lam else NULL, iterations = if (regression == "weighted_deming") fit$iterations else NULL,
constant_bias = constant_bias, proportional_bias = proportional_bias,
cusum_h = if (is.null(cus)) NULL else num_or_null(cus$h), cusum_p = if (is.null(cus)) NULL else num_or_null(cus$p),
cusum_n_above = if (is.null(cus)) NULL else cus$n_above, cusum_n_below = if (is.null(cus)) NULL else cus$n_below,
mean_difference = mean(y - x)
)
resid <- y - a - b * x
take <- unique(round(seq(1, n, length.out = min(n, 5000))))
flags <- c(constant_bias, proportional_bias)
verdict <- if (!any(flags)) "no constant or proportional difference" else paste(c("a constant difference", "a proportional difference")[flags], collapse = " and ")
cs_publish(list(
method = "agreement.method_comparison",
methodVersion = "1.0.2",
title = sprintf("Method comparison (%s)", label),
engine = cs_engine_info("stats", "nanoparquet"),
n = n,
summary = sprintf("%s: %s = %.4g + %.4g × %s; slope %s CI [%.4g, %.4g], intercept %s CI [%.4g, %.4g] — %s.",
label, yr, a, b, xr, pct, fit$slope_low, fit$slope_high, pct, fit$intercept_low, fit$intercept_high, verdict),
apa = sprintf("%s regression: slope = %s, %s CI [%s, %s]; intercept = %s, %s CI [%s, %s]", label, cs_fmt_num(b, 3), pct,
cs_fmt_num(fit$slope_low, 3), cs_fmt_num(fit$slope_high, 3), cs_fmt_num(a, 3), pct, cs_fmt_num(fit$intercept_low, 3),
cs_fmt_num(fit$intercept_high, 3)),
statistics = c(list(list(key = "slope", label = "Slope", value = b), list(key = "intercept", label = "Intercept", value = a)),
if (!is.null(cus)) list(list(key = "cusum_p", label = "Linearity p (CUSUM)", value = values$cusum_p, format = "p"))),
tables = list(list(key = "coefficients", title = sprintf("%s regression of %s on %s", label, yr, xr), columns = list(
list(key = "term", label = "", type = "string"), list(key = "estimate", label = "Estimate", type = "number"),
list(key = "se", label = "SE (jackknife)", type = "number"), list(key = "low", label = sprintf("%s CI low", pct), type = "number"),
list(key = "high", label = sprintf("%s CI high", pct), type = "number"), list(key = "reading", label = "Identity value inside the CI?", type = "string")),
rows = list(list(term = "Slope", estimate = b, se = values$slope_se, low = values$slope_low, high = values$slope_high,
reading = if (proportional_bias) "no (proportional difference)" else "yes (1)"),
list(term = "Intercept", estimate = a, se = values$intercept_se, low = values$intercept_low, high = values$intercept_high,
reading = if (constant_bias) "no (constant difference)" else "yes (0)")),
note = if (regression == "passing_bablok") "Confidence limits from the ordered pairwise slopes (Passing & Bablok 1983)."
else sprintf("Jackknife standard errors (Linnet 1990), t with %d df.", n - 2))),
effectSizes = list(),
assumptions = assumptions,
warnings = warnings,
notes = list(sprintf(paste("The methods agree when the slope interval contains 1 and the intercept interval contains 0. Ordinary least squares is not used:",
"it assumes %s is measured without error and biases the slope towards 0."), xr),
paste("Passing–Bablok is robust to outliers and needs no error-variance ratio; Deming assumes normal errors with a known ratio; weighted Deming",
"suits errors that grow with the level (constant CV).")),
charts = c(list(cs_chart("comparison", "pred-actual", sprintf("%s against %s", yr, xr), list(
actual = cs_col(yr, y[take], "numeric"), predicted = cs_col(xr, x[take], "numeric")), controls = list(identity = TRUE)),
cs_chart("residuals", "scatter", sprintf("Distance from the %s line", label), list(
x = cs_col(xr, x[take], "numeric"), y = cs_col(sprintf("%s − (%.3g + %.3g·%s)", yr, a, b, xr), resid[take], "numeric")),
controls = list(fit = "none"))),
if (regression == "deming") list(cs_chart("deming", "deming-fit", sprintf("Deming fit of %s on %s", yr, xr),
list(x = cs_col(xr, x[take], "numeric"), y = cs_col(yr, y[take], "numeric")), controls = list(delta = lam),
note = if (length(take) < n) sprintf("Drawn from %s evenly spaced rows; the fit above uses all %s.", format(length(take), big.mark = ","), format(n, big.mark = ",")) else NULL))),
values = values
))
Passing–Bablok regression: slope = 1.467, 90% CI [1.397, 1.542]; intercept = −1.687, 90% CI [−2.272, −1.240]
- Slope
- 1.467
- Intercept
- −1.687
- Linearity p (CUSUM)
- < .001
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Strong positive relation between the methods — Pearson r = 0.992; Passing–Bablok assumes highly correlated, linearly related readings.
- Linear relation (CUSUM test) — H = 2.499, p = 7.54e-06 (1.36 is the 5 % critical value).
- Enough samples (≥ 40 recommended) — 80 pairs.
Passing–Bablok regression of new_curved on reference_cv
| Estimate | SE (jackknife) | 90% CI low | 90% CI high | Identity value inside the CI? | |
|---|---|---|---|---|---|
| Slope | 1.467 | — | 1.397 | 1.542 | no (proportional difference) |
| Intercept | −1.687 | — | −2.272 | −1.240 | no (constant difference) |
Confidence limits from the ordered pairwise slopes (Passing & Bablok 1983).
- The methods agree when the slope interval contains 1 and the intercept interval contains 0. Ordinary least squares is not used: it assumes reference_cv is measured without error and biases the slope towards 0.
- Passing–Bablok is robust to outliers and needs no error-variance ratio; Deming assumes normal errors with a known ratio; weighted Deming suits errors that grow with the level (constant CV).
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Method comparison regression (Passing–Bablok, Deming)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method agreement.method_comparison 1.0.2 · spec 8a2bf1f0f7f909ef
# Dataset: c3_methcomp (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.errors import check_not_constant
from codesplash.stats import method_comparison as mc
# %% Data
df = pd.read_parquet("tests/parity/fixtures/c3_methcomp.parquet", columns=["reference_cv", "new_curved"])
n_input = len(df)
df = df.dropna(subset=["reference_cv", "new_curved"]) # 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, ["reference_cv", "new_curved"]) # infinite values stop the run (registry/errors.yaml)
# %% Regression
xr, yr = "reference_cv", "new_curved"
x = df[xr].to_numpy(dtype=float)
y = df[yr].to_numpy(dtype=float)
n = x.size
regression = "passing_bablok"
lam = 1
conf = 0.9
linearity = True
cs.check_rows(n, 3, "Method comparison") # needs at least three pairs of readings
# A method with one reading for every sample leaves the slope undefined (no variation to regress on).
check_not_constant(x, xr)
check_not_constant(y, yr)
if regression == "passing_bablok":
fit = mc.passing_bablok(x, y, conf)
label = "Passing–Bablok"
elif regression == "weighted_deming":
if np.any(x <= 0) or np.any(y <= 0):
raise ValueError("Weighted Deming regression (constant CV) needs positive readings from both methods")
fit = mc.deming(x, y, lam, weighted=True, conf=conf)
label = "Weighted Deming"
else:
fit = mc.deming(x, y, lam, weighted=False, conf=conf)
label = "Deming"
a, b = fit["intercept"], fit["slope"]
cus = mc.cusum_linearity(x, y, a, b) if (linearity and regression == "passing_bablok") else None
def num(v):
return None if v is None or not np.isfinite(v) else float(v)
constant_bias = bool(num(fit["intercept_low"]) is not None and num(fit["intercept_high"]) is not None
and (fit["intercept_low"] > 0 or fit["intercept_high"] < 0))
proportional_bias = bool(num(fit["slope_low"]) is not None and num(fit["slope_high"]) is not None
and (fit["slope_low"] > 1 or fit["slope_high"] < 1))
pct = f"{conf:.0%}"
# %% Assumptions
assumptions = []
if regression == "passing_bablok":
assumptions.append({"key": "correlation", "label": "Strong positive relation between the methods",
"status": "pass" if float(np.corrcoef(x, y)[0, 1]) > 0.5 else "warn",
"detail": f"Pearson r = {float(np.corrcoef(x, y)[0, 1]):.3f}; Passing–Bablok assumes highly correlated, linearly related readings."})
if cus and not np.isfinite(cus["p"]):
# The CUSUM test needs residuals on both sides of the line (and a nonzero slope).
assumptions.append({"key": "linearity", "label": "Linear relation (CUSUM test)", "status": "info",
"detail": "The CUSUM test needs points above and below the fitted line; it can't be computed here."})
elif cus:
assumptions.append({"key": "linearity", "label": "Linear relation (CUSUM test)", "status": "fail" if cus["p"] < 0.05 else "pass",
"detail": f"H = {cus['h']:.3f}, p = {cus['p']:.3g} (1.36 is the 5 % critical value)."})
else:
assumptions.append({"key": "error_ratio", "label": "Error-variance ratio known", "status": "info",
"detail": f"λ = Var(error in {yr}) / Var(error in {xr}) = {lam:g}; estimate it from replicate measurements."})
assumptions.append({"key": "sample_size", "label": "Enough samples (≥ 40 recommended)", "status": "warn" if n < 40 else "pass",
"detail": f"{n} pairs" + ("; intervals are wide and the linearity test has little power." if n < 40 else ".")})
# %% Result
values = {
"n": n, "regression": regression, "slope": b, "slope_low": num(fit["slope_low"]), "slope_high": num(fit["slope_high"]),
"intercept": a, "intercept_low": num(fit["intercept_low"]), "intercept_high": num(fit["intercept_high"]),
"slope_se": num(fit.get("slope_se")), "intercept_se": num(fit.get("intercept_se")),
"error_ratio": lam if regression != "passing_bablok" else None, "iterations": fit.get("iterations") if regression == "weighted_deming" else None,
"constant_bias": constant_bias, "proportional_bias": proportional_bias,
"cusum_h": num(cus["h"]) if cus else None, "cusum_p": num(cus["p"]) if cus else None,
"cusum_n_above": cus["n_above"] if cus else None, "cusum_n_below": cus["n_below"] if cus else None,
"mean_difference": float(np.mean(y - x)),
}
resid = y - a - b * x
take = np.unique(np.round(np.linspace(0, n - 1, min(n, 5000))).astype(int))
verdict = ("no constant or proportional difference" if not (constant_bias or proportional_bias)
else " and ".join(w for w, f in (("a constant difference", constant_bias), ("a proportional difference", proportional_bias)) if f))
cs.publish({
"method": "agreement.method_comparison",
"methodVersion": "1.0.2",
"title": f"Method comparison ({label})",
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": n,
"summary": f"{label}: {yr} = {a:.4g} + {b:.4g} × {xr}; slope {pct} CI [{fit['slope_low']:.4g}, {fit['slope_high']:.4g}], "
f"intercept {pct} CI [{fit['intercept_low']:.4g}, {fit['intercept_high']:.4g}] — {verdict}.",
"apa": f"{label} regression: slope = {cs.fmt_num(b, 3)}, {pct} CI [{cs.fmt_num(fit['slope_low'], 3)}, {cs.fmt_num(fit['slope_high'], 3)}]; "
f"intercept = {cs.fmt_num(a, 3)}, {pct} CI [{cs.fmt_num(fit['intercept_low'], 3)}, {cs.fmt_num(fit['intercept_high'], 3)}]",
"statistics": [{"key": "slope", "label": "Slope", "value": b}, {"key": "intercept", "label": "Intercept", "value": a}]
+ ([{"key": "cusum_p", "label": "Linearity p (CUSUM)", "value": values["cusum_p"], "format": "p"}] if cus else []),
"tables": [{"key": "coefficients", "title": f"{label} regression of {yr} on {xr}", "columns": [
{"key": "term", "label": "", "type": "string"}, {"key": "estimate", "label": "Estimate", "type": "number"},
{"key": "se", "label": "SE (jackknife)", "type": "number"}, {"key": "low", "label": f"{pct} CI low", "type": "number"},
{"key": "high", "label": f"{pct} CI high", "type": "number"}, {"key": "reading", "label": "Identity value inside the CI?", "type": "string"}],
"rows": [{"term": "Slope", "estimate": b, "se": values["slope_se"], "low": values["slope_low"], "high": values["slope_high"],
"reading": "no (proportional difference)" if proportional_bias else "yes (1)"},
{"term": "Intercept", "estimate": a, "se": values["intercept_se"], "low": values["intercept_low"], "high": values["intercept_high"],
"reading": "no (constant difference)" if constant_bias else "yes (0)"}],
"note": ("Confidence limits from the ordered pairwise slopes (Passing & Bablok 1983)." if regression == "passing_bablok"
else f"Jackknife standard errors (Linnet 1990), t with {n - 2} df.")}],
"effectSizes": [],
"assumptions": assumptions,
"warnings": warnings,
"notes": [f"The methods agree when the slope interval contains 1 and the intercept interval contains 0. Ordinary least squares is not used: "
f"it assumes {xr} is measured without error and biases the slope towards 0.",
"Passing–Bablok is robust to outliers and needs no error-variance ratio; Deming assumes normal errors with a known ratio; weighted Deming "
"suits errors that grow with the level (constant CV)."],
"charts": [cs.chart("comparison", "pred-actual", f"{yr} against {xr}", {
"actual": cs.col(yr, y[take], "numeric"), "predicted": cs.col(xr, x[take], "numeric")}, controls={"identity": True}),
cs.chart("residuals", "scatter", f"Distance from the {label} line", {
"x": cs.col(xr, x[take], "numeric"), "y": cs.col(f"{yr} − ({a:.3g} + {b:.3g}·{xr})", resid[take], "numeric")},
controls={"fit": "none"})]
+ ([cs.chart("deming", "deming-fit", f"Deming fit of {yr} on {xr}", {"x": cs.col(xr, x[take], "numeric"), "y": cs.col(yr, y[take], "numeric")},
controls={"delta": lam}, note=None if len(take) == n else f"Drawn from {len(take):,} evenly spaced rows; the fit above uses all {n:,}.")]
if regression == "deming" else []),
"values": values,
})
Read a comparison
CheckpointCheck yourself
A comparison gives slope 1.08 (95% CI 1.03 to 1.13) and intercept 0.2 (95% CI −0.5 to 0.9). What is it compatible with?
Show the answer
Answer: b
The slope's interval excludes 1 and the intercept's includes 0: the new method reads progressively higher, by about 8%.
Which method?
Read| Situation | Use |
|---|---|
| Error variance ratio unknown, outliers possible | Passing–Bablok |
| Known or estimable , roughly normal errors | Deming |
| Error grows with the level (a constant CV) | Weighted Deming |
With = 1, Deming gives slope 0.990. The ratio comes from replicate measurements; if you've no replicates, Passing–Bablok is the safer choice. All need a wide range of values, so run over the range you'll use in practice.
View data
| method A | method B |
|---|---|
| 15.210 | 15.510 |
| 18.290 | 19.750 |
| 16.390 | 18.090 |
| 9.080 | 10.100 |
| 10.850 | 12.410 |
| 4.740 | 3.870 |
| 14.960 | 16.400 |
| 19.410 | 20.330 |
| 8.450 | 10.270 |
| 13.420 | 14.920 |
| 17.970 | 20.070 |
| 17.270 | 20.150 |
| 14.260 | 14.450 |
| 3.160 | 2.340 |
| 10.230 | 13.120 |
| 11.350 | 12.830 |
| 16.220 | 18.780 |
| 11.140 | 12.830 |
| 5.490 | 4.050 |
| 16.370 | 19.390 |
| 16.670 | 20.460 |
| 4.310 | 5.840 |
| 17.570 | 19.290 |
| 2.310 | 2.840 |
| 13.360 | 16.010 |
| 12.480 | 13.350 |
| 16.940 | 17.550 |
| 7.040 | 8.490 |
| 17.310 | 19.940 |
| 2.470 | 2.960 |
| 18.920 | 18.440 |
| 6.330 | 6.780 |
| 1.340 | 3.050 |
| 5.130 | 7.260 |
| 15.150 | 15.560 |
| 16.090 | 18.520 |
| 19.550 | 22.350 |
| 18.270 | 18.080 |
| 15.480 | 16.040 |
| 18.740 | 18.560 |
Plan the samples
ReflectYou're about to compare a new assay or sensor with the current one. How would you choose samples that span the range you'll use in practice? What slope or bias would make you reject the new method? Decide before you collect anything.
Deming by hand
ReadR and Python have dedicated packages, but the Deming slope has a closed form. This reproduces the card.
import numpy as np
import pandas as pd
df = pd.read_csv("c3_methcomp.csv")
x, y = df["reference"], df["new"]
lam = 1.0 # error-variance ratio, new / reference
sxx, syy, sxy = x.var(ddof=1), y.var(ddof=1), x.cov(y)
slope = (syy - lam * sxx + np.sqrt((syy - lam * sxx) ** 2 + 4 * lam * sxy ** 2)) / (2 * sxy)
print(slope, y.mean() - slope * x.mean())df <- read.csv("c3_methcomp.csv")
x <- df$reference; y <- df$new
lam <- 1 # error-variance ratio, new / reference
sxx <- var(x); syy <- var(y); sxy <- cov(x, y)
slope <- (syy - lam * sxx + sqrt((syy - lam * sxx)^2 + 4 * lam * sxy^2)) / (2 * sxy)
c(slope = slope, intercept = mean(y) - slope * mean(x))Check your understanding
CheckpointCheck yourself
Why does ordinary least squares tend to report a slope below 1 even when two methods agree perfectly on average?
Show the answer
Answer: a
Error in the predictor pulls the slope toward zero. Deming and Passing–Bablok allow for error on both sides.
Check yourself
All your samples cluster within a narrow range. What's the consequence?
Show the answer
Answer: b
With a narrow range, the line is poorly anchored. Choose samples that span the range you'll use in practice.
Recap
Recap- With error on both sides, use Deming or Passing–Bablok instead of OLS.
- An intercept interval that excludes 0 means constant bias; a slope interval that excludes 1 means proportional bias.
- Check linearity, and cover the range you'll use.
Concepts in this lesson