-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.Rmd
More file actions
124 lines (103 loc) · 3.78 KB
/
Copy pathanalysis.Rmd
File metadata and controls
124 lines (103 loc) · 3.78 KB
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
---
title: "Course Project - Activity Monitor"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
##Loading and preprocessing the data
We load the raw data first
```{r}
raw_data <- read.csv("activity.csv", header=T)
```
we look at the raw data
```{r}
summary(raw_data)
str(raw_data)
```
we create a copy of the raw data and convert the data variable into a data type
```{r}
data <- raw_data
data$date <- as.Date(raw_data$date, format="%Y-%m-%d")
```
we look at the data again
```{r}
summary(data)
str(data)
```
##What is mean total number of steps taken per day?
total number of steps taken per day
```{r}
steps_per_day <- aggregate(steps~date, data, sum)
```
mean and median
```{r}
mean_steps_per_day <- mean(steps_per_day$steps)
median_steps_per_day <- median(steps_per_day$steps)
```
plot histogram and add mean and median as vertical lines
```{r}
hist(steps_per_day$steps, main="Histogramm of steps per day", xlab="steps per day")
abline(v=mean(steps_per_day$steps), col="royalblue", lwd=0.1,lty=4)
abline(v=median(steps_per_day$steps), col="red", lwd=0.1, lty=2)
legend(x = "topright", c("Mean", "Median"),
col = c("royalblue", "red"),
lwd = c(0.1, 0.1),
lty= c(4,2))
```
##What is the average daily activity pattern?
```{r}
avg_steps_per_interval <- aggregate(steps~interval,data, mean)
plot(avg_steps_per_interval$interval, avg_steps_per_interval$steps, type="l", xlab="Interval", main="Average Steps per Interval", ylab="Steps")
maxx <- avg_steps_per_interval[which.max(avg_steps_per_interval$steps),1]
axis(1, at=maxx)
abline(v=maxx, col="red", lwd=2)
```
##Imputing missing values
number of rows with missing data
```{r}
sum(is.na(data))
```
looking at missing values
```{r}
xg <- split(data$steps, data$date)
daysna <- sapply(xg, function(x) mean(is.na(x)))
sum(daysna)
length(xg)
```
There are 8 out of 53 days without any recorded steps in the intervals, this means all the intervals are empty. We will impute the missing intervals the mean for the specific interval.
```{r}
imputted_data <- data
sum(is.na(imputted_data$steps))
naindex <- which(is.na(data$steps))
#imputted_data$steps[naindex] <- steps_per_interval$steps[steps_per_interval$interval == imputted_data$interval[naindex]]
for(i in naindex)
{
imputted_data$steps[i] <- avg_steps_per_interval$steps[avg_steps_per_interval$interval == imputted_data$interval[i]]
}
sum(is.na(imputted_data$steps))
```
We will now look at the histogramm of the imputed data
```{r}
steps_per_day_imp <- aggregate(steps~date, imputted_data, sum)
hist(steps_per_day_imp$steps, main="Histogramm of steps per day (imputed data)", xlab="steps per day")
abline(v=mean(steps_per_day_imp$steps), col="royalblue", lwd=0.1,lty=4)
abline(v=median(steps_per_day_imp$steps), col="red", lwd=0.1, lty=2)
legend(x = "topright", c("Mean", "Median"),
col = c("royalblue", "red"),
lwd = c(0.1, 0.1),
lty= c(4,2))
```
As one can see above the median and mean values of the total steps per day do not changed when missing intervals are replaced with the mean of the interval. The histogram has changed on the middle bar which has increased and now contains the imputed values.
#Are there differences in activity patterns between weekdays and weekends?
```{r}
library(ggplot2)
imputted_data$fw <- as.factor(weekdays(imputted_data$date))
levels(imputted_data$fw)
levels(imputted_data$fw) <- c("Weekday","Weekday","Weekend","Weekend","Weekday","Weekday","Weekday")
levels(imputted_data$fw)
steps_per_interval_fw <- aggregate(steps~interval+fw, imputted_data, mean)
g <- ggplot(steps_per_interval_fw,aes(interval, steps))
g+geom_line()+
facet_wrap(~fw) + labs(x="Interval") + labs(y="Steps") + labs(title="Average Steps per Interval")
```