forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
128 lines (88 loc) · 5.05 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
# Reproducible Research - Assignment 1
## Loading and preprocessing the data
```{r echo = TRUE}
## unzip the data set
unzip("activity.zip")
# read the data
activitydata <- read.csv("activity.csv")
```
## What is mean total number of steps taken per day?
**1.Make a histogram of the total number of steps taken each day**
```{r fig.width=7, fig.height=6}
NumOfStepsPerDay <- aggregate(steps ~ date, data = activitydata, FUN = sum)
hist(NumOfStepsPerDay$steps, breaks = nrow(NumOfStepsPerDay), main = "Total Number of Steps Per Day", xlab = "Steps Per Day", col = "blue")
```
**2.Calculate and report the mean and median total number of steps taken per day**
```{r}
meanNumOfStepsPerDay <- mean(NumOfStepsPerDay$steps)
medianNumOfStepsPerDay <- median(NumOfStepsPerDay$steps)
```
The mean and the median total numbers of steps taken per day are **`r meanNumOfStepsPerDay`** and **`r medianNumOfStepsPerDay`**
## What is the average daily activity pattern?
```{r}
AvgPerInterval <- aggregate(steps ~ interval, data = activitydata, FUN = mean)
```
**1 Make a time series plot of the 5-minute interval and the average number of steps taken, averaged across all days**
```{r}
plot(AvgPerInterval, type = "l", main = "Average Daily Activity Pattern",
xlab = "5-minute Intervals Over Day", ylab = "Average Steps Taken Over All Days")
max.interval <- AvgPerInterval[which.max(AvgPerInterval$steps),"interval"]
```
**2.Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?**
The 5-minute interval, on average across all days, that contains the maximum number of steps is **`r max.interval `**
### Imputing missing values
**Calculate the total number of missing values in the dataset**
```{r}
total.missing <- sum(is.na(activitydata))
```
The total number of mising values is `r total.missing `
**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.**
The missing values will be imputed by replacing step NAs with the **Mean** of the 5-minute interval averaged across all days.
```{r}
# create a vector of steps with NAs replaced by imputed value (mean of 5-minute interval)
impusteps <- numeric()
for (i in 1:nrow(activitydata)) {
obs <- activitydata[i, ]
if (is.na(obs$steps)) {
steps <- subset(AvgPerInterval, interval == obs$interval)$steps
} else {
steps <- obs$steps
}
impusteps <- c(impusteps, steps)
}
```
**3. create a new dataset that is equal to the original dataset but with the missing data filled in.**
```{r}
impudata <- activitydata
impudata$steps <- impusteps
# find the total number of steps taken each day
impuStepsPerDay <- aggregate(steps ~ date, data = impudata, FUN = sum)
```
**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}
hist(impuStepsPerDay$steps, breaks = nrow(impuStepsPerDay), main = "Total Number of Steps Per Day With Imputed Values", xlab = "Steps Per Day", col = "green")
# Calculate the mean and median total number of steps taken per day
impuMeanStepsPerDay <- mean(impuStepsPerDay$steps)
impuMedianStepsPerDay <- median(impuStepsPerDay$steps)
```
The imputed mean total number of steps taken per day is **`r impuMeanStepsPerDay `**
The imputed median total number of steps taken per day is **`r impuMedianStepsPerDay `**
We can observer that by imputing the missing step values, the mean total number of steps per day remained unchanged while the median total number of steps per day changed from **`r medianNumOfStepsPerDay `** to **`r impuMedianStepsPerDay `**. The impact of the imputation was a very thin increase in the median total number of steps per day.
## 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 date is a weekday or weekend day.**
```{r}
## change date column from factor to Date
impudata$date <- as.Date(impudata$date)
weekend.days <- c("Saturday", "Sunday")
impudata$daytype <- as.factor(sapply(impudata$date, function(x) ifelse(weekdays(x) %in% weekend.days, "weekend", "weekday")))
```
**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}
require(plyr)
## Loading required package: plyr
avgsteps <- ddply(impudata, .(interval, daytype), summarize, steps = mean(steps))
require(lattice)
## Loading required package: lattice
xyplot(steps ~ interval | daytype, data = avgsteps, layout = c(1, 2), type = "l",
xlab = "5-minute Intervals Over Day", ylab = "Number of Steps", main = "Activity Patterns on Weekends and Weekdays")
```