forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_submit.Rmd
183 lines (122 loc) · 5.08 KB
/
PA1_submit.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
---
title: "Reproducible Research: Peer Assessment 1"
author: "Peter Dunham"
date: "February 22, 2016"
output:
html_document:
keep_md: true
---
## Loading and preprocessing the data
The raw data "activity.csv" to be included in the GitHub repo. This .csv file is to be extracted from the activity.zip file. The file can also be downloaded from: https://www.coursera.org/learn/reproducible-research/peer/gYyPt/course-project-1
Eitherway, the working directory should be set to the directory that contains "activity.csv"
After setting your working directory:
```{r}
#getwd('YOUR DIRECTORY PATH HERE')
```
Then you can load activity data using "read.csv"
```{r}
RawActivityData<-read.csv(paste0(getwd(),'/activity.csv'))
```
The following displays a small subset of the data to show how "interval" fields relate to
5 minute intervals in a 24 hour date:
```{r}
RawActivityData[861:868,]
```
## What is the average daily activity pattern?
Histogram of the total number of steps taken each day
```{r}
StepsByDay<-aggregate(list(Steps=RawActivityData$steps),list(Date=RawActivityData$date),sum)
hist(StepsByDay$Steps, col="blue", breaks=20)
```
### What is mean total number of steps taken per day?
What is mean total number of steps taken per day?
```{r}
mean(StepsByDay$Steps, na.rm=TRUE)
```
What is the median number of steps taken per day?
```{r}
median(StepsByDay$Steps, na.rm=TRUE)
```
### 5-interval activity
Make a time series plot (i.e. 𝚝𝚢𝚙𝚎 = "𝚕") of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)
```{r}
ByInterval<-aggregate(list(Steps=RawActivityData$steps),list(Interval=RawActivityData$interval),na.rm=TRUE, na.action=NULL,mean)
plot(ByInterval)
```
### The 5-minute interval that, on average, contains the maximum number of steps
Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r}
ByInterval[ByInterval$Steps == max(ByInterval$Steps),]
```
On average 8:35-8:40AM has the maximum number of streps.
## Inputing missing values
Number of missing cases:
```{r}
nrow(RawActivityData)-NROW(na.omit(RawActivityData))
```
Replacing data with mean of interval
```{r message=F, warning=F}
library(dplyr)
#Function to replace with mean of interval
myreplace<-function(interval){
as.integer(ByInterval$Steps[match(interval, ByInterval$Interval)])
}
FilledActivityData<-mutate(RawActivityData,steps = ifelse(!complete.cases(RawActivityData),myreplace(RawActivityData$interval), RawActivityData$steps))
```
This shows a small subset of filled data.
```{r}
FilledActivityData[2250:2260,]
```
### Histogram of the total number of steps taken each day after missing values are inputed
```{r, message=F, warning=F}
library(ggplot2)
#RawDataActivity with set marked as missing to show it has not been filled
ByDateMissing<-aggregate(list(Steps=RawActivityData$steps),list(Date=RawActivityData$date),sum)
ByDateMissing$set<-'missing'
#Filled DataActivity had NA data filled with mean of interval
ByDateFilled<-aggregate(list(Steps=FilledActivityData$steps),list(Date=FilledActivityData$date),sum)
ByDateFilled$set<-'filled'
compare<-rbind(ByDateFilled,ByDateMissing)
ggplot(compare, aes(Steps, fill = set)) + geom_histogram(alpha = 0.5, aes(y = ..density..), position = 'identity')
```
What is mean total number of steps taken per day?
```{r}
mean(ByDateFilled$Steps, na.rm=TRUE)
```
This number is different from omitted NA data by:
```{r}
mean(ByDateFilled$Steps, na.rm=TRUE)-mean(ByDateMissing$Steps, na.rm=TRUE)
```
What is the median number of steps taken per day?
```{r}
median(ByDateFilled$Steps, na.rm=TRUE)
```
This number is different from omitted NA data by:
```{r}
median(ByDateFilled$Steps, na.rm=TRUE)-median(ByDateMissing$Steps, na.rm=TRUE)
```
## Are there differences in activity patterns between weekdays and weekends?
```{r}
library(dplyr)
library(lattice)
#Function to tag a date as a "weekend" or "weekday"
daytype<-function(daystring)
{
weekend<-c("Saturday","Sunday")
ifelse(is.element(as.character(daystring),weekend),"weekend","weekday")
}
#converting date fields to "Date" data type
ByDateFilled$Date<-as.Date(ByDateFilled$Date)
FilledActivityData$date<-as.Date(FilledActivityData$date)
DayAddedActivityData<-mutate(FilledActivityData, DayOfWeek = weekdays(FilledActivityData$date))
DayAddedActivityData<-mutate(DayAddedActivityData,WeekType=daytype(DayAddedActivityData$DayOfWeek))
DayCompare<-aggregate(data=DayAddedActivityData,list(Steps=DayAddedActivityData$steps),list(days=DayAddedActivityData$WeekType, interval=DayAddedActivityData$interval),na.rm=TRUE, na.action=NULL,mean)
#foprinting debug
#weekdays(ByDateFilled$Date)
#Adding a DayOfWeek column with "weekend" or "weekday" tag
ByDateFilled<-mutate(ByDateFilled, DayOfWeek = weekdays(ByDateFilled$Date))
##ByDay
MeanByDayOfWeek<-aggregate(list(Steps=ByDateFilled$Steps),list(Days=ByDateFilled$DayOfWeek),na.rm=TRUE, na.action=NULL,mean)
DataWithDayType<-mutate(ByDateFilled, WeekType=daytype(ByDateFilled$DayOfWeek))
xyplot(Steps ~ interval | days, type="l",data = DayCompare, layout = c(1, 2))
```