Skip to content
 
 

Repository files navigation

🏥 Predicting Patient Encounter Cost

A machine learning framework for estimating hospital encounter costs, built to improve healthcare cost transparency for uninsured patients in the U.S.


📋 Overview

Uninsured patients in the United States frequently delay or avoid necessary medical care due to uncertainty about costs. Without reliable, upfront pricing information, patients face significant financial risk.

This project develops a predictive model for estimating the cost burden of a patient during a hospital encounter, using realistic synthetic patient data generated by Synthea. By capturing the complex factors that drive healthcare expenses, this framework aims to provide personalized, actionable cost estimates — empowering patients to make more informed healthcare decisions.

Course: DS5003 — University of Virginia, School of Data Science
Team: Claudio Cela, Claire Sullivan, Jack Hays, Sree Prabhav Bandakavi


📁 Repository Contents

File Description
DS5003_Groupproject_MLcode_v3_clean.ipynb Full modeling pipeline — preprocessing, XGBoost, Random Forest, evaluation, and publication-quality figures
1kdata_v2.zip Synthea 1K synthetic patient dataset (CSV format)
DS5003_Tableau.twbx Tableau workbook for exploratory data analysis
Presentation_Slides.pptx Final project presentation slides

🔬 Dataset

Data sourced from Synthea — an open-source synthetic patient population simulator produced by The MITRE Corporation.

  • 1,000 simulated patients
  • Encounter-level observations (each hospital visit = one row)
  • 61,459 observations across 240 features after preprocessing
  • Features include: age, gender, race, conditions (one-hot encoded), procedures (binned), medications (binned), encounter class, encounter duration, insurance provider, and more
  • Target variable: log(total encounter cost) = base encounter cost + procedure cost

⚙️ Methodology

Preprocessing

  • Merged patient, encounter, condition, procedure, and medication tables on encounter ID
  • One-hot encoded all 180 conditions present in the dataset
  • Binned procedures into 8 categories and medications into 9 categories
  • Log-transformed encounter cost to handle right-skewed distribution
  • 70/15/15 train/validation/test split

Models

Model Tuning Method Key Parameters
XGBoost Optuna (Bayesian search, 50 trials) max_depth, learning_rate, subsample, colsample_bytree, reg_alpha, reg_lambda
Random Forest RandomizedSearchCV (20 iterations, 3-fold CV) n_estimators, max_depth, min_samples_split, min_samples_leaf, max_features

📊 Results

Metric XGBoost Random Forest
MAE (log scale) 0.0926 0.0837
RMSE (log scale) 0.2072 0.1962
R² (log scale) 0.9887 0.9899
MAE (dollars) $735 $714
RMSE (dollars) $5,880 $6,546

Both models explain over 98% of the variance in log-transformed encounter cost. Random Forest achieves a lower average error, while XGBoost is more robust to high-cost outliers.

🔑 Top Feature Importances

Procedural features consistently outrank diagnosis indicators in both models:

  1. procedure_count — #1 in both models
  2. proc_obstetrics — #2 in Random Forest
  3. ENCOUNTER_TIME_MIN — encounter duration
  4. ENCOUNTERCLASS — inpatient/emergency vs. outpatient
  5. AGE, PRIOR_ENCOUNTER_COUNT — moderate signal

Key insight: What is done during an encounter drives cost more than the underlying diagnosis.


🚀 Getting Started

Requirements

pip install pandas numpy scikit-learn xgboost optuna matplotlib shap jupyter

Run the notebook

# Clone the repo
git clone https://github.com/Claudio-Cela/Predicting-Patient-Encounter-Cost.git
cd Predicting-Patient-Encounter-Cost

# Unzip the data
unzip 1kdata_v2.zip

# Launch Jupyter
jupyter notebook DS5003_Groupproject_MLcode_v3_clean.ipynb

Note: The XGBoost model supports GPU acceleration. If running on CPU, set tree_method='hist' in the XGBoost parameters. Windows users may need to set KMP_DUPLICATE_LIB_OK=TRUE to suppress OMP errors.

Cell execution order

  1. Data loading & preprocessing — merges all Synthea CSVs into a single encounter-level dataframe
  2. EDA — distribution plots and KDE of log-transformed cost
  3. XGBoost training — Optuna tuning, evaluation on val/test
  4. Random Forest training — RandomizedSearchCV tuning, evaluation on val/test
  5. Publication figures — generates Figures 1–5 for the paper

📈 Publication Figures

The final cell generates 5 publication-quality figures saved as PNG:

Figure Description
fig1_model_comparison.png MAE, RMSE, R² comparison (log scale)
fig2_actual_vs_predicted.png Actual vs. predicted scatter (99th pct clipped)
fig3_residual_distributions.png Residual histograms (1st–99th pct clipped)
fig4_feature_importances.png Top 15 features — XGBoost gain & RF permutation importance
fig5_val_vs_test.png Validation vs. test generalization

⚠️ Limitations

  • Data is synthetic (Synthea) — results may not fully generalize to real-world billing complexity
  • High-cost encounter tail is harder to predict accurately (lower dollar-scale R²)
  • Procedure and medication binning may obscure granular cost signals

📄 License

This project is licensed under the MIT License — see LICENSE for details.


🙏 Acknowledgments

  • Synthea™ by The MITRE Corporation for the synthetic patient data
  • University of Virginia School of Data Science — DS5003

About

This is our project building a model to predict healthcare cost for an uninsured individual so that we can take the stress out of U.S. healthcare

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages