forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
147 lines (133 loc) · 3.84 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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
By Sebastian Leal
## Loading and preprocessing the data
```{r loaddata}
# Load libraries
library(reshape2)
library(data.table)
# Unzip and read file, with the proper data type for each column
activities <- read.csv(
unz('activity.zip', 'activity.csv'),
sep=",",
colClasses=c("integer", "Date", "integer")
)
summary(activities)
```
## What is mean total number of steps taken per day?
```{r totalSteps}
stepsByDay <- with(activities, tapply(steps, date, sum, na.rm = T))
stepsByDayMean <- mean(stepsByDay)
stepsByDayMedian <- median(stepsByDay)
# Create histogram
hist(stepsByDay, breaks = 15)
# Add mean and median lines, with a legen do identify them
abline(v=stepsByDayMean, col="blue")
abline(v=stepsByDayMedian, col="orange")
legend(
x="topright",
legend=c(
paste("Mean: ", round(stepsByDayMean)),
paste("Median: ", round(stepsByDayMedian))
),
fill=c("blue", "orange"),
bty="n"
)
```
## What is the average daily activity pattern?
```{r avgInterval}
stepsByIntervalAvg <- reshape2::melt(tapply(
activities$steps,
activities$interval,
mean,
na.rm=T
))
names(stepsByIntervalAvg) <- c("Interval", "StepsAvg")
plot(stepsByIntervalAvg, type="l")
```
Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r}
IntervalWithMaxSteps <- stepsByIntervalAvg[order(stepsByIntervalAvg$StepsAvg, decreasing = T),]
IntervalWithMaxSteps[1,]
```
## Imputing missing values
How many NAs in the dataset
```{r totalnas}
sum(is.na(activities))
```
```{r}
activitiesWithoutNAs <- copy(activities)
# For NA steps, insert the average of the day
for(i in 1:nrow(activitiesWithoutNAs)) {
if (is.na(activitiesWithoutNAs[i,]$steps)) {
newSteps <- subset(
stepsByIntervalAvg, Interval == activitiesWithoutNAs[i,]$interval
)$StepsAvg
activitiesWithoutNAs[i,]$steps <- newSteps
}
}
stepsByDay <- with(activitiesWithoutNAs, tapply(steps, date, sum, na.rm = T))
stepsByDayMean <- mean(stepsByDay)
stepsByDayMedian <- median(stepsByDay)
# Create histogram
hist(stepsByDay, breaks = 15)
# Add mean and median lines, with a legen do identify them
abline(v=stepsByDayMean, col="red")
abline(v=stepsByDayMedian, col="green", lty=3)
legend(
x="topright",
legend=c(
paste("Mean: ", round(stepsByDayMean)),
paste("Median: ", round(stepsByDayMedian))
),
fill=c("red", "green"),
bty="n"
)
```
Yes, we see that imputing missing values change a bit the initial figures. Now the mean and median are the same.
## Are there differences in activity patterns between weekdays and weekends?
```{r}
#weekdays(activitiesWithoutNAs$date)
#weekdays(activitiesWithoutNAs$date) %in% c("sábado", "domingo", "saturday", "sunday")
activitiesWithoutNAs[,"isWeekend"] <- NA
for(i in 1:nrow(activitiesWithoutNAs)) {
if (weekdays(activitiesWithoutNAs[i,]$date)
%in% c("sábado", "domingo", "saturday", "sunday")) {
activitiesWithoutNAs[i,]$isWeekend = TRUE
} else {
activitiesWithoutNAs[i,]$isWeekend = FALSE
}
}
weekendActivities <- subset(activitiesWithoutNAs, isWeekend == T)
weekdayActivities <- subset(activitiesWithoutNAs, isWeekend == F)
stepsByIntervalAvgWeekend <- reshape2::melt(tapply(
weekendActivities$steps,
weekendActivities$interval,
mean,
na.rm=T
))
stepsByIntervalAvgWeekday <- reshape2::melt(tapply(
weekdayActivities$steps,
weekdayActivities$interval,
mean,
na.rm=T
))
names(stepsByIntervalAvgWeekend) <- c("Interval", "StepsAvg")
names(stepsByIntervalAvgWeekday) <- c("Interval", "StepsAvg")
# Make the plot
plot(stepsByIntervalAvgWeekday,
type="l",
main="Avg Steps by Day Interval - Weekday",
col="red")
lines(stepsByIntervalAvgWeekend, type="l", col="blue")
legend(
x="topleft",
legend=c("Weekdays", "Weekends"),
fill=c("red", "blue"),
bty="n"
)
```