Count Missing Heart Beats.ipynb
Plot your most recent 1000 Apple Watch heart rate numbers along with red lines that guess where you might be missing some heart beats.
Make a dataframe with the last 1000 heart rates and their deviations from the average of the past 1000.
Plot heart rates for the past 1000 data points and draw a red vertical line at each point where two adjacent heart rates differ by more than 50 bpm from what you'd expect. The diff
calculation above corrects for the average heart rate for this individual over time, hopefully to make this more comparable across individuals
library(tidyverse)
library(dplyr)
library(lubridate)
library(httr)
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")
user_data <- user$data
while (!is.null(user$`next`)) {
resp <- GET(user$`next`)
user <- content(resp, "parsed")
user_data <- append(user_data, user$data)
}
for (data_source in user_data){
if (data_source$source == 'direct-sharing-453'){
hr <- readr::read_csv(url(data_source$download_url),col_names=c("hr","date","type"))
}
}
Make a dataframe with the last 1000 heart rates and their deviations from the average of the past 1000.
missing_hr <- hr %>% head(1000) %>%
dplyr::filter(type=="H") %>%
transmute(date=date,hr=hr,diff=hr-median(hr), lag = lag(hr)-hr)
missing_hr %>% head()
Plot heart rates for the past 1000 data points and draw a red vertical line at each point where two adjacent heart rates differ by more than 50 bpm from what you'd expect. The diff
calculation above corrects for the average heart rate for this individual over time, hopefully to make this more comparable across individuals
missing_hr %>% ggplot(aes(x=date,y=hr)) +
geom_line() +
geom_vline(xintercept = missing_hr %>% dplyr::filter(lag - diff > 50) %>% pull(date), color = "red") +
labs(title="Missing Heart Beats?", x=element_blank()) +
theme(plot.title = element_text(color = "red", size = 24, face = "bold"),
axis.text.x = element_text(angle = 90, hjust = 1)) +
geom_line(aes(x=date,y=hr), color = "blue") +
scale_y_continuous(sec.axis = sec_axis(~ . + 50))