forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
175 lines (119 loc) · 5.97 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
---
title: "Reproducible Research: Peer Assessment 1"
author: "Hilda"
date: "23/09/2021"
output:
html_document:
keep_md: true
---
## Loading and preprocessing the data
Show any code that is needed to
1.Load the data (i.e. read.csv())
```{r , echo = TRUE}
Data <- read.csv("activity.csv")
```
2.Process/transform the data (if necessary) into a format suitable for your analysis
```{r , echo = TRUE}
#Change date format
Data$date <- as.Date(Data$date, format = "%Y-%m-%d")
```
## What is mean total number of steps taken per day?
For this part of the assignment, you can ignore the missing values in the dataset.
1.Calculate the total number of steps taken per day.
```{r , message=FALSE, warning=FALSE}
library(tidyverse)
```
```{r , echo = TRUE}
steps_per_day <- Data %>%
group_by(date) %>%
summarize(steps_per_day = sum(steps))
```
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}
with(steps_per_day,hist(steps_per_day,main = "Histogram of the Total Number of Steps Taken Each Day", xlab="Steps per Day"))
```
3.Calculate and report the mean and median of the total number of steps taken per day
```{r , echo = TRUE}
#MEAN: 10766.19
mean_steps_per_day <- mean(steps_per_day$steps_per_day,na.rm = TRUE)
mean_steps_per_day
#Median: 10765
med_steps_per_day <- median(steps_per_day$steps_per_day,na.rm = TRUE)
med_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}
#Remove all NAs as input of steps
Data_NoNa <- subset(Data, !is.na(Data$steps))
Data_NoNa$interval <- as.factor(Data_NoNa$interval)
steps_per_interval <- Data_NoNa %>%
group_by(interval) %>%
summarize(SPI = sum(steps))
steps_per_interval$interval <- as.character(steps_per_interval$interval)
with(steps_per_interval,plot(interval,SPI,type = "l",
main = "Average Daily Activity Pattern",
xlab = "5-minute Interval",
ylab = "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}
#MAX: 835
Max_5minute_interval <- steps_per_interval[steps_per_interval$SPI == max(steps_per_interval$SPI),"interval"]
Max_5minute_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}
#total number of rows with NAs: 2304
Missing_Values <- sum(apply(Data, 1, anyNA))
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.
3.Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r , echo = TRUE}
#Filled_Data is the new dataset that is equal to the original dataset but with the missing data filled in.
Filled_Data <- Data %>%
group_by(interval) %>%
mutate(steps = ifelse(is.na(steps), mean(steps, na.rm = TRUE), steps))
```
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}
Filled_steps_per_day <- Filled_Data %>%
group_by(date) %>%
summarize(steps_per_day = sum(steps))
par(mfrow=c(1,2))
with(steps_per_day,hist(steps_per_day,main = "", xlab="Steps per Day (with NAs)"))
with(Filled_steps_per_day,hist(steps_per_day,main = "", xlab="Steps per Day"))
#MEAN: 10766.19
mean_steps_per_day <- mean(Filled_steps_per_day$steps_per_day,na.rm = TRUE)
mean_steps_per_day
#Median: 10766.19 (previously it was 10765!)
med_steps_per_day <- median(Filled_steps_per_day$steps_per_day,na.rm = TRUE)
med_steps_per_day
```
The is a difference in median of two data before and after filling the NAs. Also, the histogram of these two data is different comparing the frequency axis.
## 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}
Filled_Data$day <- weekdays(Filled_Data$date)
weekdayOnly <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
Filled_Data$day <- factor((Filled_Data$day %in% weekdayOnly),
levels=c(FALSE, TRUE), labels=c('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 , echo = TRUE, message=FALSE}
Filled_Data$interval <- as.factor(Filled_Data$interval)
steps_per_interval <- Filled_Data %>%
group_by(interval,day) %>%
summarize(SPI = sum(steps))
steps_per_interval$interval <- as.numeric(as.character(steps_per_interval$interval))
library(ggplot2)
ggplot(steps_per_interval,aes(x=interval,y=SPI))+
geom_line()+
facet_grid(rows=vars(day)) +
theme(strip.background = element_rect(colour="black", fill="#FEDFC1")) +
labs(x = "Interval",y="Number of Steps")
```