Skip to content
svgnested-compare

Nested model comparison

Does each added block of predictors improve the model enough?

Demo data · seed 20261101 · 4 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read left to right as adding blocks of predictors.
  2. Lower deviance means a closer fit, and it always falls.
  3. Read the labels between points: does each drop beat its test?
  1. Callout 1: The simplest model.
  2. Callout 2: A big drop, with its test.
  3. Callout 3: A small drop that doesn't earn its complexity.

Watch out

  • The order of adding blocks changes each step's test.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Draws the engine's numbers as given (precomputed by the Python or R analysis); the chart computes nothing inferential.
  • Models are in the order given (smallest first); each annotation is the test of that model against the previous one (LR χ², Wald, score or partial F).
# Nested model comparison — Codesplash Chart Lab (recipe "nested-compare")
# controls: defaults
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("nested-compare-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["model"] = pd.Categorical(df["model"].astype("string"), categories=["intercept", "+ age", "+ age + sex", "+ age × sex"])

_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")
M = "model"
V = "deviance"
S = "LR chi2"
P = "p"
d = df.dropna(subset=[V]).drop_duplicates(subset=[M]).reset_index(drop=True)
x = np.arange(len(d))
fig, ax = plt.subplots(figsize=(8, 4.5))
ax.plot(x, d[V], color=GREY, marker="o", markerfacecolor=PALETTE[0], markeredgecolor=PALETTE[0])
for i in range(1, len(d)):
    bits = ([f"stat {d[S][i]:.3g}"] if S and pd.notna(d[S][i]) else []) + ([("p < .001" if d[P][i] < 0.001 else f"p = {d[P][i]:.3f}".replace("0.", "."))] if P and pd.notna(d[P][i]) else [])
    ax.annotate(", ".join(bits), ((x[i - 1] + x[i]) / 2, (d[V][i - 1] + d[V][i]) / 2), ha="center", va="top", xytext=(0, -6), textcoords="offset points", fontsize=8)
ax.set_xticks(x, d[M].astype(str))
ax.set_ylabel(V)
plt.show()

Needs pandas, numpy, matplotlib · reads nested-compare-demo.csv · app theme from the optional codesplash helper