Belen Chavez
  • Introduction
  • About
  • Data Blog
  • Calligraphy
  • Teaching

Retrieving Historical Resting Heart Rate from Fitbit API

11/29/2015

4 Comments

 
If you have a Fitbit Charge HR, have you ever tried looking at your historical heart rate data through Fitbit's App or Dashboard? Unfortunately, you can only see 30 days worth of data, and that's about it (see picture below). Which I think is weird considering you can see your historical step data from day 1. 

If you're curious like me and want to retrieve your historical resting heart rate using Fitbit's API, then this blog post is for you!
Picture
I have a registered app through Fibit Developer API and I use Fitbit's API explorer to download my own data. For steps on using Fitbit's API go to www.dev.fitbit.com/docs. The steps for the API are also below:
Picture
​Skipping to step 2, I use the Fitbit API Explorer to get my heart rate information by typing the following request URL and replacing the USERNUM with my user number, and replacing the to- and from- dates. 
Picture
The resulting response is in JSON format which I then parse using Stata. The screenshot below shows the JSON file for yesterday's heart rate information. Unfortunately, the output doesn't contain continuous heart rate information, but it does tell me calories burned in each of the rate zones, as well as the resting heart rate for each day. Just curious, does anyone know where I can get continuous heart rate information? ​​
Picture
The file is parsed using the following code, keeping only the date and resting heart rate:  
/* Author: Belen Chavez */
/* Parse daily heart data from Fitbit JSON file */

clear
version 12.1
global fname "bc_heart.txt"

forv i = 1/7{
        tempfile f`i'
}

filefilter $fname `f1', f("{") t("")
filefilter `f1' `f2', f("},") t("")
filefilter `f2' `f3', f("[") t("")
filefilter `f3' `f4', f("],") t("")
filefilter `f4' `f5', f(",\n") t(",")
filefilter `f5' `f6', f("}") t("")
filefilter `f6' `f7', f(":") t(",")

insheet using `f7', clear delimit(",")
keep if v1=="dateTime" | v1=="restingHeartRate"
drop v3-v10

gen date = date(v2,"YMD") if v1=="dateTime"
gen resting = v2 if date==. 
replace resting = resting[_n+1] if resting==""
keep if date!=.
destring resting, replace
format date %td

The graph below shows the historical resting heart rate that Fitbit has on me ever since I got my Charge HR: 
Picture
The dashed line represents my daily resting heart rate (beats per minute), the pink solid line represents my monthly average resting heart rate and the red numbers with the "<3" symbols detail what the monthly average is.

Interestingly, my resting heart rate is quite variable and seems cyclical, with the lowest range in September and October. Does anyone else's heart rate look like this? I read up on this and apparently resting heart rates vary due to medications, illness, fitness levels, and stress. 

In a few weeks I'll be going on vacation and taking some time off work, I'm expecting my resting heart rate to go down when I'm not at the office, but, we'll see what happens. Also, in a couple of months, I will have had my Charge HR for a year, and I also look forward to looking at the year-on-year change. I'll post more on that in late January or early February! 

Update April 20- Here's the code I used to make the graph above:

gen ym = mofd(Date)
egen avg_resting = mean(resting), by(ym)
format avg %6.0f
format Date %tdMon/DD
tostring avg_resting, gen(avg_str) usedisplayformat force
gen hearts = "<3"

 * Create a mid-month variable to place the markers 
gen midmo = Date if day(Date)==15
 * Create a shifted mid-month variable for the heart markers 
gen midmo1 = midmo+4

twoway (line resting Date, ///
        lcolor(eltblue) lpattern(dash_dot) lwidth(medthick)) ///
        (line avg_resting Date, lcolor(red*.40)) ///
        (scatter avg_resting mid,  /// 
        msymb(none) mlab(avg_str) mlabcolor(red) mlabposition(6))  ///
        (scatter avg_resting midmo1, /// 
        msymb(none) mlab(hearts) mlabcolor(red) mlabposition(6) mlabgap(2) ///
        mlabangle(90) mlabsize(*.75)), xlabel(#12, labsize(*.70)) legend(off)  /// 
        title("My Historial Resting Heart Rate")

 
4 Comments

    Author

    My name is Belen, I like to play with data using Stata during work hours and in my free time.  I like blogging about my Fitbit, Stata, and random musings.

    If you like the Stata posts you see here, I guarantee you'll also like what's over at
    wmatsuoka.com


    Archives

    March 2018
    January 2018
    September 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015
    September 2015
    August 2015
    July 2015


    Categories

    All
    API
    Beer
    BJJ
    BreweryDB
    CURL
    Education Research
    Excel
    Fitbit
    Fitbit API
    Google
    Google Charts API
    Google Maps
    LinkedIn
    Love
    Parsing
    PPIC
    Putexcel
    Rant
    San Diego
    Stata
    Tableau
    Twitter API
    Valentine's Day


Powered by Create your own unique website with customizable templates.
  • Introduction
  • About
  • Data Blog
  • Calligraphy
  • Teaching