forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
205 lines (183 loc) · 6.96 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
Peer Assessment 1
========================================================
## Set working directory
```{r}
setwd("/Users/GaryLo/Desktop/Data Science Specialization/Reproducible Research/Peer Assessment 1")
```
## Loading necessary packages
```{r}
install.packages("knitr")
library(knitr)
install.packages("ggplot2")
library(ggplot2)
```
## Set Global Options
```{r setoptions}
opts_chunk$set(echo = TRUE)
```
## Downloading/Unzipping Data and checking if file exists.
```{r}
url = "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
if (file.exists("repdata-data-activity.zip")){
#skip download step
}else{
download.file(url,"repdata-data-activity.zip",method="curl")
}
if (file.exists("activity.csv")){
#skip unzip step
}else{
unzip("repdata-data-activity.zip")
}
```
## Reading in data.
```{r}
data = read.csv("activity.csv")
```
## Formating date variable to Date type and interval variable to contigous mins.
```{r}
data$date = as.Date(data$date, format = "%Y-%m-%d")
data$interval_hours = as.integer(data$interval/100)
data$interval_mins = data$interval - data$interval_hours*100
data$cont_mins = data$interval_hours*60 + data$interval_mins
```
## What's the Total/Mean/Median number of steps per day (ignoring NA values)?
```{r}
measureDF = data.frame(matrix(nrow=length(unique(data$date)),ncol=4))
names(measureDF) = c("Date","TotalSteps","Mean","Median")
measureDF$Date = unique(data$date)
measureDF$TotalSteps = 0
measureDF$Mean = 0
measureDF$Median = 0
for (i in 1:nrow(measureDF)){
subset = data[data$date == measureDF$Date[i],] #subsetting for each date.
subset = subset[!is.na(subset$steps),] #removing observations with NA steps
if (nrow(subset)==0){
#skip, all steps were NA
}else{
measureDF$TotalSteps[i] = sum(subset$steps)
measureDF$Mean[i] = mean(subset$steps)
measureDF$Median[i] = median(subset$steps)
}
}
overallMean = mean(measureDF$TotalSteps)
overallMed = median(measureDF$TotalSteps)
```
**The overall mean is `r overallMean` and the overall median is `r overallMed`.**
## Histogram of Total Steps per day
```{r, fig.height=4}
barplot(measureDF$TotalSteps,main="Histogram of Total Steps per Date",ylab = "Total Steps", xlab = "Date", names.arg=measureDF$Date)
```
## What is the daily average activity pattern?
```{r}
uniqueIntervals = unique(data$cont_mins)
activityDF = data.frame(matrix(nrow=length(uniqueIntervals), ncol = 3))
names(activityDF) = c("Intervals","TotalSteps","MeanSteps")
activityDF$Intervals = uniqueIntervals
for (i in 1:nrow(activityDF)){
subset2 = data[data$cont_mins == activityDF$Intervals[i],] #subset for intervals.
subset2 = subset2[!is.na(subset2$steps),] #removing observations with NA steps
if (nrow(subset2)==0){
#skip, all steps were NA
}else{
activityDF$TotalSteps[i] = sum(subset2$steps)
activityDF$MeanSteps[i] = mean(subset2$steps)
}
}
with(activityDF,plot(Intervals,MeanSteps,type="l",main="Plot of Interval against Mean Steps"))
highAvg = activityDF[activityDF$MeanSteps == max(activityDF$MeanSteps),]$Intervals
```
**The 5-minute interval with the highest average number of steps is: `r highAvg`**
## Total number of rows in dataset with NA's.
```{r}
numNA = nrow(data)-sum(complete.cases(data))
```
**Total number of rows in dataset with NA's: `r numNA`.**
## Imputing missing values by using mean number of steps on that interval.
```{r}
fullData = data
for (i in 1:nrow(fullData)){
if (is.na(fullData$steps[i])){
# impute value
fullData$steps[i] = activityDF[activityDF$Intervals == fullData$cont_mins[i],]$MeanSteps
}else{
# populated value - no need to impute
}
}
newNumNA = nrow(fullData)-sum(complete.cases(fullData))
```
**The new number of NA's in the imputed dataset is `r newNumNA`.**
## Histogram of Total Steps per day with fullData set (with imputed values)
```{r, fig.height=4}
fullDF = data.frame(matrix(nrow=length(unique(fullData$date)),ncol=4))
names(fullDF) = c("Date","TotalSteps","Mean","Median")
fullDF$Date = unique(fullData$date)
fullDF$TotalSteps = 0
fullDF$Mean = 0
fullDF$Median = 0
for (i in 1:nrow(fullDF)){
subset3 = fullData[fullData$date == fullDF$Date[i],] #subsetting for each date.
subset3 = subset3[!is.na(subset3$steps),] #removing observations with NA steps
if (nrow(subset3)==0){
#skip, all steps were NA
}else{
fullDF$TotalSteps[i] = sum(subset3$steps)
fullDF$Mean[i] = mean(subset3$steps)
fullDF$Median[i] = median(subset3$steps)
}
}
barplot(fullDF$TotalSteps,main="Histogram of Total Steps per Date with Imputed Values",ylab = "Total Steps", xlab = "Date", names.arg=fullDF$Date)
```
```{r}
newOverallMean = mean(fullDF$TotalSteps)
newOverallMed = median(fullDF$TotalSteps)
```
**The imputed overall mean is `r newOverallMean` and the imputed overall median is `r newOverallMed`.**
**Both the new imputed means and medians are higher than the non-imputed values.**
## Are there differences between weekends and weekdays? Creating new factor variable
```{r}
for (i in 1:nrow(fullData)){
if (weekdays(fullData$date[i])=="Saturday" | weekdays(fullData$date[i])=="Sunday"){
fullData$weekday[i] = "Weekend"
}else{
fullData$weekday[i] = "Weekday"
}
}
fullData$weekday = as.factor(fullData$weekday)
```
```{r}
fullActivityDF = data.frame(matrix(nrow=length(uniqueIntervals), ncol = 3))
names(fullActivityDF) = c("Intervals","TotalSteps","MeanSteps")
fullActivityDF$Intervals = uniqueIntervals
fullActivityWeekdayDF = fullActivityDF
fullActivityWeekendDF = fullActivityDF
for (i in 1:nrow(fullActivityWeekdayDF)){
subset4 = fullData[fullData$cont_mins == fullActivityWeekdayDF$Intervals[i],]#subset for intervals.
subset4 = subset4[subset4$weekday == "Weekday",] #subset for weekdays
subset4 = subset4[!is.na(subset4$steps),] #removing observations with NA steps
if (nrow(subset4)==0){
#skip, all steps were NA
}else{
fullActivityWeekdayDF$TotalSteps[i] = sum(subset4$steps)
fullActivityWeekdayDF$MeanSteps[i] = mean(subset4$steps)
}
}
fullActivityWeekdayDF$Weekday = "Weekday"
for (i in 1:nrow(fullActivityWeekendDF)){
subset5 = fullData[fullData$cont_mins == fullActivityWeekendDF$Intervals[i],]#subset for intervals.
subset5 = subset5[subset5$weekday == "Weekend",] #subset for weekdays
subset5 = subset5[!is.na(subset5$steps),] #removing observations with NA steps
if (nrow(subset5)==0){
#skip, all steps were NA
}else{
fullActivityWeekendDF$TotalSteps[i] = sum(subset5$steps)
fullActivityWeekendDF$MeanSteps[i] = mean(subset5$steps)
}
}
fullActivityWeekendDF$Weekday = "Weekend"
```
## Panel plot comparing weekdays and weekends.
```{r}
fullActivityAllDaysDF = rbind(fullActivityWeekdayDF,fullActivityWeekendDF)
g = ggplot(fullActivityAllDaysDF,aes(Intervals,MeanSteps))
g + geom_line() + facet_grid(Weekday~.) + labs(x="5 min Interval", y = "Number of Steps", title = "Mean number of Steps against Interval by Weekday/Weekend")
```