Hacking my infant twins’ sleep with machine learning and data science
A month ago, I tried A/B testing to see how different “treatments” or input parameters might result in longer sleep for our twins — and of course by proxy, us. Through this, I found that sleep patterns were fairly erratic and didn’t find much that correlated strongly to increased sleep. As time went on, they started more largely on their own naturally. However, now that they have reached four months, they’ve begun the apparently common but rarely discussed sleep regression phase. I once again found myself desperate for more sleep. Was I, as one commented on the previous post pointed out, “looking to find anything no matter how desperate for more sleep”? Yes. Yes I was. Hence, I turned to another computer science technique, machine learning.
Machine learning is a field within computer science that provides methods for “teaching” computers or programs without giving them discrete instructions. In normal programming, I would give the computer a sequence of commands to execute, throwing in logic to make determinations on what to do based on input, but the program could never go off of these well defined rails. Though this description sounds closer to Terminator than what it actually looks like in practice, machine learning allows one to take a set of “learning” data and use that for the basis of making predictions. Though it is becoming increasingly popular across the technology industry, it has mostly been used for looking at purchasing behavior and recommendations, AI, and perhaps most interestingly, to categorize and identify pictures and art — largely by Google. I couldn’t find many instances of it being applied to parenting.
Thanks to my wife’s accounting background and our type A personalities, we have detailed records on feeding and sleep behavior for the boys. Remember this spreadsheet?
Using this data, I set out to find the optimal combination of input parameters, in this case I looked at total food consumed in the day, time of last feeding, and amount of last feeding, to determine what would result in the longest night time sleep for the boys. And best of all I could let the computer do the hard work. Plus, we had twice as much data on these patterns because we of course have not one but two boys.
You can find my code on github if interested.
Exporting the data we collected from the spreadsheet, I extracted just the past month of datapoints. Because of the rapid growth and developmental changes infants go through, I felt one month’s data — times two infants — was a good balance of recency and enough data to make a prediction.
Putting it together and coding it up
I also got some stats that were interesting including that on average we fed the boys 27.5 oz per day, put them to bed most often at 7:25, fed them 5.22 oz at bedtime, and they slept 9 hours on average. It also turned out that the boys slept over 10 hours 75% of the time. Not too bad. But I knew with some more work we could improve this.
total-food last-feed-time last-feed-amount hours-of-sleep
count 62.000000 62.000000 62.000000 62.000000
mean 27.491935 7.455645 5.225806 9.084677
std 2.001008 0.331181 0.857357 1.726092
min 24.000000 6.750000 2.000000 3.500000
25% 26.000000 7.250000 5.000000 8.000000
50% 27.000000 7.375000 5.000000 9.000000
75% 29.000000 7.687500 6.000000 10.187500
max 32.000000 8.500000 7.000000 13.500000
Using this data, I was able to plot each parameter against the sleep time.
You can see that there already isn’t a strong correlation, at least linear between most of these. The closest is the somewhat linear — with lots of outliers — relationship between total food and hours of sleep. Oddly more food seems to result in less sleep.
There are many different types of machine learning algorithms. These are largely classified into linear and non-linear types. I took the data and ran it through six fairly widely used algorithms to see how accurate each could get. Here are the results.
Logistic Regression: 0.303333 (0.211056)
Linear Discriminant Analysis: 0.376667 (0.157797)
K Neighbors Classifier: 0.286667 (0.073333)
Decision Tree Classifier: 0.356667 (0.196667)
GaussianNB: 0.183333 (0.076376)
Support Vector Machine: 0.410000 (0.200028)
Or in a more pictographic form
Here you can see the Support Vector Machine algo clearly performed the best against the data, though the range is very wide and the mean isn’t much better than the others. None are even at 50% which is pretty poor for predicting. This is entirely due to the pseudo-random nature of the data. Even with such results, I decided to forge ahead, hoping to gleam any insight at all.
Using SVM, I trained the algorithm on the input data. Using this, I could now predict how much sleep we’d be getting based on the amount of food taken in a day, when we put them to bed, and how much we fed them at the last feeding. For example, giving them 28 ounces, putting them to bed at 7, and giving 6 ounces at that last feeding would result in a fairly poor 8 hours of sleep.
With the algorithm now trained, I could predict with some certainty how much sleep we would be getting. More importantly, I could look at the trend lines for these and see what type of behavior led to more sleep in an attempt to optimize. Counterintuitively, going to bed earlier, with less food then and throughout the day actually increased sleep. This is likely due to many factors but my theory is that less food means less gas, less stomach shrinkage when hungry, and therefore more restful sleep. How many times have you pigged out and then bizarrely woken up in the middle of the night starving?
Unfortunately, just as with the A/B testing, no one individual input seems to have a strong direct input on sleep. I guess if it did someone would have already discovered it and would make millions. Taken together, machine learning can find some of these trends and associations between variables, which leads to better, more accurate results than A/B testing or trial and error, but the results are still far from great. From this data, at most I could get a 41% accuracy rate of predictions. That means more often than not they are wrong. Due to the frequent changes in development and even the differences between the boys, it’s hard to apply data across them. Again, a larger sample size might help, but we’re not aiming for triplets anytime soon.
Still, these results are better than nothing and help demonstrate the promise of the field of machine learning and data science. I for one prefer to make decisions from data over gut feelings and this data even just confirming some of my guesses makes me feel better about our parenting approach.