forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
101 lines (89 loc) · 4.54 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
---
title: "Peer Assignment 1"
output:
html_document:
fig_width: 5
---
##Loading and preprocessing the data
1. Load the data
```{r chunk1.1}
data <-read.csv("activity.csv")
```
2. Process/transform the data (if necessary) into a format suitable for your analysis
```{rchunk1.2}
totalStepsperDay <- aggregate(steps ~ date, data = data, sum, na.rm=TRUE)
```
##What is mean total number of steps taken per day?
1. Make a histogram of the total number of steps taken each day
```{rchunk2.1}
hist(totalStepsperDay$steps, col="red", xlab="Total number of steps per day",
main="Histogram of Total number of steps per day")
```
2.Calculate and report the mean and median total number of steps taken per day
```{rchunk2.2}
MeanSteps <- mean(totalStepsperDay$steps)
MedianSteps<-median(totalStepsperDay$steps)
```
The mean total number of steps taken per day is 10766 steps.
The median total number of steps taken per day is 10765 steps.
##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)
```{rchunk3.1}
avgSteps <-aggregate(steps ~ interval, data = data, mean, na.rm=TRUE)
plot(avgSteps$interval, avgSteps$steps, type = "l", xlab = "5-minutes Time Intervals",ylab = "Average number of steps taken", main = "Average number of Steps Taken at 5 minute Intervals",col = "red")
```
2.Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
``` {r chunk3.2}
maxInterval <-avgSteps[which.max(avgSteps$steps), ]$interval
```
It is the interval 835.
##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)
```{rchunk4.1}
missingVal <-sum(is.na(data))
```
There are 2034 NA values.
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.
```{rchunk4.2}
FillSteps <-function(interval) {
avgSteps[avgSteps$interval ==interval,]$steps
}
```
3.Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{rchunk4.3}
dataFilled <-data
count = 0
for (i in 1:nrow(dataFilled)) {
if (is.na(dataFilled[i, ]$steps)) {
dataFilled[i,]$steps<- FillSteps(dataFilled[i,]$interval)
count =count + 1
}
}
cat("Total ", count, "NA values were filled.\n")
```
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.
```{r chunk 4.4}
totalStepsperDayFilled <- aggregate(steps ~ date, data = dataFilled, sum, na.rm=TRUE)
hist(totalStepsperDayFilled$steps, col="red", xlab="Total number of steps per day",
main="Histogram of Total number of steps per day")
MeanStepsFilled <- mean(totalStepsperDayFilled$steps)
MedianStepsFilled<-median(totalStepsperDayFilled$steps)
```
Both the mean and median values is 10766 steps 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?
The **mean value** is the same as the value before the missing data is replaced, because the strategy relies on mean value of the steps for the particular 5-min interval. The median value is slighlt higher, but it depends on the distribution of the misisng values
##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 chunk5.1}
weekdays <- c("Monday", "Tuesday", "Wednesday", "Thursday",
"Friday")
dataFilled$day = as.factor(ifelse(is.element(weekdays(as.Date(dataFilled$date)),weekdays), "Weekday", "Weekend"))
```
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.
``` {r chunk 5.2}
avgStep2 <- aggregate(steps ~ interval + day, data = dataFilled, mean)
library(lattice)
xyplot(avgStep2$steps ~ avgStep2$interval|avgStep2$day, data = dataFilled,
main="Average Steps per Day by Interval",xlab="Interval", ylab="Steps",
layout=c(1,2), type="l")
```