forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
127 lines (89 loc) · 4.44 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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
## Loading and preprocessing the data
Start by loading and preprocessing already downloaded and unpacked csv file into data table
```{r}
library(data.table)
setwd("\\\\nash/mtec-home/vdenis/My Documents/coursera")
data <- fread("activity.csv")
summary(data)
```
Further prepare data: Convert date into proper date format
```{r}
data[,date := as.Date(date)]
```
## What is mean total number of steps taken per day?
1.1 Calculate the total number of steps taken per day
```{r}
total_steps <- data[,sum(steps), by = date]
setnames(total_steps, "V1", "sum_steps")
head(total_steps, 10)
```
1.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}
library(ggplot2)
qplot(sum_steps, data = total_steps, geom = "histogram")
```
1.3 Calculate and report the mean and median of the total number of steps taken per day
```{r}
mm_steps <- total_steps[,list(mean(sum_steps, na.rm = TRUE),median(sum_steps, na.rm = TRUE))]
setnames(mm_steps, "V1", "mean_steps_per_day")
setnames(mm_steps, "V2", "median_steps_per_day")
mm_steps
```
## What is the average daily activity pattern?
2.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}
i_steps <- data[,mean(steps, na.rm = TRUE), by = interval]
setnames(i_steps, "V1", "mean_steps_per_interval")
ggplot(data = i_steps, aes(x = interval, y = mean_steps_per_interval)) + geom_line() + geom_smooth(method = "loess")
```
2.2 Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r}
i_steps[which(i_steps[,mean_steps_per_interval] == max(i_steps[,mean_steps_per_interval])), ]
```
## Imputing missing values
3.1 Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
```{r}
sum(is.na(data[,steps]) | is.na(data[,date]) | is.na(data[,interval]))
```
3.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}
help_interval <- data[!is.na(steps), as.integer(round(mean(steps))), by = interval]
setnames(help_interval, "interval", "testinterval")
setnames(help_interval, "V1", "newvalue")
```
3.3 Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r}
new_data <- data
new_data[is.na(steps),steps:=help_interval[testinterval == interval,newvalue]]
```
3.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}
total_steps_new <- new_data[,sum(steps), by = date]
setnames(total_steps_new, "V1", "sum_steps")
qplot(sum_steps, data = total_steps_new, geom = "histogram")
```
Both mean and median slightly decrease
```{r}
mm_steps <- total_steps_new[,list(mean(sum_steps, na.rm = TRUE),median(sum_steps, na.rm = TRUE))]
setnames(mm_steps, "V1", "mean_steps_per_day")
setnames(mm_steps, "V2", "median_steps_per_day")
mm_steps
```
## Are there differences in activity patterns between weekdays and weekends?
4.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}
new_data[, day_of_week := as.factor(ifelse(weekdays(date, abbreviate = TRUE) == "Sa" | weekdays(date, abbreviate = TRUE) == "So", "Weekend", "Weekday"))]
```
4.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).
```{r}
i_steps <- new_data[,mean(steps, na.rm = TRUE), by = list(day_of_week, interval)]
setnames(i_steps, "V1", "mean_steps_per_interval")
ggplot(data = i_steps, aes(x = interval, y = mean_steps_per_interval)) + geom_line() + geom_smooth(method = "loess") + facet_wrap(~day_of_week)
```
Weekdays seem on average more spiky while during weekends activities are more spread during the day and start and end later.