ML Inquiry into Sleep
I'm a measured self enthusiast, and in the past couple of years I've been logging my nightly sleep with a Dreem headset, which monitors the different stages of sleep with relative high accuracy. The IoT device uses an infrared pulse and oxygen sensor, 6 electro-encephalograms (EEG) sensors to measure brain waves, and an accelerometer to measure movement and breathing patterns. All this data is presented in an app, and available for export as a CSV file.
As part of a project at Lambda School's nightly part time two-year machine learning (ML) course that I'm taking, I decided to use this data to try to see if I could find new insights using Python/ML which can be found in the following github repo
Process (skip to Key Insight below if you're not interested in the code): I imported the data by mounting it from Google Drive onto a Google Colab page, a free Jupyter notebook environment that requires no setup and runs entirely in the cloud, thanks to GCP.
I proceed to cleaning up the code by getting rid of missing items and dropping unnecessary columns
And convert time to seconds to standardize the timestamp string
Next I split the data into train and validation sets
And produce the following view
I proceed to wrangle the dataset by cleaning it up further. Refer to the github repo if you want to see how I did that in detail.
Once I arrange the data into an X features matrix and y target vector (Deep Sleep Duration), I produce a few visuals to gain insights into the data, like this SHapley value using the SHAP Python library where red represents features that contributed positively to the model, and blue, negatively:
Next I run a random forest regression and feature importance matrix:
A picture quickly emerges that stimulations, position changes, and awakenings have a high effect on deep sleep duration.
Here I need to explain a realization: First, deep sleep, which is a very important if not the most important sleep stage right up there with REM, occurs usually about 1 hour after hitting the sack and can last from nothing to on average 30min to 2 hours or more, is a stage in which Dreem claims to be able to enhance, using sound stimulations that enhance the delta brainwaves that occur in this state, therefore prolonging it. What's important to note is that the reason that column Stimulations had such strong correlation with Deep Sleep Duration, is because there is no stimulations without being in the state of deep sleep in the first place. I realized that I was introducing data leakage, which skewed my findings. I had to isolate those to get to anything meaningful. I proceed, after going in circles on a field trip, to stripping all features from the model but column Number of Awakenings. I create a model for predicting the length of deep sleep, given the number of awakenings, for example below I predict that I'll get 3500 seconds, or almost 1 hour worth of deep sleep, on a night in which I wake up 10 times:
Key insight: Eureka! I learned something huge: I can predict my deep sleep duration by how many awakenings I've experienced that night. Deep sleep pretty much precedes all other stages and sets the stage for the rest of the night's sleep quality. When I get solid deep sleep, I can expect to wake up less times during the night, say 1–3 times instead of a lot more and up to even 30. When I get poor deep sleep, I can expect to toss and turn, have many awakenings, and feel crappy the next day. Deep sleep sets the stage to better REM as well. Anecdotally I've noticed that what most interferes with deep sleep is going to bed late (say after midnight), at par with research about human sleep patterns and circadian rhythm. If I don't want to feel bad the next day, early to bed early to rise is in the calling.
(Edit, after spending more time with the data): But what good is the above model, without knowing what the accuracy of the prediction is? I proceed to compare my Rˆ2 and mean absolute error between my training set and my validation set. After experimenting a few times, I decide to reintroduce a few more features, such as Position Changes and Number of Stimulations (I decided to reintroduce this for better accuracy despite some leakage, again, stimulations occur when one is already in deep sleep, the manufacturer suggests that the sound stimulations enhance the quality of the deep sleep). I also toss the RandomForrestRegressor, which showed an overfitted model, and use instead good old LinearRegression:
And now I redo my prediction box for how much deep sleep I'd get given a number of nightly awakenings:
Final check for predicted values vs actual: