$$\rightleftharpoonup{xx}$$
$$\longleftharp{xx}$$,
$$\longrightharp{xx}$$,
Data source and ethics statement
This retrospective study used data from the MIMIC-IV (version 3.1), a publicly available, de-identified critical care EHR database hosted on PhysioNet. The database includes demographic information, vital signs, laboratory tests, diagnoses, procedures, and medications.
Access to MIMIC-IV requires credentialed authorization and adherence to the PhysioNet data use agreement and training requirements. The investigators completed the required training and were granted access (certificate number: 68351548). Because the database is de-identified, this study analyzed anonymized data and did not involve direct contact with patients; therefore, informed consent was not required. The complete, step-by-step executable modeling workflow is illustrated in Figure 1.

Figure 1. Overall modeling workflow for osteoporosis (OP) risk prediction in patients with diabetes. Schematic representation of the study pipeline, including cohort identification, data extraction, feature engineering, multi-model benchmarking, model selection based on validation performance, and SHAP-based interpretability of the final model. Please click here to view a larger version of this figure.
Study population and data extraction
Data were extracted from the Medical Information Mart for Intensive Care IV (MIMIC-IV) database using a database management tool. To ensure reproducibility, the exact SQL queries, including table names and filtering logic used for cohort retrieval, are provided in Supplementary File 1. Adult patients (≥18 years old) diagnosed with diabetes mellitus were identified using ICD-9 codes (249, 250) and ICD-10 codes (E08–E13). The primary outcome, OP, was defined using ICD-9 codes (733.x) and ICD-10 codes (M80, M81, M82). Within the initial eligible diabetic cohort (n = 46,226), 3,672 positive events (patients with OP) and 42,554 negative events (patients without OP) were identified. A detailed patient selection and attrition flowchart is presented in Figure 2.

Figure 2. Flowchart of patient selection and cohort construction. The flowchart illustrates the stepwise cohort derivation from the MIMIC-IV (v3.1) database. Adult patients with diabetes mellitus were identified using ICD codes, followed by exclusion of patients with age <18 years, missing critical data >30%, or invalid records. The final cohort was divided into OP (positive events) and non-OP (negative events). Class imbalance was addressed using random undersampling and SMOTE applied to the training data prior to stratified dataset splitting. Please click here to view a larger version of this figure.
Feature engineering
To ensure fair benchmarking across multiple ML algorithms and precise reproducibility, an identical sequence of preprocessing steps was applied: feature selection, missing value imputation, continuous feature scaling, class balancing, and dataset splitting. The full list of input features, including variable names, units, and data sources, is detailed in Table 1.
| Features | Total
(n = 14,688) | Training set
(n = 9,546) | Validation set
(n = 2,204) | Test set
(n = 2,938) | P value |
| Gender | | | | | 0.345 |
| Male | 6222 (42.36%) | 4045 (42.37%) | 935 (42.42%) | 1242 (42.27%) | |
| Female | 8466 (57.64%) | 5501 (57.63%) | 1269 (57.58%) | 1696 (57.73%) | |
| Age | 0.28 (−0.40, 0.94) | 0.28 (−0.43, 0.94) | 0.23 (−0.46, 0.89) | 0.28 (−0.39, 0.95) | 0.1655 |
| Red Blood Cells | −0.14 (−0.79, 0.49) | −0.13 (−0.79, 0.49) | −0.17 (−0.83, 0.48) | −0.14 (−0.76, 0.49) | 0.3641 |
| Hematocrit | −0.14 (−0.81, 0.52) | −0.13 (−0.80, 0.52) | −0.14 (−0.87, 0.51) | −0.16 (−0.81, 0.52) | 0.3709 |
| Hemoglobin | −0.12 (−0.79, 0.52) | −0.12 (−0.79, 0.51) | −0.15 (−0.86, 0.53) | −0.13 (−0.78, 0.52) | 0.3616 |
| RDW | −0.16 (−0.59, 0.45) | −0.17 (−0.59, 0.46) | −0.14 (−0.59, 0.45) | −0.17 (−0.60, 0.42) | 0.5803 |
| Phosphate | −0.14 (−0.60, 0.38) | −0.15 (−0.61, 0.38) | −0.11 (−0.57, 0.38) | −0.13 (−0.56, 0.34) | 0.415 |
| MCV | 0.05 (−0.50, 0.63) | 0.06 (−0.50, 0.65) | 0.03 (−0.49, 0.65) | 0.02 (−0.50, 0.59) | 0.5408 |
| Magnesium | −0.10 (−0.52, 0.37) | −0.10 (−0.52, 0.37) | −0.10 (−0.52, 0.37) | −0.09 (−0.50, 0.37) | 0.7797 |
| White Blood Cells | −0.16 (−0.47, 0.21) | −0.16 (−0.47, 0.20) | −0.15 (−0.48, 0.25) | −0.16 (−0.46, 0.20) | 0.6856 |
| Anion Gap | −0.12 (−0.64, 0.45) | −0.13 (−0.64, 0.45) | −0.07 (−0.61, 0.48) | −0.13 (−0.64, 0.45) | 0.1217 |
| Creatinine | −0.30 (−0.48, 0.01) | −0.30 (−0.48, 0.01) | −0.30 (−0.46, 0.01) | −0.30 (−0.48, −0.00) | 0.3323 |
| MCH | 0.11 (−0.48, 0.62) | 0.12 (−0.48, 0.62) | 0.11 (−0.47, 0.62) | 0.09 (−0.48, 0.61) | 0.5195 |
| Platelet Count | −0.09 (−0.61, 0.49) | −0.09 (−0.61, 0.49) | −0.08 (−0.62, 0.47) | −0.10 (−0.62, 0.48) | 0.9709 |
| MCHC | −0.00 (−0.59, 0.59) | −0.00 (−0.60, 0.59) | −0.00 (−0.57, 0.58) | 0.00 (−0.57, 0.60) | 0.9263 |
| Chloride | 0.05 (−0.54, 0.64) | 0.05 (−0.52, 0.65) | 0.03 (−0.59, 0.62) | 0.07 (−0.52, 0.62) | 0.4675 |
| Potassium | −0.07 (−0.67, 0.53) | −0.09 (−0.67, 0.53) | −0.07 (−0.67, 0.53) | −0.07 (−0.62, 0.53) | 0.3071 |
| Sodium | 0.05 (−0.56, 0.60) | 0.05 (−0.55, 0.60) | −0.00 (−0.60, 0.60) | 0.07 (−0.57, 0.61) | 0.3492 |
| Urea Nitrogen | −0.28 (−0.60, 0.29) | −0.28 (−0.60, 0.29) | −0.26 (−0.59, 0.31) | −0.28 (−0.59, 0.25) | 0.6826 |
| Total Calcium | 0.02 (−0.61, 0.59) | 0.02 (−0.61, 0.59) | −0.01 (−0.59, 0.56) | 0.01 (−0.61, 0.60) | 0.8203 |
Table 1. Baseline characteristics and engineered features of the study cohort. Categorical variables are presented as n (%). Continuous variables are presented as median (interquartile range) of standardized values. P values were calculated to compare distributions across the training, validation, and test sets using appropriate statistical tests.
Continuous variables with repeated measurements were aggregated using the arithmetic mean within the entire ICU stay observation window to yield a single feature vector per patient. Variables with a missingness rate exceeding 30% were excluded. For the remaining numerical variables, missing values were imputed using the K-Nearest Neighbors (KNN) algorithm (n_neighbors = 5, weights = 'uniform'). Categorical variables were imputed using the most frequent category and subsequently transformed using binary mapping, with any unseen categories assigned a vector of zeros.
Continuous variables were standardized using the z-score scaling formula (). Features exhibiting zero variance were explicitly removed prior to scaling. All preprocessing parameters (µ and σ.) were estimated strictly on the training set and applied consistently to the validation and test sets.
Given the severe class imbalance (3,672 vs. 42,554), a hybrid balancing strategy was applied exclusively to the training data to avoid inflating performance estimates. First, random undersampling was used to reduce the majority negative class to achieve a 1:2 (positive:negative) ratio (yielding 7,344 negative samples). Next, the Synthetic Minority Over-sampling Technique (SMOTE) was applied to the positive class to achieve a final 1:1 balanced ratio (7,344 vs. 7,344)8.
Finally, the cohort was split at the patient level into training (65%), validation (15%), and test (20%) sets using stratified sampling. To strictly control all random processes across imputation, balancing, and splitting, a global random seed (random_state = 42) was enforced.
Model architecture
To identify the most suitable predictive model for OP risk in patients with diabetes, a large-scale benchmarking framework was used to compare 70 ML algorithm variants under an identical data representation and evaluation protocol. Candidate model families included regularized linear classifiers, support vector machines (SVMs), kNN, tree ensembles, gradient boosting architectures, and multilayer perceptrons9,10,11,12,13,14,15,16,17.
Rather than using a traditional grid search, hyperparameter optimization was performed by evaluating predefined, robust configurations across these 70 model variants on the training split. Selection was strictly based on maximizing the area under the receiver operating characteristic (ROC) curve (AUC) on the validation set. The final best-performing model was configured with n_estimators = 200, with other parameters remaining at default settings of the software package listed in the Table of Materials. Evaluation metrics, including AUC, accuracy, F1-score, precision, and recall, were calculated using a default 0.5 probability threshold.
To support clinical transparency, SHAP was applied to the selected model using the TreeExplainer method. Given the tree-based nature of the final model, exact tree path-dependent expected values were used as the background configuration.