forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
136 lines (104 loc) · 4.63 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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
```{r}
library(ggplot2)
library(scales)
library(Hmisc)
```
## 1) Loading and preprocessing the data
```{r,echo=TRUE}
setwd("~/GitHub/RepData_PeerAssessment1")
if(!file.exists('activity.csv')){
unzip('./repdata-data-activity.zip')
}
activityData <- read.csv('activity.csv',header=T,sep=",")
str(activityData)
```
### Process/transform the date field in Activity monitoring data into a date format suitable for your analysis
```{r,echo=TRUE}
activityData$date<-as.Date(activityData$date)
str(activityData)
```
## 2) What is mean total number of steps taken per day?
### i) Make a histogram of the total number of steps taken each day
```{r,results='hide'}
activityDatasteps<-tapply(activityData$steps,activityData$date,sum,na.rm=TRUE)
```
```{r,echo=TRUE}
library(reshape2)
activityDatamelt<-melt(activityDatasteps)
names(activityDatamelt)<-c('Date','SumofSteps')
head(activityDatamelt)
```
```{r,echo=TRUE}
hist(activityDatamelt$SumofSteps,main="Histogram of Total Number of Steps per Day",xlab="Total Number of Steps per Day",ylab="Frequency",col='blue',breaks=30)
```
### ii) Calculate and report the mean and median total number of steps taken per day
```{r,echo=TRUE}
mean(activityDatamelt$SumofSteps,na.rm=T);median(activityDatamelt$SumofSteps,na.rm=T)
```
## 3) What is the average daily activity pattern?
### i) 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,echo=TRUE}
activityDataavg<-tapply(activityData$steps,activityData$interval,mean,na.rm=T)
activityDatamelt_avg<-melt(activityDataavg)
names(activityDatamelt_avg)<-c("interval","avg")
nrow(activityDatamelt_avg)
plot(avg~interval,data=activityDatamelt_avg,type="l",main="Average daily activity pattern")
```
### ii) which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r,echo=TRUE}
activityDatamelt_avg[activityDatamelt_avg$avg==max(activityDatamelt_avg$avg),]
```
## 4) Imputing missing values
### i) Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
```{r,echo=TRUE}
colSums(is.na(activityData))
```
### ii) 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.
```{r,echo=TRUE}
mean(activityData$steps,na.rm=T)
```
#### iii) Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r,echo=TRUE}
activityDataimpute<-activityData
activityDataimpute$steps[is.na(activityDataimpute$steps)]<-mean(activityDataimpute$steps,na.rm=T)
colSums(is.na(activityDataimpute))
```
### iv) 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,results='hide'}
activityDataimputesteps<-tapply(activityDataimpute$steps,activityDataimpute$date,sum)
```
```{r,echo=TRUE}
library(reshape2)
activityDatameltimpute<-melt(activityDataimputesteps)
names(activityDatameltimpute)<-c('Date','SumofSteps')
head(activityDatameltimpute)
```
```{r,echo=TRUE}
hist(activityDatameltimpute$SumofSteps,main="Histogram of Total Number of Steps per Day on Impute Value",xlab="Total Number of Steps per Day",ylab="Frequency",col='blue',breaks=30)
```
### ii) Calculate and report the mean and median total number of steps taken per day
```{r,echo=TRUE}
mean(activityDatameltimpute$SumofSteps,na.rm=T);median(activityDatameltimpute$SumofSteps,na.rm=T)
```
#### (Mean = Median) after imputing missing value with mean value of steps. Now it became less skewed.
## 5) Are there differences in activity patterns between weekdays and weekends?
```{r,echo=TRUE}
activityDataimpute$weekdays<-weekdays(activityDataimpute$date)
activityDataimpute$weeks[(activityDataimpute$weekdays=="Saturday" | activityDataimpute$weekdays=="Sunday")]<-"weekend"
activityDataimpute$weeks[!(activityDataimpute$weekdays=="Saturday" | activityDataimpute$weekdays=="Sunday")]<-"weekdays"
```
```{r,echo=TRUE}
library(plyr)
week_comp<- ddply(activityDataimpute, c("interval","weeks"), function (x) apply(x[1], 2, mean))
head(week_comp)
```
```{r,echo=TRUE}
library(lattice)
xyplot(steps~interval | weeks,data=week_comp,type="l",xlab="Interval", ylab="Number of steps",layout=c(1,2))
```