forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
123 lines (82 loc) · 4.61 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
---
title: "Reproducible Research Project 1"
author: "Rafael Mejia"
date: "April 16, 2017"
output: html_document
---
1. Load libraries:
```{r, echo=TRUE}
library(ggplot2)
library(lattice)
```
2. Load data:
```{r, echo=TRUE}
activity <- read.csv("activity.csv", header = TRUE,stringsAsFactors = FALSE)
#View a data summary before graphing
summary(activity)
```
#What is mean total number of steps taken per day?
1. Make a histogram of ttal steps taken by day:
```{r, echo=TRUE}
hist(tapply(activity$steps, activity$date, sum, na.rm = FALSE), main = "Number of steps by day",xlab = "Number of steps")
```
2. Calculate and report the mean and median total number of steps taken per day
```{r, echo=TRUE}
noNAs_activity <- na.omit(activity)
mean_totalSteps <- mean(tapply(na.omit(noNAs_activity$steps),noNAs_activity$date,sum,na.rm=FALSE))
median_totalSteps <- median(tapply(na.omit(noNAs_activity$steps),noNAs_activity$date,sum,na.rm=FALSE))
print(mean_totalSteps)
print(median_totalSteps)
```
If we don't cosider the NA values the mean is: 10,766.19 steps per day; and the median is: 10,765 steps per day.
#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}
weekAverage <- aggregate(steps ~ interval, data = activity,FUN = "mean")
plot(weekAverage$interval, weekAverage$steps, type = "l", xlab = "Intervalo",ylab = "Number of steps",main = "Average Daily Activity Pattern")
```
2. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r, echo=TRUE}
intervalMaxNsteps <- weekAverage$interval[which.max(weekAverage$steps)]
print(intervalMaxNsteps)
```
The interval that contains the maximum number of steps is 835.
#Imputing missing values
1. Calculate and report the total number of missing values in the dataset
```{r, echo=TRUE}
missingVals <- length(activity$steps[which(is.na(activity$steps)==TRUE)])
print(missingVals)
```
The total number of missing values in the data set is 2304
2. Devise a strategy for filling in all of the missing values in the dataset. The strategy does not need to be sophisticated.
I will to replace the missing values with the value of the mean number of steps for each 5-minute interval
```{r, echo=TRUE}
replaceMissings <- as.data.frame(tapply(activity$steps, activity$interval, mean,na.rm=TRUE))
names(replaceMissings) <- c("steps")
```
3. Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r, echo=TRUE}
activity[is.na(activity)] = replaceMissings$steps
activity$steps<-round(activity$steps,0)
summary(activity)
```
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}
hist(tapply(activity$steps,activity$date,sum,na.rm=FALSE),main="Distribution of number of steps taken each day", xlab="Number of steps")
#New mean and median values
mean_noMissVal<-mean(tapply(na.omit(activity$steps),activity$date,sum,na.rm=FALSE))
median_noMissVal<-median(tapply(na.omit(activity$steps),activity$date,sum,na.rm=FALSE))
```
After we changed the NA values for the mean values, the mean is 10,765.64 and the median is 10,762. After to replace the missing values with the mean value, did not change significatively the mean and the median values. In the histogram we can see a distribution with more frequency in the interval where the median is.
#Are there differences in activity patterns between weekdays and weekends?
```{r, echo=TRUE}
activity$date <- as.Date(activity$date)
activity$weekday <- weekdays(activity$date)
activity$weekday[activity$weekday=="lunes" | activity$weekday=="martes" | activity$weekday=="miércoles" | activity$weekday=="jueves" | activity$weekday=="viernes"] <- "weekday"
activity$weekday[activity$weekday=="sábado" | activity$weekday=="domingo"] <- "weekend day"
activity$weekday <- factor(activity$weekday)
new_weekAverage <- aggregate(steps ~ interval + weekday, data = activity, FUN= "mean" )
#Comparative graph
xyplot(new_weekAverage$steps ~ new_weekAverage$interval | new_weekAverage$weekday, type="l", layout=c(1,2), xlab="Interval", ylab="Number of Steps")
```
We can see that in weekends people walks more than in the weekdays. But in the weekdays we can see a peak in the interval between 800 to 950 and people starts to walk earlier than during the weekend.