svgdeming-fit
Deming fit
What is the line between two noisy measurements, when both have error?
How to read it
- Read the bold line as the fit when both measurements have error.
- Compare it with the identity line: an offset suggests constant bias, a different slope proportional bias.
- Change δ to see how the assumed error ratio moves the line.
- Callout 1: The Deming line.
- Callout 2: The identity line: perfect agreement.
- Callout 3: Segments slant: both x and y have error.
Watch out
- The slope depends on δ. Choose it from replicate measurements rather than from what looks good.
Defaults
- Error variance ratio δ (y ÷ x)
- 1
- How much noisier y is than x. δ = 1 is orthogonal regression; very large δ tends to ordinary least squares.
- Range 0.1 to 10, step 0.1
- Residual segments
- On
- Draw each row's path to the line.
Demo data
- Proportional bias in the demo
- 1.08
- The true slope behind the demo data.
- Range 0.8 to 1.3, step 0.01
Pinned in the code
- Slope b = (s_yy − δ·s_xx + √((s_yy − δ·s_xx)² + 4δ·s_xy²)) / (2·s_xy), intercept ȳ − b·x̄ (Linnet 1990).
- Fitted x̂ᵢ = xᵢ + b/(b² + δ) · (yᵢ − a − b·xᵢ); each segment joins (xᵢ, yᵢ) with (x̂ᵢ, a + b·x̂ᵢ).
- Both axes share one scale, so the identity line is the diagonal.
# Deming fit — Codesplash Chart Lab (recipe "deming-fit")
# controls: delta=1, segments=true
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]
df = pd.read_csv("deming-fit-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
X = "method A"
Y = "method B"
DELTA = 1 # ratio of y-error variance to x-error variance
d = df.dropna(subset=[X, Y])
x, y = d[X].to_numpy(float), d[Y].to_numpy(float)
sxx, syy, sxy = np.var(x, ddof=1), np.var(y, ddof=1), np.cov(x, y)[0, 1]
b = (syy - DELTA * sxx + np.sqrt((syy - DELTA * sxx) ** 2 + 4 * DELTA * sxy**2)) / (2 * sxy) # Linnet (1990)
a = y.mean() - b * x.mean()
fx = x + b / (b**2 + DELTA) * (y - a - b * x)
lim = np.array([min(x.min(), y.min()), max(x.max(), y.max())])
fig, ax = plt.subplots(figsize=(5.5, 5.5))
ax.plot(lim, lim, color=REF, linestyle="--", label="identity (y = x)")
for i in range(len(x)):
ax.plot([x[i], fx[i]], [y[i], a + b * fx[i]], color=REF, alpha=0.35, linewidth=0.8)
ax.plot(lim, a + b * lim, color=PALETTE[0], linewidth=2, label=f"y = {a:.3g} + {b:.3g}·x")
ax.scatter(x, y, s=16, color=PALETTE[0], edgecolors="white", linewidths=0.5, zorder=3)
ax.set_aspect("equal", adjustable="datalim")
ax.set_xlabel(X)
ax.set_ylabel(Y)
ax.legend(fontsize=8)
plt.show()Needs pandas, numpy, matplotlib · reads deming-fit-demo.csv · app theme from the optional codesplash helper
# Deming fit — Codesplash Chart Lab (recipe "deming-fit")
# controls: delta=1, segments=true
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else px.colors.qualitative.Plotly
df = pd.read_csv("deming-fit-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
X = "method A"
Y = "method B"
DELTA = 1 # ratio of y-error variance to x-error variance
d = df.dropna(subset=[X, Y])
x, y = d[X].to_numpy(float), d[Y].to_numpy(float)
sxx, syy, sxy = np.var(x, ddof=1), np.var(y, ddof=1), np.cov(x, y)[0, 1]
b = (syy - DELTA * sxx + np.sqrt((syy - DELTA * sxx) ** 2 + 4 * DELTA * sxy**2)) / (2 * sxy) # Linnet (1990)
a = y.mean() - b * x.mean()
fx = x + b / (b**2 + DELTA) * (y - a - b * x)
lim = np.array([min(x.min(), y.min()), max(x.max(), y.max())])
fig = go.Figure()
fig.add_scatter(x=lim, y=lim, mode="lines", line=dict(color=REF, dash="dash"), name="identity (y = x)")
sx = [v for i in range(len(x)) for v in (x[i], fx[i], None)]
sy = [v for i in range(len(x)) for v in (y[i], a + b * fx[i], None)]
fig.add_scatter(x=sx, y=sy, mode="lines", line=dict(color=REF, width=0.7), opacity=0.4, showlegend=False)
fig.add_scatter(x=lim, y=a + b * lim, mode="lines", line=dict(color=PALETTE[0], width=2.5), name=f"y = {a:.3g} + {b:.3g}·x")
fig.add_scatter(x=x, y=y, mode="markers", marker=dict(color=PALETTE[0], size=6), showlegend=False)
fig.update_layout(xaxis_title=X, yaxis_title=Y, yaxis_scaleanchor="x")
fig.show()Needs pandas, numpy, plotly · reads deming-fit-demo.csv · app theme from the optional codesplash helper
# Deming fit — Codesplash Chart Lab (recipe "deming-fit")
# controls: delta=1, segments=true
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("deming-fit-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
tok <- if (has_cs) codesplashr::cs_palette(dark = FALSE) else list()
seq_ramp <- if (is.null(tok$sequential)) c("#eff6fd", "#1a469d") else tok$sequential # low → high
div_ramp <- if (is.null(tok$diverging)) c("#c13e2e", "#f3f5f8", "#0859ac") else tok$diverging # −, 0, +
ref_col <- if (is.null(tok$reference)) "#4e5661" else tok$reference
grey_col <- if (is.null(tok$dim)) "#ced1d6" else tok$dim
X <- "method A"
Y <- "method B"
DELTA <- 1 # ratio of y-error variance to x-error variance
d <- data.frame(x = df[[X]], y = df[[Y]])
d <- d[is.finite(d$x) & is.finite(d$y), ]
sxx <- var(d$x); syy <- var(d$y); sxy <- cov(d$x, d$y)
b <- (syy - DELTA * sxx + sqrt((syy - DELTA * sxx)^2 + 4 * DELTA * sxy^2)) / (2 * sxy) # Linnet (1990)
a <- mean(d$y) - b * mean(d$x)
d$fx <- d$x + b / (b^2 + DELTA) * (d$y - a - b * d$x)
d$fy <- a + b * d$fx
p <- ggplot(d, aes(x, y)) +
geom_abline(slope = 1, intercept = 0, colour = ref_col, linetype = "dashed") +
geom_segment(aes(xend = fx, yend = fy), colour = ref_col, alpha = 0.35) +
geom_abline(slope = b, intercept = a, colour = cs_cat[[1]], linewidth = 0.9) +
geom_point(colour = cs_cat[[1]], size = 1.4) +
coord_equal() +
labs(x = X, y = Y, subtitle = sprintf("y = %.3g + %.3g x", a, b)) + theme_cs
print(p)Needs ggplot2 · reads deming-fit-demo.csv · app theme from the optional codesplashr helper