forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
255 lines (168 loc) · 8.27 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
```
This file descripts my course project. reproducible research assignment.
##0. Install and Load Packages
```{r load,echo=TRUE,results='hide'}
library(lattice)
library(dplyr)
```
##1. Loading and preprocessing the data
1. Load the data (i.e. read.csv())
2. Process/transform the data (if necessary) into a format suitable for your analysis
###1.1 Downloading Data
we download original data from this website:
Dataset:[Activity monitoring data](https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip)[52K]
also, we can download in Rstudio console by R command:
```{r download,echo=TRUE}
## download dataset to R current work directory,filename "activity.zip"
## download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip","./acticity.zip")
## decompressed dataset to current work directory
## unzip("activity.zip",exdir=".")
## sometimes, I can't download dataset using URL, so I annotated them
## I assume that you have downloaded and decompressed the data into the current directory.
## loading dataset to R console,stored at variable "activity""
activity <- read.csv("activity.csv")
```
###1.2 Check Data
Now, we can simply check dataset:
```{r check, echo=TRUE}
dim(activity)
str(activity)
head(activity)
## change data format
activity$date <- as.Date(activity$date,"%Y-%m-%d")
## calculate NA's number in column one
sum(is.na(activity$steps))/nrow(activity)
```
##2. What is mean total number of steps taken per day?
1. Calculate the total number of steps taken per day
2. Calculate and report the mean and median of the total number of steps taken per day
###2.1 Total number of steps
Fist,we can calculate total number of steps taken each day:
```{r step/day,echo=TRUE}
fixed_data <- na.omit(activity)
step_day <- tapply(fixed_data$steps,fixed_data$date,sum)
day_names <-names(step_day)
step_day <- data.frame(step = step_day,day=day_names)
hist(step_day$step,main="Total number of steps taken each day",xlab="steps",ylab="Frequency",col="purple")
###Next, we calculate **mean** and **median**, add each lines to histgram.
## mean calculate
abline(v=mean(step_day$step),col="red")
## median calculate
abline(v=median(step_day$step),col="blue",lty=3)
## add legend descript lines
legend("topleft",legend=c("mean","median"),col=c("red","blue"),lty=c(1,3),bty="n",text.col = c("red","blue"),yjust=3)
step_mean <- mean(step_day$step)
step_median <- median(step_day$step)
step_mean
step_median
```
###2.2 Calculate mean and median value
the mean of the total number of steps taken per day is `r step_mean`.
the median of the total number of steps taken per day is `r step_median`.
##3. 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)
2. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
###3.1 Times series analysis
This section we analysis time series problem
First, we need calculate average steps for each day each interval
```{r ava_steps_each_day,echo=TRUE}
## each day records number
interval <- tapply(activity$steps,activity$interval,sum,na.rm=TRUE)
interval <- interval/61 ##normalize data,total 61 days
plot(names(interval),interval,type="l",main="average steps for each day/5 min",col="blue",lwd=2.0,xlab="One day time series/5 min",ylab="steps")
```
###3.2 Calculate specific interval
So, we can easily find which 5-minute interval contains the maximun number of steps
```{r max_step,echo=TRUE}
max_step<- which(interval == max(interval))
max_step
```
The data tells us,these two month,every day's morning 8:35 our research object had maximum number of steps:`r max_step`
##4. Imputing missing values
1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
2. Create a new dataset that is equal to the original dataset but with the missing data filled in.
3. 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?
###4.1 The total number of row with NA
NA records all comes from steps avariable. but we can use `complete.cases()` function to calculate total rows contain NA value.
```{r NA_count,echo=TRUE}
sum(!complete.cases(activity))
## Above number of day/interval contains NAs records.
```
###4.2 Refilling NA obeservations
I used that day's step mean value to refill NA oberservation
```{r NA_refill, echo=TRUE}
## calculte every day total interval records number
24*60/5
## every day we will record 288 times step
step_day2 <- tapply(activity$steps,activity$date,sum,na.rm=TRUE)
day_names2 <-names(step_day2)
step_day2 <- data.frame(step = step_day2,day=day_names2)
step_day2$average <- step_day2$step/288
## calculate mean of steps each day,stored at variable: ave_step
ave_step <- rep(step_day2$average,each=288)
refill_data <- activity
for (i in 1:nrow(refill_data)){
if (is.na(refill_data$steps[i])){
refill_data$steps[i] <- ave_step[i]
}
}
head(refill_data);tail(refill_data)
sum(!complete.cases(refill_data))
```
###4.3 Recalculte mean and median
```{r refill_data,echo=TRUE}
step_day2 <- tapply(refill_data$steps,refill_data$date,sum)
step_day2 <- data.frame(step = step_day2,day=names(step_day2))
## plotting refilled data
hist(step_day2$step,main="Total number of steps taken each day(refilled NAs)",xlab="steps",ylab="Frequency",col="purple")
## calculate mean and median
step_mean2 <- mean(step_day2$step)
step_median2 <- median(step_day2$step)
```
we can see refilled data mean=`r step_mean2`,median=`r step_median2`.
The Result shows the difference between mean value, because we delete row contains NA before,so second mean larger than first mean.
##5. 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.
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). See the README file in the GitHub repository to see an example of what this plot should look like using simulated data.
###5.1 Determine weekdays or weekends
```{r weekdays/weekends,echo=TRUE}
## created new variable contains "weekend" and"weekday" sequence
for( i in 1:nrow(refill_data)){
if (weekdays(refill_data$date[i])=="Saturday"|weekdays(refill_data$date[i])=="Sunday"){
refill_data$week[i] <- "Weekend"
}else{
refill_data$week[i] <-"Weekday"
}
}
```
###5.2 Plotting the difference
we can sovle this part problem like before.
```{r ploting,echo=TRUE}
## select specific subset data
weekend <- filter(refill_data,refill_data$week =="Weekend")
weekday <- filter(refill_data,refill_data$week =="Weekday")
wd_step <- tapply(weekend$steps,weekend$interval,sum)
wy_step <- tapply(weekday$steps,weekday$interval,sum)
## creating normalized interval steps data
wd_step <- wd_step/16
wy_step <- wy_step/45
## create data.frame format dataset
wd_step <- data.frame(steps=wd_step,interval=names(wd_step),week=rep("weekend",288))
wy_step <- data.frame(steps=wy_step,interval=names(wy_step),week=rep("weekday",288))
week_step <- rbind(wd_step,wy_step)
## change interval variable to numeric
week_step$interval <- as.character(week_step$interval)
week_step$interval <- as.numeric(week_step$interval)
## plotting by lattice package function `xyplot`
xyplot(steps~interval|week,data=week_step,type="l",layout=c(1,2),stack=TRUE,lwd=2)
```
we can find that the person's weekend is similar to the work day.
Now, we finished the course project