forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
178 lines (118 loc) · 5.23 KB
/
PA1_template.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
## Loading and preprocessing the data
### Reading the compressed activity data
```{r}
infile <- "activity.csv"
if (!file.exists(infile)) {
unzip("activity.zip")
}
```
### Processing/transforming the data
```{r}
# Read activity data making sure the date column is recognized as a Date
data <- read.csv(infile, colClasses=c(NA, 'Date', NA))
```
## What is mean total number of steps taken per day?
### Histogram of the total number of steps taken each day
First, let's compute the total number of steps per day.
```{r}
library(plyr)
# Compute total number of steps on a daily basis (aggregate per date)
steps_per_day <- ddply(data, .(date), summarize, total=sum(steps))
```
Next, display a histogram showing the frequency of total daily steps.
```{r}
hist(steps_per_day$total,
breaks = 10,
main = "Total daily steps",
xlab = "Number of daily steps")
```
### Mean and median total number of steps taken per day
The mean and median of the total number of daily steps are:
```{r}
mean_steps_per_day <- mean(steps_per_day$total, na.rm = TRUE)
median_steps_per_day <- median(steps_per_day$total, na.rm = TRUE)
```
- Mean: `r as.integer(mean_steps_per_day)`
- Median: `r as.integer(median_steps_per_day)`
## What is the average daily activity pattern?
### Daily activity time series plot
First, let's compute the average daily activity by aggregating each interval taking the mean number of steps (i.e. average the number of steps taken across all days).
```{r}
avg_daily_activity <- ddply(data, .(interval), summarize, total=mean(steps,na.rm=TRUE))
```
Next, display the average daily activity by time interval.
```{r}
plot(avg_daily_activity$interval, avg_daily_activity$total,
type = 'l',
main = "Average daily activity by interval",
xlab = 'Interval',
ylab = 'Number of steps')
```
### 5-minute interval containing the maximum number of steps
The 5-min interval which contains the maximum number of steps is:
```{r}
interval_max <- avg_daily_activity$interval[which.max(avg_daily_activity$total)]
```
The interval **`r interval_max`** contains the maximum number of steps.
## Imputing missing values
### Total number of missing values
The number of missing values is:
```{r}
sum(!complete.cases(data$steps))
```
The missing value breakdown is as follows:
Variable | Missing Value Count
---: | :---
Steps | `r sum(is.na(data$steps))`
Date | `r sum(is.na(data$date))`
Interval | `r sum(is.na(data$interval))`
> We will only consider missing step values as they are the sole missing value contributor as demonstrated by the previous table.
### Strategy for filling in all of the missing values
The strategy for filling the missing step values is to use the rounded mean for the corresponding 5-min interval. We round the values because steps need to be whole integer numbers.
### Corrected data set
The corrected data set is computed as follows:
```{r}
corrected <- data
mean_intervals <- join(corrected, avg_daily_activity, by = 'interval')
corrected$steps <- with(mean_intervals, ifelse(is.na(steps), round(total), steps))
```
> Note that we round the average steps (i.e. `round(total)`) because steps need to be whole integer numbers.
Let's update the total daily steps based on the new corrected values.
```{r}
corr_steps_per_day <- ddply(corrected, .(date), summarize, total=sum(steps))
```
### Comparing to the uncorrected data set
Let's display a histogram showing the frequency of the corrected total daily steps.
```{r}
hist(corr_steps_per_day$total,
breaks = 10,
main = "Corrected total daily steps",
xlab = "Number of daily steps")
```
The corrected mean and median of the total number of daily steps become:
```{r}
corr_mean_steps_per_day <- mean(corr_steps_per_day$total, na.rm = TRUE)
corr_median_steps_per_day <- median(corr_steps_per_day$total, na.rm = TRUE)
```
- Mean: `r as.integer(corr_mean_steps_per_day)`
- Median: `r as.integer(corr_median_steps_per_day)`
**Both are pratically equal** to the uncorrected ones. This is because we used the rounded mean of the daily steps per interval to cope for the missing step values. This has the effect of replicating the existing behavior observed hence not impacting the mean and median much.
## Are there differences in activity patterns between weekdays and weekends?
### Weekend factor
Let's extend the corrected data set with a new factor variable, `daytype`, which indicates the type of day (`weekday` or `weekend`):
```{r}
corrected$daytype <- as.factor(ifelse(weekdays(data$date) %in% c('Saturday', 'Sunday'), 'weekend', 'weekday'))
```
### Weekend impact on activity plots
Let's plot the day type contribution to the average number of steps per interval. This is achieved by averaging the number of steps over each interval and grouping per day type. Again, we round the mean step values because they need to be whole integer numbers.
```{r}
library(lattice)
# Compute the average number of steps per interval over each day type
steps_per_daytype <- ddply(corrected, .(daytype, interval), summarize, total=round(mean(steps)))
xyplot(total ~ interval | daytype,
type = "l",
data = steps_per_daytype,
main = 'Day type contribution to average number of steps per interval',
ylab = 'Total number of steps',
layout = c(1, 2))
```