forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
206 lines (148 loc) · 7.84 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
# Reproducible Research: Peer Assessment 1
## Loading and preprocessing the data
Note: This program assumes that the data is located in the working directory
The data is initially loaded and appended with factor and index columns.
```{r echo=TRUE}
activities <- read.csv("activity.csv")
# Rename the date column to datefactor to indicate that it is a factor variable
names(activities)[2] <- "datefactor"
# Create a Date column with the Date class type
date <- as.Date(as.character(activities$datefactor))
activities <- cbind(activities, date)
# Create a column to indicate the day of the week
dayofweek <- weekdays(activities$date)
dayofweekfactor <- as.factor(dayofweek)
activities <- cbind(activities, dayofweekfactor)
# Create a daytype factor column to indicate a weekday or weekend
weekendTF <- weekdays(activities$date)=="Sunday" | weekdays(activities$date)=="Saturday"
weekendday <- dayofweek
weekendday[weekendTF] <- "weekend"
weekendday[weekendTF==FALSE] <- "weekday"
daytypefactor <- as.factor(weekendday)
activities <- cbind(activities, daytypefactor)
# Create a factor column for the intervals. This will be used to split the data.
intervalfactor <- as.factor(activities$interval)
activities <- cbind(activities, intervalfactor)
# Create a column that indexes the 5-minute interval
intervalindex <- as.numeric(intervalfactor)
activities <- cbind(activities, intervalindex)
# tidy up the data set
activities <- data.frame(
steps=activities$steps,
date=activities$date,
datefactor=activities$datefactor,
dayofweekfactor=activities$dayofweekfactor,
daytypefactor=activities$daytypefactor,
interval=activities$interval,
intervalfactor=activities$intervalfactor,
intervalindex=activities$intervalindex)
# Inspect the resulting data set
head(activities)
```
## What is mean total number of steps taken per day?
The data is split by date. Then a vector is created to hold the total steps by date.
```{r echo=TRUE}
activities_steps_splitbydate <- with(activities, split(steps, datefactor))
activities_steps_splitbydate_total <- sapply(activities_steps_splitbydate, sum)
```
This histogram shows the frequency distribution of steps per day.
```{r,stepsperdayhistogram}
hist(activities_steps_splitbydate_total, xlab="Steps per day", main="Histogram of Steps per day", breaks=10)
activities_steps_splitbydate_total_df <-
data.frame(total=activities_steps_splitbydate_total)
print (activities_steps_splitbydate_total_df)
```
Finally, calculate the mean and median steps per day. Ignore any NAs.
```{r, echo=TRUE}
activities_steps_splitbydate_mean = format(mean(activities_steps_splitbydate_total_df$total, na.rm=TRUE), digits=6, nsmall=2)
activities_steps_splitbydate_median = median(activities_steps_splitbydate_total_df$total, na.rm=TRUE)
```
The mean steps per day is `r activities_steps_splitbydate_mean`. The median steps per day is `r activities_steps_splitbydate_median`. The distribution is fairly symmetric.
## What is the average daily activity pattern?
Split the data by time interval and calculate the mean for each interval.
```{r echo=TRUE}
# Split by interval
activities_steps_splitbyinterval <- with(activities,split(steps, intervalfactor))
# Calculate each interval's mean.
activities_steps_splitbyinterval_mean <- sapply(activities_steps_splitbyinterval, mean, na.rm=TRUE)
# Inspect the data.
head(activities_steps_splitbyinterval_mean)
```
The name of each value in the mean vector corresponds to the time of day (e.g., 0=midnight, 100=1:00 AM, 1300=1:00 PM, etc.).
Plot the average number of steps as a time series. Since, the time series must be over contiguous segments, the x-axis is represented by the interval index.
```{r echo=TRUE, averagestepsbytimeinterval}
plot(activities_steps_splitbyinterval_mean, type='l',main="Average Steps by Time Interval", xlab="Time Interval", ylab='Average Steps')
```
Find the interval that is represented by the peak in the above graph.
```{r echo=TRUE}
maxindex_steps_splitbyinterval_mean <- which(activities_steps_splitbyinterval_mean==max(activities_steps_splitbyinterval_mean))
maxtimeinterval <- names(maxindex_steps_splitbyinterval_mean)[1]
```
The most activity occurred at the `r maxindex_steps_splitbyinterval_mean` interval which corresponds to the `r maxtimeinterval` time interval.
## Imputing missing values
Find the number of records that are missing steps.
```{r echo=TRUE}
# Total number of missing values
missingsteps <- is.na(activities$steps)
missingstepscount <- sum(missingsteps)
```
There are `r missingstepscount` observations in which the step count is missing.
The step count will be imputed by using the average number of steps in the same time interval in the other days of the study. A column titled 'imputedsteps' will be added to the data frame.
```{r echo=TRUE}
# Create the imputed steps based on the average steps in each interval
imputedsteps <- activities$steps
activities <- cbind(activities, imputedsteps)
activities$imputedsteps[missingsteps] <-
activities_steps_splitbyinterval_mean[activities$intervalfactor[missingsteps]]
# Analyze the impact of the imputed steps by splitting across days again.
activities_imputedsteps_splitbydate <- with(activities, split(imputedsteps, datefactor))
activities_imputedsteps_splitbydate_total <- sapply(activities_imputedsteps_splitbydate, sum)
```
This histogram shows the frequency distribution of steps per day, including the imputed steps.
```{r echo=TRUE,imputedstepsperdayhistogram}
hist(activities_imputedsteps_splitbydate_total, xlab="Imputed Steps per day", main="Histogram of Imputed Steps per day", breaks=10)
activities_imputedsteps_splitbydate_total_df <-
data.frame(total=activities_imputedsteps_splitbydate_total)
print (activities_imputedsteps_splitbydate_total_df)
```
Finally, calculate the mean and median steps per day. This time there are no NAs to ignore.
```{r, echo=TRUE}
activities_imputedsteps_splitbydate_mean = format(mean(activities_imputedsteps_splitbydate_total), digits=6, nsmall=2)
activities_imputedsteps_splitbydate_median = format(median(activities_imputedsteps_splitbydate_total), digits=6, nsmall=2)
```
The mean steps per day is `r activities_imputedsteps_splitbydate_mean`. The median steps per day is `r activities_imputedsteps_splitbydate_median`. The mean did not change, but now the median is more in line with the mean resulting in a distribution that is now even more symmetric.
## Are there differences in activity patterns between weekdays and weekends?
subset the data into two data frames, one for weekend dates and the other for weekend dates and then split each by the time interval.
```{r}
# Split each subset by the time interval.
activities_imputedsteps_splitbyinterval_weekend <-
with(subset(activities, daytypefactor=="weekend")
,split(imputedsteps, intervalfactor))
activities_imputedsteps_splitbyinterval_weekday <-
with(subset(activities, daytypefactor=="weekday")
,split(imputedsteps, intervalfactor))
# Calculate the mean within each data frame.
activities_imputedsteps_splitbyinterval_weekend_mean <-
sapply(activities_imputedsteps_splitbyinterval_weekend, mean)
activities_imputedsteps_splitbyinterval_weekday_mean <-
sapply(activities_imputedsteps_splitbyinterval_weekday, mean)
```
Plot the average number of steps in each time interval for each data frame.
```{r,imputedstepspertimeinterval}
par(mfrow=c(2,1))
plot(activities_imputedsteps_splitbyinterval_weekend_mean,
type='l',
main="weekend",
xlab="Time Interval",
ylab='Average Imputed Steps',
ylim=c(0,250)
)
plot(activities_imputedsteps_splitbyinterval_weekday_mean,
type='l',
main="weekday",
xlab="Time Interval",
ylab='Average Imputed Steps',
ylim=c(0,250)
)
```
Notice that the weekend activity has peaks throughout the day, but the weekday activity tends to consolidate more activity earlier in the day.