forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
116 lines (90 loc) · 4.52 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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
## Loading R packages
```{r}
library(dplyr)
library(lattice)
options("scipen" = 5)
```
## Loading and preprocessing the data
```{r}
unzip("./activity.zip")
data <- tbl_df(read.csv("./activity.csv"))
```
## What is mean total number of steps taken per day?
```{r}
data_daily <- data %>%
group_by(date) %>%
summarize(daily_steps = sum(steps))
hist(data_daily$daily_steps
,xlab = "Number of Steps Taken per Day"
,main = "Histogram of Steps Taken per Day")
data_summary <- data_daily %>%
ungroup %>%
summarize(mean_daily_steps = mean(daily_steps, na.rm = TRUE)
,median_daily_steps = median(daily_steps, na.rm = TRUE))
```
The mean number of steps per day is `r data_summary$mean_daily_steps`.
The median number of steps per day is `r data_summary$median_daily_steps`.
## What is the average daily activity pattern?
```{r}
data_interval <- data %>%
group_by(interval) %>%
summarize(time_steps = mean(steps,na.rm=TRUE))
data_max <- data_interval %>%
filter(time_steps == max(time_steps))
plot(data_interval
, type ="l"
, ylab="Number of Steps Taken"
, main="Mean Number of Steps Taken") +
abline(v=data_max$interval,col='red')
```
The 5-minute interval, on average across all the days in the dataset,
with the maximum number of steps is `r data_max$interval`.
## Imputing missing values
```{r}
# 1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with `NA`s)
#
Num_NA <- sum(is.na(data$steps))
# 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.
#
# My strategy with to fill in NAs with the mean of that 5-minute interval
# 3. Create a new dataset that is equal to the original dataset but with the missing data filled in.
#
data2 <- left_join(x = data, y=data_interval,by = as.character("interval")) %>%
mutate(filled_steps = ifelse(is.na(steps), time_steps, 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?
data2_daily <- data2 %>%
group_by(date) %>%
summarize(daily_filled_steps = sum(filled_steps))
hist(data2_daily$daily_filled_steps
,xlab = "Number of Steps Taken per Day"
,main = "Histogram of Steps Taken per Day"
,sub = "Missing values imputed with the mean steps during 5-minute interval for non-missing values")
data2_summary <- data2_daily %>%
ungroup %>%
summarize(mean_daily_filled_steps = mean(daily_filled_steps)
,median_daily_filled_steps = median(daily_filled_steps))
```
After imputing missing data for steps, the mean number of daily steps is `r data2_summary$mean_daily_filled_steps`.
The median number of daily steps is `r data2_summary$median_daily_filled_steps`.
Previously, the mean number of steps per day was `r data_summary$mean_daily_steps`.
The median number of steps per day was `r data_summary$median_daily_steps`.
Imputing missing data causes no impact on the mean number of steps, but increases the median to match the mean.
## Are there differences in activity patterns between weekdays and weekends?
```{r}
# 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.
#
data3 <- data2 %>%
mutate(dayType = ifelse(weekdays(as.Date(date)) %in% c("Saturday", "Sunday")
, "Weekend", "Weekday"))
data3_interval <- data3 %>%
group_by(dayType,interval) %>%
summarize(time_steps = mean(filled_steps))
# 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 created using **simulated data**:
xyplot(time_steps ~ interval | dayType, data=data3_interval, type ="l", layout = c(1,2), ylab = "Number of Steps Taken")
```