A Closer Look at Fitbit Data

Sat May 04 2019

Health trackers are the current craze. After I bought a Fitbit, I wanted to determine what exactly could I do with my Fitbit data. Can we learn something from this data that we did not know before? Most people don’t need a watch to tell them that they walked a lot today or that they got a ton of sleep. We typically have a pretty good gauge of our basic physical health. I am interested in figuring out how we can use data science to look at our health over a longer period of time and learn something.

Lets first look at a few things that people typically use Fitbit data for before we jump into the weeds.

Setting Goals

Motivation

Tracking Progress

Ever since I bought a Fitbit, I found that I went to the gym a lot more frequently. Having something which keeps track of your progress is a great motivator. Not only is your daily steps recorded for your own viewing, you can share that data with your friends as a competition. Although I only have one friend on Fitbit, I found that was a good motivator to hit ten thousand steps every day.

Goals which are not concrete never get accomplished. Simply saying that “I will get in shape” is a terrible goal. In order for you to actually accomplish your goals, they need to be quantifiable, reasonable, and measurable. Rather than saying “I will improve my health this year”, you can say “I will loose ten pounds this year by increasing my daily step count to twelve thousand and go to the gym twice a week”. One goal is wishy washy where the other is concrete and measurable. Having concrete data from Fitbit allows you to quantify your goals and set milestones for you to accomplish. Along the way to achieving your goal, you can easily track your progress.

Simply knowing your Fitbit data can help you make more informed decisions about your fitness. You can tweak your life style after comparing your data against what is considered healthy. For example: if you notice that you are only getting 6 hours of sleep per night, you can look up the recommended amount of sleep and tweak your sleep routine until you hit that target.

Alright, lets do some data science!

Tom and Jerry Data Science Meme

1 Getting The Data

There are two options that we can use to fetch data from Fitbit.

1.1 Using Fitbit’s API

Fitbit has an OAuth 2.0 web API that you can use. You have to register your application on Fitbit’s website to receive a client ID and a client secret to connect to the API.

I decided to fetch the Fitbit data using an Express app with node. Fetching the data this way will make it really easy to use on a live website. Node has tons of NPM modules which makes connecting to Fitbit’s API relatively easy. I’m using Passport which is a common authentication middleware for Express.

Now that we are authenticated with Fitbit, we can finally make queries to Fitbit’s Server. I created a helper function called “queryAPI” which attempts to authenticate if it is not already authenticated and then fetches the API result from a provided URL.

1.2 Exporting Data from Website

The on demand export is pretty useless because it can only go back a month. On top of that, you don’t get to download any detailed heart rate data. The data that you get is aggregated by day. This might be fine for some use cases; however, this will not suffice for any interesting analysis.

I decided to try the account archive option out of curiosity.

Fitbit Archive Data

The Fitbit data archive was very organized and kept meticulous records of everything. All of the data was organized in separate JSON files labeled by date. Fitbit keeps around 1MB of data on you per day; most of this data is from the heart rate sensors. Although 1MB of data may sound like a ton of data, it is probably a lot less if you store it in formats other than JSON. Since Fitbit hires a lot of people for hadoop and SQL development, they are most likely using Apache Hive to store user information on the backend. Distributing the data to users as JSON is really convenient since it makes learning the data schema very easy.

2 Visualizing The Data

Since the Data Archive is far easier, I’m going to start visualizing the data retrieved from the JSON archive. In the future I may use the Fitbit API if I decide to make this a live website. Using R to visualize this would be convenient, however; I want to use some pretty javascript graphs so I can host this as a demo on my website.

2.1 Heart Rate

My biggest complaint with Fitbit’s website is that they only display your continuous heart rate in one day intervals. If you zoom out to the week or month view, it aggregates it as the number of minutes you are in each heart rate zone. This is fine for the fitbit app where you have limited screen space and no good ways of zooming in and out of the graphs.

Fitbit Daily Heart Rate GraphFitbit Monthly Heart Rate Graph

I really want to be able to view my heart rate over the course of a few days. To visualize the continuous heart rate I’m going to use VisJS because it works really well with time series data.

To start, I wrote some Javascript which reads the local JSON files from the Fitbit data export.

I found it interesting that each point had a confidence value associated with it. I wonder how Fitbit is using that confidence information. Since it does not directly appear anywhere in the app, Fitbit may just be using it to exclude inaccurate points from the heart rate graphs and calculations. A really annoying thing about this data is that the time stamps don’t contain any information on the timezone. When graphing this data, I shifted the times by 4 hours so that it aligns with eastern standard time.

After we read the data from the user selected heart rate files, we can treat that object as an array of arrays. Each array represents a file containing an entire days worth of heart rate measurements. Each day is an array of time stamped points with heart rate information. Using the code from the VisJS examples, it is relatively straightforward to plot this data.

It works! As an example, this is what my heart rate looks like over a week.

Heart Rate for One Week

2.2 Time Line

Fitbit does a pretty good job of detecting and recording health related activities. The two major things that Fitbit detects is sleep and workout activities. Although the app does a good job at informing you about these activities, the app is lacking a comprehensive timeline. Rather than provide a timeline for these activities, the app only displays a simple list.

Fitbit Activity History Log

The JSON filea for sleep store a ton of data! For the sake of the time line, I am only interested in the start and finish times. Unlike the heart rate data, this actually stores the time zone.

The JSON file for each activity stores a lot of information on heart rate during the exercise. Similar to the heart rate file, this date format does not take into account time zones. Grr! Rather than storing the finish time like the sleep JSON file, this keeps track of the total duration of the event in milliseconds.

I am pretty pleased with how these two graphs turned out. When you zoom too far out of the graph, the events get really small, but, it does a pretty good job at visualizing a few days worth of data at a time.

Fitbit Activity TimeLine With Heart RateFitbit Activity TimeLine

3 Future Analysis

There is a ton of data to look at here and thousands of different angles which I can take. Since this blog post is getting rather long, I’m going to split this up and write a few more articles on Fitbit data. I currently have three ideas for future blog posts on this topic.

What factors affect sleep quality the most.

Using fuzzy logic with Fitbit data to plan, train, and assess fitness goals.

Third party data to use with Fitbit.

The full source code to the web page that I created today can be found on my Github.