This repository was archived by the owner on Aug 15, 2018. It is now read-only.
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
156 lines (117 loc) · 6.25 KB
/
PA1_template.Rmd
File metadata and controls
156 lines (117 loc) · 6.25 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
##Reproducible Research: Peer Assessment 1
It is now possible to collect a large amount of data about personal movement using activity monitoring devices such as a Fitbit, Nike Fuelband, or Jawbone Up. These type of devices are part of the "quantified self" movement - a group of enthusiasts who take measurements about themselves regularly to improve their health, to find patterns in their behavior, or because they are tech geeks. But these data remain under-utilized both because the raw data are hard to obtain and there is a lack of statistical methods and software for processing and interpreting the data.
This assignment makes use of data from a personal activity monitoring device. This device collects data at 5 minute intervals through out the day. The data consists of two months of data from an anonymous individual collected during the months of October and November, 2012 and include the number of steps taken in 5 minute intervals each day.
####Loading and processing data
Using the below code ,i am going to read the csv file i download earlier
Also displayed the summary of activity data set.
```{r}
options(warn=-1)
activity=read.csv('activity.csv')
summary(activity)
```
From the sumary , we can see there are 3 variables steps ,date and interval .
Steps variable have many NA entries, which we will handle later.
#### 1. What is mean total number of steps taken per day?
<br/>
<br/>
##### a) Make a histogram of the total number of steps taken each day
```{r}
library(plyr)
Steps_Per_Day=ddply(activity,~date,summarise,steps_per_day=sum(steps))
hist(Steps_Per_Day$steps_per_day,breaks=10,col='blue',main='Histogram of Steps Per Day',xlab='Total Steps Per Day',ylab='Freq')
```
<br/>
<br/>
##### b) Calculate and report the mean and median total number of steps taken per day
```{r}
mean(Steps_Per_Day$steps_per_day,na.rm=T)
median(Steps_Per_Day$steps_per_day,na.rm=T)
```
<br/>
<br/>
<br/>
<br/>
#### 2.What is the average daily activity pattern?
<br/>
<br/>
##### a) 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}
library(chron)
Average_Steps=ddply(activity,~interval,summarise,average_steps=mean(steps,na.rm=T))
Average_Steps$time=times(0:287/288)
Average_Steps$time=strptime(Average_Steps$time,"%X")
with(Average_Steps,plot(time,average_steps,type='l',col='blue',lwd=2,main='Average Steps across All Days',xlab='Time in 24 Hour Format',ylab='Average Steps'))
```
<br/>
<br/>
##### b) Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r}
Average_Steps[which.max(Average_Steps[,2]),]$interval
```
<br/>
<br/>
<br/>
<br/>
#### 3.Imputing missing values
<br/>
<br/>
##### a) Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
From the summary we saw that only steps variable had missign values ,so we will consider only 'steps' variable to find the number of missing values
```{r}
sum(is.na(activity$steps))
```
<br/>
<br/>
##### b) 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.
Here we will choose to fill the missing values using the mean for that 5-minute interval across all days . we chose mean for 5 minute period because 5 minute interval can be compared to same 5 minute interval another day , it is apple to apple comprasion .
<br/>
<br/>
##### c) Create a new dataset that is equal to the original dataset but with the missing data filled in
Now we will create a dataset 'activity_impute' by using the strategy we deviced in previous steps
```{r}
activity_impute=activity
only_na=activity_impute[is.na(activity_impute$steps),]
merge=merge(x = only_na, y =Average_Steps , by = "interval", all.x=TRUE)
activity_impute[is.na(activity_impute$steps),]$steps=merge$average_steps
summary(activity_impute)
```
<br/>
<br/>
##### d) 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}
library(plyr)
Steps_Per_Day=ddply(activity_impute,~date,summarise,steps_per_day=sum(steps))
hist(Steps_Per_Day$steps_per_day,breaks=10,col='blue',main='Histogram of Steps Per Day',xlab='Total Steps Per Day',ylab='Freq')
```
After imputing , our mean reamins the same but median increased to 11015 from 10765
```{r}
mean(Steps_Per_Day$steps_per_day,na.rm=T)
median(Steps_Per_Day$steps_per_day,na.rm=T)
```
<br/>
<br/>
<br/>
<br/>
#### 4.Are there differences in activity patterns between weekdays and weekends?
<br/>
<br/>
##### a) 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.
we will create a new POSIXlt field from date field ,after which we will find the day using 'weekdays' function.
Finally we will create a field day_category which contains a given day is weekday or weekend
```{r}
activity_impute$date_new=strptime(activity_impute$date,"%F")
activity_impute$day_category=(weekdays(activity_impute$date_new)==('Sunday'))|(weekdays(activity_impute$date_new)==('Saturday'))
activity_impute$day_category=as.factor(activity_impute$day_category)
levels(activity_impute$day_category)=c('weekday','weekend')
```
<br/>
<br/>
##### b) 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.
```{r}
library(ggplot2)
Average_Steps=ddply(activity_impute,.(interval,day_category),summarise,average_steps=mean(steps,na.rm=T))
Average_Steps$time=times(0:287/288)
Average_Steps$time=strptime(Average_Steps$time,"%X")
p=ggplot(Average_Steps,aes(interval,average_steps))
p+geom_line(col='blue',lwd=1)+facet_grid(day_category~.)+labs(x='Interval',y='Number of Steps')
```