Research prototype — not for clinical use. MSc dissertation project; results are internal validation and one external transportability check, not a clinical claim.

MSc Data Analytics Heart Failure ML 2026

Predicting heart
failure outcomes,
honestly.

Five models, four outcome horizons, 2,008 patients — and a full leakage audit that cut the headline AUROC from 0.819 to 0.710 before I believed a single digit of it. Then I retrained inside MIMIC-IV to test whether the neurological signal was real.

Author
Nikunj Prajapati
Institution
London Met.
Course
MSc Data Analytics
Cohort
Zhang / PhysioNet
External set
MIMIC-IV (42,990)
Updated
Oct 2026 · GCS extension

2008

Zhang patients

Heart-failure clinical records, Sichuan, China 2016–2019

42990

MIMIC-IV admissions

External transport set

4

Outcomes

28-day mortality, 3-month mortality, 6-month mortality, 6-month readmission

143

Clinical measurements

After leakage audit removal

4 outcomes · 143 clinical measurements · AUROC on held-out test set

Why

Doctors still rely on a score from 2013. Can machine learning do better, and explain itself?

Heart failure affects over 64 million people worldwide. After discharge, clinicians use risk scores such as MAGGIC, which ranks patients correctly about 74% of the time. These scores use a handful of variables and cannot explain individual predictions. This project asks whether machine learning on routine hospital data can do better, and show its reasoning.

64 M

people worldwide

have heart failure

~25%

30-day readmission (US)

of heart-failure patients

~0.74

MAGGIC C-index

traditional score benchmark

These scores use a handful of variables and cannot explain individual predictions. This project asks whether machine learning on routine hospital data can do better, and show its reasoning.

The plan

One study, two continents, and an audit

Train on Zhang, explain it, audit it, then see whether it survives contact with a US hospital.

Zhang data
Models (5 × 4 outcomes)
Survival analysis
SHAP explanations
MIMIC-IV test
Audit & calibration
Tool

Data

Built on 2,008 patients, but only 11 of them died in the test set

The Zhang cohort is 2,008 patients hospitalised with heart failure at Zigong Fourth People's Hospital in Sichuan, China (2016–2019), with outcomes from active follow-up. Deaths are rare: 57 patients (2.84%) died within 6 months, and only 11 of them fall in the 402-patient test set. That one fact shapes every number that follows.

Outcome rates

  • 28-day death: 37 (1.84%)
  • 3-month death: 42 (2.09%)
  • 6-month death: 57 (2.84%)
  • 6-month readmission: 773 (38.5%)

Key characteristics

VariableValue
BNP median753 pg/mL [IQR 304–1,739]
LVEF recorded635 patients (32%), 68% missing
NYHA class III52% most common
Age band (69,79]36% most common
Details accordion: cohort split

Train/test split: 1,606 / 402; deaths in train/test per outcome (28d 31/6, 3m 35/7, 6m 46/11); 11 variables dropped for over 80% missing; 2 near-zero-variance variables dropped; three-tier imputation (median under 10% missing, MICE 10–80%); SMOTE only inside CV training folds.

Outcome class balance: 28-day 37 deaths, 3-month 42 deaths, 6-month 57 deaths, 6-month readmission 773 events
Outcome class balance from Zhang cohort

Audit

I audited my own model. The headline number fell from 0.82 to 0.71.

My first pipeline scored AUROC 0.819 for 6-month mortality. With only 11 deaths in the test set that was too good, so I went looking for ways it could be cheating. After the fix, the honest figure is 0.710. Every number below comes from the audited pipeline.

Before audit

0.819

After audit

0.710

Outcome horizonBeforeAfterΔModel
6-month mortality (primary) 0.819 0.710 −0.109 XGBoost
28-day mortality 0.893 0.781 −0.112 Random Forest
3-month mortality 0.919 0.839 −0.080 Random Forest
6-month readmission 0.648 0.650 +0.002 XGBoost

Removed as leakage

  • Unnamed: 0
  • inpatient.number
  • dischargeDay
  • outcome.during.hospitalization
  • readmission & emergency-return timing proxies

Every removed feature was available only after the outcome was already decided — perfect hindsight, zero clinical value.

Two discharge variables, discharge destination and discharge department, are still among the 143 features.

Their effect has not yet been measured. Strongly recommend running the ablation before launch.

A second audit — the outcome label itself

Re-deriving the labels for the GCS extension exposed a defect in this project's own numbers.

The models target "death within six months of the discharge date". Re-deriving that label meant reading the extraction SQL line by line, and what came out of it is not a GCS result: it is a labelling defect that reaches the published headlines and the dissertation's own cohort. It is reported here rather than quietly patched.

Day 0 is inside the outcome window

scripts/08_mimic_gcs_extraction.py, lines 77 and 80

The labels are built with DATE_DIFF(dod, dischtime, DAY) BETWEEN 0 AND 28 and BETWEEN 0 AND 180, so a death on the discharge day counts as a death after discharge. In MIMIC-IV the recorded date of death equals the discharge date for patients who died in hospital, so this is the normal case for them rather than an edge case.

In-hospital death was never identified

hospital_expire_flag: zero occurrences in the repository

No script, notebook or model in this project has ever read the in-hospital-death field. That means no arm of the analysis has counted those patients, let alone restricted an evaluation to survivors to discharge — so no number here has yet been computed on post-discharge death alone.

How large the overlap is

MIMIC-IV extract: 2,323 of 42,990 admissions ended in hospital

Those admissions are 53% of the 4,416 28-day positives and 25% of the 9,438 six-month positives. The published headline is therefore a mixture of in-hospital and post-discharge death, and any "after discharge" wording attached to it is wrong for those patients.

The dissertation cohort has the same defect

Zhang cohort cross-tabulation, 2,008 patients, recomputed locally

Eleven patients are recorded as dying in hospital; all eleven sit inside the 3-month and 6-month death labels and nine of the eleven inside the 28-day label, so 19–30% of the dissertation's positives are the wrong kind of death. Eleven in 2,008 (0.55%) is far below the usual heart-failure in-hospital mortality, so treat it as a floor on the exposure rather than a count. Two of the eleven are not labelled 28-day deaths at all, which is internally inconsistent and still needs resolving upstream.

The dissertation cohort, cross-tabulated

2,008 patients from the Zhang / PhysioNet heart-failure cohort, whose records carry both an in-hospital outcome field and the death labels the dissertation was fitted on. Recomputed from the local file in this project.

In-hospital outcome against the labels used for modelling. Every in-hospital death is inside the 3-month and 6-month labels.
Recorded in-hospital outcome Patients Labelled 28-day death Labelled 3-month death Labelled 6-month death
Alive1,89081126
Died in hospital1191111
Discharge against advice107202020
Total positives2,008374257
  • 19–30% of each outcome's positives are in-hospital deaths. All eleven are inside the 3-month and 6-month labels; nine of the eleven are inside the 28-day label.
  • Eleven is a floor, not a count. 0.55% in-hospital mortality is implausibly low for heart-failure admissions against the usual 4–8%, so the field may under-record or the cohort may have excluded dying patients by construction.
  • Two rows are internally inconsistent. Two patients recorded as dying in hospital are not labelled 28-day deaths, which is not possible if the labels mean what they say. That points at a defect upstream that has not yet been located.
  • Events per variable. The dissertation fitted 143 features to 37, 42 and 57 events. Cross-validated AUROC standard deviations ran 0.129–0.165, and a Random Forest reported 0.9315 at 28 days with sensitivity 0.195 — strong ranking, almost no detection. Those point estimates need bootstrapped optimism correction before they are quoted again.

What this does not change

  • The patient-grouped split. Every MIMIC number on this site already used it, with zero patient overlap asserted before scoring.
  • The GCS extraction itself: GCS is left as a genuine missing value rather than imputed, gcs_total is excluded as a fabricated placeholder, and verbal response 0 is treated as untestable.
  • The direction of the leakage-audit result. If anything, a label that includes in-hospital deaths makes the audited numbers look better than they are.

What is now in progress

  • A single re-extraction carrying hospital_expire_flag and deathtime, with the old and corrected label columns side by side so every arm reads from one file.
  • A four-rung ladder — row-level split with old labels, grouped split with old labels, grouped split with day 0 excluded, grouped split restricted to survivors to discharge — to separate patient-overlap inflation from outcome-window inflation.
  • The mechanism test: the GCS increment for in-hospital death against post-discharge death.
  • Competing risks rather than survivor-only exclusion, so the whole cohort stays in the analysis.
  • A variable-availability statement, because part of the baseline is derived from ICD codes that are finalised after discharge.

Results

Four outcomes, honestly scored.

Gradient boosting and random forest clearly beat the linear and single-tree baselines. Mortality is predictable at moderate accuracy; readmission is hard, because it depends on things routine discharge data cannot see, such as support at home and medication adherence.

0.781

28-day mortality

Random Forest

0.839

3-month mortality

Random Forest

0.710

6-month mortality

XGBoost

0.650

6-month readmission

XGBoost

The gap between cross-validated and test AUROC is large for mortality. With 6 to 11 test deaths, a single patient moves the score a lot, so read the test numbers as rough.

Reading the threshold (6-month mortality, XGBoost): At the default 0.5 threshold, precision 0.40 (about 14× the 2.84% base rate), sensitivity 0.18, specificity 0.99.

At the default threshold the model flags few patients, but those it flags are about 14 times more likely to die than average.

Survival

When, not just whether.

Classification says whether a patient dies within 6 months. Survival models estimate how risk unfolds over time. A Random Survival Forest (C-index 0.850) outperformed Cox regression (0.803), suggesting non-linear effects matter.

Kaplan-Meier survival curves by NYHA class in the Zhang cohort — severity gradient the models lean on
Kaplan–Meier by NYHA class in the Zhang cohort

Cox regression: 0.803 C-index

Random Survival Forest: 0.850 C-index

Note: Intervals are wide due to small test set (11 deaths). Do not compare directly to MAGGIC's 0.741.

What the model looks at

What the model looks at (and what surprised me).

SHAP shows which measurements push a prediction up or down. For 6-month mortality the top signals are neurological status (Glasgow Coma Scale) and chronic kidney disease, followed by an echo marker of how the heart relaxes, left ventricular size, liver disease and congestion. BNP, the classic heart failure marker, ranks only 13th. Note that 97.2% of patients score GCS 15, so the model is picking out a small impaired group, not a smooth scale.

FeatureMean |SHAP|Label
GCS0.750Glasgow Coma Scale (alertness)
moderate_to_severe_chronic_kidney_disease0.648Chronic kidney disease
mitral_valve_AMS0.575Mitral valve A-wave (heart relaxation)
left_ventricular_end_diastolic_diameter_LV0.388Left ventricle size
liver_disease0.367Liver disease
congestive_heart_failure0.357Congestive heart failure flag
eye_opening0.337Eye opening (part of GCS)
reduced_ef_flag0.265Reduced ejection fraction (<40%)
basophil_ratio0.258Basophil ratio (inflammation)
creatine_kinase0.232Creatine kinase (heart muscle injury)

SHAP was run on the Zhang model and on both MIMIC-IV arms. The GCS family leads each cohort independently, but only 7 of the Zhang top-20 features overlap the MIMIC ranking, so the cross-cohort comparison stays qualitative rather than a rank correlation.

SHAP beeswarm plot for the final 6-month model
Click to expand
Scatter of feature rank positions in the Zhang and MIMIC-IV SHAP rankings
Click to expand
SHAP beeswarm plot for the ICU-only MIMIC-IV six-month model
Click to expand

External validation

Does it work in the US? Not directly, but retrained it does.

I tested the Zhang-trained model on 42,990 US heart failure admissions from MIMIC-IV. Only 24 features overlap between the datasets, and US 6-month mortality is 22% versus 2.8% in Zhang. Applied directly, the model scored AUROC 0.600 (95% CI 0.593–0.606): a tight interval, so this is a real failure to transport, not noise. Retrained on US data with the 24 shared features, it reached 0.801.

0.600

Zhang model applied directly

0.801

Retrained on MIMIC-IV

0.710

Audited internal

ROC curves for direct transfer to MIMIC-IV
Click to expand
28-day 0.781 held-out 0.524 direct transfer
6-month 0.710 primary 0.599 conservative, positive

Under true domain shift the AUROC falls substantially. A model trained in one hospital system should not be deployed in another without local retraining and checking.

Trust

How far can you trust it?

Ranking patients is not the same as giving reliable probabilities, or working equally for everyone. I checked calibration, subgroups and decision value. The honest summary: promising, but the test set is too small to be sure.

Calibration (6-month death)

Brier 0.027, ECE 0.023.

A model that always predicts the average risk has a Brier score of about 0.027, so the Brier score shows no gain over that baseline. Treat probabilities as rankings until recalibrated.

Subgroups

No-CKD: AUROC 0.753; CKD: AUROC 0.571 (CI 0.09–1.00, only 2 deaths).

Female: AUROC 0.684; Male: AUROC 0.750.

Intervals overlap and are very wide, so I cannot claim equal performance, only that the data are too thin to say.

Decision curve analysis

Model vs treat-all vs treat-none, thresholds 1–50%. Model net benefit beats treat-all from about the 2% threshold up.

From the CSV it appears to be only low thresholds (roughly up to about 10–15%).

Test set: 402 patients, 11 six-month deaths. Confidence intervals are wide.

Tool

Try the prototype.

The retrained model pair runs embedded on this site: change any of 37 inputs and watch the six-month mortality estimate move, with and without the GCS components. Evaluated in your browser — nothing leaves your machine. A research demo, not a clinical device.

Two patient-grouped XGBoost arms on the same holdout — a 34-feature baseline and the same 34 plus the three GCS components — with the holdout calibration and per-component GCS attribution shown alongside the reading.

Open the tool Research demo, not a clinical device.

Next

What I would do next.

Four priority directions for the next phase of this project.

1

Pull GCS from MIMIC-IV

chartevents items 220739, 223900, 223901, then retrain and run SHAP on US data

2

Test the discharge-variable question

If not already done in the audit section, run the ablation and report the result

3

Prospective and UK validation

NHS data, plus social determinants for readmission

4

Medicines optimisation

Use the medication records (already in the project) to see whether treatment adds signal

Method

Methods and reproducibility (collapsed, closed by default)

Data and cohorts · Preprocessing (drops, encoding, 3-tier imputation, MICE, feature engineering) · Models (LR, DT, RF, XGBoost, LightGBM; stratified 5-fold CV; SMOTE inside folds) · Survival (Kaplan-Meier, Cox, RSF) · Explainability (TreeSHAP) · External validation design · Reproducibility (143 features, random_state 17, repo link, nine scripts in run order 01_eda.py … 07_external_validation.py).

Data and cohorts

Zhang cohort: 2,008 heart-failure patients from Sichuan, China, 2016–2019. MIMIC-IV: 42,990 heart-failure admissions. Patient-grouped 80/20 split on subject_id with seed 17.

Preprocessing

Dropped 11 unnamed/id columns, 2 near-zero-variance variables. Three-tier imputation: median under 10% missing, MICE 10–80%, remaining filled with training-set median. SMOTE only inside CV training folds.

Models

Five models: Logistic Regression, Decision Tree, Random Forest, XGBoost, LightGBM. Stratified 5-fold CV grouped on subject_id. SMOTE only inside training folds. Final model selection based on test AUROC.

Survival

Kaplan-Meier curves, Cox proportional hazards model, Random Survival Forest. C-index reported for both.

Explainability

TreeSHAP for global and per-patient explanations. SHAP summary bar and beeswarm plots.

External validation

Zhang models transported to MIMIC-IV with 24 shared features. AUROC reported for direct transfer and after retraining.

Reproducibility

Repo: github.com/Nik85-png/hf-risk-heart-failure-ml. Nine scripts in run order: 01_eda.py … 07_external_validation.py. random_state 17 for all splits.

References

Five sources, no padding.

  1. Zhang et al. (2021). Heart failure clinical records. Scientific Data 8:46. doi:10.1038/s41597-021-00835-9
  2. Goldberger et al. (2000). PhysioBank, PhysioToolkit, and PhysioNet. Circulation 101(23), e215–e220.
  3. Johnson et al. MIMIC-IV. PhysioNet.
  4. Lundberg et al. (2020). SHAP. Nature Machine Intelligence.