Discovering sleep quality with Machine Learning techniques
Latam online. Second Edition. 2021 The COVID-19 outbreak and the social responses adopted to combat its spread (e.g. lockdown and social distancing) have had far-reaching consequences,…
A learning project shared by the community. The article documents its team, methods and results; it does not establish that the prototype is ready for real-world use.

This archive includes automatic translations. Audio, video and text inside images remain in their original language.
Latam online. Second Edition. 2021
The outbreak of COVID-19 and the societal responses adopted to combat its spread (e.g., lockdowns and social distancing) have had far-reaching consequences, but have also provided a unique opportunity to examine the effects that chronic stress and uncertainty can have on sleep patterns in the general population.
It is well documented that sleep can be affected when under stress and that changes in sleep can have negative consequences on emotions and mental well-being. A recent meta-analysis showed that the COVID-19 pandemic(1) has led to a high prevalence of sleep disorders, affecting approximately 40% of people in the general population.
It is known that in addition to stress due to the COVID-19 pandemic, there are other factors that could alter the resilience shown under stress, for example, lack of social interaction, lack of physical exercise, economic well-being, etc.
This project aims to determine what factors can affect the quality of the population's sleep when they are subject to chronic stress situations, such as a pandemic. Predicting these factors would allow us to issue useful advice to the population, so that they are better prepared for future pandemics and/or situations of chronic stress that allow us to mitigate the emotional impact caused by poor quality sleep.
Methodology
The database selected for this project has been published by Boston University2, which consists of a series of surveys conducted with 1,518 people through social networks to evaluate the impact of the pandemic on the emotional and mental well-being of people depending on various factors such as age, economic impact, minority status, and risk status.
Once the database is selected, we continue to clean and process the data. After this process, the number of variables in our database was 216 and the sample size was reduced to 839 subjects because not all the information was complete.
The main variables include demographic information (age, gender identity, income, etc.), information related to sleep habits, anxiety levels, cognitive emotional regulation, and personality, among others.
The metric chosen to evaluate sleep quality was the Pittsburgh Sleep Quality Index (PSQI) variable, which has a scale from 0 to 21. On this scale, a global score of 5 or more indicates poor sleep quality; The higher the score, the worse the quality.
Dataset analysis
We observe the distribution of the data

Sleep score


We observe the correlation between sleep quality and other variables not associated with sleep:
- Difference in pre and post covid chronotype

- Anxiety variable (Can't stop worrying)

- Personality variable (Considered relaxed)

- Social anxiety variable (Fear of strangers post covid)

Models
Classification
In the data set, our chosen metric for evaluation takes values from 0 to 18 (no cases from 19 to 21 have been observed). If we take these values as if they were classes or subsets we can apply a classification method.
We have chosen to apply the RandomForestClassifier algorithm with which we initially obtain very low precision values around 0.20. Next, we try to reduce the classes by grouping the original values of the metric. As we reduce the classes we observe that the precision values improve, for 6 classes the result improves around 0.5 and for 4 classes the result reaches 0.7.
To have an explanation of these results we show the shap values of the predictions:

We can observe that the variables that appear with the most weight are those that are directly linked to sleep data (psqi_*). The first of all are whether the person has resorted to sleeping medicines, their self-reported perception of their sleep quality, and the time it takes to fall asleep. Within these variables we also observe that personality data (big5_*) has an impact, such as whether one considers oneself a relaxed person, or anxiety (gad_*), such as the ability to let go of worries.
Based on what we have observed, we can conclude that the algorithm is working correctly and that the data on which it is based to make the estimates are as expected. We believe that the number of observations we have is very low for the number of classes to predict and that increasing the data could improve the predictions.
Regression
For the regression we have chosen four different algorithms: RandomForest, Logistic, GradientBoosting and HistGradientBoosting.


The algorithm that produced the highest coefficient of determination was GradientBoosting, with R2=0.9. Which means that 90% of the points fit the regression line.

Once the best algorithm was selected we tried to use optimization of the arguments using RandomizedSearchCV but we did not get anything better. We also tried to reduce the number of variables by using PCA. We obtained the optimal number of variables and reduced the size of the database to that number, in this case 138, but the results worsened, the R2 decreased to 0.57.

PCA analysis to reduce the number of variables
For this reason we decided to stick with the result obtained with the GradientBooster algorithm as the best option to predict sleep quality.
Conclusion
It is possible to predict sleep quality with 90% accuracy. We were able to observe that sleep quality depends to a greater extent on variables related to:
- Medication
- Time it takes for a person to fall asleep
- Enthusiasm for doing things
- Stress management
- Controlling worries
This prediction, although intuitive, can be useful to implement measures that can help the population improve the quality of sleep in situations of chronic stress such as that suffered during a pandemic.
In the future, this project could be improved by increasing the number of samples in the current database.
References
1. Jahrami, H. et al. Sleep problems during COVID-19 pandemic by population: a systematic review and meta-analysis. J Clin Sleep Med, jcsm-8930. (2020).
2. Cunningham, T.J., Fields, E.C. & Kensinger, E.A. (2021) Boston College daily sleep and well-being survey data during early phase of the COVID-19 pandemic. Sci Data 8, 110. https://www.nature.com/articles/s41597-021-00886-y
Members
- Ana Galindo Alonso
- Bianey Palma
Project presentation: DemoDay
More Artificial Intelligence!
The mission of Saturdays.ai is to make artificial intelligence more accessible (#ai4all) through intensive courses and programs where projects for good are carried out (#ai4good).
If you want to learn more about artificial intelligence join our community at community.saturdays.ai or visit us on our website www.saturdays.ai Join us!
A history of the Saturdays.AI community. Originally published on March 9, 2022.
← More community stories


