forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
174 lines (125 loc) · 4.93 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
---
title: 'Reproducible Research: Peer Assessment 1'
author: "Wagner Pinheiro"
date: "January, 2017"
output:
html_document:
keep_md: true
---
## Introduction
It is now possible to collect a large amount of data about personal
movement using activity monitoring devices such as a
[Fitbit](http://www.fitbit.com), [Nike
Fuelband](http://www.nike.com/us/en_us/c/nikeplus-fuelband), or
[Jawbone Up](https://jawbone.com/up). These type of devices are part of
the "quantified self" movement -- a group of enthusiasts who take
measurements about themselves regularly to improve their health, to
find patterns in their behavior, or because they are tech geeks. But
these data remain under-utilized both because the raw data are hard to
obtain and there is a lack of statistical methods and software for
processing and interpreting the data.
This assignment makes use of data from a personal activity monitoring
device. This device collects data at 5 minute intervals through out the
day. The data consists of two months of data from an anonymous
individual collected during the months of October and November, 2012
and include the number of steps taken in 5 minute intervals each day.
```{r}
# load libraries
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(dplyr))
```
## Loading and preprocessing the data
```{r}
activity_csv <- read.csv("activity.csv")
activity_csv$date = as.Date(activity_csv$date)
activity <- activity_csv[complete.cases(activity_csv),]
str(activity)
```
## What is mean total number of steps taken per day
1. histogram of the total number of steps taken each day
```{r}
total_activity_per_day <- activity %>%
group_by(date) %>%
summarise(steps = sum(steps))
ggplot(total_activity_per_day, aes(x=steps)) +
geom_histogram()
```
2. The **mean** and **median** of the total number of steps taken per day
```{r}
mean(total_activity_per_day$steps)
```
```{r}
median(total_activity_per_day$steps)
```
## What is the average daily activity pattern?
1. Time series plot of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)
```{r}
mean_activity_per_day <- activity %>%
group_by(interval) %>%
summarise(steps_avg = mean(steps))
ggplot(data=mean_activity_per_day, aes(x=interval, y=steps_avg)) +
geom_line(colour = "blue")
```
2. maximum number of steps on 5 minute interval
```{r}
mean_activity_per_day[mean_activity_per_day$steps_avg == max(mean_activity_per_day$steps_avg), ]
```
## Imputing missing values
1. total number of missing values in the dataset (i.e. the total number of rows with `NA`s)
```{r}
table(is.na(activity_csv$steps))
```
```{r}
table(is.na(activity_csv$date))
```
```{r}
table(is.na(activity_csv$interval))
```
2-3. filling in all of the missing values in the dataset with the mean of 5 min interval
```{r}
fillValue <- function(steps, interval) {
filled <- NA
if (!is.na(steps))
filled <- c(steps) else filled <- (mean_activity_per_day[mean_activity_per_day$interval == interval, "steps_avg"])
return(filled)
}
filled_activity <- activity_csv
filled_activity$steps <- mapply(fillValue, filled_activity$steps, filled_activity$interval)
filled_activity$steps <- as.integer(filled_activity$steps)
table(is.na(filled_activity))
```
4. histogram of the total number of steps taken each day with filled data
```{r}
total_filled_activity <- filled_activity %>%
group_by(date) %>%
summarise(steps = sum(steps))
ggplot(total_filled_activity, aes(x=steps)) +
geom_histogram()
```
```{r}
mean(total_filled_activity$steps)
```
```{r}
median(total_filled_activity$steps)
```
Mean and median values are higher after imputing missing data. The reason is that in the original data, there are some days with steps values NA for any interval. The total number of steps taken in such days are set to 0s by default. However, after replacing missing steps values with the mean steps of associated interval value, these 0 values are removed from the histogram of total number of steps taken each day.
## Are there differences in activity patterns between weekdays and weekends?
1. Creating 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}
activity$weekday <- weekdays(activity$date)
activity$weekday <- as.factor(activity$weekday)
levels(activity$weekday)
activity$is_weekday <- "weekday"
activity$is_weekday[activity$weekday %in% c("sábado", "domingo")] <- "weekend"
activity$is_weekday <- as.factor(activity$is_weekday)
str(activity)
```
2. time series plot 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}
mean_activity_per_weekday <- activity %>%
group_by(interval, is_weekday) %>%
summarise(steps_avg = mean(steps))
ggplot(data=mean_activity_per_weekday, aes(x=interval, y=steps_avg)) +
geom_line(colour = "blue") +
facet_grid(is_weekday ~ .)
```