Analyzing Sleep with Sleep Cycle App and R

[This article was first published on R – Tafkas' Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I have been tracking my sleep for almost two years now using my Fitbit. I started with the Fitbit Ultra and then moved on the the Fitbit One after it came out. In October 2013 I found out about the Sleep Cycle (Link) app for the iPhone. For weeks, Sleep Cycle was listed as the best-selling health app in Germany, where currently (as of January 2014) it is in second place. The program promises, to wake you up in the morning without being tired. This is indeed possible if the alarm goes off in light sleep and not in deep sleep. It also allows you to set some kind of sleep music (white noise) to assist you to fall asleep. After reading all the positive reviews on the AppStore I decided to give it a try.

The app promises to wake you up in a time frame up to 30 minutes prior to the alarm you set if it detects your movement in the morning. Even more important to me than the actual smart alarm feature was the possibility to collect some data while sleeping. In the morning you are presented with a chart of your sleep pattern of last night:
Sleep Cycle Screenshot of last night

The app also allows to export the database as a comma separated file containing: time you went to bed, time you woke up, sleep quality in %, wake up mood and user defined sleep notes. This gives you the opportunity to do some more analysis. I decided to fire up R and create my own charts.

So far I have used the app to track 100 nights of sleep and decided to peak into the data. Let’s take a look how long I slept each night:
Sleep Duration over time

It looks like the longer I slept the higher the sleep quality is. A scatter plot of the data gives:
Sleep time vs. sleep quality

The chart takes also the sleep notes into consideration. You can see clearly that sleeping away from home results in lower sleep quality. The same applies for exercising (note: I tagged a sleep with exercising when I worked out late in the evening). On the contrary taking a melatonin (dosage 3mg) increased the sleep quality.

Averaging the sleep quality by month shows, that the January worse than the previous month. One explanation is a vacation I took, where I did not sleep so well at all.
Average sleep quality by month

The R code for the data wrangling and the charts:

To leave a comment for the author, please follow the link and comment on their blog: R – Tafkas' Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)