forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPA1_template.Rmd
More file actions
126 lines (89 loc) · 4.36 KB
/
Copy pathPA1_template.Rmd
File metadata and controls
126 lines (89 loc) · 4.36 KB
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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
## Loading and preprocessing the data
```{r loadlib, echo=T, results='hide', message=F, warning=F}
library (dplyr)
library (lubridate)
```
```{r}
if (!file.exists ("activity.csv"))
unzip ("activity.zip")
act <- read.csv ("activity.csv")
```
## What is mean total number of steps taken per day?
```{r}
dailyStepTotal <- aggregate (. ~ date, data = act, sum)
hist (dailyStepTotal$steps, xlab = "Total Steps per Day")
abline (v = mean (dailyStepTotal$steps), col = "red")
legend ("topright", lty = 1, bty = "n", col = "red", legend = "Mean Steps per Day")
# There are up to five significant digits in each number; by reporting integers, we keep that
# relatively taken care of
meanStepsPerDay <- sprintf ("%.0f", mean (dailyStepTotal$steps))
medianStepsPerDay <- sprintf ("%.0f",median (dailyStepTotal$steps))
```
**Mean steps per day: `r meanStepsPerDay`**
**Median steps per day: `r medianStepsPerDay`**
## What is the average daily activity pattern?
```{r}
stepsEachInterval <- aggregate (steps ~ interval, data = act, mean)
with (stepsEachInterval, plot (interval, steps, type = "l",
xlab = "5 Minute Intervals Throughout Each Day", ylab = "Steps"))
rowNum <- which (stepsEachInterval$steps == max (stepsEachInterval$steps))
intervalWithMaxSteps <- stepsEachInterval$interval[rowNum]
abline (v = intervalWithMaxSteps, col = "blue")
legend ("topright", lty = 1, bty = "n", col = "blue", legend = "Interval With Max Steps")
```
**The interval number with the maximum average number of steps is: `r intervalWithMaxSteps`.**
## Imputing missing values
```{r}
numMissingValues <- sum (is.na(act$steps))
```
**Total number of missing values: `r numMissingValues`.**
```{r}
# Strategy for imputing missing values: use average for that interval over all days in dataset
# Create a reverse lookup vector so that we can get the average steps for a given interval, given the interval
# intervals start with 0, vectors start with 1, so I'm looking up (interval + 1) in the vector
avgLookupVector <- rep (0L, max (stepsEachInterval$interval) + 1)
for (i in 1:length (stepsEachInterval$steps)) {
interval <- stepsEachInterval$interval[i]
avgLookupVector[interval+1] <- stepsEachInterval$steps[i]
}
imputedAct <- act
for (i in 1:length (imputedAct$steps)) {
if (is.na (imputedAct$steps[i])) {
imputedAct$steps[i] <- avgLookupVector[imputedAct$interval[i] + 1]
}
}
dailyStepTotal <- aggregate (. ~ date, data = imputedAct, sum)
hist (dailyStepTotal$steps, main = "After Imputing Missing Data", xlab = "Total Steps per Day")
abline (v = mean (dailyStepTotal$steps), col = "red")
legend ("topright", lty = 1, bty = "n", col = "red", legend = "Mean Steps per Day")
meanStepsPerDay <- sprintf ("%.0f", mean (dailyStepTotal$steps))
medianStepsPerDay <- sprintf ("%.0f",median (dailyStepTotal$steps))
```
**Mean steps per day, using imputed missing values: `r meanStepsPerDay`**
**Median steps per day, using imputed missing values: `r medianStepsPerDay`**
**These values do not differ significantly from those in the first part of the assignment.**
**Imputing missing data on the estimates of the total daily number of steps made no significant difference.**
## Are there differences in activity patterns between weekdays and weekends?
```{r}
imputedDaysOfWeek <- weekdays (ymd (imputedAct$date))
imputedWeekends <- imputedDaysOfWeek == "Saturday" | imputedDaysOfWeek == "Sunday"
imputedWeekParts <- rep ("weekday", length (imputedWeekends))
imputedWeekParts[imputedWeekends] = "weekend"
imputedAct[["partOfWeek"]] <- factor (imputedWeekParts)
weekendSteps <- subset (imputedAct, partOfWeek == "weekend")
aggWeekendSteps <- aggregate (steps ~ interval, data = weekendSteps, mean)
weekdaySteps <- subset (imputedAct, partOfWeek == "weekday")
aggWeekdaySteps <- aggregate (steps ~ interval, data = weekdaySteps, mean)
par (mfrow = c(2, 1), mar = c(2,4,2,1), oma = c(0, 2, 0, 0))
with (aggWeekendSteps, plot (interval, steps, type = "l", ylim = c(0, 250), main = "weekend", xlab = "", ylab = ""))
par (mar = c(5,4,2,1))
with (aggWeekdaySteps, plot (interval, steps, type = "l", ylim = c(0, 250), main = "weekday", xlab = "Interval", ylab = ""))
mtext ("Number of steps", side = 2, line = 0, outer = TRUE)
```
**Weekdays have more steps per day on average then do weekend days.**