forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
164 lines (127 loc) · 6.85 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
---
title: "Reproducible Research: Peer Assessment 1"
author: Antonio Torres
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, Nike Fuelband, or Jawbone 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.
The data for this assignment can be downloaded from the course web site:
* Dataset: [Activity monitoring data](https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip)
The variables included in this dataset are:
* steps: Number of steps taking in a 5-minute interval (missing values are coded as `NA`)
* date: The date on which the measurement was taken in YYYY-MM-DD format
* interval: Identifier for the 5-minute interval in which measurement was taken
The dataset is stored in a comma-separated-value (CSV) file and there are a total of 17,568 observations in this dataset.
## Loading and preprocessing the data
Show any code that is needed to
1. Load the data (i.e. `read.csv()`)
```{R echo = TRUE}
unzip("./activity.zip")
data <- read.csv("./activity.csv")
```
2. Process/transform the data (if necessary) into a format suitable for your analysis
```{R echo = TRUE}
data$date <- as.Date(data$date, "%Y-%m-%d")
head(data, 5)
```
## What is mean total number of steps taken per day?
1. Calculate the total number of steps taken per day
```{R echo = TRUE}
sums_day <- tapply(data$steps, data$date, sum)
sums_day <- cbind.data.frame(unique(data$date), sums_day)
names(sums_day) <- c("date", "sum")
head(sums_day, 5)
```
2. If you do not understand the difference between a histogram and a barplot, research the difference between them. Make a histogram of the total number of steps taken each day
```{R echo = TRUE}
library(ggplot2)
g <- ggplot(sums_day, aes(x = sum))
g + geom_histogram(fill = "red", bins = 20) + labs(title = "Daily steps", x = "Steps", y = "Frequency")
```
3. Calculate and report the mean and median of the total number of steps taken per day
```{R echo = TRUE}
mean1 <- mean(sums_day$sum, na.rm = TRUE)
mean1
```
```{R echo = TRUE}
median1 <- median(sums_day$sum, na.rm = TRUE)
median1
```
## What is the average daily activity pattern?
1. Make a time series plot (i.e. `type = "l"`) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)
```{R echo = TRUE}
mean_interval <- tapply(data$steps, data$interval, mean, na.rm = TRUE)
mean_interval <- cbind.data.frame(unique(data$interval), mean_interval)
names(mean_interval) <- c("interval", "mean_steps")
g <- ggplot(mean_interval, aes(interval, mean_steps))
g + geom_line() + labs(title = "Average daily steps", x = "Interval", y = "Average Steps")
```
2. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{R echo = TRUE}
subset(mean_interval, mean_steps == max(mean_interval$mean_steps))$interval
```
## Imputing missing values
Note that there are a number of days/intervals where there are missing values (coded as `NA`). The presence of missing days may introduce bias into some calculations or summaries of the data.
1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with `NAs`)
```{R echo = TRUE}
nrow(subset(data, is.na(steps)))
```
2. Devise a strategy for filling in all of the missing values in the dataset. The strategy does not need to be sophisticated. For example, you could use the mean/median for that day, or the mean for that 5-minute interval, etc.
3. Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{R echo =TRUE}
data_na <- data
## Filling using the mean for that interval
data[is.na(data$steps), "steps"] <- mean_interval[mean_interval$interval %in% data[is.na(data$steps), "interval"], "mean_steps"]
head(data, 10)
```
4. Make a histogram of the total number of steps taken each day and Calculate and report the mean and median total number of steps taken per day. Do these values differ from the estimates from the first part of the assignment? What is the impact of imputing missing data on the estimates of the total daily number of steps?
```{R echo = TRUE}
sums_day2 <- tapply(data$steps, data$date, sum)
sums_day2 <- cbind.data.frame(unique(data$date), sums_day2)
names(sums_day2) <- c("date", "sum")
g <- ggplot(sums_day2, aes(x = sum))
g + geom_histogram(fill = "red", bins = 20) + labs(title = "Daily steps", x = "Steps", y = "Frequency")
```
```{R echo = TRUE}
mean2 <- mean(sums_day2$sum, na.rm = TRUE)
mean2
```
```{R echo = TRUE}
median2 <- median(sums_day2$sum, na.rm = TRUE)
median2
```
DataSet| Mean Steps | Median Steps
--- | --- | ---
With NA | `r mean1` | `r median1`
NA's filled with mean of that interval | `r mean2` | `r median2`
## Are there differences in activity patterns between weekdays and weekends?
For this part the `weekdays()` function may be of some help here. Use the dataset with the filled-in missing values for this part.
1. Create 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 echo = TRUE}
vect <- weekdays(data$date, abbreviate = TRUE) %in% c("sáb", "dom")
divide <- function(x) {
if (x) {
"weekend"
}
else {
"weekday"
}
}
data$type <- as.factor(sapply(vect, divide))
head(data, 10)
```
2. Make a panel plot containing a time series plot (i.e. `type = "l"`|) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all weekday days or weekend days (y-axis). See the README file in the GitHub repository to see an example of what this plot should look like using simulated data.
```{R echo = TRUE}
mean_interval <- tapply(data$steps, list(as.factor(data$interval), as.factor(data$type)), mean, na.rm = TRUE)
funct <- function(x) {
interval <- trimws(as.character(x["interval"]))
type <- as.character(x["type"])
mean_interval[interval, type]
}
data$avg <- as.numeric(apply(data, 1, funct))
g <- ggplot(data, aes(interval, avg))
g + geom_line() + facet_grid(. ~ type) + labs(title = "Average Steps By Intervals", x = "Interval", y = "Average Steps")
```