forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
127 lines (85 loc) · 5.13 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
---
title: "Reproducible Research: Peer Assessment 1"
author: "Erica Pehrsson"
date: "7/22/2018"
output:
html_document:
keep_md: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Load libraries
```{r}
library(plyr)
library(reshape2)
library(ggplot2)
```
# Loading and preprocessing the data
The variables included in this dataset are:
+ steps: Number of steps taken in a 5-minute interval (missing values are coded as NA)
+ date: The date on which the measurement was taken in YYYY-MM-DD format
+ interval: Identifier for the 5-minute interval in which measurement was taken
The dataset is stored in a comma-separated-value (CSV) file, and there are a total of 17,568 observations in this dataset.
## Code for reading in the dataset and/or processing the data
```{r}
activity = read.table(unz("activity.zip", "activity.csv"), header=T, sep=",")
summary(activity)
```
# What is mean total number of steps taken per day?
For this part of the assignment, you can ignore the missing values in the dataset.
## Total number of steps taken per day
```{r}
activity_total = ddply(activity,.(date),summarise,Total=sum(steps))
```
## Histogram of the total number of steps taken each day
```{r}
ggplot(activity_total,aes(x=Total)) + geom_histogram() + xlab("Total steps per day") + ylab("Days")
```
## Mean and median total number of steps taken each day
```{r}
ddply(activity_total,.(), summarise,Median=median(na.omit(Total)),Mean=mean(na.omit(Total)))[2:3]
```
# What is the average daily activity pattern?
## Time series plot of the average number of steps taken
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}
activity_interval = ddply(activity,.(interval),summarise,Steps=mean(na.omit(steps)))
ggplot(activity_interval,aes(x=interval,y=Steps)) + geom_line() + xlab("Interval") + ylab("Mean steps")
```
## The 5-minute interval that, on average across all the days in the dataset, contains the maximum number of steps
```{r}
activity_interval[which.max(activity_interval$Steps),]
```
# Imputing missing values
Note that there are a number of days/intervals where there are missing values (coded as NA). The presence of missing days may introduce bias into some calculations or summaries of the data.
## Code to describe and show a strategy for imputing missing data
Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
```{r}
dim(activity[which(is.na(activity$steps)),])[1]
```
Devise a strategy for filling in all of the missing values in the dataset. The strategy does not need to be sophisticated. For example, you could use the mean/median for that day, or the mean for that 5-minute interval, etc.
Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r}
activity_impute = merge(activity,activity_interval,all.x=TRUE,by="interval")
activity_impute$steps = ifelse(is.na(activity_impute$steps),activity_impute$Steps,activity_impute$steps)
activity_impute = activity_impute[1:3]
```
## Histogram of the total number of steps taken each day after missing values are imputed
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}
activity_impute_total = ddply(activity_impute,.(date),summarise,Total=sum(steps))
ggplot(activity_impute_total,aes(x=Total)) + geom_histogram() + xlab("Total steps per day") + ylab("Days")
ddply(activity_impute_total,.(), summarise,Median=median(na.omit(Total)),Mean=mean(na.omit(Total)))[2:3]
```
Imputing data slightly increases the median total number of steps per day, although the mean remains unchanged.
# Are there differences in activity patterns between weekdays and weekends?
Use the dataset with the filled-in missing values for this part. 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.
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). See the README file in the GitHub repository to see an example of what this plot should look like using simulated data.
## Panel plot comparing the average number of steps taken per 5-minute interval across weekdays and weekends
```{r}
activity_impute$Weekday = factor(ifelse(weekdays(as.Date(activity_impute$date)) %in% c("Saturday","Sunday"),"weekend","weekday"),levels=c("weekday","weekend"))
activity_impute_interval = ddply(activity_impute,.(interval,Weekday),summarise,Steps=mean(na.omit(steps)))
ggplot(activity_impute_interval,aes(x=interval,y=Steps)) + geom_line() + facet_wrap(~Weekday) + xlab("Interval") + ylab("Mean steps")
```
Activity begins more gradually on weekends and does not include an interval of particularly high activity in the morning.