forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
122 lines (92 loc) · 3.37 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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
Dplyr is used during the loading and preprocessing the data
```{r, echo=TRUE}
setwd("C:/Users/Simon/Google Drive/R/coursera/reproducible research/RepData_PeerAssessment1")
unzip("activity.zip")
activity <- read.csv("activity.csv")
suppressPackageStartupMessages(library(dplyr))
```
## What is mean total number of steps taken per day?
Total steps per day
```{r, echo=TRUE}
activity %>%
group_by(date) %>%
summarize(total=sum(steps)) %>%
print
```
```{r, echo=TRUE}
hist(activity$steps, xlab="Steps", main="Histogram of overall steps")
```
Mean and median number of steps per day
```{r, echo=TRUE}
activity %>%
group_by(date) %>%
summarize(mean=mean(steps, na.rm=TRUE), median=median(steps, na.rm=TRUE)) %>%
print
```
## What is the average daily activity pattern?
```{R echo=TRUE}
interval_orig <-activity %>%
group_by(interval) %>%
summarize(meanint=mean(steps, na.rm=T))
order_int <- interval_orig %>%
arrange(desc(meanint))
max_steps <- order_int[[1]][[1]]
````
The 5 minute interval with the highest number of steps is `r max_steps` as seen in the time series plot below.
```{R echo=TRUE}
plot(interval_orig$interval,interval_orig$meanint, type="l", xlab="interval", ylab="Mean steps")
```
## Imputing missing values
```{R, echo=TRUE}
missnum <- activity %>%
is.na %>%
sum
````
Nr of missing values are `r missnum`
```{R, echo=TRUE}
missingsteps <- is.na(activity$steps)
activity$steps[missingsteps] <- mean(activity$steps,na.rm=T)
impute_activity <- data.frame(activity)
activity <- read.csv("activity.csv")
````
```{R, echo=TRUE}
total_impute <- impute_activity %>%
group_by(date) %>%
summarize(total=sum(steps))
hist(total_impute$total,xlab="Total steps", main="Histogram of total steps")
````
Mean & median steps per day
```{R, echo=TRUE}
impute_activity %>%
group_by(date) %>%
summarize(mean=mean(steps, na.rm=TRUE), median=median(steps, na.rm=TRUE)) %>%
print
````
Does these numbers differ from the first part of the assignment? Yes.The imputation increased the proportion of low activity compared to the non-imputed values seen below.
```{R, echo=TRUE}
activity %>%
group_by(date) %>%
summarize(mean=mean(steps, na.rm=TRUE), median=median(steps, na.rm=TRUE)) %>%
print
````
## Are there differences in activity patterns between weekdays and weekends?
```{R, echo=TRUE}
stripped_date <- strptime(impute_activity$date,format="%Y-%m-%d")
daysofweek <- weekdays(stripped_date)
weekends <- daysofweek=="Saturday"|daysofweek=="Sunday"
workdays <- daysofweek=="Monday"|daysofweek=="Tuesday"|daysofweek=="Wednesday"|daysofweek=="Thursday"|daysofweek=="Friday"
weekly_activity <- data.frame(impute_activity, workdays, weekends)
````
```{R, echo=TRUE}
weekend_steps <- weekly_activity[weekends==TRUE,] %>% group_by(interval) %>% summarize(mean=mean(steps,na.rm=T))
workday_steps <- weekly_activity[weekends==FALSE,] %>% group_by(interval) %>% summarize(mean=mean(steps,na.rm=T))
par(mfcol=c(2,1))
plot(workday_steps$interval,workday_steps$mean, type="l", main="Workdays", xlab="Interval", ylab="Number of steps")
plot(weekend_steps$interval,weekend_steps$mean, type="l", main="Weekends", xlab="Interval", ylab="Number of steps")
````