forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
140 lines (103 loc) · 4.6 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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
```{r}
#setwd("D:/MyDocs/Lynn/Books/coursera/Reproducible_Research/RepData_PeerAssessment1")
library(ggplot2)
library(lubridate)
library(lattice)
#library(dplyr)
library(dplyr,warn.conflicts=FALSE)
```
## Loading and preprocessing the data
```{r}
unzip("activity.zip")
data<-read.csv("activity.csv",)
data$date<-ymd(data$date)
data_noNA<-data[which(data$steps!="NA"),]
head(data_noNA)
```
## What is mean total number of steps taken per day?
1. Make a histogram of the total number of steps taken each day
```{r}
steps_per_day<-data_noNA %>%
group_by(date) %>%
summarize(steps=sum(steps))
head(steps_per_day)
ggplot(steps_per_day,aes(date,steps))+ geom_bar(stat="identity", color = "steelblue", fill = "steelblue") + labs(title = "Histogram of Total Number of Steps Taken Each Day", x = "Date", y = "Total number of steps")
```
2. Calculate and report the **mean** and **median** total number of steps taken per day
```{r}
mean_noNA<-mean(steps_per_day$steps)
median_noNA<-median(steps_per_day$steps)
mean_noNA
median_noNA
```
## What is the average daily activity pattern?
1. 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}
steps_per_interval<-data_noNA %>%
group_by(interval) %>%
summarize(steps=mean(steps))
head(steps_per_interval)
ggplot(steps_per_interval, aes(interval, steps)) + geom_line(color = "steelblue") + labs(title = "Time Series Plot of 5-minute Interval", x = "5-minute intervals", y = "Average Number of Steps")
```
2. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r}
filter(steps_per_interval,steps==max(steps))
```
## Imputing missing values
1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with `NA`s)
```{r}
sum(is.na(data))
```
2. 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.
use **mean for the 5-minute interval** as the filling of missing values.
3. Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r}
data_NA<-select(data[is.na(data$steps),],date,interval)
data_NA<-merge(data_NA,steps_per_interval,by="interval")
data_bind<-rbind(data_noNA,data_NA)
data_bind<-arrange(data_bind,date,interval)
head(data_bind)
```
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}
ggplot(data_bind, aes(date, steps)) + geom_bar(stat = "identity",color = "steelblue",fill = "steelblue") + labs(title = "Total Number of Steps Taken Each Day", x = "Date", y = "Total number of steps")
new_steps_per_day<-data_bind %>%
group_by(date) %>%
summarize(steps=sum(steps))
head(new_steps_per_day)
mean_all<-mean(new_steps_per_day$steps)
median_all<-median(new_steps_per_day$steps)
mean_all-mean_noNA
median_all-median_noNA
```
the new mean of total steps taken per day is the same as the old mean; the new median of total steps taken per day is greater than the old median.
```{r}
new_steps_per_interval<-data_bind %>%
group_by(interval) %>%
summarize(steps=mean(steps))
head(new_steps_per_interval)
new_steps_per_interval$steps-steps_per_interval$steps
```
the total daily number of steps is not affected by the imputing missing data.
## Are there differences in activity patterns between weekdays and weekends?
1. 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}
data_bind<-mutate(data_bind,wday=factor(wday(date)))
head(data_bind)
levels(data_bind$wday)<-list(weekday = c(2,3,4,5,6),weekend = c(1,7))
head(data_bind)
```
2. 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}
wday_steps_per_interval<-data_bind %>%
group_by(interval,wday) %>%
summarize(steps=mean(steps))
head(wday_steps_per_interval)
xyplot(steps~interval|wday,data=wday_steps_per_interval,layout=c(1,2),type = "l", xlab = "Interval", ylab = "Number of steps")
```