TU Dortmund · On the Theory & Practice of Monte Carlo SimulationsBayesian Statistics2026Team of 2 · Prof. Dr. Katja Ickstadt & Zeyu Ding

Bayesian Logistic Regression via Metropolis-Hastings

Building a multi-chain Random Walk Metropolis-Hastings sampler from scratch in R to infer full posterior distributions for binary diabetes prediction without relying on Stan or JAGS.

RMetropolis-HastingsMCMCcodamvtnormcaretrenvLaTeX
120,000 Draws 3 Chains × 40k Post-Burn-In

Custom RWMH sampler in R: 3 chains × 50,000 iterations (10,000 burn-in) initialized from overdispersed N(0, 2) states.

R-hat <= 1.01 Multi-Chain Convergence

Univariate and multivariate Gelman-Rubin PSRF strictly <= 1.01, with ESS from 2,054 (BMI) to 3,113 (Intercept).

77.78% Acc Held-Out Test Set (N = 153)

95% CI [70.36%, 84.09%] vs. 65.36% baseline (p = 0.00059); 74.13% balanced accuracy, 62.26% diabetes recall, 86.00% specificity.

MLE Aligned Validated Against glm()

All 9 posterior means and 95% credible intervals closely match Frequentist MLE on standardized clinical predictors.

The Problem

Why this mattered

Logistic regression has no conjugate prior for a multivariate Gaussian prior on its coefficients, so the posterior normalizing constant cannot be solved in closed form. Rather than treating Stan or JAGS as a black box, this project implements a Random Walk Metropolis-Hastings (RWMH) sampler directly in R to estimate full posterior distributions and credible intervals across 8 clinical risk factors on the Pima Indians Diabetes dataset (768 patients).

My Contribution

What I built and owned

Completed as a two-person M.Sc. Data Science project (with Harsh Rana) for Prof. Dr. Katja Ickstadt and Zeyu Ding's course On the Theory and Practice of Monte Carlo Simulations (Winter Semester 2025/26, TU Dortmund). I built the reproducible 3-chain MCMC workflow in R with overdispersed starts, coda Gelman-Rubin (R-hat) and ESS diagnostics, renv dependency locking, portable here::here() paths, and 3x3 diagnostic figures, and authored the final LaTeX technical report.

Preprocessing, Custom MCMC Kernel & Diagnostics

01 · Preprocessing & Baseline GLM

Standardization & 80/20 Split

Standardized all 8 clinical predictors (Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, BMI, DiabetesPedigreeFunction, Age) to mean 0 and SD 1 so an isotropic proposal navigates posterior geometry evenly. Split the 768 patients 80/20 (615 train, 153 test, seed 2026) and fit a binomial glm() baseline as the MLE reference.

02 · Custom RWMH Sampler

Log-Posterior & 3 Overdispersed Chains

Implemented the log-posterior manually combining a Bernoulli logistic likelihood (with epsilon clamping against log(0)) and a weakly informative N(0, 100 I) prior. Simulated 3 independent chains of 50,000 iterations (10,000 burn-in) starting from N(0, 2) draws with an isotropic multivariate normal random walk tuned to step variance 0.003 (~30-50% acceptance).

03 · Convergence & Validation

R-hat, ESS, MLE Overlay & Test Scoring

Audited convergence via coda::gelman.diag (all univariate and multivariate R-hat <= 1.01), coda::effectiveSize (ESS 2,054 to 3,113), and LOWESS trace overlays. Verified posterior accuracy by plotting Frequentist MLE lines directly over all 9 marginal posterior densities and evaluating posterior-mean predictions on the 153-patient held-out set.

Bayesian RWMH Posteriors vs. Frequentist MLE (Standardized Predictors)

Pooled across 3 chains (120,000 post-burn-in draws) on 615 training patients with weakly informative prior Beta ~ N(0, 100 I).

ParameterFreq. MLEMLE 95% CIBayes MeanBayes 95% CI95% CI Signal
Glucose 1.020 [0.773, 1.283] 1.042 [0.785, 1.308] Strong positive driver
BMI 0.794 [0.541, 1.062] 0.813 [0.556, 1.079] Strong positive driver
Pregnancies 0.379 [0.148, 0.616] 0.384 [0.153, 0.617] Positive effect (> 0)
DiabetesPedigreeFunction 0.325 [0.107, 0.548] 0.334 [0.107, 0.561] Positive effect (> 0)
Age 0.206 [-0.030, 0.442] 0.212 [-0.026, 0.451] Mild positive (crosses 0)
BloodPressure -0.191 [-0.409, 0.025] -0.198 [-0.418, 0.019] Uncertain (crosses 0)
Insulin -0.090 [-0.319, 0.141] -0.091 [-0.324, 0.143] No clear effect (374 raw zeros)
SkinThickness -0.018 [-0.258, 0.225] -0.017 [-0.264, 0.232] Centered near 0 (227 raw zeros)
Intercept -0.853 [-1.070, -0.646] -0.863 [-1.074, -0.654] Baseline log-odds

Held-out test confusion matrix (N = 153, 0.5 cutoff): 86 TN, 14 FP, 20 FN, 33 TP for Diabetes (Outcome = 1). Overall accuracy = 77.78%, balanced accuracy = 74.13%, diabetes recall (sensitivity) = 62.26%, healthy specificity = 86.00%, diabetes precision = 70.21%.

MCMC Convergence & Posterior Validation Plots

Vector PDFs exported directly by Code/main_analysis.R across 120,000 pooled post-burn-in draws. Click any figure to inspect full resolution.

Key Takeaways

  • The from-scratch RWMH sampler reproduced all 9 Frequentist MLE estimates within 0.02 standardized log-odds units while giving direct posterior probability intervals for every clinical risk factor.
  • Glucose (mean 1.042, 95% CI [0.785, 1.308]) and BMI (0.813, [0.556, 1.079]) dominate diabetes risk, followed by Pregnancies and DiabetesPedigreeFunction; SkinThickness and Insulin remain centered near zero.
  • At the default 0.5 threshold on the 153-patient test split, the classifier reaches 77.78% accuracy (86.00% specificity on healthy patients, 62.26% recall on diabetic patients, 70.21% diabetes precision). Clinical screening would tune the probability cutoff below 0.5 and integrate over full posterior draws.
  • Two direct next steps for stronger clinical modeling: impute zero-encoded missing measurements in the Pima dataset (374 raw zeros in Insulin, 227 in SkinThickness) as latent variables, and replace isotropic random-walk proposals (ESS ~2% of raw draws) with adaptive covariance MCMC or HMC/NUTS.