Skip to content
svginfluence-index

Cook's distance by row

Which rows pull the fit the most?

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

How to read it

  1. Read each stem as how much the fit changes if you leave that row out.
  2. Compare with the dashed cutoff: rows above it are named.
  3. Look at those rows and find out why they differ.
  1. Callout 1: A named row, above the cutoff.
  2. Callout 2: The dashed line: the cutoff.
  3. Callout 3: Most rows have little pull.

Watch out

  • An influential row isn't automatically wrong: don't delete it for being influential. Report the fit with and without it.

Defaults

Cutoff
4 / n
4/n flags rows worth a look; 1 flags rows that shift the whole fit.
Also: 1

Pinned in the code

  • Bars (stems) from zero at each row's Cook's distance, as R's plot.lm(which = 4).
  • A row above the cutoff is drawn in the warning colour and labelled.
# Cook's distance by row — Codesplash Chart Lab (recipe "influence-index")
# controls: rule=four
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("influence-index-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")
D = "Cook's distance"
I = None
L = "row"
d = df.dropna(subset=[D]).reset_index(drop=True)
idx = d[I].to_numpy(float) if I else np.arange(1, len(d) + 1)
cut = 4 / len(d)  # the 4/n convention
flag = d[D] > cut
fig, ax = plt.subplots(figsize=(7, 4))
ax.vlines(idx, 0, d[D], colors=[PALETTE[1] if f else PALETTE[0] for f in flag])
ax.axhline(cut, color=REF, linestyle="--", linewidth=1, label=f"cutoff {cut:.3g}")
for i in np.flatnonzero(flag)[:12]:
    ax.annotate(str(d[L].iloc[i]) if L else str(int(idx[i])), (idx[i], d[D].iloc[i]), xytext=(0, 3), textcoords="offset points", ha="center", fontsize=8)
ax.set_xlabel("row")
ax.set_ylabel("Cook's distance")
ax.legend(fontsize=8)
plt.show()

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