Comparing Fitbit and Polar H7 heart rate data
How good is the Fitbit measures comparing to Polar H7? The wearable Fitbit bracelet measures the heart rate based on the expansion and contraction of the capillaries in the skin throught measurement of the reflection and absorption of LED lights, different from the method used heart rate monitor Polar H7, which captures the electrical signals from the heart beat. In this post, we’ll access a WebAPI using OAuth2.0 to get Fitbit data and compare it with those obtained by a Polar H7, imported from a GPX file during the same training session.
Introduction
In this post we will compare acquisition of heart rate data performed by two different devices in the same session training: a sport wearable wristband called Fitbit and a Polar heart monitoring.
How good is the Fitbit measures comparing to Polar H7? They agree with the measures? How the differences are distributed? I did a MTB session using both devices, now I can use R to access the data and compare the measures.
The devices
Fitbit Charge HR
The bracelet model used was Fitbit ChargeHR, which is no longer marketed, but use the same technology embedded in newer models. The Fitbit uses a proprietary technology called PurePulse a to perform a heart rate measurement. When your heart beats, yours capillaries in the skin expand and contract based on changes in blood volume. The light of the PurePulse LEDs on your Fitbit device reflect on the skin to detect changes in blood volume, and finely tuned algorithms are applied to measure heart rate automatically and continuously.
Polar H7
The heart rate monitor used was Polar H7 Heart Rate Sensor, which works as an Electrocardiogram, or in other words, electrodes in contact with the skin detect the electrical signal triggered by the heart in each heart beat.
In this way we will compare the quality and accuracy of heart rate data obtained from two very different technologies.
Data Acquisitions
The first steps is to get the devices data, and for each one we will use a different strategy, for Fitbit we’ll access the data via Web API
, and for the Polar H7 we’ll extract from the training session GPX file
.
Fitbit
The Fitbit data is availabe throught an Web API in the Fitbit’s Development Portal. It’s necessary to use Oauth 2.0 protocol for authorization and authentication, so you must obtain an ID and Secret doing a registration in the portal first.
Follow the steps:
- Log in and go to Manage > Register An App
- enter whatever you want for Application name and description
- in the application website box, any valid URL (usually I create a link from a google doc)
- for organization put “self”
- for organization website any valid URL
- for OAuth 2.0 Application Type select “Personal”
- for Callback URL put in http://localhost:1410/
- for Default Access Type select “Read Only”
- click “save”
After that, you should now be at a page that shows your
- The App Name you choose
- OAuth 2.0 Client ID
- Client Secret
- URL Callback you defined
- Authentication URL
- Refresh Token URL
These parameters will be used to get or renew the API Access Token. Fill a fitbit_config.yml
configuration file (see the appendix at the end of this post) with them and we’ll be ready to request and get the Fitbit data using the httr package
.
|
|
The oauth_app
, oauth_endpoint
and oauth2.0_token
functions execute the authentication and authorization flow of the OAuth 2.0
protocol to obtain the Access Token
, which must be passed for each request made to the Fitbit Web API
. When executing these function the browser will be called for you to authenticate in the site, and then the callback URL will be called by passing the authentication token
confirming that you have access to the APIs.
Then, we can call the endpoint responsible for querying heart rate information.
|
|
|
|
If everything went well, the http request returned [status 200](https://www.w3.org/ Protocols / rfc2616 / rfc2616-sec10.html), then we can process the json of the response content to extract the requested heart rate data.
|
|
datetime | fitbit_hr |
---|---|
2019-02-24 00:00:00 | 73 |
2019-02-24 00:01:00 | 70 |
2019-02-24 00:02:00 | 70 |
2019-02-24 00:03:00 | 72 |
2019-02-24 00:04:00 | 77 |
2019-02-24 00:05:00 | 74 |
2019-02-24 00:06:00 | 77 |
2019-02-24 00:07:00 | 72 |
2019-02-24 00:08:00 | 73 |
2019-02-24 00:09:00 | 70 |
The Fitbit’s heart rate data obtained are minute-by-minute measurements of how much the heart beated, i.e., heart beats per minute, it is possible to plot the heart rate captured by the wearable throughout the day.
|
|
Polar H7
Unlike the Fitbit, to access the heart rate data of the Polar H7, the easiest way is to pull the data from the App used in to track the training session, at that time I used the Strava Application connected to the heart monitor by bluetooth. As we did in the post “Ploting your mtb track with R”, we download the GPX file containing the data recorded during the exercise session, directly from the Strava website, and then process the XML to extract the data we are looking for.
|
|
|
|
|
|
datetime | polar_hr |
---|---|
2019-02-24 11:13:36 | 105 |
2019-02-24 11:13:57 | 105 |
2019-02-24 11:13:59 | 101 |
2019-02-24 11:14:00 | 102 |
2019-02-24 11:14:01 | 103 |
2019-02-24 11:14:02 | 103 |
2019-02-24 11:14:03 | 103 |
2019-02-24 11:14:04 | 103 |
2019-02-24 11:14:05 | 104 |
2019-02-24 11:14:06 | 103 |
As Fitbit’s data, the heart rate from the Polar H7 obtained are minute-by-minute beats rate, it is possible to visualize the heart rate along the training session.
|
|
Analysis
Comparing measurements
With the dataset at hand, we now can compare the measurements obtained by the two devices. In both measurements, the heart rate is measured in beats per minute and stored minute by minute, let’s join them by timestamp.
|
|
datetime | polar_hr | fitbit_hr |
---|---|---|
2019-02-24 09:14:00 | 102 | 106 |
2019-02-24 09:15:00 | 108 | 109 |
2019-02-24 09:16:00 | 117 | 114 |
2019-02-24 09:17:00 | 114 | 112 |
2019-02-24 09:18:00 | 112 | 113 |
2019-02-24 09:19:00 | 113 | 114 |
2019-02-24 09:20:00 | 117 | 117 |
2019-02-24 09:21:00 | 116 | 116 |
2019-02-24 09:22:00 | 118 | 117 |
2019-02-24 09:23:00 | 117 | 117 |
Ploting both data toghether.
|
|
We can see that the measurements of Fitbit follows the Polar H7 data with remarkable proximity, we can better evaluate the relation between them plotting one against other.
|
|
The correlation between the two measurements, although not exactly accurate, is clear, let’s test it
|
|
|
|
The correlation are 0.87 and significant (p-value < 2.2e-16). We can do the linear regression of the Fitbit measurements on the Polar h7 and analyze how the residues behaves.
|
|
|
|
|
|
Bland Altman Agreement Analysis
Bland and Altman published in 1983 the first article as an alternative methodology to the calculation of the coefficient of correlation, methodology used until then. The correlation coefficient does not evaluate agreement and yes association between two measures, very different things.
The methodology initially proposed by Bland and Altman to evaluate the agreement between two variables (X and Y) starts from a graphical view from a dispersion between the difference of the two variables (X - Y) and the average of the two (X + Y) / 2.
Let’s reproduce the methodology with these data.
|
|
datetime | polar_hr | fitbit_hr | mean | diff | diff.pct | diff.mn | diff.sd | upper.lim | lower.lim |
---|---|---|---|---|---|---|---|---|---|
2019-02-24 09:14:00 | 102 | 106 | 104.0 | 4 | 0.0392157 | -0.3793103 | 7.396573 | 14.41384 | -15.17246 |
2019-02-24 09:15:00 | 108 | 109 | 108.5 | 1 | 0.0092593 | -0.3793103 | 7.396573 | 14.41384 | -15.17246 |
2019-02-24 09:16:00 | 117 | 114 | 115.5 | -3 | -0.0256410 | -0.3793103 | 7.396573 | 14.41384 | -15.17246 |
2019-02-24 09:17:00 | 114 | 112 | 113.0 | -2 | -0.0175439 | -0.3793103 | 7.396573 | 14.41384 | -15.17246 |
2019-02-24 09:18:00 | 112 | 113 | 112.5 | 1 | 0.0089286 | -0.3793103 | 7.396573 | 14.41384 | -15.17246 |
2019-02-24 09:19:00 | 113 | 114 | 113.5 | 1 | 0.0088496 | -0.3793103 | 7.396573 | 14.41384 | -15.17246 |
2019-02-24 09:20:00 | 117 | 117 | 117.0 | 0 | 0.0000000 | -0.3793103 | 7.396573 | 14.41384 | -15.17246 |
2019-02-24 09:21:00 | 116 | 116 | 116.0 | 0 | 0.0000000 | -0.3793103 | 7.396573 | 14.41384 | -15.17246 |
2019-02-24 09:22:00 | 118 | 117 | 117.5 | -1 | -0.0084746 | -0.3793103 | 7.396573 | 14.41384 | -15.17246 |
2019-02-24 09:23:00 | 117 | 117 | 117.0 | 0 | 0.0000000 | -0.3793103 | 7.396573 | 14.41384 | -15.17246 |
In this graph, it’s possible to visualize the bias (how much the differences deviate from the zero value) and the error distribution (the dispersion of the points of the differences around the mean), in addition to outliers and tendencies.
From the calculation of bias (d) and its standard deviation (sd) it is possible to reach the limits of agreement: d ± 1,96sd, which must be calculated and included in the graph. If the bias presents normal distribution, these limits represent the region where 95% of the differences in the studied cases are found.
|
|
Visually we see that there is no bias (average of the differences is close to zero) and that the dispersion of the differences are within a very small range:
- Bias: -0.3793103
- Dispersion ($2\sigma$): 14.4138355 bpm
Before proceeding with the analysis, let’s take a look at the distribution of the differences in measurements of fitbit relative to polar h7:
|
|
|
|
|
|
For the Bland-Altman test, what should be evaluated is whether the differences between the variables depend on the measurement size or not. This can be done through a correlation between differences and averages, which should be null.
|
|
|
|
Our numbers showed some correlation, where it should not be found, even with the large p-value. The bias hypothesis may or may not be equal to zero can be tested by a t-test for paired samples.
|
|
|
|
Here, the bias was practically zero, demonstrating agreement between the measurements of Fitbit and Polar H7.
Conclusion
In this post we use the httr package
to access a WebAPI
using OAuth2.0
to get data from Fitbit and compare it with that obtained by a Polar H7, imported from a GPX file
The fitbit captures the heart rate based on the expansion and contraction of the capillaries in the skin and makes this measurement based on the reflection / absorption of LED lights. This method proved to be comparable and in agreement with the measurements obtained in the Polar H7 heart monitor, which captures the electrical signals of the beat.
Apendix
config.yml
To prevent passwords, IDs and secrets from being hard coded and getting versioned and exposed in Github accidentally, I usually create a yaml file and put it in .gitignore
. In this code, the yaml file
has the following format:
|
|
References
References used in this post:
- https://www.polar.com
- https://www.fitbit.com
- https://dev.fitbit.com
- https://seer.ufrgs.br/hcpa/article/view/11727/7021
- https://yetanotheriteration.netlify.com/2018-01-16-ploting-your-mtb-track-with-r/
- https://www.telegraph.co.uk/technology/news/12086337/Fitbit-heart-rate-tracking-is-dangerously-inaccurate-lawsuit-claims.html