Details for fitbit-load-in-R.ipynb

Published by gedankenstuecke

Description

This notebooks demonstrates how to use the Fitbit data in R and how to make simple plots with the data.

3

Tags & Data Sources

activity tracking Fitbit Connection

Comments

Please log in to comment.

Notebook
Last updated 3 years, 9 months ago

Analyzing Fitbit-data in R

This notebook requires you to use the Fitbit connection to get your activity data into Open Humans.

Given that the imported Fitbit data has a rather particular JSON format this notebook just gives an idea of how to extract individual data from the larger data set.

For a start let's load our required packages. To load the JSON we are using rjson, which needs to be installed with install.packages in the first step:

With that out of the way we can find the Fitbit data from our Open Humans account:

Now that we have our fitbit data stored in json_data we can start to extract individual data types from it.

Let's focus on steps for now. We create two empty vectors and from there we first iterate over the yearly_entry in json_data$'tracker-steps'. Each yearly_entry has one main list, which is activities-tracker-steps. Which is why we now can iterate over yearly_entry$'activities-tracker-steps' and get the dateTime and value for each of these entry.

This is not how you should do things in R, but I couldn't find out a better way to extract that data. If you do: Please be in touch at bastian@openhumans.org!

Once that's done we can merge the dates and steps into a single data frame and convert the data types:

Now we can start our first plot: All of our daily steps

Let's now also compare the daily step counts between the years. For this we generate fake_dates, which contain only the real day and month but are all normalized onto the same year. With that we can now plot everything onto a single x-axis by using the fake_date for it and still keep the year, using the variable of the same name:

`geom_smooth()` using method = 'loess' and formula 'y ~ x'

This should give you an idea of how to read the Fitbit data. To see all the different data types that are in the fitbit data you can use the following:

  1. 'sleep-efficiency'
  2. 'tracker-calories'
  3. 'tracker-minutes-very-active'
  4. 'sleep-minutes'
  5. 'weight-log'
  6. 'tracker-steps'
  7. 'tracker-elevation'
  8. 'sleep-awakenings'
  9. 'sleep-minutes-after-wakeup'
  10. 'tracker-minutes-sedentary'
  11. 'tracker-minutes-lightly-active'
  12. 'sleep-start-time'
  13. 'weight'
  14. 'awake-minutes'
  15. 'tracker-minutes-fairly-active'
  16. 'minutes-to-sleep'
  17. 'activities-overview'
  18. 'time-in-bed'
  19. 'tracker-floors'
  20. 'tracker-distance'
  21. 'heart'
  22. 'profile'
  23. 'tracker-activity-calories'

And with that happy exploring!

Notebook
Last updated 3 years, 9 months ago

Analyzing Fitbit-data in R

This notebook requires you to use the Fitbit connection to get your activity data into Open Humans.

Given that the imported Fitbit data has a rather particular JSON format this notebook just gives an idea of how to extract individual data from the larger data set.

For a start let's load our required packages. To load the JSON we are using rjson, which needs to be installed with install.packages in the first step:

In [42]:
library('tidyverse')
library('httr')
library('jsonlite')

With that out of the way we can find the Fitbit data from our Open Humans account:

In [43]:
access_token <- Sys.getenv("OH_ACCESS_TOKEN")
url <- paste("https://www.openhumans.org/api/direct-sharing/project/exchange-member/?access_token=",access_token,sep="")
resp <- GET(url)
user <- content(resp, "parsed")

for (data_source in user$data){
    if (data_source$source == "direct-sharing-102"){
         fitbit_url <- data_source$download_url
    }
}

temp <- tempfile()
download.file(fitbit_url,temp,method='wget')
json_data <- fromJSON(txt=temp)

Now that we have our fitbit data stored in json_data we can start to extract individual data types from it.

Let's focus on steps for now. We create two empty vectors and from there we first iterate over the yearly_entry in json_data$'tracker-steps'. Each yearly_entry has one main list, which is activities-tracker-steps. Which is why we now can iterate over yearly_entry$'activities-tracker-steps' and get the dateTime and value for each of these entry.

This is not how you should do things in R, but I couldn't find out a better way to extract that data. If you do: Please be in touch at bastian@openhumans.org!

In [44]:
dates <- c()
steps <- c()

for (yearly_entry in json_data$'tracker-steps'){
    dates <- append(dates,yearly_entry$'activities-tracker-steps'$dateTime)
    steps <- append(steps,yearly_entry$'activities-tracker-steps'$value)
    }

Once that's done we can merge the dates and steps into a single data frame and convert the data types:

In [45]:
df <- data.frame('date' = dates, 'steps' = steps)
df$steps <- as.numeric(as.character(df$steps))
df$date <- as.Date(as.character(df$date))
df$year <- as.character(format(df$date,"%Y"))

Now we can start our first plot: All of our daily steps

In [46]:
ggplot(subset(df,df$steps != 0),aes(date,steps,color=year)) + geom_point() + theme_minimal()

Let's now also compare the daily step counts between the years. For this we generate fake_dates, which contain only the real day and month but are all normalized onto the same year. With that we can now plot everything onto a single x-axis by using the fake_date for it and still keep the year, using the variable of the same name:

In [47]:
df$date_wo_year <- as.character(format(as.Date(df$date),"%m-%d"))
df$fake_date <- as.Date(paste('2001',df$date_wo_year,sep='-'))

ggplot(subset(df,df$steps != 0),aes(fake_date,steps,color=year)) + geom_line() + stat_smooth() + facet_grid(year ~ .) + theme_minimal()
`geom_smooth()` using method = 'loess' and formula 'y ~ x'

This should give you an idea of how to read the Fitbit data. To see all the different data types that are in the fitbit data you can use the following:

In [41]:
names(json_data)
  1. 'sleep-efficiency'
  2. 'tracker-calories'
  3. 'tracker-minutes-very-active'
  4. 'sleep-minutes'
  5. 'weight-log'
  6. 'tracker-steps'
  7. 'tracker-elevation'
  8. 'sleep-awakenings'
  9. 'sleep-minutes-after-wakeup'
  10. 'tracker-minutes-sedentary'
  11. 'tracker-minutes-lightly-active'
  12. 'sleep-start-time'
  13. 'weight'
  14. 'awake-minutes'
  15. 'tracker-minutes-fairly-active'
  16. 'minutes-to-sleep'
  17. 'activities-overview'
  18. 'time-in-bed'
  19. 'tracker-floors'
  20. 'tracker-distance'
  21. 'heart'
  22. 'profile'
  23. 'tracker-activity-calories'

And with that happy exploring!