Accepted for/Published in: Journal of Medical Internet Research
Date Submitted: Jul 10, 2025
Date Accepted: Jan 23, 2026
Electrocardiogram-Based Mental Stress Detection Amid Everyday Activities Using Machine Learning: Model Development and Validation Study
ABSTRACT
Background:
Frequent, sustained stress links to adverse health outcomes and needs monitoring for early intervention. The ECG is a promising biomarker because it can be recorded non-invasively and continuously with wearable devices. However, tracking stress with ECG is challenging because daily activities can cause responses similar to mental stress. Additionally, the variety of mental stimuli individuals experience complicates the use of machine learning (ML) models trained on only a limited set of stressors.
Objective:
We aim to (1) evaluate ML models' ability to distinguish mental-stress episodes from a composite "no-stress" background, including rest and low- to moderate-intensity activities; (2) assess their generalizability to new stressors and participants; and (3) test robustness to lower sampling rates and fewer features, exploring their suitability for lightweight wearables.
Methods:
We used a comprehensive ECG dataset sampled at 1,000 Hz from 127 participants who underwent various mental stressors and engaged in diverse physical activities. A 30-second window extracted 55 features across time, frequency, non-linear, and morphological domains. We trained a logistic regression (LR) model and an extreme gradient boosting (XGBoost) model, splitting the data 60/20/20 for training, validation, and testing data. Performance was assessed using the area under the receiver operating characteristic (AUROC) and the area under the precision-recall curve (AUPRC). Shapley additive explanation values were computed to explain model predictions. Additional analyses included leave-one-stressor-out, downsampling to 500, 250, and 125 Hz, a time-window sensitivity analysis, and reducing the number of features to as few as five.
Results:
XGBoost achieved an AUROC of 0.740 (95% CI 0.728–0.752) and an AUPRC of 0.705 (95% CI 0.687–0.723), versus 0.723 (95% CI 0.710–0.736) and 0.692 (95% CI 0.674–0.707) for logistic regression. The mean performance difference between XGBoost and logistic regression was Δ=0.016 for AUROC (95% CI 0.010–0.023) and Δ=0.013 for AUPRC (95% CI 0.003–0.024) (200 resamples). When downsampled to 125 Hz, XGBoost’s AUROC and AUPRC dropped to 0.731 and 0.700 (LR: AUROC: 0.720; AUPRC: 0.686), demonstrating robustness to lower sampling rates. Likewise, using ten features preserved over 95% of the full model’s performance. Extending the analysis window from 30 to 60 seconds improved AUROC and AUPRC across all sampling rates (AUROC: XGBoost, 0.757; LR, 0.740; AUPRC: XGBoost, 0.741; LR, 0.729), highlighting a trade-off between quick detection and performance. Both models were able to generalize to unseen stressors, although their performance varied depending on the held-out stimulus. Feature importance analysis identified fuzzy entropy and frequency-based features as important to our ML models.
Conclusions:
We demonstrate that ML models can distinguish between mental stress and routine activities using ECG, generalize to new mental stimuli, and remain robust to lower sampling rates and fewer features. This supports the development of lightweight, single-sensor ECG wearables for detecting mental stress.
Citation
Request queued. Please wait while the file is being generated. It may take some time.
Copyright
© The authors. All rights reserved. This is a privileged document currently under peer-review/community review (or an accepted/rejected manuscript). Authors have provided JMIR Publications with an exclusive license to publish this preprint on it's website for review and ahead-of-print citation purposes only. While the final peer-reviewed paper may be licensed under a cc-by license on publication, at this stage authors and publisher expressively prohibit redistribution of this draft paper other than for review purposes.