-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-fitbit.Rmd
442 lines (297 loc) · 11.3 KB
/
2-fitbit.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
---
title: "2. Fitbit"
subtitle: "Analytics Sandbox"
author: "K. Bret Staudt Willet | Florida State University"
date: "January 18, 2023"
output:
xaringan::moon_reader:
lib_dir: libs
seal: true
css: [default, 'css/custom.css', 'css/custom-fonts.css']
nature:
titleSlideClass: [left, bottom]
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
library(knitr)
library(tidyverse)
library(xaringan)
library(fontawesome)
library(fitbitr)
```
class: inverse, center, middle
# `r fa("fas fa-heart-pulse", fill = "#fff")` <br><br> **Part 2:** <br> Fitbit
---
class: inverse, center, middle
# `r fa("fas fa-heart-pulse", fill = "#fff")` <br><br> Setup <br> with Fitbit API
### Step 1: Register a Fitbit developer app
This health assessment utilizes the {fitbitr} R package ([Kaye, 2021](https://mrkaye97.github.io/fitbitr/)). The functions in the package are relatively straightforward, but getting access to the [Fitbit API](https://dev.fitbit.com/build/reference/web-api/) set up is a bit tricky.
---
class: inverse, center, middle
# `r fa("fas fa-heart-pulse", fill = "#fff")` <br><br> Setup <br> with Fitbit API
### Step 1: Register a Fitbit developer app
The first thing to do is register a new Fitbit developer app at [dev.fitbit.com](https://dev.fitbit.com/apps/new).
---
# `r fa("fas fa-heart-pulse", fill = "#fff")` Setup with Fitbit API
### Step 1: Register a Fitbit developer app
Here's how I completed the form:
<hr>
- **Application Name:** `r-health-tracker`
- **Description:** `fitbitr script`
- **Application Website URL:** `https://github.com/bretsw/fitbit` (replace with your GitHub repo)
- **Organization:** `bretsw` (replace with your GitHub user name)
- **Organization Website URL:** `https://github.com/bretsw/fitbit` (replace with your GitHub repo)
- **Terms of Service URL:** `https://github.com/bretsw/fitbit` (replace with your GitHub repo)
- **Privacy Policy URL:** `https://github.com/bretsw/fitbit` (replace with your GitHub repo)
- **OAuth 2.0 Application Type:** `Personal`
- **Redirct URL:** `http://localhost:1410/`
- **Default Access Type:** `Read Only`
---
# `r fa("fas fa-heart-pulse", fill = "#fff")` Setup with Fitbit API
### Step 1: Register a Fitbit developer app
I got a warning from the dev website that the redirect URL needs to be an `https`, but it needs to be in the exact format I have above, because this is how R is set up to take you back to your R console. If you have to, complete the initial form with `https://localhost:1410/` initially, then go back and edit it before you run your R script.
Once you submit your Fitbit dev app, you will be taken to a webpage that lists your **OAuth 2.0 Client ID** and **Client Secret**.
---
class: inverse, center, middle
# `r fa("fas fa-heart-pulse", fill = "#fff")` <br><br> Setup <br> with Fitbit API
### Step 2: Record your dev app credentials
---
# `r fa("fas fa-heart-pulse", fill = "#fff")` Setup with Fitbit API
### Step 2: Record your dev app credentials
Be sure to make a copy of your Fitbit dev app **OAuth 2.0 Client ID** and **Client Secret**. I find it easiest to securely store these in my local R user environment. You can open this file with the command `usethis::edit_r_environ()` and then retrieve stored variables with `Sys.getenv()`.
In my R script below, note that you should save your credentials in the form `FITBIT_CLIENT_ID = XXXXXX` and `FITBIT_CLIENT_SECRET = XXXXXX`.
Once you have your credentials stored, close the .Renviron file and quit out of R entirely. When you restart R, your credentials are ready to go. You can check if things are stored correctly by running: `Sys.getenv('FITBIT_CLIENT_ID')` and `Sys.getenv('FITBIT_CLIENT_SECRET')`.
Just be sure that you don't share these credentials with anyone!
---
class: inverse, center, middle
# `r fa("fas fa-heart-pulse", fill = "#fff")` <br><br> Setup <br> with Fitbit API
### Step 3: Retrieve your Fitbit data
---
# `r fa("fas fa-heart-pulse", fill = "#fff")` Setup with Fitbit API
### Step 3: Retrieve your Fitbit data
```{r, message=FALSE, eval=FALSE}
generate_token(client_id = Sys.getenv('FITBIT_CLIENT_ID'),
client_secret = Sys.getenv('FITBIT_CLIENT_SECRET')
)
```
---
class: inverse, center, middle
# `r fa("fas fa-otter", fill = "#fff")` <br><br> Try it Out!
---
# `r fa("fas fa-otter", fill = "#fff")` Try it Out!
What do you think this code will do?
```{r, eval=FALSE}
start_date <- lubridate::today() - lubridate::years(2)
end_date <- lubridate::today()
steps_df <- steps(start_date, end_date)
write_csv(steps_df, "data/fitbit-steps.csv")
```
---
# `r fa("fas fa-otter", fill = "#fff")` Try it Out!
### Look at your daily steps
```{r, include=FALSE, message=FALSE}
steps_df <- read_csv("data/fitbit-steps.csv")
```
```{r}
glimpse(steps_df)
```
---
# `r fa("fas fa-otter", fill = "#fff")` Picture it!
```{r, message=FALSE, warning=FALSE, out.width="33%", fig.align="center"}
ggplot(steps_df, aes(x=date, y=steps)) +
geom_point() +
geom_smooth() +
geom_vline(aes(xintercept = as.Date("2020-03-11")), color = 'green') +
xlab(NULL) +
ylab("Steps per Day") +
ggtitle("Number of Daily Steps") +
theme_bw()
```
```{r, include=FALSE, eval=FALSE}
ggsave(file="output/2-daily-steps.png", width=8, height=4.5)
```
---
# `r fa("fas fa-otter", fill = "#fff")` Compare!
```{r, out.width="49%", echo=FALSE, fig.align="center"}
include_graphics("output/2-daily-steps-old.png")
```
```{r, out.width="49%", echo=FALSE, fig.align="center"}
include_graphics("output/2-daily-steps.png")
```
---
# `r fa("fas fa-otter", fill = "#fff")` Try it Out!
What do you think this code will do?
```{r, eval=FALSE}
hr <- heart_rate_intraday(lubridate::today() - 1)
write_csv(hr, "data/fitbit-hr.csv")
```
---
# `r fa("fas fa-otter", fill = "#fff")` Try it Out!
### Look at yesterday's heart rate
```{r, include=FALSE, message=FALSE}
hr <- read_csv("data/fitbit-hr.csv")
```
```{r}
glimpse(hr)
```
---
# `r fa("fas fa-otter", fill = "#fff")` Picture it!
```{r, message=FALSE, warning=FALSE, out.width="33%", fig.align="center"}
ggplot(hr, aes(x=time, y=heart_rate)) +
geom_point() +
geom_smooth() +
xlab(NULL) +
ylab("Beats per Minute") +
ggtitle("Yesterday's Heart Rate") +
theme_bw()
```
```{r, include=FALSE, eval=FALSE}
ggsave(file="output/2-yesterday-heart-rate.png", width=8, height=4.5)
```
---
# `r fa("fas fa-otter", fill = "#fff")` Compare!
```{r, out.width="49%", echo=FALSE, fig.align="center"}
include_graphics("output/2-heart-rate-yesterday-old.png")
```
```{r, out.width="49%", echo=FALSE, fig.align="center"}
include_graphics("output/2-heart-rate-yesterday.png")
```
---
# `r fa("fas fa-otter", fill = "#fff")` Try it Out!
What do you think this code will do?
```{r, eval=FALSE}
yesterday_df <- activity_summary(lubridate::today() - 1)
write_csv(yesterday_df, "data/fitbit-yesterday-df.csv")
```
---
# `r fa("fas fa-otter", fill = "#fff")` Try it Out!
```{r, include=FALSE, message=FALSE}
yesterday_df <- read_csv("data/fitbit-yesterday-df.csv")
```
```{r}
glimpse(yesterday_df)
```
---
# `r fa("fas fa-otter", fill = "#fff")` Try it Out!
What do you think this code will do?
```{r, eval=FALSE, message=FALSE, warning=FALSE}
health_df = NULL
for (i in 0:60) {
new_row =
tibble(date = lubridate::today() - i,
resting_hr = activity_summary(date)$resting_heart_rate,
steps = activity_summary(date)$steps
)
health_df <-
health_df %>%
bind_rows(new_row)
}
write_csv(health_df, "data/fitbit-health-df.csv")
```
---
# `r fa("fas fa-otter", fill = "#fff")` Try it Out!
### Look at yesterday's heart rate
```{r, include=FALSE, message=FALSE}
health_df <- read_csv("data/fitbit-health-df.csv")
```
```{r}
glimpse(health_df)
```
---
# `r fa("fas fa-otter", fill = "#fff")` Try it Out!
This block of code looks at my resting heart rate across the past 2 months (60 days). Note that a Fitbit dev app's rate limit is 150 API requests per hour for each user who has consented to share their data; and it resets at the top of each hour. This means that you are limited to retrieving the `activity_summary()` data for 150 days at a time. You could store this data, wait an hour, and the retrieve the next 150 days if you wanted to.
---
# `r fa("fas fa-otter", fill = "#fff")` Picture it!
```{r, message=FALSE, warning=FALSE, out.width="33%", fig.align="center"}
ggplot(health_df, aes(x=date, y=resting_hr)) +
geom_point() +
geom_smooth() +
xlab(NULL) +
ylab("Beats per Minute while Resting") +
ggtitle("Resting Heart Rate") +
theme_bw()
```
```{r, include=FALSE, eval=FALSE}
ggsave(file="output/2-resting-heart-rate.png", width=8, height=4.5)
```
---
# `r fa("fas fa-otter", fill = "#fff")` Compare!
```{r, out.width="49%", echo=FALSE, fig.align="center"}
include_graphics("output/2-resting-heart-rate-old.png")
```
```{r, out.width="49%", echo=FALSE, fig.align="center"}
include_graphics("output/2-resting-heart-rate.png")
```
---
# `r fa("fas fa-otter", fill = "#fff")` Try it Out!
What do you think this code will do?
```{r, eval=FALSE, message=FALSE, warning=FALSE, out.width="33%", fig.align="center"}
ggplot(health_df, aes(x=steps, y=resting_hr)) +
geom_point() +
geom_smooth() +
xlab("Daily Steps") +
ylab("Beats per Minute while Resting") +
ggtitle("Resting Heart Rate vs. Daily Steps") +
theme_bw()
```
---
# `r fa("fas fa-otter", fill = "#fff")` Picture it!
```{r, message=FALSE, warning=FALSE, out.width="33%", fig.align="center"}
ggplot(health_df, aes(x=steps, y=resting_hr)) +
geom_point() +
geom_smooth() +
xlab("Daily Steps") +
ylab("Beats per Minute while Resting") +
ggtitle("Resting Heart Rate vs. Daily Steps") +
theme_bw()
```
```{r, include=FALSE, eval=FALSE}
ggsave(file="output/2-resting-heart-rate-vs-daily-steps-a.png", width=8, height=4.5)
```
---
# `r fa("fas fa-otter", fill = "#fff")` Picture it!
```{r, message=FALSE, warning=FALSE, out.width="33%", fig.align="center"}
ggplot(health_df, aes(x=steps, y=resting_hr)) +
geom_jitter(alpha=0.6) +
geom_smooth(method='lm') +
xlab("Daily Steps") +
ylab("Beats per Minute while Resting") +
ggtitle("Resting Heart Rate vs. Daily Steps") +
theme_bw()
```
```{r, include=FALSE, eval=FALSE}
ggsave(file="output/2-resting-heart-rate-vs-daily-steps-b.png", width=8, height=4.5)
```
---
# `r fa("fas fa-otter", fill = "#fff")` Compare!
```{r, out.width="49%", echo=FALSE, fig.align="center"}
include_graphics("output/2-resting-heart-rate-vs-daily-steps-a.png")
```
```{r, out.width="49%", echo=FALSE, fig.align="center"}
include_graphics("output/2-resting-heart-rate-vs-daily-steps-b.png")
```
---
class: inverse, center, middle
# `r fa("fas fa-code", fill = "#fff")` <br><br> Try it out!
---
# `r fa("fas fa-code", fill = "#fff")` Try it out!
- Download a copy of this repository.
- If you have a Fitbit, see if you can retrieve and analyze your own data.
- Regardless, use the saved data in the "data" folder to play around a bit more, changing different parameters.
- Reflect:
- What other comparisons might you make?
- How else might you analyze these Fitbit data?
---
class: inverse, center, middle
# `r fa("fas fa-list", fill = "#fff")` <br><br> Appendix: <br> Helpful Resources <br> and Troubleshooting
---
# Resources
```{r child="notes/resources.Rmd"}
```
---
# Troubleshooting
```{r child="notes/troubleshooting.Rmd"}
```