-
Notifications
You must be signed in to change notification settings - Fork 1
/
physical_activity_data_science_code.Rmd
182 lines (149 loc) · 4.51 KB
/
physical_activity_data_science_code.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
---
title: "Physical Activity and Data Science"
author: "Daniel Fuller"
date: "06/05/2019"
output:
html_document:
keep_md: true
---
```{r setup, include=TRUE}
knitr::opts_chunk$set(echo = TRUE)
```
# Answer key for Physical Activity Data Science Workshop
## Get the libraries we need
```{r}
library(tidyverse)
library(ggmap)
```
## Accelerometer data section
### Read in the data
```{r}
accel_data <- read_csv("accel_data_example.csv")
```
### Show the first 6 rows
```{r}
head(accel_data)
```
### Accel 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
### Exploring x,y,z
Calculate the mean and standard deviation for the x axis and create a new data frame.
```{r}
accel_mean_x <- accel_data %>%
summarize(
x_mean = mean(x),
x_sd = sd(x)
)
head(accel_mean_x)
```
Calculate the mean and standard deviation for all variables and create a new data frame.
```{r}
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}
accel_hist_x <- ggplot(accel_data) +
geom_histogram(aes(x))
plot(accel_hist_x)
```
### Create a plot of the desnity plot of X
```{r}
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}
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)
```
### Create a plot of the x axis over time with time on the x axis and acceleration
```{r}
accel_plot_x <- ggplot(accel_data) +
geom_point(aes(x = utcdate, y = x), colour = "blue", alpha = 0.1)
plot(accel_plot_x)
```
Cool. Now we can add all the other axes and see them together.
```{r}
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 = "purple", alpha = 0.1)
plot(accel_plot)
```
## GPS data section
### Read in the data
```{r}
gps_data <- read_csv("gps_data_example.csv")
```
### Check out the data
```{r}
head(gps_data)
```
### Missing data
I know there is missing data. We are going to remove all missing. If you had
```{r}
gps_data <- gps_data %>%
drop_na()
```
### GPS Data description
* ts: second of measurement to 13 decimal places
* utcdate: the data to the second in utc time
* lat: the latitude
* geographic coordinate that specifies the north–south position of a point on the Earth's surface.
* lon: the longitude
* geographic coordinate that specifies the east–west position of a point on the Earth's surface.
* sat_used: number of satellites used to fix the lat and lon coordinates
### Calculate the mean and standard deviation of speed
```{r}
gps_mean_x <- gps_data %>%
summarize(
speed_mean = mean(speed),
speed_sd = sd(speed)
)
head(gps_mean_x)
```
### Make a plot of the lat and lon data
Which axes should we have on the x and y axes
```{r}
gps_plot_1 <- ggplot(gps_data) +
geom_point(aes(x = lon, y = lat))
plot(gps_plot_1)
```
### Connect with Google Maps using `ggmap`
```{r}
avalon_basemap <- get_map(location = "St. John's, Newfoundland, Canada",
source = "google",
maptype = "roadmap", crop = FALSE,
zoom = 14)
plot(avalon_basemap)
```
### Combine ggmap with points
```{r}
maps_points <- ggmap(avalon_basemap) +
geom_point(aes(x = lon, y = lat), data = gps_data, alpha = 0.2)
plot(maps_points)
```
### Colour GPS data by speed
```{r}
maps_speed <- ggmap(avalon_basemap) +
geom_point(aes(x = lon, y = lat, colour = speed), data = gps_data, alpha = 0.2)
plot(maps_speed)
```