Abstract
Natural language processing (NLP) is increasingly being applied to obtain unsupervised representations of electronic healthcare record (EHR) data, but their performance for the prediction of clinical endpoints remains unclear. Here we use primary care EHRs from 6,286,233 people with Multiple Long-Term Conditions in England to generate vector representations of sequences of disease development using two input strategies (212 disease categories versus 9,462 diagnostic codes) and different NLP algorithms (Latent Dirichlet Allocation, doc2vec and two transformer models designed for EHRs). We also develop a new transformer architecture, named EHR-BERT, which incorporates socio-demographic information. We then compare use of each of these representations to predict mortality, healthcare use and new disease diagnosis. We find that representations generated using disease categories perform similarly to those using diagnostic codes, suggesting models can equally manage smaller or larger vocabularies. Sequence-based algorithms perform consistently better than bag-of-words methods, with the highest performance for EHR-BERT.
Introduction
Electronic Healthcare Records (EHRs) represent a longitudinal "sentence" of a patient's medical history, captured through thousands of distinct diagnostic codes. Traditionally, analyzing this data required manual curation—grouping these thousands of codes into a few hundred broad categories to make the data computationally manageable. However, this process is labor-intensive and risks losing granular clinical information. This study evaluates whether modern Natural Language Processing (NLP) can automate this process by creating mathematical "embeddings" of patient histories to predict future health outcomes, and whether the level of data granularity (raw codes vs. broad categories) significantly impacts model performance.
Methods
The researchers compared several NLP algorithms to generate unsupervised patient representations, moving from simple frequency-based models to complex sequence-aware architectures. The methodology followed a pipeline of data processing, embedding generation, and downstream classification.
Four primary modeling approaches were tested:
- Latent Dirichlet Allocation (LDA): A "Bag-of-Words" approach that treats the medical record as an unordered collection of codes.
- Doc2vec: An extension of word-embedding models that learns a single vector for the entire patient record.
- Transformers (Med-BERT and BEHRT): State-of-the-art models using attention mechanisms to weigh the importance of specific events in a sequence.
- EHR-BERT (Novel): A custom architecture that extends the BEHRT model by integrating socio-demographic factors (gender, ethnicity, and socioeconomic deprivation) and the calendar year directly into the transformer's embedding layers.
To isolate the quality of the data representation, the resulting embeddings were fed into a simple, "frozen" logistic regression classifier to predict clinical outcomes.
Datasets and Cohort Selection
The study utilized the CPRD Aurum dataset, a representative primary care database from England. The final cohort consisted of over 6.2 million adult patients with Multiple Long-Term Conditions (MLTC).
The researchers compared two input strategies: a vocabulary of 212 broad disease categories versus a high-dimensional set of 9,462 specific diagnostic "Medcodes." This data was linked to Office for National Statistics (ONS) mortality records and Hospital Episode Statistics (HES) for emergency department and admission tracking.
Key Insights and Results
The Importance of Sequence
Models that account for the order and timing of diagnoses (Transformers) consistently outperformed those that merely count occurrences (LDA). This confirms that the chronological progression of disease contains vital latent signals for predicting future risk.
The Vocabulary Paradox
A significant finding was that models using nearly 10,000 specific codes performed almost identically to those using 212 broad categories. This suggests that NLP models are robust enough to handle raw, high-dimensional medical data, potentially removing the need for manual, human-curated code groupings in future research.
Predictive Performance
- Mortality: EHR-BERT achieved the highest predictive accuracy (ROC-AUC of 0.866), outperforming both standard transformers and traditional binary indicators.
- Disease Onset vs. Recurrence: All models were significantly more effective at predicting the recurrence of an existing condition (AUC ~0.92) than the first-time onset of a new disease (AUC ~0.75).
- Emergency Care: Predicting emergency department attendance remained the most challenging task (AUC ~0.64), likely due to external socioeconomic and geographic factors not fully captured in medical histories.
Discussion and Future Work
While sequence-aware models like EHR-BERT show clear superiority, the study highlights that "crude code frequency" (how often a code is repeated in a file) is often administrative noise rather than a marker of disease severity.
Future research may improve these results by employing non-linear classifiers (such as Gradient Boosting) or by "fine-tuning" the BERT models for specific clinical tasks rather than using unsupervised embeddings. Additionally, exploring how these representations vary across demographic subgroups will be essential to ensure algorithmic fairness and mitigate potential biases inherent in EHR coding practices.
Summary updated