-
Notifications
You must be signed in to change notification settings - Fork 0
/
heart_rate_analysis.Rmd
239 lines (186 loc) · 8.75 KB
/
heart_rate_analysis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
---
title: "heart_rate_analysis"
author: "John Goldin"
date: "2/17/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r setup_libs, echo = FALSE}
library(tidyverse)
library(jsonlite)
library(lubridate)
library(ggplot2)
library(scales)
library(XML)
```
```{r load_data, cache = TRUE, echo = FALSE, eval = FALSE}
# load("full_data_from_Apple_Health.RData")
# Using example from github: https://gist.github.com/ryanpraski/ba9baee2583cfb1af88ca4ec62311a3d
# ryanpraski/apple_health_load_analysis_R.r
# http://www.ryanpraski.com/apple-health-data-how-to-export-analyze-visualize-guide/
# see also: https://github.com/deepankardatta/AppleHealthAnalysis
#load apple health export.xml file
xml <- xmlParse("apple_health_export/export.xml")
#json_example <- read_json("apple_health_export/export.xml")
#transform xml file to data frame - select the Record rows from the xml file
df <- XML:::xmlAttrsToDataFrame(xml["//Record"])
#make value variable numeric
df$value <- as.numeric(as.character(df$value))
#make endDate in a date time variable POSIXct using lubridate with eastern time zone
df$endDate <-ymd_hms(df$endDate,tz="America/New_York")
df$startDate <-ymd_hms(df$startDate,tz="America/New_York")
##add in year month date dayofweek hour columns
df$month<-format(df$endDate,"%m")
df$year<-format(df$endDate,"%Y")
df$date<-format(df$endDate,"%Y-%m-%d")
df$dayofweek <-wday(df$endDate, label=TRUE, abbr=FALSE)
df$hour <-format(df$endDate,"%H")
steps <- df %>%
filter(type == 'HKQuantityTypeIdentifierStepCount') %>%
group_by(date) %>%
mutate(steps = as.numeric(as.character(value))) %>%
summarise(steps = sum(steps))
distance <- df %>%
filter(type == 'HKQuantityTypeIdentifierDistanceWalkingRunning') %>%
group_by(date) %>%
mutate(distance = as.numeric(as.character(value))) %>%
summarise(distance = sum(distance))
#filter(steps, month(date) == 5, year(date) == 2016) %>% print(n=1000)
```
```{r create_plot_function, echo = FALSE}
plot_day <- function(adf, adate, events = FALSE, hour_min = NULL, hour_max = NULL) {
browser()
print(hour_max)
adf <- adf %>% filter(type %in% c("HKQuantityTypeIdentifierHeartRate", "HeartRate"), local_date == adate) %>%
arrange(local_start)
if (!is.null(hour_max)) adf <- adf %>% filter(hour(local_start) <= hour_max)
if (!is.null(hour_min)) adf <- adf %>% filter(hour(local_start) >= hour_min)
p <- ggplot(data = adf, aes(x = local_start, y = value))
if (events) {
base <- adf$local_start[10]
left_house <- base
hour(left_house) <- 5
minute(left_house) <- 55
arrived <- base
hour(arrived) <- 6
minute(arrived) <- 3
p <- p + geom_vline(xintercept = left_house, colour = "yellow") +
geom_vline(xintercept = arrived, colour = "yellow")
}
p <- p + geom_point(size = 0.3) +
xlab("Time (Hour)") + ylab("Pulse (Apple Watch)") +
scale_x_datetime(date_labels = "%H", breaks = date_breaks("1 hour"),
minor_breaks = date_breaks("10 min")) +
facet_wrap(~ local_date, ncol = 1, scales = "free_x")
}
```
https://www.cnn.com/2019/03/16/health/apple-watch-heart-rate-arrhythmia-afib-study/index.html
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6111985/
```{r recreate_oct_2020_plot}
load("~/Dropbox/Programming/R_Stuff/john_vitals/Apple-Health-Data/key_days_export.RData")
p <- plot_day(day_2020_10_25_health, c(ymd("2020-10-25")), events = TRUE, hour_min = 8, hour_max = 15)
days_events <- tribble(
~x, ~lab,
as_datetime("2020-10-25 12:30:00"), "ECG",
as_datetime("2020-10-25 13:47:00"), "ECG"
)
ynhh <- tribble(
~x, ~lab,
as_datetime("2020-10-25 14:04:00"),
"arrive at ER"
)
p <- p + geom_segment(data = days_events, aes(x = x, xend = x, y = 154, yend = 159.3), colour = "orange", arrow = arrow(angle = 20, ends = "first", length = unit(0.03, "npc"))) +
geom_text(data = days_events, aes(x = x, y = 155, label = lab), hjust = 0.5, color = "orange", nudge_y = 5, size = 2) +
geom_segment(data = ynhh, aes(x = x, xend = x, y = 154, yend = 159.3), colour = "blue", arrow = arrow(angle = 20, ends = "first", length = unit(0.03, "npc"))) +
geom_text(data = ynhh, aes(x = x, y = 155, label = lab), hjust = 0, color = "blue", nudge_y = 5, size = 3) +
annotate("label", y = 130, x = as_datetime("2020-10-25 11:11:00"),
xmin = as_datetime("2020-10-25 10:41:00"), xmax = as_datetime("2020-10-25 11:50:00"),
label = "hike in the woods", size = 3)
ggsave("pdfs/ER visit John Goldin 10-25-2020-alt.pdf", width = 6.5, height = 4.5 )
ptoday <- plot_day(day_2020_10_25_health, adate = as_date("2020-10-25"), events = TRUE, hour_min = 8, hour_max = 15)
# health_df %>% filter(as_date(start_date) == ymd("2019-02-16")) %>% arrange(start_date) %>% View()
```
```{r}
plot_day(health_df, ymd("2019-02-15"), events = TRUE, hour_min = 1, hour_max = 2) %>% print()
# health_df %>% filter(as_date(start_date) == ymd("2019-02-16")) %>% arrange(start_date) %>% View()
```
```{r}
plot_day(health_df, ymd("2020-10-25"), hour_min = 8, hour_max = 15) %>% print()
plot_day(health_df, ymd("2020-11-01"), hour_min = 6, hour_max = 12) %>% print()
# health_df %>% filter(as_date(start_date) == ymd("2019-02-16")) %>% arrange(start_date) %>% View()
```
```{r anomalous_readings, echo = FALSE, eval = FALSE}
df <- df %>% filter(type == "HKQuantityTypeIdentifierHeartRate", start_date >= ymd("2017-10-01"))
above145 <- df %>% filter(type == "HKQuantityTypeIdentifierHeartRate", value >= 145 ) %>%
arrange(start_date) %>%
select(start_date, value, dayofweek, hour, date, type, year)
table(above145$date)
plot_day(df, "2019-02-02") %>% print()
above145_night <- df %>% filter(type == "HKQuantityTypeIdentifierHeartRate", value >= 145,
(as.numeric(hour) <= 6) | (as.numeric(hour) >= 23)) %>%
# (hour <= 6) ) %>%
arrange(start_date) %>%
select(start_date, value, dayofweek, hour, date, type, year)
# This gives us four dates, one of which is 2/15/2019.
```
```{r summary}
p15th <- plot_day(df, "2019-02-15", hour_min = 0, hour_max = 24)
weird_dates <- c("2018-08-24", "2018-08-26", "2018-03-27", "2018-06-13")
weird_plot <- plot_day(df, weird_dates, hour_min = 0, hour_max = 24)
strenuous <- plot_day(df, c("2018-03-24", "2018-06-12", "2018-04-13", "2018-04-08"), hour_min = 0, hour_max = 24)
```
```{r}
vhr <- df %>% filter(type == "HKQuantityTypeIdentifierHeartRateVariabilitySDNN")
```
Where xx is heart rate only:
xx %>% filter(value >= 160) %>% select(major_version, startDate, local_start, value, workoutActivityType) %>% View()
Two readings of 162 on 3/13/2021. 19:27:06 and 19:27:08
two EKGs on 3/13 at 7:26 and 7:28, heart rate 74 and 63
Looking at the plot_day graph, frequent readings from 16:00:00 onward
and the 162 readings are total outliers (from abouit 78).
From EKG looks like I was doing EKG about that time so may have been
physically fiddling with the watch.
plot_day(health_df, ymd("2021-03-13")) %>% print()
plot_day(health_df, ymd("2020-06-21")) %>% print()
plot_day(health_df, ymd("2020-04-15")) %>% print()
plot_day(health_df, ymd("2020-04-17")) %>% print()
plot_day(health_df, ymd("2020-03-26")) %>% print()
plot_day(health_df, ymd("2019-04-01")) %>% print()
plot_day(health_df, ymd("2018-12-18"), hour_min = 11, hour_max = 17) %>% print()
plot_day(health_df, ymd("2018-12-16"), hour_min = 11, hour_max = 17) %>% print()
plot_day(health_df, ymd("2018-12-18"), hour_min = 11, hour_max = 17) %>% print()
plot_day(health_df, ymd("2020-10-25"), hour_min = 11, hour_max = 17) %>% print()
plot_day(health_df, ymd("2020-11-14")) %>% print() # 172
plot_day(health_df, ymd("2018-04-12")) %>% print()
Can we look for outliers?
Look only at rows that are not in Worout. Remember that
for data earlier than 90 days there is not much
opportunity to be an outlier. For recent data,
workouts swamp the count.
```{r}
hr <- health_df %>%
filter(str_detect(sourceName, "Watch"), type == "HeartRate", is.na(workoutActivityType))
outliers <- hr %>%
mutate(prior = lag(value) / value,
outlier_type = case_when(
(lag(value) < 0.6*value) & (lead(value) < 0.6*value) ~ "high",
(lead(value) > 1.4*value) | (lag(value) > 1.4*value) ~ "low",
TRUE ~ "none"))
out_table_low <- outliers %>%
filter(outlier_type != "high") %>%
tabyl(outlier_type, major_version)
out_table_low %>% chisq.test(tabyl_results = TRUE)
View(outliers %>% select(local_start, local_end, prior, value, sourceVersion, timezone, major_version))
xx <- hr %>%
mutate(outlier = case_when(
value < 40 ~ "<= 39",
value >= 160 ~ ">= 160",
value >= 150 ~ "150-159",
TRUE ~ "common"
))
xx %>% filter(value >= 160) %>% select(major_version, local_start, local_end, value, workoutActivityType) %>% View()
hr %>% filter(value > 150, major_version == "7") %>% View()
hr %>% filter(value > 120, Period == "Sleep") %>% View()
```