Skip to content
svgmissing-by-group

Missing by group

Does missingness depend on a group (a hint that data are not missing completely at random)?

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

How to read it

  1. Read a cell as the share missing in that column within that group.
  2. Compare a row across groups: differences suggest missingness depends on the group.
  3. Check the group sizes in the headers before reading small groups.
  1. Callout 1: This column's missing share in one group.
  2. Callout 2: A row that differs across groups: not missing completely at random.
  3. Callout 3: The group's size.

Watch out

  • Depending on an observed group doesn't rule out dependence on unobserved values too.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Cell = missing rows / rows in the group; a missing group value forms its own “(missing)” column.
  • Groups: declared level order, else by frequency; at most 10 (plus “(missing)”).
  • Colour scales to the largest share in the table (at least 5 %), so small differences stay visible; the printed % is exact.
  • A pattern that differs by group is evidence against MCAR — confirm with Little's MCAR test (Analyze → Missing-data patterns).
# Missing by group — Codesplash Chart Lab (recipe "missing-by-group")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

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("missing-by-group-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["last_purchase"] = pd.to_datetime(df["last_purchase"])
df["plan"] = pd.Categorical(df["plan"].astype("string"), categories=["Basic", "Plus", "Pro"])

_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")

COLS = ["age", "income", "last_purchase", "satisfaction", "nps", "comments"]
g = df["plan"].astype(object).where(df["plan"].notna(), "(missing)").astype(str)  # a missing group value forms its own column
groups = [l for l in ["Basic", "Plus", "Pro"] if l in set(g)][:10]  # declared level order
groups += ["(missing)"] if (g == "(missing)").any() else []
share = df[COLS].isna().groupby(g).mean().T.reindex(columns=groups)  # missing rows / rows in the group
n = g.value_counts().reindex(groups)
vmax = max(0.05, float(share.max().max()))  # colour scales to the largest share (at least 5 %)
fig, ax = plt.subplots(figsize=(1.5 + 0.9 * len(groups), 0.8 + 0.45 * len(COLS)))
ax.imshow(share.to_numpy(), cmap=LinearSegmentedColormap.from_list("seq", SEQ), vmin=0, vmax=vmax, aspect="auto")
for i in range(share.shape[0]):
    for j in range(share.shape[1]):
        ax.text(j, i, f"{share.iat[i, j]:.0%}", ha="center", va="center", fontsize=7, color="white" if share.iat[i, j] > vmax * 0.6 else "black")
ax.set_xticks(range(len(groups)), [f"{l}\nn = {n[l]:,}" for l in groups], fontsize=8)
ax.set_yticks(range(len(COLS)), COLS, fontsize=8)
ax.set_title("Missing share by plan", fontsize=10, loc="left")
ax.grid(False)
plt.show()

Needs pandas, numpy, matplotlib · reads missing-by-group-demo.csv · app theme from the optional codesplash helper