-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_data.Rmd
255 lines (207 loc) · 7.91 KB
/
sample_data.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
---
title: "Analysis of 24Hr Movement Guideline Fibit Data"
author: "Daniel Fuller"
date: "26/07/2021"
output:
html_document:
keep_md: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# 24 Hour Movement Guidelines Data Analysis
## Required packages
```{r, echo = FALSE, include = FALSE}
library(tidyverse)
library(lubridate)
library(ggplot2)
library(naniar)
library(ggmap)
library(ggthemes)
library(sf)
library(rgeos)
library(cancensus)
library(cowplot)
library(ggspatial)
library(knitr)
library(ggpubr)
library(reshape)
library(scales)
```
# Data Description
There are a number of data types and frequencies. Below is a summary:
1. Minute Frequency Data
- heartrate_1min_merged
- minuteCaloriesNarrow_merged
- minuteIntensitiesNarrow_merged
- minuteMETsNarrow_merged
- minuteSleep_merged
- minuteStepsNarrow_merged
2. Daily Frequency Data
- dailyCalories_merged
- dailyIntensities_merged
- dailySteps_merged
- sleepDay_merged
- heartRateZones_merged
- Daily time in 4 heart rate zones (Out of Range, Fat Burn, Cardio, Peak)
3. Other Frequency data
- activitylogs_merged
- An activity is recorded whenever the user inputs it
- battery_merged
- Battery level is recorded whenever there is a sync event
- syncEvents_merged
- All Sync events
- 30secondSleepStages_merged
- Sleep stages every 30 seconds.
# To do
1. Combine the minute level data
2. Combine day level data
3. Aggregate minute level data to the day and confirm it makes sense with day level
4. Join battery and sync events to the minute level data
5. Aggregate everything up to the day level
## 1. Combine the minute level data
### Heart rate per minute
```{r}
hr_data <- read_csv("Data/heartrate_1min_merged.csv")
hr_data$time <- mdy_hms(hr_data$Time)
hr_data$Time <- NULL
colnames(hr_data) <- c("id", "heart_rate_bmp", "time")
summary(hr_data$heart_rate_bmp)
ggplot(hr_data, aes(heart_rate_bmp)) +
geom_histogram()
```
### Calories per minute
```{r}
ee_data <- read_csv("Data/minuteCaloriesNarrow_merged.csv")
ee_data$time <- mdy_hms(ee_data$ActivityMinute)
ee_data$ActivityMinute <- NULL
colnames(ee_data) <- c("id", "calories", "time")
summary(ee_data$calories)
ggplot(ee_data, aes(calories)) +
geom_histogram()
```
See fitbit description for calories [https://dev.fitbit.com/build/reference/web-api/intraday/get-activity-intraday-by-date/](https://dev.fitbit.com/build/reference/web-api/intraday/get-activity-intraday-by-date/)
```{}
## Using BMR/EER Algorithms
If a user had no Fitbit tracker data for the specific day then the greater of Logged Activities + BMR (for minutes when there is no activity) and the calories calculated from the EER for that day (if EER enabled for this user's profile) are taken. In case, there was some data from the tracker for the specific day, that data where available is used and for time where data is unavailable, the BMR is used. If the total is less than 20% greater than BMR then the EER (cals < EER * 0.8) is used. EER never used to calculate calories for today.
Using BMR Formula
Fitbit uses the standard MD Mifflin-St Jeor equation:
9.99 * weightKg + 6.25*heightCm - 4.92*ageYears + s, where s is +5 for
males
and -161 for female
EER Formula (TEE total energy expenditure)
The EER Formula is based on http://www.cdc.gov/pcd/issues/2006/oct/pdf/06_0034.pdf, which in turn is based on "Food and Nutrition Board. Dietary reference intakes for energy, carbohydrate, fiber, fat, fatty acids, cholesterol, protein, and amino acids (macronutrients). Washington (DC): National Academy Press; 2005." http://www.nap.edu/openbook.php?isbn=0309085373&page=204
MALE-based EER Formula:
TEE = 864 - 9.72 x age (years) + 1.0 x (14.2 x weight(kg) + 503 x height
(meters))
FEMALE-based EER Formula:
TEE = 387 - 7.31 x age (years) + 1.0 x (10.9 x weight(kg) + 660.7 x height
(meters))
```
### Intensity per minute
```{r}
intense_data <- read_csv("Data/minuteIntensitiesNarrow_merged.csv")
intense_data$time <- mdy_hms(intense_data$ActivityMinute)
intense_data$ActivityMinute <- NULL
colnames(intense_data) <- c("id", "intensity", "time")
intense_data$intensity <- as.factor(intense_data$intensity)
table(intense_data$intensity)
ggplot(intense_data, aes(intensity)) +
geom_bar()
```
This is a four category variable. Looks like it will match up with the heart rate zone data but not sure yet.
### METS per minute
```{r}
met_data <- read_csv("Data/minuteMETsNarrow_merged.csv")
met_data$time <- mdy_hms(met_data$ActivityMinute)
met_data$ActivityMinute <- NULL
colnames(met_data) <- c("id", "mets", "time")
summary(met_data$mets)
ggplot(met_data, aes(mets)) +
geom_histogram()
```
Interesting. Will need to look into how this is calculated. 10 METS as the median and 15 METS as the mean is not right.
### Sleep per minute
```{r}
sleep_data <- read_csv("Data/minuteSleep_merged.csv")
sleep_data$time <- mdy_hms(sleep_data$date)
sleep_data$date <- NULL
colnames(sleep_data) <- c("id", "sleep", "log_id", "time")
sleep_data$sleep <- as.factor(sleep_data$sleep)
table(sleep_data$sleep)
ggplot(sleep_data, aes(sleep)) +
geom_bar()
```
This is a three category variable. There is a lot of missing here so I'm guessing that these represent sleep stages or something with a null value meaning not sleeping.
### Steps per minute
```{r}
step_data <- read_csv("Data/minuteStepsNarrow_merged.csv")
step_data$time <- mdy_hms(step_data$ActivityMinute)
step_data$ActivityMinute <- NULL
colnames(step_data) <- c("id", "steps", "time")
summary(step_data$steps)
ggplot(step_data, aes(steps)) +
geom_histogram()
```
## Joining the datasets
```{r}
ee_intense <- full_join(ee_data, intense_data, by = c("id", "time"))
ee_intense_met <- full_join(ee_intense, met_data, by = c("id", "time"))
ee_intense_met_step <- full_join(ee_intense_met, step_data, by = c("id", "time"))
ee_intense_met_step_hr <- full_join(ee_intense_met_step, hr_data, by = c("id", "time"))
data <- full_join(ee_intense_met_step_hr, sleep_data, by = c("id", "time"))
## Filter out coordinator data
data <- filter(data, id != "Study Coordinator QU")
### Create a location variable
data$location <- substr(data$id, 1, 1)
### Create an intervention
data <- data %>%
mutate(intervention = case_when(
time < "2021-10-25 00:00:00" ~ "pre",
time >= "2021-10-25 00:00:00" & time < "2021-12-05 00:00:00" ~ "intervention",
time > "2021-12-05 00:00:00" ~ "post"
))
table(data$intervention)
### Recoding Sleep
data <- data %>%
mutate(sleep_yn = case_when(
sleep == 1 ~ "sleep",
sleep == 2 ~ "sleep",
sleep == 3 ~ "sleep",
TRUE ~ "awake"
))
table(data$sleep_yn)
rm(ee_data, ee_intense, ee_intense_met, ee_intense_met_step, ee_intense_met_step_hr, hr_data, intense_data, met_data, sleep_data, step_data)
```
## Quick descriptive stats
```{r warning= FALSE}
data$day <- day(data$time)
data$hour <- hour(data$time)
hourly_summary <- data %>%
group_by(id, hour) %>%
get_summary_stats(calories, heart_rate_bmp, steps)
daily_summary <- data %>%
group_by(id, day) %>%
get_summary_stats(calories, heart_rate_bmp, steps)
```
## Heat Map over the study period per hour
```{r}
ggplot(hourly_summary, aes(x = hour, y = variable, fill = mean)) +
geom_tile() +
facet_wrap(~ id) +
scale_fill_gradient(low = "grey20", high = "red") +
labs(x = "", y = "") +
theme_classic() +
theme(legend.position = "none")
```
## Heat Map over the study period per day
```{r}
ggplot(daily_summary, aes(x = day, y = variable, fill = mean)) +
geom_tile() +
facet_wrap(~ id) +
scale_fill_gradient(low = "grey20", high = "red") +
labs(x = "", y = "") +
theme_classic() +
theme(legend.position = "none")
```
## 2. Combine day level data