-
Notifications
You must be signed in to change notification settings - Fork 1
/
physical_activity_data_science.Rpres
337 lines (282 loc) · 10.7 KB
/
physical_activity_data_science.Rpres
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
Physical Activity and Data Science
========================================================
author: BEAP Lab
date: May 9-10, 2019
autosize: true
Welcome
========================================================
![BEAP Lab](BEAP_Lab_logo.png)
So excited, so afraid
========================================================
<iframe src="https://giphy.com/embed/1uyFaGpt2ilmE" width="576" height="576" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/grandma-1uyFaGpt2ilmE"></a></p>
Questions and interactions
========================================================
<iframe src="https://giphy.com/embed/7PfwoiCwBp6Ra" width="576" height="460" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/ellen-degeneres-questions-ask-7PfwoiCwBp6Ra"></a></p>
Introduction
========================================================
* Name
* Why you chose this course
* Experience with computer code
Accelerometers
========================================================
* All phones have them (new phones have 2-3)
* Measure physical activity, sleep, and sedentary behaviour
* Provide lots of cool data
Accelerometers
========================================================
<iframe width="680" height="420" src="https://www.youtube.com/embed/KZVgKu6v808" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
SenseDoc
========================================================
![SenseDoc](sensedoc.jpg)
SenseDoc
========================================================
* Easy to configure and download data
* Lots of user control
* Fixed position on the body
* Global Positioning System
SenseDoc Setup
========================================================
* Only works on PC
* We have setup most of the devices
* Will walk through setup of one device
Data Collection
========================================================
<iframe src="https://giphy.com/embed/lMTtFs2PJVu9O" width="480" height="360" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/credit-lMTtFs2PJVu9O"></a></p>
Data Download
========================================================
* Plug in your SenseDoc
* Export the data to desktop
* Name it something reasonable
* Check the file
* How might you do that?
R and RStudio
========================================================
The [R project for statistical computing](https://www.r-project.org/) is a free open source statistical programming language and project.
<iframe src="https://player.vimeo.com/video/97166163?color=428bca&title=0&byline=0&portrait=0" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<p><a href="https://vimeo.com/97166163"></a></p>
R and RStudio
========================================================
* 2 types of coding
* Base R
* Tidyverse
* We will be using [Tidyverse](https://www.tidyverse.org/packages/)
* Lots of online tutorials
* [@AmeliaMN](https://twitter.com/AmeliaMN)
* https://github.com/AmeliaMN/IntroToR/blob/master/README.md
Good habits
========================================================
* Name things
* Use a case convention
* We will use `snake_case`
* Use tidy data principles
In your groups
========================================================
- Peer programming
- Pick a driver for now
- Pick who's data you will use
Super Quick Overview of RStudio
========================================================
R
========================================================
You can use R as a basic calculator. If you pass the command 2+2 it will return 4.
```{r}
2+2
```
R
========================================================
You can use arithmetic and mathematical operators in your commands
Code | Action
------------- | -------------
+ | Addition
- | Subtraction
_*_ | Multiplication
/ | Division
^ | Raise to the power of
R
========================================================
```{r}
2^4
```
Packages
========================================================
Packages let you do things. If you have thought about doing it, there is a package already. There are two basic steps to using a package:
* Installing the package
* `install.packages("tidyverse")`
* Loading the package
* `library(tidyverse)`
Anatomy of a R call
========================================================
```{r, eval=FALSE}
accel_data <- read_csv("/Users/dfuller/Desktop/accel_data.csv")
```
Code | Description
------------- | -------------
`accel_data` | Name of the object to save to memory
`<-` | Left assignment operator. Use it to store an object
`read_csv` | A function that does something
`("/Users/dfuller/Desktop/accel_data.csv")` | The object or location of the object the function should be applied to
Read in some data
========================================================
Mac
```{r, eval=FALSE}
accel_data <- read_csv("/Users/dfuller/Desktop/accel_data.csv")
```
PC
```{r, eval=FALSE}
accel_data <- read_csv("C:\\Users\\Andrie\\Desktop\\accel_data.csv")
```
Data description
========================================================
* rowid: unique identifier for each row
* utcdate: the data to the second in utc time
* ts: second of measurement to 13 decimal places
* x: the x axis of acceleration measured in g units of gravity (1g = 9.81m/s^2)
* y: the x axis of acceleration measured in g
* z: the x axis of acceleration measured in g
Check the data
========================================================
```{r, eval=FALSE}
head(accel_data)
```
Mean and standard deviation for x axis
========================================================
```{r, eval=FALSE}
mean(data$xcounts)
```
Mean and standard deviation for x axis
========================================================
```{r, eval=FALSE}
accel_mean_x <- accel_data %>%
summarize(
x_mean = mean(x),
x_sd = sd(x)
)
head(accel_mean_x)
```
Mean and standard deviation all axes
========================================================
```{r, eval=FALSE}
accel_mean <- accel_data %>%
summarize(
x_mean = mean(x),
y_mean = mean(y),
z_mean = mean(z),
x_sd = sd(x),
y_sd = sd(y),
z_sd = sd(z),
)
head(accel_mean)
```
Create a plot of the histogram of X
========================================================
```{r, eval=FALSE}
accel_hist_x <- ggplot(accel_data) +
geom_histogram(aes(x))
plot(accel_hist_x)
```
Create a plot of the desnity plot of X
========================================================
```{r, eval=FALSE}
accel_dens_x <- ggplot(accel_data) +
geom_density(aes(x), colour = "blue")
plot(accel_dens_x)
```
Create a plot of the desnity plot of X, Y, and Z
========================================================
```{r, eval=FALSE}
accel_hist <- ggplot(accel_data) +
geom_density(aes(x), colour = "blue") +
geom_density(aes(y), colour = "red") +
geom_density(aes(z), colour = "purple")
plot(accel_hist)
```
Plot of the x axis over time
========================================================
```{r, eval=FALSE}
accel_plot_x <- ggplot(accel_data) +
geom_point(aes(x = utcdate, y = x), colour = "blue", alpha = 0.1)
plot(accel_plot_x)
```
Plot of the all axes over time
========================================================
```{r, eval=FALSE}
accel_plot <- ggplot(accel_data) +
geom_point(aes(x = utcdate, y = x), colour = "blue", alpha = 0.1) +
geom_point(aes(x = utcdate, y = y), colour = "red", alpha = 0.1) +
geom_point(aes(x = utcdate, y = z), colour = "green", alpha = 0.1)
plot(accel_plot)
```
We are doing awesome!
========================================================
<iframe src="https://giphy.com/embed/mXnO9IiWWarkI" width="480" height="324" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/trolli-lego-trolli-weirdly-awesome-mXnO9IiWWarkI"></a></p>
Run the same analysis on another team members data
========================================================
Day 2
========================================================
<iframe src="https://giphy.com/embed/VxbvpfaTTo3le" width="480" height="262" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/cat-kitten-kitty-VxbvpfaTTo3le"></a></p>
GPS Data
========================================================
<iframe width="560" height="315" src="https://www.youtube.com/embed/cTrsvGytGG0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Setup SenseDoc
========================================================
Scavenger Hunt
========================================================
* Walk to the Clock Tower
* Walk through the UC
* Walk around the duck pond
* Walk to the Tim's in the Aquarena
* Come back
Data Download
========================================================
* Plug in your SenseDoc
* Export the data to desktop
* Name it something reasonable
* Check the file
* How might you do that?
In your groups
========================================================
- Peer programming
- Pick a driver for now
- Pick who's data you will use
Read in the data
========================================================
```{r, eval = FALSE}
gps_data <- read_csv("/Users/dfuller/Desktop/gps_data.csv")
```
Mean and standard deviation of speed
========================================================
```{r, eval = FALSE}
??????
```
Plot the lat and lon data
========================================================
```{r, eval = FALSE}
gps_plot_1 <- ggplot(gps_data) +
geom_point(aes(x = lon, y = lat))
plot(gps_plot_1)
```
Get Google Maps
========================================================
Need a Google Maps API Key
```{r, eval = FALSE}
avalon_basemap <- get_map(location = "St. John's, Newfoundland, Canada",
source = "google",
maptype = "roadmap",
crop = FALSE,
zoom = 14)
plot(avalon_basemap)
```
Combine the ggplot and the Google map
========================================================
```{r, eval = FALSE}
maps_points <- ggmap(avalon_basemap) +
geom_point(aes(x = lon, y = lat), data = gps_data, alpha = 0.2)
plot(maps_points)
```
Colour the plot by speed
========================================================
```{r, eval = FALSE}
maps_points <- ggmap(avalon_basemap) +
geom_point(aes(x = lon, y = lat, colour = speed), data = gps_data, alpha = 0.2)
plot(maps_points)
```