forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
218 lines (130 loc) · 6.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
**Set up working directory for chunk code**
```{r setup}
knitr::opts_chunk$set(root.dir = '~/Coursera/5. Reproducible research/Week 2')
```
**Load required libraries**
```{r reqlib,results = 'hide', message=FALSE}
library(dplyr)
library(ggplot2)
library(Hmisc)
library(psych)
library(lubridate)
```
## Loading and preprocessing the data
1.Load the data (i.e. read.csv())
```{r load}
# Unzip the file
unzip("repdata%2Fdata%2Factivity.zip")
# Read the data
activity <- read.csv("activity.csv", header = T, stringsAsFactors = F,
na.strings = c("",NA))
```
2.Process/transform the data (if necessary) into a format suitable for your analysis
Look at the structure of dataset
```{r structure}
str(activity)
```
* steps variable is the Number of steps taken in a 5-minute interval which is a count and data type of this variable is integer which is obvious.
* date variable is read as character variable which has to be transformed to date format.
```{r transform}
activity$date <- as.Date(activity$date, format = "%Y-%m-%d")
```
* interval variable is read as an integer, as this variable is an identifier it can be either character or integer type.
## What is mean total number of steps taken per day?
1.Calculate the total number of steps taken per day
```{r stepPerDay}
Steps_per_day <- activity %>%
group_by(date) %>% summarise(stepsPerDay = sum(steps,na.rm = TRUE))
```
2.Make a histogram of the total number of steps taken each day
```{r hist}
hist(Steps_per_day$stepsPerDay, main = "Histogram of the total number of steps taken each day",
xlab = "stepsPerDay", col = "red", breaks = 20)
```
3.Calculate and report the mean and median of the total number of steps taken per day
```{r mean_median}
MeanstepsPerDay <- round(mean(Steps_per_day$stepsPerDay, na.rm = TRUE),2)
MedianstepsPerDay <- round(median(Steps_per_day$stepsPerDay, na.rm = TRUE),2)
```
* Mean total number of steps per day is : `r MeanstepsPerDay`
* Median total number of steps per day is: `r MedianstepsPerDay`
## 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 timeseries}
AvgStepsPerInterval <- activity %>%
group_by(interval) %>% summarise(Avgsteps = round(mean(steps,
na.rm = TRUE),2))
# Using base plot
plot(AvgStepsPerInterval$interval ,AvgStepsPerInterval$Avgsteps,type ="l",
main = "Average Steps taken acroos 5 min interval",
xlab = "5 min interval",
ylab = "Average No of Steps")
```
2.Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r}
max_interval <- AvgStepsPerInterval$interval[which.max(AvgStepsPerInterval$Avgsteps)]
```
* Most Steps at: `r max_interval`
## 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}
missingCount <- length(which(is.na(activity)))
```
Total number of missing values in the dataset `r missingCount`
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.
```{r skew_kurt}
skewness <- skew(activity$steps, na.rm = TRUE)
kurtosis <- kurtosi(activity$steps, na.rm = TRUE)
```
steps variable in activity dataset has `r skewness` skewness and
`r kurtosis` kurtosis.
So imputation with median will be an optimal solution, rather than doing same imputation for all the time intervals, it's better solution to impute median by interval.
3.Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r new}
activity$steps <- impute(activity$steps, fun = median, by = activity$interval)
```
**steps variable in activity dataset is imputed with median.**
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?
**Calculate the total number of steps taken per day for imputed dataset**
```{r stepperday_imp}
Steps_per_day_imp <- activity %>%
group_by(date) %>% summarise(stepsPerDay = sum(steps,na.rm = TRUE))
```
**Make a histogram of the total number of steps taken each day for imputed dataset**
```{r hist_imp}
hist(Steps_per_day_imp$stepsPerDay, main = "Histogram of the total number of steps taken each day for Imputed dataset",
xlab = "stepsPerDay for imputed dataset", col = "red", breaks = 20)
```
**Calculate and report the mean and median of the total number of steps taken per day for imputed dataset**
```{r mean_median_imp}
imp_MeanStepsPerDay <- round(mean(Steps_per_day_imp$stepsPerDay),2)
imp_MedianStepsPerDay <- round(median(Steps_per_day_imp$stepsPerDay),2)
```
* Mean total number of steps per day for imputed dataset is : `r imp_MeanStepsPerDay`
* Median total number of steps per day for imputed dataset is:`r imp_MedianStepsPerDay`
_There is no change in the mean and median values of Number of steps taking in a 5-minute interval_
_Missing value imputation has no effect on the estimates of the total daily number of steps_
## 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 factor}
activity$weektype <- ifelse(wday(activity$date,label = T) == "Sat","weekend",
"weekday")
```
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 panel_plot}
AvgStepsByWeekType <- activity %>%
group_by(interval,weektype) %>% summarise(Avgsteps = round(mean(steps,na.rm = TRUE),2))
# using ggplot
ggplot(AvgStepsByWeekType, aes(x = interval,y = Avgsteps)) +
geom_line() +
facet_wrap(~ weektype, nrow = 2, ncol = 1) +
xlab("Interval") +
ylab("Number of steps") +
theme_bw()
```