forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
181 lines (131 loc) · 6.86 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
Reproducible Research: Peer Assessment 1
==============================================================================================
Julie L. Steyn
This document uses the activity dataset which can be found at https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip contained in this directory, and assumes the dataset has been unzipped into the current working directory.
### Loading and preprocessing the data
```{r loaddata, echo =TRUE}
# load the dataset
activity <- read.csv("activity.csv")
head(activity)
str(activity)
```
```{r formatdate, echo =TRUE}
# format the date column
activity$date <- as.Date(activity$date, format = "%Y-%m-%d")
```
```{r processdata, echo =TRUE}
# process the data
# total steps per day
total_steps <- aggregate(activity$steps, by = list(activity$date), sum)
names(total_steps)[1] <- "date"
names(total_steps)[2] <- "steps"
# total steps per interval
total_interval <- aggregate(activity$steps, by = list(activity$interval), sum, na.rm = TRUE)
names(total_interval)[1] <- "interval"
names(total_interval)[2] <- "steps"
# mean steps per interval
mean_interval <- aggregate(activity$steps, by = list(activity$interval), mean, na.rm = TRUE)
names(mean_interval)[1] <- "interval"
names(mean_interval)[2] <- "steps"
```
### 1. What is mean total number of steps taken per day?
#### 1.1 Make a histogram of the total number of steps taken each day
```{r histogram1, echo =TRUE}
hist(total_steps[,2], main = "Histogram of the total number of steps taken each day (ignoring NA)",
xlab = "total steps taken each day", col="#CCCCFF")
```
#### 1.2 Calculate and report the mean and median total number of steps taken per day
```{r meanmedian1, echo =TRUE}
calcmean <- mean(total_steps[,2], na.rm = TRUE)
calcmean
calcmedian <- median(total_steps[,2], na.rm = TRUE)
calcmedian
```
The mean is `r calcmean` and the median is `r calcmedian`.
### 2. What is the average daily activity pattern?
#### 2.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)
```{r timeseries1, echo =TRUE}
plot(mean_interval$interval, mean_interval$steps, type = "l", xlab="5-minute interval", ylab="average number of steps taken")
```
#### 2.2 Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r maxinterval, echo =TRUE}
maxsteps <- max(mean_interval$steps)
maxsteps
whichinterval <- mean_interval[which.max(mean_interval$steps), 1]
whichinterval
```
The 5-minute interval containing the maximum number of steps is `r whichinterval` and the maximum number of steps is `r maxsteps`.
### 3. Imputing missing values
#### 3.1 Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
```{r howmanymissing, echo =TRUE}
nummissing <- sum(is.na(activity$steps))
nummissing
```
The number of missing values is `r nummissing`.
#### 3.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.
Using the mean for the interval as a replacement for missing values:
```{r replacementmean, echo =TRUE}
# new df with new column of mean per interval
new_activity <- merge(activity, mean_interval, by = "interval", sort = FALSE)
# sort on date and interval
new_activity <- new_activity[with(new_activity, order(date, interval)), ]
# assign names to columns
names(new_activity)[2] <- "steps"
names(new_activity)[4] <- "mean.steps"
# replace NA with mean.steps
new_activity$steps[is.na(new_activity$steps)] <- new_activity$mean.steps[is.na(new_activity$steps)]
# round-off number of steps to 0 decimal places
new_activity$steps <- round(new_activity$steps, digits = 0)
# remove the mean.steps column
new_activity$mean.steps <- NULL
```
#### 3.3 Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r missingvalues, echo =TRUE}
new_activity <- new_activity[, c(2, 3, 1)]
head(new_activity)
```
#### 3.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 histogram2, echo =TRUE}
new_total_steps <- aggregate(new_activity$steps, by = list(new_activity$date), sum)
names(new_total_steps)[1] <- "date"
names(new_total_steps)[2] <- "steps"
hist(new_total_steps[,2], main = "Histogram of the total number of steps taken each day (replacing NA)", xlab = "total steps taken each day", col="#CCCCFF")
```
Comparison of both Histograms:
```{r histogramcompare, echo =TRUE}
par(mfrow = c(1, 2))
hist(total_steps$steps, main = "(with NA)", xlab = "total number of steps taken each day", col="#CCCCFF")
hist(new_total_steps$steps, main = "(NA replaced)", xlab = "total number of steps taken each day", col="#CCCCFF")
#na.rm is now redundant since all NA have been replaced
calcnewmean <- mean(new_total_steps[,2], na.rm = TRUE)
calcnewmean
#na.rm is now redundant since all NA have been replaced
calcnewmedian <- median(new_total_steps[,2], na.rm = TRUE)
calcnewmedian
```
The mean is `r calcnewmean` and the median is `r calcnewmedian`.
### 4. Are there differences in activity patterns between weekdays and weekends?
#### 4.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 newfactors, echo =TRUE}
# new factor column with name of day of the week
new_activity$weekdays <- factor(format(new_activity$date, "%A"))
# to view the values of the levels
levels(new_activity$weekdays)
# replace the levels with weekday or weekend
levels(new_activity$weekdays) <- list(weekday = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"), weekend = c("Saturday", "Sunday"))
```
#### 4.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 panelplot, fig.width=7, fig.height=6, echo =TRUE}
# new dataset with mean across intervals
new_mean_interval <- aggregate(new_activity$steps, by = list(new_activity$weekdays, new_activity$interval), mean, na.rm = TRUE, na.action = NULL)
# assign names to columns
names(new_mean_interval)[1] <- "weekday"
names(new_mean_interval)[2] <- "interval"
names(new_mean_interval)[3] <- "mean.steps"
# new dataset
head(new_mean_interval)
# panel plot of new dataset library(lattice)
library(lattice)
xyplot(new_mean_interval$mean.steps ~ new_mean_interval$interval |
new_mean_interval$weekday, layout = c(1, 2), type = "l", xlab = "Interval", ylab = "Number of steps")
```