14 · Fehlende Werte und Imputation
missing_mechanismen.svg
Abbildung · Quellcode
Erzeugt von missing_mechanismen() in lib/diagrams.py, Zeile 417–449.
Python
Python-Code: in eine Datei mit Endung
.py schreiben und mit dem ▶-Knopf in VS Code ausführen – oder Zeile für Zeile in die Python-Konsole. Setzt die in Modul 02 eingerichtete Umgebung voraus.def missing_mechanismen() -> str: b = [eyebrow(40, 40, "Missingness-Mechanismen · MCAR vs. MAR vs. MNAR")] cw, ch = 200, 180 def draw_panel(x, title, subtitle, desc, missing_logic): parts = [rect(x, 80, cw, ch, r=10, fill=T["card"], stroke=T["rule"]), txt(x + cw / 2, 105, title, size=13, fill=T["ink"], w=700, anchor="middle"), txt(x + cw / 2, 122, subtitle, size=10, fill=T["mut"], anchor="middle")] # Draw 5 data rows y_start = 140 for i in range(5): ry = y_start + i * 20 is_missing = missing_logic(i) # Dot representing patients parts.append(rect(x + 20, ry, 60, 14, r=4, fill=T["soft"])) parts.append(txt(x + 25, ry + 11, f"Pat. {i+1}", size=9, fill=T["ink2"], mono=True)) if is_missing: parts.append(rect(x + 90, ry, 90, 14, r=4, fill=T["deatht"])) parts.append(txt(x + 135, ry + 11, "NA (Fehlt)", size=9, fill=T["death"], w=700, anchor="middle")) else: parts.append(rect(x + 90, ry, 90, 14, r=4, fill=T["acct"])) parts.append(txt(x + 135, ry + 11, "Wert gemessen", size=9, fill=T["acc2"], anchor="middle")) parts.append(txt(x + cw / 2, 280, desc, size=10, fill=T["mut"], anchor="middle")) return "".join(parts) b.append(draw_panel(40, "MCAR", "Völlig zufällig (Random)", "Zufälliger Laborfehler", lambda i: i in (1, 3))) b.append(draw_panel(280, "MAR", "Systematisch (Beobachtet)", "Ältere werden eher gemessen", lambda i: i in (0, 1))) b.append(draw_panel(520, "MNAR", "Systematisch (Unbeobachtet)", "Sehr Kranke sterben vor Messung", lambda i: i in (3, 4))) return canvas(760, 310, "".join(b), "Missingness mechanisms comparison MCAR MAR MNAR")