forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
142 lines (115 loc) · 5.22 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
# Reproducible Research: Peer Assessment 1 by Soumya Ghosh
## Loading and preprocessing the data
1. Load the activity data and preprocess and load some R libraries
```{r echo=TRUE, message=FALSE}
activity_data <- read.csv("activity.csv", header=TRUE);
activity_data$date <- as.Date(activity_data$date, format = "%Y-%m-%d")
require(dplyr)
require(lattice)
```
## What is mean total number of steps taken per day?
1. Calculate the total number of steps taken per day
Make a histogram of the total number of steps taken each day
The total number of steps taken per day was calculated by using aggregate function.
```{r echo=TRUE}
steps_day <- activity_data %>% group_by(date) %>%
summarise(total_steps = sum(steps))
```
Histogram
```{r echo=TRUE}
hist(steps_day$total_steps, breaks=10, col="red",
main="Histogram of total number of steps per day",
xlab="Total number of steps in a day")
```
2. Calculate and report the mean and median of the total number of steps taken per day
```{r echo=TRUE}
mean(steps_day$total_steps, na.rm = T)
```
Median
```{r echo=TRUE}
median(steps_day$total_steps, na.rm = T)
```
## 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}
steps_interval <- activity_data %>% group_by(interval) %>%
summarise(avg_steps = mean(steps, na.rm=T))
plot(steps_interval, type = "l", main="The average number of steps")
```
2. Which 5-minute interval, on average across all the days in the dataset,
contains the maximum number of steps?
```{r echo=TRUE}
steps_interval$interval[which.max(steps_interval$avg_steps)]
```
## Imputing missing values
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}
sum(is.na(activity_data$steps))
```
Total `r sum(is.na(activity_data$steps))` rows have missing values.
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.
: I used mean values of 5-minute intervals as fillers for missing values.
3. Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r}
activity_filled <-activity_data # Make a new dataset with the original data
activity_filled <- merge(activity_data, steps_interval, by="interval")
NAs <- is.na(activity_filled$steps)
activity_filled$steps[NAs] <- activity_filled$avg_steps[NAs]
activity_filled$avg_steps <- NULL # Delete this column which was introduced due to merge
sum(complete.cases(activity_data))
sum(complete.cases(activity_filled))
```
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}
steps_day <- activity_filled %>% group_by(date) %>%
summarise(total_steps = sum(steps))
head(steps_day)
```
Histogram
```{r echo=TRUE}
hist(steps_day$total_steps, breaks=10, col="red",
main="Histogram of total number of steps per day",
xlab="Total number of steps in a day")
```
Calculate and report the mean and median of the total number of steps taken per day
```{r echo=TRUE}
mean(steps_day$total_steps, na.rm = T)
```
Median
```{r echo=TRUE}
median(steps_day$total_steps, na.rm = T)
```
By running some commands, we can see that data for steps are missing for 8 whole days.
As missing values are substituted with respective means for 5-minute intervals, this means total steps for that day is same as mean obtained in part 1 of this assignment.
Due to this change in distribution, median changes and here it is same as mean.
## Are there differences in activity patterns between weekdays and weekends?
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}
#two new columns day and day_type are added to activity_filled, by default day_type is weekday for all weekdays
activity_filled$day <- weekdays(activity_filled$date)
activity_filled$day_type <- c("weekday")
#loop to assign weekends
for (i in 1:nrow(activity_filled)){
if (activity_filled$day[i] == "Saturday" || activity_filled$day[i] == "Sunday"){
activity_filled$day_type[i] <- "weekend"
}
}
```
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). The plot should look something
like the following, which was creating using simulated data:
```{r}
steps_interval <- activity_filled %>% group_by(day_type, interval) %>%
summarise(steps = mean(steps))
xyplot(steps ~ interval | day_type, steps_interval, type = "l", layout = c(1, 2),
xlab = "Interval", ylab = "Number of steps",
main = "Difference in activity patterns between weekdays and weekends")
```