{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# egfr40 quickstart\n", "\n", "Score the 3-year risk of a ≥40% eGFR decline or kidney failure for a small, made-up\n", "cohort, then see what drives each individual's risk.\n", "\n", "## 1. A small cohort\n", "\n", "Five made-up individuals: one in each of the model's four strata (diabetes or not,\n", "eGFR ≥60 or <60), plus one with a missing blood pressure." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from egfr40 import add_egfr40_risk_col, egfr40_risk\n", "\n", "# One row per individual; column names match add_egfr40_risk_col's defaults\n", "cohort = pd.DataFrame({\n", " \"individual\": [\"A\", \"B\", \"C\", \"D\", \"E\"],\n", " \"age\": [50, 58, 72, 70, 64],\n", " \"sex\": [\"female\", \"male\", \"male\", \"male\", \"female\"],\n", " \"eGFR\": [95, 82, 42, 40, 75],\n", " \"uACR\": [8, 40, 150, 150, 25], # mg/g\n", " \"sbp\": [120, 138, 140, 140, None], # individual E has no BP reading\n", " \"bp_meds\": [\"no\", \"yes\", \"yes\", \"yes\", \"no\"],\n", " \"hf\": [0, 0, 0, 0, 0],\n", " \"chd\": [0, 0, 1, 1, 0],\n", " \"afib\": [0, 0, 1, 1, 0],\n", " \"smoking\": [\"never\", \"current\", \"former\", \"former\", \"never\"],\n", " \"bmi\": [25, 31, 28, 28, 27],\n", " \"dm\": [0, 1, 0, 1, 0],\n", " \"hba1c\": [None, 8.0, None, 7.0, None], # % ; only needed when dm = 1\n", " \"oral_dm\": [0, 1, 0, 0, 0],\n", " \"insulin\": [0, 0, 0, 0, 0],\n", "})\n", "cohort" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## 2. Add a risk column for every individual\n", "\n", "`add_egfr40_risk_col` scores each row with its own stratum and adds `egfr40_3year`.\n", "Individual E gets `NaN` because a required input (systolic BP) is missing." ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "scored = add_egfr40_risk_col(cohort)\n", "\n", "# Label each individual's stratum to see which model was used\n", "scored[\"stratum\"] = (\n", " scored[\"dm\"].map({0: \"No DM\", 1: \"DM\"})\n", " + \", eGFR \"\n", " + scored[\"eGFR\"].ge(60).map({True: \">=60\", False: \"<60\"})\n", ")\n", "scored[\"risk_%\"] = (scored[\"egfr40_3year\"] * 100).round(1)\n", "\n", "scored[[\"individual\", \"stratum\", \"eGFR\", \"uACR\", \"egfr40_3year\", \"risk_%\"]]" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## 3. What drives one individual's risk\n", "\n", "`explain=True` breaks individual D's risk into each predictor's contribution to the\n", "log-odds. Positive values raise risk; the largest drivers come first." ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "out = egfr40_risk(\n", " age=70, male=1, egfr=40, uacr=150, sbp=140, bp_meds=1,\n", " hf=0, chd=1, afib=1, smoking=\"former\", bmi=28,\n", " dm=True, hba1c=7, explain=True,\n", ")\n", "print(out[\"stratum\"], f\"risk = {out['risk']:.1%}\")\n", "\n", "pd.Series(out[\"contributions\"], name=\"log-odds contribution\").round(3).to_frame()" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "## 4. Top drivers for every individual\n", "\n", "Apply `explain=True` row by row and keep each individual's three largest drivers.\n", "Rows with missing inputs are skipped." ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "def top_drivers(row, n=3):\n", " \"\"\"Return the n largest contributions for one complete row.\"\"\"\n", " out = egfr40_risk(\n", " age=row.age, male=int(row.sex == \"male\"), egfr=row.eGFR, uacr=row.uACR,\n", " sbp=row.sbp, bp_meds=int(row.bp_meds == \"yes\"), hf=row.hf, chd=row.chd,\n", " afib=row.afib, smoking=row.smoking, bmi=row.bmi, dm=bool(row.dm),\n", " hba1c=row.hba1c if row.dm else None, oral_dm=row.oral_dm,\n", " insulin=row.insulin, explain=True,\n", " )\n", " top = list(out[\"contributions\"].items())[:n]\n", " return \", \".join(f\"{k} {v:+.2f}\" for k, v in top)\n", "\n", "complete = scored.dropna(subset=[\"egfr40_3year\"])\n", "complete.assign(top_drivers=complete.apply(top_drivers, axis=1))[\n", " [\"individual\", \"stratum\", \"risk_%\", \"top_drivers\"]\n", "]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }