Applied ML · Peer-reviewed ResearchSHT 03 OF 06

Predicting Long-Term Pain After Total Hip Replacement

A machine-learning pipeline that estimates a patient's expected pain score 3 and 5 years after primary total hip arthroplasty, published in The Journal of Arthroplasty and deployed as a clinician-facing calculator.

The Journal of Arthroplasty · 2026 · DOI 10.1016/j.arth.2026.04.023 · 2nd of 7 authors

Published: J. Arthroplasty 2026
Cover of The Journal of ArthroplastyThe Journal of Arthroplasty · open accessMachine Learning Using Preoperative Patient Factors Can Predict the Severity of Pain Following Primary Total Hip ArthroplastyDOI 10.1016/j.arth.2026.04.023 · 2nd of 7 authors · Read the paper ↗
Best T3 MSE
2.70
KNN: beats the 3.07 mean baseline; nonlinear models won at both timepoints
Authorship
2nd of 7
the only engineer among the MDs + PhDs, led the ML implementation end to end
Patients
513
SAFE-T prospective cohort, two academic hospitals
Models compared
13
linear · tree ensembles · neural nets

Stack & methods

13 models
linear, tree ensembles, KNN, and neural nets
CatBoost
strongest at T5; carried into the calculator
scikit-learn
imputation, scaling, and GridSearchCV
Streamlit
the deployed clinician calculator
SAFE-T cohort
513 patients across T3 and T5
Peer-reviewed
J. Arthroplasty · 2nd of 7 authors

From the paper

Bar charts of CatBoost top-10 feature importances at 3 and 5 years: preoperative pain-perception items, age, BMI, length of stay, and back/neck history dominate.

Figure 4 of the paper: what drives long-term pain in CatBoost (strongest at 5 years, and the model carried into the calculator): patient-reported pain-perception items, age, BMI, and length of stay.

Bar charts of the pain-score distribution at 3 and 5 years showing most patients report zero or near-zero pain.

Figure 1 of the paper: the heavy skew toward zero pain. This class imbalance is why headline 'accuracy' flatters the baseline, and why the study leads with MSE and buffer accuracy.

ModelMSEBuffer ±1Buffer ±2
KNN2.7063.4%83.1%
XGBoost2.7759.2%80.3%
Random Forest2.7952.1%77.5%
CatBoost2.8350.7%85.9%
Linear Regression4.2042.3%70.4%
Mean baseline3.0721.1%90.1%
Selected rows from Table 2 of the paper: 3-year (T3) predictions on the held-out test set (n = 72, the 20% held out of the 355 patients with a complete 3-year outcome). Lower MSE is better; buffer ±1 / ±2 is the share of predictions within 1 / 2 points of the true 0–10 pain score. Nonlinear models beat both the mean baseline and every linear model. The baseline's high ±2 (90.1%) is the class-imbalance artifact discussed in Results: predicting ‘near zero’ lands within 2 points for the many low-pain patients. Full 13-model tables (T3 + T5) in the publication.

Where the data comes from

I didn't collect any of this data. I was the only engineer on a team of orthopaedic surgeons and researchers, and my job was to turn an existing clinical dataset into something predictive. Understanding that dataset, and cleaning it, was most of the work.

Everything runs on one cohort called SAFE-T: 513 people who had a hip replacement at two academic hospitals and were then checked in with for years afterward. Before their surgery, the usual things were recorded, their age and BMI, the details of the operation, other health conditions, and standard pain-and-function questionnaires (WOMAC and ICOAP). Then at the three-year and five-year marks, one number mattered most: how much hip pain they still had, rated 0 to 10. The model learns from the before-surgery answers to predict that later pain score, so a surgeon could estimate a new patient's risk during a consultation.

Data provenance

3 sources

SRC 01SAFE-T cohort513 people who had a primary hip replacement at two academic hospitals, followed from before surgery out to five years.An existing study, given to me, not collected by me
SRC 02Before-surgery inputsAge, BMI, the surgery details, other health conditions, and standard WOMAC / ICOAP pain-and-function questionnaires.Recorded at the pre-operative visit
SRC 03The pain to predictEach patient's average hip pain, 0 to 10, at the three-year and five-year follow-ups.Follow-up visits (T3 and T5)
one row per patient

Pipeline

  1. Keep usable slice
  2. Clean & impute
  3. Select features
  4. Modeling dataset
513 patients, 71 candidate variables (60 numeric, 11 categorical). On clinical data this messy, most of the accuracy is won here, in the cleaning and feature selection, before any model runs.

Approach

ML pipeline

  1. 513 patients71 preop features
  2. Impute + scaleiterative · robust-scale
  3. 13 modelslinear · trees · neural
  4. Select bestKNN / CatBoost
  5. CalculatorStreamlit tool
GridSearchCV ran inside the training set; the held-out test set was touched once, only after the hyperparameters were locked.

The dataset was Cohort 1 of the prospective SAFE-T study: 513 patients undergoing primary unilateral THA at two large academic hospitals, with data collected from preoperative baseline through five years postoperatively. Pain was measured on a 0–10 visual analog scale at 3-year (T3) and 5-year (T5) follow-ups and served as the target variable.

Feature engineering. 71 candidate features (60 numeric, 11 categorical) spanning demographics, surgical variables, comorbidities, and patient-reported outcome measures (WOMAC and ICOAP osteoarthritis pain scales). Numeric features were imputed via iterative imputation to preserve inter-feature relationships; categorical features used most-frequent imputation. Continuous variables like height and weight were robust-scaled to dampen outliers; bounded scales were normalized to [0, 1].

Model family. 13 models spanning four families: linear (linear regression, elastic net, linear SVM, SGD), tree ensembles (decision tree, random forest, AdaBoost, XGBoost, CatBoost, LightGBM), an instance-based method (KNN), and neural networks (scikit-learn MLP, PyTorch MLP). A mean regressor served as the floor-comparison baseline.

Training protocol. Stratified 80/20 train/test split, taken within each timepoint's outcome-complete subset: of the 513 eligible patients, the 3-year model used the 355 with a recorded T3 outcome (283 train / 72 test) and the 5-year model the 459 with a T5 outcome (366 / 93). GridSearchCV inside the training set to minimize MSE; the held-out test set was only touched once final hyperparameters were locked. Deep models used Adam with batch size 128.

Feature importance. Four complementary methods (Spearman correlation, Kruskal–Wallis, Random Forest importance, recursive feature elimination) were combined via cross-validated rank aggregation to identify the variables that drove the top-performing models. Age, BMI, history of back and neck problems, and specific WOMAC / ICOAP items consistently surfaced as the strongest preoperative predictors.

Problem

Total hip arthroplasty (THA) is one of the most successful procedures in modern medicine (over half a million performed annually in North America), but up to 23% of patients still report chronic postoperative pain, which contributes to higher opioid use, readmission rates, and worse overall outcomes. Identifying patients at higher risk of persistent pain preoperatively is a critical clinical challenge: surgeons currently have no validated tool to estimate this risk, and traditional regression models have struggled to capture the non-linear relationships that drive the outcome.

The collaboration with the Holland Bone & Joint Program at Sunnybrook and the University of Toronto Department of Orthopaedic Surgery aimed to (1) build and validate ML models that predict long-term pain from preoperative patient factors, and (2) translate the best-performing model into a web tool that a clinician could use during a patient consultation.

My role

Among the seven co-authors I was the only engineer; the senior team were MDs and PhDs at Sunnybrook + UofT. I led the technical implementation: feature preprocessing and imputation, model selection and training across 13 algorithms, hyperparameter search with cross-validation, the four-method feature importance pipeline, and the Streamlit calculator that surfaces the model in a clinically usable form.

Results

Non-linear models won decisively at both timepoints. Across all four metrics, CatBoost, Random Forest, and KNN were the most consistent top performers: KNN took the lowest 3-year MSE (2.70), CatBoost the lowest at 5 years (4.11). Linear models (Linear Regression and Elastic Net) had higher MSE and weaker buffer accuracy at both T3 and T5, consistent with prior orthopedic literature showing linear methods struggle to capture the non-linear relationships in postoperative-pain data.

At T3, KNN achieved the lowest MSE (2.70) followed closely by XGBoost (2.77), Random Forest (2.79), and CatBoost (2.83), all beating the 3.07 mean baseline. CatBoost had the highest ±2 buffer accuracy of any trained model at 85.9% (the mean baseline's higher 90.1% is the class-imbalance artifact discussed below, not skill). At T5, CatBoost achieved the lowest MSE (4.11), with Random Forest second (4.30).

An honest caveat on the 90.1% classification accuracy. The paper reports 90.1% classification accuracy across the low / moderate / high pain categories, but this number was also reached by the mean regressor, reflecting the heavy class imbalance in the dataset (most patients report minimal or no pain at T3/T5). The more clinically meaningful signal is the MSE gap (2.70 vs 3.07) and the buffer accuracy spread.

Clinical translation

Feature-importance analyses guided the input set for a web-based calculator that estimates expected pain severity from a small number of preoperative inputs. KNN edged the field on 3-year MSE, but CatBoost is what drives the calculator: strongest at the 5-year horizon, native handling of the categorical inputs, and the most interpretable of the top models, so its feature importances (Figure 4) are what made the input set clinically legible. The tool was built with Streamlit, deployed to Streamlit Community Cloud, and is the version cited in the published paper.

Live tool · the version cited in the paperTry the pain-prediction calculator: enter preoperative inputs, get a 3- and 5-year estimateStreamlit · CatBoost model · deployed to Streamlit Community CloudRUN ↗
The deployed Streamlit calculator: a Pain Score Prediction sidebar with a T3 (3 years) / T5 (5 years) timepoint selector, and Patient Information inputs (length of stay, BMI, current weight, WOMAC and ICOAP pain items, and pre-op age) that produce a predicted post-operative pain score.

The deployed clinician calculator, the CatBoost model from the paper wrapped in a Streamlit UI: pick the 3- or 5-year horizon, enter the preoperative inputs, and it returns the predicted pain score. This is the version cited in the publication.

Tech stack

Languages & data

  • Python
  • pandas
  • NumPy

ML frameworks

  • scikit-learn
  • PyTorch

Gradient boosting

  • XGBoost
  • CatBoost
  • LightGBM

Tuning & deployment

  • GridSearchCV
  • Streamlit

Reflection

The biggest lesson had nothing to do with which model won: it was how much of the result was decided before any model ran. The SAFE-T cohort was real, messy clinical data: 71 candidate features spanning demographics, surgery, comorbidities, and patient-reported WOMAC / ICOAP pain scales, full of missing values and outliers. Getting meaningful predictions out of it was mostly a data problem: iterative imputation to fill numeric gaps without flattening the relationships between features, most-frequent imputation for categoricals, robust-scaling so outliers couldn't dominate, and then distilling 71 raw variables down to the few that actually carried signal using four complementary feature-importance methods pooled by cross-validated rank aggregation. The modeling was almost the easy part. On data this messy, the accuracy is won in the cleaning and the feature engineering, and it's the step I'd tell anyone starting a clinical-ML project to budget the most time for.