Zzzzzz….. – Quantified Self Toronto #14

[This article was first published on everyday analytics, 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.

Sleep is another one of those things like diet, where I feel if you asked anyone if they wanted to improve that area of their life most would say yes.

I remember hearing a quote that sleep is like sex; no one is quite sure how much everyone else is getting, but they are pretty sure it is more than them. Or wait, I think that was salary. With sleep it is more like – no one is quite sure how much they should be getting, but they sure as hell wish they were getting a lot more.

A lot of research has been done on the topic and it seems like the key takeaway from it is always the same: we are not getting enough sleep and this is a problem.

I know that I am a busy guy, that I am young, and that I go out on the weekends, so I know for a fact that my sleep is ‘bad’. But I was curious as to how ‘bad’ it actually is. I started tracking my sleep in April to find out, and also to see if there were any interesting patterns in it of which I was not aware.
 
I spoke again at Quantified Self Toronto (#14) (I spoke previously at #12 on June 7th) about it on August 7th. I gave an overview of my sleep-tracking activities and my simple examination of the data I had gathered. Here is the gist of my talk, as I remember it.


Hi everyone, I’m Myles Harrison and this is my second time speaking at Quantified Self Toronto, and the title of my second presentation is ‘Zzzzzzzz….’. 

I started tracking how much I was sleeping per night starting in April of this year, to find out just how good or bad my sleep is, and also to see if there are any patterns in my sleep cycle.
Now I want to tell you that the first thing I thought of when I started to putting this slide deck together was Star Trek. I remember there was the episode of Star Trek called ‘Deja Q’. Q is an omnipotent being from another dimension that torments the crew of the Enterprise for his own amusement, and in this particular episode he becomes mortal. In one part of the episode he is captured and kept in a cell onboard the ship, and he describes a terrible physical experience he has:


Q
I have been entirely preoccupied by a most frightening experience of my own. A couple of hours ago, I started realizing this body was no longer functioning properly… I felt weak, the life oozing out of me… I could no longer stand… and then I lost consciousness…

PICARD
You fell asleep.

Q
It’s terrifying…. how can you stand it day after day?

PICARD
One gets used to it…



And this is kind of how I have always felt about sleep: I may not like it, there are many other things I’d rather be doing during all those hours, however it’s a necessary evil, and you get used to it. If I could be like Kramer on Seinfeld and try to get by on ‘Da Vinci Sleep’, I probably would. However for me, and for most of the rest of us, that is not a reasonable possibility.


So now we come to the question of ‘how much sleep do we really need?’. Obviously there is a hell of a lot of research which has been done on sleep, and if you ask most people how much sleep they need to get every night, they will tell you something like ‘6-8 hours’. I believe that number comes from this chart which is from the National Sleep Foundation in the States. Here they give the figure of 7-9 hours of sleep for an adult, however this is an average. If you read some of the literature you will find, unsurprisingly, that the amount of sleep needed depends on a lot physiological factors and so varies from person to person. Some lucky people are perfectly capable of functioning normally during the day on only 3 or 4 hours of sleep a night, whereas some other unlucky people really need about 10 to 12 hours of sleep a night to feel fully rested. I highly doubt these unlucky folks regularly get that much sleep a night, as most of us have to get up in the morning for this thing called ‘work’. So yes, these are the extremes but they serve to illustrate the fact that this 6-8 (or 7-9) hours per night figure is an average and is not for everyone.

Also I found a report compiled by Statistics Canada in 2005 which says that the average Canadian sleeps about 8 and a half hours a night, usually starting at about 11 PM. Additionally, most Canadians get about 20 extra minutes of sleep on weekend nights as they don’t have to go to work in the morning and so can hit the snooze button.

So knowing this, now I can look at my own sleep and say, how am I doing and where do I fit in?



So as I said, I have been recording my sleep since early April up until today. In terms of data collection, I simply made note of the approximate time I went to bed and the approximate time at which I woke up the following morning, and recorded these values in a spreadsheet. Note that I counted only continuous night-time sleep and so the data do not include sleep during the day or things napping [Note: this is the same as the data collected by StatsCan for the 2005 report]. Also as a side interest I kept a simple yes/no record of whether or not I had consumed any alcohol that evening, counting as a yes any evening on which I had a drink after 5 PM.

O
n to the data. Now we can answer the question ‘What does my sleep look like?’ and the answer is this:



There does not appear to be any particular rhyme or reason to my sleep pattern. Looking at the graph we can conclude that I am still living like a University student. There are some nights where I got a lot of sleep (sometimes in excess of 11 or 12 hours) and there are other nights where I got very, very little sleep (such as this one particular night in June where I got no sleep at all, but that is another story). The only thing I can really pick out of this graph of note is that following nights or sequences of nights where I got very little sleep or went to bed very late, there is usually a night where I got a very large amount of sleep. Interestingly this night is sometimes not until several days later but this may be due to the constraints of the work week.

So despite the large amount of variability in my sleep we can still look at it and do some simple descriptive statistics and see if we can pull any meaningful patterns out of it. This is a histogram of the number of hours of sleep I got each night.


Despite all the variability in the data from what we saw earlier, it looks like the amount of sleep I get is still somewhat normally distributed. It looks like I am still getting about 7 hours of sleep on average, which actually really surprised me and in my opinion is quite good, all things considered and given the chaotic nature of my personal life. [Note: the actual value is 6.943 hrs for the mean, 7 for the median with a standard deviation of 1.82 hours]. 

So we can ask the question, ‘Is my amount of nightly sleep normally distributed?’. Well, at first glance it sure appears like it might be. So we can compare to what the theoretical values should be, and this certainly seems to be the case, though using a histogram is maybe not the best way as it will depend on how you choose your bin sizes.



We can also look at what is called a Q-Q plot which plots the values against the theoretical values, and if the two distributions are the same then the values should lie along that straight line. They do lie along it well, with maybe a few up near the top there straying away… so perhaps it is a skew-normal distribution or something like that, but we can still safely say that the amount of sleep I get at night is approximately normally distributed.


Okay, so that is looking at all the data, but now we can also look at the data over the course of the week, as things like the work week and weekend may have an affect on how many hours of sleep I get.

So here is a boxplot of the number of hours of sleep I got for each day of the week and we can see some interesting things here.

Most notably, Wednesday and Saturday appear to be the ‘worst’ nights of the week for me for sleep. Saturday is understandable, as I tend to go out on Saturday nights, and so the large amount of variability in the number of hours and low median value is to be expected; however, I am unsure as to why Wednesday has less hours than the other days (although I have do go out some Wednesday nights). Tuesdays and Thursdays appears to be best both in terms of variability and the median amount, these days being mid-week where presumably my sleep cycle is becoming regular during the work week (despite the occasional bad Wednesday?).

We can also examine when I feel asleep over the course of the week. Wait, that sounds bad, like I am sleeping at my desk at work. What I mean is we can also examine what time I went to bed each night over the course of the week:

Again we can see some interesting things. First of all, it is easy to note that on average I am not asleep before 1 AM! Secondly we can see that I get to sleep latest on Saturday nights (as this is the weekend) and that there is a large amount of variability in the hour I fall asleep on Fridays. But again we see that in terms of getting to bed earliest, Wednesday and Saturday are my ‘worst’ days, in addition to being the days when I get the least amount of sleep on average. Hmmmmmm….! Could there be some sort of relationship here?

So we can create a scatterplot and see if there exists a relationship between the hour at which I get to bed and the number of hours of sleep I get. And when we do this we can see that there is appears to be [surprise, surprise!] a negative correlation between the hour at which I get to sleep and the number of hours of sleep I get.



And we can hack a trend line through there to verify this:

> tl1 <- lm(sleep$hours ~ starthrs)
> summary(tl1)

Call:
lm(formula = sleep$hours ~ starthrs)

Residuals:
    Min      1Q  Median      3Q     Max
-7.9234 -0.6745 -0.0081  0.5569  4.8669

Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept)  9.78363    0.43696  22.390  < 2e-16 ***
starthrs    -0.62007    0.09009  -6.883 3.56e-10 ***

Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.533 on 112 degrees of freedom
Multiple R-squared: 0.2973,    Adjusted R-squared: 0.291
F-statistic: 47.38 on 1 and 112 DF,  p-value: 3.563e-10


So there is a highly statistically significant relationship between how late I get to sleep and the number of hours of sleep I get. For those of you that are interested, the p-value is very small (on the order of e-10). However you can see that the goodness of fit is not that great, as the R-squared about 0.3. This means that perhaps there are other explanations as to why getting to sleep later results in me getting less sleep, however I could not immediately think of anything. I am open to other suggestions and interpretations if you have any.


Also I got to thinking that this is the relationship between how late I get to sleep and how much sleep I get for all the data. Like a lot of people, I have a 9 to 5, and so I do not have the much choice about when I can get up in the morning. Therefore I would expect that this trend is largely dependent upon the data from the days during the work week.

So I thought I would do the same examination only for the days of the week where the following day I do not have to be up by a certain hour, that is, Friday and Saturday nights. And we can create the same plot, and:



We can see that, despite there being less data, there still exists the negative relationship.

> tl2 <- lm(wkend$hours ~ hrs)
> summary(tl2)

Call:
lm(formula = wkend$hours ~ hrs)

Residuals:
    Min      1Q  Median      3Q     Max
-5.4288 -0.4578  0.0871  0.5536  4.4300

Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept)  12.1081     0.9665  12.528 1.89e-13 ***
hrs          -0.8718     0.1669  -5.224 1.24e-05 ***

Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.737 on 30 degrees of freedom
Multiple R-squared: 0.4764,    Adjusted R-squared: 0.4589
F-statistic: 27.29 on 1 and 30 DF,  p-value: 1.236e-05


So it appears that on the days on which I could sleep in and make up the hours of sleep I am losing by going to bed later I am not necessarily doing so. Just because I can sleep in until a ridiculously late hour doesn’t necessarily mean that my body is letting me do so. This came as a bit of a surprise to me, as I thought that if I didn’t have to be up at a particular hour in the morning to do something, I would just sleep more to make up for the sleep I lost. An interesting insight – even though I can sleep in and make up for hours lost doesn’t necessarily mean that I will. 


So basically I just need to get to sleep earlier. Also, I am reminded of what my Dad always used to say to me when I was a kid, ‘An hour of sleep before midnight is worth two afterwards.’

Lastly, as I said, I did keep track of which nights I had consumed any alcohol in the evening to see what impact, if any, this was having on the quality and duration of my sleep. For this I just did a simple box plot of all the data and we can see that having a drink does mean I get less sleep overall.


Though this is a very simple overview it is consistent with what you can read in the research done on alcohol consumption and sleep. The belief that having a drink before bed will help you sleep better is a myth, as alcohol changes physiological processes in the body which are necessary for a good night’s sleep, and disrupts it.




So those were the conclusions I drew from tracking my sleep and doing this simple analysis of it. In terms of future directions, I could also further quantify my tracking of my sleep. I have simply measured the amount of sleep I have been getting, going with the assumption that getting close to the recommended amount of time is better. I could further quantify things by rating how rested I feel when I awake (or during the day) or rating how I felt the quality of rest I got was, on a scale of 1-10.

I could also measure other factors, such eating and exercise, and the time these things occur and how this play in to the amount and quality of the sleep I get.

Lastly, though I did have a simple yes/no measurement for whether or not I had consumed alcohol each evening, I did not quantify this. In the future I could measure caffeine consumption as well, as this known to be another important factor affecting sleep and restfulness.

That concludes my presentation, I hope I kept you awake. I thank you for your time, and for listening. If you have any questions I would be happy to answer them.

References & Resources 

National Sleep Foundation
http://www.sleepfoundation.org/

Who gets any sleep these days? Sleep patterns of Canadians (Statistics Canada)
http://www.statcan.gc.ca/pub/11-008-x/2008001/article/10553-eng.htm

The Harvard Medical School’s Guide to A Good Night’s Sleep
http://books.google.ca/books?id=VsOWD6J5JQ0C&lpg=PP1&pg=PP1#v=onepage&q&f=false

Quantified Self Toronto
http://quantifiedself.ca/

To leave a comment for the author, please follow the link and comment on their blog: everyday analytics.

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)