forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
105 lines (89 loc) · 4.74 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
---
title: "Reproducible Research: Peer Assessment 1"
author: "timwilliate"
date: "August 15, 2014"
output: html_document
---
----
#### Loading and preprocessing the data
```{r load_raw_data, echo=TRUE}
activityData <- read.csv("activity.csv")
activityData_noNA <- subset(activityData, steps != 'NA')
```
#### What is mean total number of steps taken per day?
Make a histogram of the total number of steps taken each day:
```{r total_daily_steps, echo=TRUE}
dailyTotalSteps <- aggregate(steps~date, activityData_noNA, sum)
hist(dailyTotalSteps$steps, main = ("Histogram of Daily Total Step Count"), xlab = "Total Daily Steps", freq = TRUE)
```
Calculate and report the mean and median total number of steps taken per day:
The **mean** daily step count is:
``` {r mean_daily_steps, echo=TRUE}
mean(dailyTotalSteps$steps)
```
The **median** daily step count is:
```{r median_daily_steps, echo=TRUE}
median(dailyTotalSteps$steps)
```
#### What is the average daily activity pattern?
Make a time series plot (i.e. `type = "l"`) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis):
```{r, mean_steps_per_interval, echo=TRUE}
meanStepsPerInterval <- aggregate(steps~interval, activityData_noNA, mean)
plot(meanStepsPerInterval, type = 'l', main = ("Time Series of Mean Steps Per 5-Minute Interval"))
```
Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r, echo=TRUE}
subset(meanStepsPerInterval, steps == max(meanStepsPerInterval[,2]))
```
#### Section 3: Imputing missing values
Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with `NA`s):
```{r, echo=TRUE}
nMissingValues <- nrow(activityData) - nrow(activityData_noNA)
nMissingValues
```
Devise a strategy for filling in all of the missing values in the dataset. Below I chose to fill in missing values using the mean
step count recorded over the entire set of equivalent 5-minute intervals. Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r, echo=TRUE}
if (require(plyr) == FALSE) {install.packages('plyr')}
require(plyr)
rowsWithNA <- subset(activityData, is.na(steps) == TRUE)
estimateByMeans <- join(rowsWithNA, meanStepsPerInterval, by = "interval", type = "left")
interpolatedDataSet <- rbind(activityData_noNA, estimateByMeans[,2:4])
```
Make a histogram of the total number of steps taken each day and Calculate and report the **mean** and **median** total number of steps taken per day. Do these values differ from the estimates from the first part of the assignment? What is the impact of imputing missing data on the estimates of the total daily number of steps?
```{r total_daily_steps_interpolated, echo=TRUE}
dailyTotalStepsInterpolated <- aggregate(steps~date, interpolatedDataSet, sum)
hist(dailyTotalStepsInterpolated$steps, main = ("Histogram of Daily Total Step Count"), xlab = "Total Daily Steps", freq = TRUE)
```
The **mean** daily step count is:
``` {r mean_daily_steps_interpolated, echo=TRUE}
mean(dailyTotalStepsInterpolated$steps)
```
The **median** daily step count is:
```{r median_daily_steps_interpolated, echo=TRUE}
median(dailyTotalStepsInterpolated$steps)
```
Imputing the missing values does not change the mean value of the daily step count from the previous estiamte. The median daily step count changed by only a single step. Overall the impact of imputing the missing values is neglible when measured at the level of mean and meadian daily step counts.
#### Section 4: Are there differences in activity patterns between weekdays and weekends?
Create a new factor variable in the dataset with two levels -- "weekday" and "weekend" indicating whether a given date is a weekday or weekend day.
```{r add_factor_data, echo=TRUE, cache=TRUE}
if (require(lubridate) == FALSE) {install.packages('lubridate')}
require(lubridate)
interpolatedDataSet$date <- ymd(interpolatedDataSet$date)
classifyDate <- function(date) {
if (weekdays(date) == "Saturday") {"weekend"}
else if (weekdays(date) == "Sunday") {"weekend"}
else {"weekday"}
}
interpolatedDataSet$wday <- lapply(interpolatedDataSet$date, classifyDate)
interpolatedDataSet$wday <- as.factor(unlist(interpolatedDataSet$wday))
collapse <- aggregate(steps~interval+wday, interpolatedDataSet, mean)
```
Make a panel plot containing a time series plot (i.e. `type = "l"`) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all weekday days or weekend days (y-axis).
```{r two_panel_plot, echo=TRUE}
if (require(ggplot2) == FALSE) {install.packages('ggplot2')}
require(ggplot2)
g <- ggplot(collapse, aes(interval, steps))
g <- g + geom_line() + facet_wrap(~wday, nrow=2, ncol=1)
g + labs(x = "Inerval", y = "Number of Steps")
```