Skip to content
All projects

Project 07 // Machine learning · Oct 2026

Medical Cost Modeling and Prediction

Which factors are associated with medical costs, and where does the model fail?

1,338 policyholders split into three cost bands by smoking and obesity, and R-squared rises from 0.09 (age and sex) to 0.75 (all predictors) to 0.86 (with the smoker × obese interaction), with 0.88 on held-out data.
R², in-sample (age + sex → full model)
0.09 → 0.86
Models compared
9
Held-out R²
0.88

In short

The question

Age and sex explain only 9% of medical charges. What explains the rest, and how well can charges be predicted?

What I built

  1. 01
    Insurance CSV1,338 policyholders
  2. 02
    R nested modelsNested models, then a smoker × obese interaction
    • Linear regression
    • Interaction effects and confounding
    • R
  3. 03
    Python / pandas
  4. 04
    scikit-learn CVLinear model matched ridge, lasso and random forest
    • Cross-validation
    • scikit-learn
  5. 05
    Subgroup errorsError differs a lot by subgroup
    • Subgroup error analysis

What it found

Average relative error by group
  • Non-smokers29%
  • Obese smokers5%

Same model, very different reliability.

  • A smoking × obesity interaction lifts in-sample R² from 0.75 to 0.86 (0.88 on held-out data). Because the cost bands are defined by smoking and obesity, this describes the data rather than validating a predictor.
  • The earlier sex effect was confounded by smoking: it flips from +$1,539 to −$494 once smoking is in the model.
  • The interpretable linear model matched ridge, lasso, and random forest and beat boosting, with a held-out R² of 0.88.
  • Python
  • scikit-learn
  • R

Full case study // tap a section to open it

01Problem & context

Age and sex explain only 9% of medical charges. What explains the rest, and how well can charges be predicted?

Before pricing a policy or targeting a health program, analysts need to know which factors actually drive costs, and how far a prediction can be trusted for each group of people.

A two-part study of 1,338 U.S. health-insurance records. Part 1 (R, 2021) asked how much age and sex explain annual charges, and found they explain only 9% of the variance.

Part 2 (Python, 2026) looks at what else is associated with charges: a smoking × obesity interaction is associated with three distinct cost bands, nine models are compared with cross-validation, and the best model is tested for where it fails.

02My role & approach

Two parts: my own 2021 R study of age and sex, and a 2026 Python extension with the full feature set, a nine-model comparison, and subgroup error analysis. I chose the questions and designed the evaluations, and used AI tools to double-check code for edge cases.

  1. 01

    Part 1 (R, 2021): five nested linear models on age and sex.

  2. 02

    Part 2 (Python, 2026): explained three cost bands with a smoker × obesity interaction, tested with a nested F-test and bootstrap confidence intervals.

  3. 03

    Compared nine models (linear, ridge, lasso, random forest, gradient boosting, and baselines) with 5-fold cross-validation, then scored a held-out test set once.

  4. 04

    Measured error by subgroup and ranked features by permutation importance.

Problems I hit and how I handled them

  • Testing whether a smoker × obese interaction explains three cost bands that age and sex could not (nested F-test, not just a chart).
  • Keeping model selection honest: 5-fold cross-validation on the training split only, with the test set scored once.
03Tech stack
  • Python (pandas, NumPy)

    Feature analysis, the cost bands, and bootstrap confidence intervals.

  • scikit-learn

    Pipelines, 5-fold cross-validation, ridge, lasso, random forest, gradient boosting, and permutation importance.

  • Statistical tests

    Nested-model F-tests and a Breusch–Pagan check for constant variance, written with NumPy.

  • R and ggplot2

    Part 1: five nested linear models and an R Markdown report.

Skills shown

  • Linear regression
  • Interaction effects and confounding
  • Cross-validation
  • scikit-learn
  • Bootstrap confidence intervals
  • Subgroup error analysis
  • R
04More results
  • Errors are uneven: 29% relative error for non-smokers versus 5% for obese smokers.
05What I learned & would improve

Lessons

  • One interaction term (smoking × obesity) took in-sample R² from 0.75 to 0.86 on top of the other predictors (0.88 on held-out data).
  • A good average can hide who the model serves worst: 29% relative error for non-smokers versus 5% for obese smokers.
  • The apparent sex effect flipped sign once smoking was accounted for, a real example of confounding.

What I would do next

  • Report error by subgroup next to the overall score by default.
  • Add prediction intervals and check their coverage for each subgroup.

Try it // runs in your browser

Write your own code

The 1,338 policyholders from this project, loaded into your browser. Edit the code and run it.

Table insurance: age, sex, bmi, children, smoker, region, charges

Ctrl + EnterLoading data...

Results appear here (first 10 rows). In Python, the last expression is shown, and print() works too.

Walkthrough

What each dashboard and diagram shows, and why it matters. Click an image to enlarge it.

  1. 1

    The three cost bands

    Charges fall into three bands that age and sex cannot explain. Smoking and obesity do.

    • 94% of people in the top band (over $30K) smoke, and 92% have a BMI of 30 or more.
    • Obesity barely changes costs for non-smokers but doubles them for smokers: a median of $40,904 for obese smokers.
    • Adding a smoker × obesity term raises R² from 0.75 to 0.86 (nested F-test, p < 0.001 in-sample; the final model’s held-out R² is 0.88).
  2. 2

    Nine models, compared fairly

    All model selection uses 5-fold cross-validation on the training split; the test set is scored once.

    • The interpretable linear model with the interaction matched ridge, lasso, and random forest (CV R² 0.86) and beat boosting.
    • Held-out test: R² 0.88, RMSE $4,209.
    • A log transform hurt here, likely because back-transforming underestimates the high-cost smokers.
  3. 3

    Where the model fails

    A strong overall R² hides uneven errors.

    • Relative error is 29% for non-smokers, 12% for smokers with BMI under 30, and 5% for obese smokers.
    • Once smoking is in the model, the earlier sex effect flips from +$1,539 to −$494: consistent with it standing in for smoking.
  4. 4

    Part 1 (2021): summary of findings

    What the models say about age, sex, and charges.

    • Age is a moderate, significant predictor: about +$258 in charges per year of age.
    • In Part 1, sex added little once age was accounted for.
    • Most variation comes from factors outside these two variables.
  5. 5

    Nested model comparison

    Each model adds one term, so the comparison shows exactly what each variable contributes.

    • Comparing nested models avoids over-reading a single regression.
  6. 6

    Report: exploration and fit

    Pages from the rendered R Markdown report.

    • Plots and model output sit next to the interpretation, so the reasoning is easy to follow.
Next projectMelbourne Housing Price Prediction
Esc

↑ ↓ to moveEnter to open