Analyzing Sleep with Sleep Cycle App and R

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 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. 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 seprarated 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 3mg melatonin 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:

  • http://www.gwern.net/ gwern

    What does it define “sleep quality in %” as? I found that with Zeo, its ‘ZQ’ seems to be largely based on sleep length and so correlating my sleep length with ZQ was simply a tautology – of course they were strongly and statistically-significantly correlated…

    • http://blog.tafkas.net Tafkas

      The sleep quality is a proprietary measure used by the app.
      Unfortunately there is no documentation about it. I assume it is a
      mixture of length and how much the phone moves at night.
      I removed the outliers from my cruise vacation and applied a linear regression giving me Adj R^2 of 0.71447

      • http://www.gwern.net/ gwern

        Yeah, looks like it. If it’s recording movement, maybe you could ask them to add that to the data export instead of providing a worthless composite measure?

  • http://blog.tafkas.net Tafkas

    The sleep quality is a proprietary measure used by the app. Unfortunately there is no documentation about it. I assume it is a mixture of length and how much the phone moves at night.
    I removed the outliers from my cruise vacation and applied a linear regression giving me Adj R^2 of 0.71447:

  • rafica

    Very interesting! I wanted to do the same with my data. I’m very new to R. I ran your code and got this error.
    Attaching package: ‘plyr’
    The following object is masked from ‘package:lubridate’:
    here
    Any idea what should be done?

    Thanks!