forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
179 lines (130 loc) · 5.66 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
# Activity Statistics
## Getting the data
**<span style="color:blue">1 - Code for reading in the dataset and processing the data</span>**
Let's first download and read the data into a data frame
```{r read}
temp <- tempfile()
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip",temp)
activity <- read.csv(unz(temp, "activity.csv"))
unlink(temp)
```
Some fields have undesired data format:
```{r str}
str(activity)
```
Let's convert *interval* variable into factor and create a variable *hour* based on intervals:
```{r reformat}
library(plyr)
activity$hour<-as.factor(round_any((as.numeric(activity$interval)+1)/100, 1, f = ceiling)) # create a variable for each hour
activity$interval<-as.factor(activity$interval)
```
## Exploratory Data Analysis
**<span style="color:blue">2 - Histogram of the total number of steps taken each day</span>**
```{r stepseachday}
library(ggplot2)
ggplot(data = activity,
aes(as.Date(as.character(activity$date), "%Y-%m-%d"), steps)) +
stat_summary(fun.y = sum, # adds up all observations
geom = "bar") +
xlab ("Dates") +
ggtitle("Total number of steps taken each day")
```
**<span style="color:blue">3 - Mean and median number of steps taken each day</span>**
Let's create a data frame with means and medians for each day (keeping NAs for now)
```{r means_medians}
means<-ddply(activity, "date", summarise, mean = mean(steps))
medians<-ddply(activity, "date", summarise, median = median(steps))
means_medians<-merge(means,medians)
```
All medians are 0:
```{r print2, echo=FALSE}
print(means_medians)
```
That happens because there are no steps in more than 50% of intervals.
**<span style="color:blue">4 - Time series plot of the average number of steps taken</span>**
Let's visualize avarage number of steps taken (ignoring NAs for now):
```{r timeseries}
ggplot(means_medians, aes(y=mean, x=as.Date(as.character(means_medians$date), "%Y-%m-%d"))) +
geom_bar(stat='identity') +
ylab ("Average daily steps") +
xlab ("Dates") +
ggtitle("Average number of steps taken")
```
**5 - The 5-minute interval that, on average, contains the maximum number of steps</span>**
```{r meansbyintervals}
meansbyintervals<-ddply(activity, "interval", summarise, mean = mean(steps,na.rm = TRUE))
meansbyintervals<-arrange(meansbyintervals, desc(mean))
meansbyintervals[1,]
```
The most walkable interval is 8:35am
## Navigating Missing Values
**<span style="color:blue">6 - Code to describe and show a strategy for imputing missing data</span>**
Let's see at the missing values
```{r}
NAs <- is.na(activity$steps)
sum(NAs, na.rm = T)
mean(NAs, na.rm = T)
```
13% is a rather high number. Let's see if there any particular days or intervals where there are missing values:
```{r}
dates <- activity$date
dates <- as.Date(as.character(dates), "%Y-%m-%d")
hist(dates[NAs], "day", , xlab = "Date"
, main = "Histogram: what dates are missing")
```
Sounds like that missing values are spread between 8 out of 61 days. Not too bad. Let's remove these observations:
```{r}
completeactivity <- activity[complete.cases(activity), ]
```
**<span style="color:blue">7 - Histogram of the total number of steps taken each day after missing values are imputed</span>**
The plot obviously doesn't change much:
```{r}
ggplot(data = completeactivity,
aes(as.Date(as.character(completeactivity$date), "%Y-%m-%d"), steps)) +
stat_summary(fun.y = sum,
geom = "bar") +
xlab ("Dates") +
ggtitle("total number of steps taken each day after missing values are imputed")
```
**<span style="color:blue">8 - Panel plot comparing the average number of steps taken per 5-minute interval across weekdays and weekends</span>**
Let's first do some formatting and define weekdays and weekends
```{r}
library(timeDate)
completeactivity$date<-as.Date(as.character(completeactivity$date), "%Y-%m-%d")
completeactivity$weekday<-isWeekday(completeactivity$date)
completeactivity$weekday[completeactivity$weekday==TRUE] <- "Weekday"
completeactivity$weekday[completeactivity$weekday==FALSE] <- "Weekend"
completeactivity$weekday<-as.factor(completeactivity$weekday)
```
Create a new data frame with means by weekdays and weekends and by hourly intervals:
```{r}
completeactivitymeans<-aggregate(completeactivity$steps
, by=list(completeactivity$hour,completeactivity$weekday)
, FUN=mean)
names(completeactivitymeans)<-c("hour","day","steps")
```
Finally, build a plot of means:
```{r}
ggplot(data = completeactivitymeans,
aes(hour, steps)) +
geom_point(aes(color=hour)) +
facet_grid(day~.) +
ggtitle("Means by hourly intervals")
```
And other statistics:
```{r}
ggplot(data = completeactivity,
aes(hour, steps)) +
geom_boxplot(aes(color=hour)) +
facet_grid(weekday~.) +
ggtitle("Statistics by hourly intervals")
```
## Summary
**Weekday and Weekend patters are different**
- Wakes up later on weekends
- Average amount of steps per interval is higher during weekends
- Walking on weekends is more popular
**Other data caviats and findings**
- 8 days are missing oobeservations (forgot to track?)
- The excercise showed that exploring daily aggregated or hourly aggregated data would make more sense - interval data is skewed because there are a lot of 0 steps as well as intervals with very high numbers of steps
**Thank you for the review!**