Data Science Applied to Wearables: Can Your Watch Help You Improve Your Lifestyle?
Activity trackers have made the inner workings of our bodies accessible to anyone who’s willing to wear one and wants to know more about their resting heart rate, sleep patterns, and exercise output. In some cases, these trackers have even saved lives. It is truly inspiring how we collect massive amounts of data, but can we get more out of our Fitbits, Apple watches, and Garmins? Unfortunately for most, it can be more of an entertaining gadget rather than a useful tool for a healthy population. What do you do with hundreds of nights of sleep data recorded? Have you made changes to your sleep routine based on the percent of awake time displayed on your watch? Are you taking your resting heart rate seriously and taking steps to lower it?
Let’s look at those questions from a data science perspective. Wearable devices collect continuous physiological measurement and are generating gigabytes of data every single minute. Fitbit reports that they have over 150 billion hours of heart rate recorded, and over 6 billion recorded nights of human sleep.
While this data is extremely useful for gathering information at the population-level, how can your own personal analysis assist you in living a better life? All the information exists at your fingertips (or on your wrist), and we can make it actionable.
If you have a Fitbit, here is an example of how you can use your own body’s resting heart rate as a map to reveal any patterns, outliers, and if it’s time for a change.
Get your own Fitbit data
The first step is to download your own data from Fitbit using fitbitr package (use devtools::install_github(“teramonagi/fitbitr”)). You will need to get your own token (see instructions here)
token <- fitbitr::oauth_token(language = "en_US")
Whole day heart rate
Let’s assume we want to start with downloading your daily heart rate for 5 days in January:
df_hr <- get_heart_rate_intraday_time_series_multiple(token = token, date_start = "2020-01-05", date_end = "2020-01-10")
df_hr$time <- times(df_hr$time)
kable(head(df_hr)) %>% kable_styling(position = "center", full_width = F)
Now, let’s plot your heart rate versus time of day:
breaks2 <- c(0.0000000, 0.2500000, 0.500000, 0.7500000, 0.9999999)
labels2 <- times(breaks2)
ggplot(df_hr, aes(x = time, y = value, col = as.factor(day))) +
geom_line(alpha = 0.7) +
scale_x_continuous(labels = labels2, breaks = breaks2) +
ylab("Heart rate") +
guides(color = guide_legend(title = "Date")) +
We can notice that the heart rate is low during the night and then oscillates during the day depending on the activities. However, it may be hard to interpret it without going deep into time series and physiological signal processing.
We will be using resting heart rate (RHR) that is considered “a real-time snapshot of how your heart muscle is functioning”. Fortunately for us, Fitbit uses its own algorithm to calculate RHR and it is available through their API:
rhr <- get_activity_time_series(token, "restingHeartRate", date = as.character(Sys.Date()), period = "max")
rhr$dateTime <- as.Date(rhr$dateTime)
rhr$value <- as.numeric(rhr$value)
ggplot(rhr, aes(x = dateTime, y = value)) +
geom_line(col = "gray") +
stat_smooth(aes(x = dateTime, y = value), method = "lm", formula = y ~ poly(x, 21), se = FALSE) +
We can see that there is some periodicity and structure here, but it is hard to say what is affecting RHR without looking at more data. Is it affected by sleep? By training? By diet? Although it would be really useful to look at aggregated data from many people (and we do this at InsideTracker!), here we are looking at an “n of 1” example.
When I combine sleep and RHR, I can calculate the weekly sleep score (formulas and code not shown here) and see a trend:
Of course, we need much more data to draw conclusions but this preliminary analysis looks promising and suggests that there may be a correlation between sleep quality and RHR. And this creates room for improvement: it is rather easy to implement changes in your sleep habits that can result in overall health improvement.
There are multiple other factors affecting your RHR and sports performance. Last month I was working on my sleep and diet and I can already see some improvement. I find it exciting how simple changes and some fun stats can help you in archiving your goals and finally make sense of all the data generated by your watch.
About the author/ODSC East speaker:
Svetlana Vinogradova is a Data Scientist at Inside Tracker, leading the Data Science team to integrate blood biomarkers and DNA data with physiological data from activity trackers to improve lifestyle recommendations and discover new patterns and optimal zones in sleep, heart rate, and blood biomarkers.
Prior to Inside Tracker, Svetlana was a research fellow at Dana Farber Cancer Institute and Harvard Medical School where she worked as a bioinformatician developing statistical methods to study epigenetic mechanisms affecting gene expression.
Svetlana has a PhD in Bioinformatics and Mathematical Biology from Lomonosov Moscow State University. During her PhD, she worked on developing algorithms to study RNA secondary structures and also enjoyed teaching and developing bioinformatics, algorithms, and R programming courses.