ANALYSING YOUR FITBIT SLEEPING PATTERNS

    Using the FitbitR package

      Paul Schneider
     



This is a brief R-tutorial how to access your fitbit data and visually analyse your sleeping patterns. However, before you can retrieve data from fitbit, you have to set up a new ‘application’.

Go to https://dev.fitbit.com/apps/new

Fill in the form (you can simply copy-paste the fields below)

Click Register

Application Name*:

Description:

Application Website:

Organisation:

Organisation Website:

Terms Of Service Url*:

Privacy Policy Url *:

Server Client Personal

Callback URL :



After you have registered your new application, you should find your OAuth ID and a Client secret, which we will need in a moment (see below).

Sample Oauth


Now, Use the code below to install a slightly extended version of the FitbitR package (the original can be found here).
install.packages("devtools") # install devtools if needed
devtools::install_github("bitowaqr/fitbitr")
library(fitbitr)


Put in your OAuth ID and Client secret to connect with your fitbit data.
  FITBIT_KEY    <- "YOUR_OAUTH_ID"  
  FITBIT_SECRET <- "YOUR_CLIENT_SECRET"
  token <- oauth_token(key = FITBIT_KEY, secret = FITBIT_SECRET)
## Waiting for authentication in browser...
## Press Esc/Ctrl + C to abort
## Authentication complete.


You have to log in into your fitbit account, and then it will ask you to specify what data you want to access. Select whatever you are interested in, but to analyse sleeping patterns, you just need to select sleep, of course. Then click Allow.

Sample Oauth


Finally, we can extract the sleep data and create some informative plots.
We use an extended version of fitbitr’s sleep_analyzer(...) function (See the helpfile for further info: ?sleep_analyze). The data for this demo was kindly donated by A..
# retrieve your data from fitbit
  my_sleep = sleep_analyzer(
    start_date    =  "2017-06-01", # When did you start wearing fitbit?
    end_date      =   "2019-12-01",   # Select yesterday
    # We can set limit and contrast to study the different sleeping
    # patterns on working days and weekends
    limit         =    c("Monday","Tuesday" , "Wednesday", "Thursday","Friday"), 
    contrast      =    c("Saturday",  "Sunday"  ),
    # input the times you intend to go to sleep and wake up
    # to draw 2 reference lines in the plot
    planned_start = 23, planned_end = 7, 
  )

# Have a look at the relevant bits of the extracted data
head(my_sleep$raw_data)[,c(1,4,3)]
# 1. Overview
  # Shows the start (yellow) and the end (red) of sleep for each day
  # the dark blue lines are smooth trend lines
  # the black lines indicate your planned sleeping times
  my_sleep$sleep_plot


# 2. Weekdays versus weekends
  # Showing start and end times separately
  my_sleep$sleep_contrasts_plot

# 3. sleep duration over time
  # shows the sleep duration for each day over the 
  # entire observation period. A smooth trend line
  # is shown in blue
  my_sleep$sleep_over_time  


# 4. sleep duration frequencies
  # A histogram showing the frequency of your sleep durations
  # the vertical line indicates the median sleep duration
  my_sleep$histogram


# 5. Sleep by day of the week
  # boxplots show the hours of sleep per day of the week
  my_sleep$boxplot

  
# 6. sleep by month
  # shows the variation of sleep durations by month
  # (useful if you have >1 year of data)
  # most people sleep longer during winter months
  # and shorter in summer 
  my_sleep$sleep_by_month_plot


That’s it. More analyses may be coming later.



    Paul Schneider 2019 - CC-BY

LS0tCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KCjxoZWFkPgo8c3R5bGU+CmJvZHkgewogIHBhZGRpbmctbGVmdDogMjBweDsKfQouYm94IHsKICAgIGJvcmRlci1yYWRpdXM6IDVweDsKICAgIGJhY2tncm91bmQ6IGxpZ2h0Z3JheTsKICAgIGJvcmRlcjogMXB4IHNvbGlkIGJsYWNrOwogICAgZmxvYXQ6IGNlbnRlcjsKICAgIGZvbnQtc2l6ZTogMTFweDsKICAgIGhlaWdodDogYXV0bzsKICAgIG1hcmdpbjogMjBweDsKICAgIHBhZGRpbmc6IDVweDsKICAgIHdpZHRoOiAzMDBweDsKfQouY29udGVudCB7CiAgICBwYWRkaW5nOiAxMHB4Owp9CgoKPC9zdHlsZT4KPC9oZWFkPgoKPGJyPjxicj4KCiMjIyAmbmJzcDsmbmJzcDtBTkFMWVNJTkcgWU9VUiBGSVRCSVQgU0xFRVBJTkcgUEFUVEVSTlMKPGg0PiAmbmJzcDsmbmJzcDsmbmJzcDsgVXNpbmcgdGhlIDxhIGhyZWY9Imh0dHBzOi8vZ2l0aHViLmNvbS90ZXJhbW9uYWdpL2ZpdGJpdHIiPkZpdGJpdFIgcGFja2FnZTwvYT48L2g1PgombmJzcDs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