-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
141 lines (111 loc) · 7.43 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
# Reproducible Research:
## Programming Assignment 1:
In this document i describe the steps i did and show the code i used to analyse the given data.
I used R version 3.2.2., my OS is Windows 10.
As a first task the data has to be imported and processed:
### Loading and pre-processing the data
To import the data the path has to be set. I have changed the class of the date-variable from factor to date. Additionally i have also changed the interval-variable from integer to factor as it is not really a continous variable, but an ordinal number representing the time.
I haven´t changed it explictly into a timestamp as this is the exploratory phase and i find it very easy to handle it that way.s
```{r chunk="Import data", echo=T}
#setwd("C:/Coursera/Reprod/")
library(lattice)
tab3l_raw <- read.csv("activity.csv")
tab3l_raw$date <- as.Date(tab3l_raw$date)
tab3l_raw$interval <- as.factor(tab3l_raw$interval)
```
After loading and preprocessing we want to answer the following question:
### What is the average daily activity pattern?
For this question we firstly discard all rows which have a missing value in any column. To do that the complete.cases-function is used. The data is stored in a new variable 'tab3l' as we might need the (almost) raw data afterwards.
At first we show the total number of steps per day.
```{r chunk="Total number of steps per day", echo=T}
tab3l <- tab3l_raw[complete.cases(tab3l_raw),]
total_per_day <- tapply(tab3l$steps, tab3l$date,sum,na.rm=T)
total_per_day
```
To give some insights in the total number of steps per day and to see its distrubition, a histogram was made:
```{r chunk, echo=T}
hist(total_per_day,10, main="Histogram: Total number of steps per day", xlab = "Number of steps")
```
#
We see that the frequency for 10k-12k steps is the highest, but there are also a few days, where very few or many steps were reported. To get a better look on the actual numbers we want to calculate the mean and the median of the total number of steps per day:
```{r chunk1, echo=T}
mean(total_per_day,na.rm=T)
median(total_per_day,na.rm=T)
```
Now we want to investigate what the average daily pattern looks like.
###What is the average daily activity pattern?
Therefore we take the mean value for every interval. Afterwards we plot the timeseries. We use the interval as the "time of the day". Keep in mind 835 means 08:35am. As having a factor as xaxis wasn´t working in a easy fashion, i used a trick: i used an index for the inital plot and added the x-axis by using scales.
```{r plot, echo=T}
average_per_intervall <- tapply(tab3l$steps, tab3l$interval,mean,na.rm=T)
xaxi <- seq(1,length(average_per_intervall),by= 24)
xyplot(average_per_intervall ~ rep(1:length(average_per_intervall),1),layout=c(1,1),ylab ="steps in average", xlab="time of the day", type="l",scales=list(x=list(labels=tab3l$interval[xaxi],at=xaxi)))
```
Additionally we look for the interval with the highest mean. That means the time of the day the average number of steps is the highest.
```{r chunk2, echo=T}
max_interval <- as.numeric(as.character(tab3l$interval))[which(average_per_intervall==max(average_per_intervall))]
max_interval
```
That means around 08:35am, in average the highest number of steps is done.
Now we want to analyse what happens if we include substitutes for missing values:
###Imputing missing values
At first we compute the number of rows which have a missing value for at least one variable.
```{r chunk4, echo=T}
num_of_NA_rows <- sum(!complete.cases(tab3l_raw))
num_of_NA_rows
```
That means there are 2304 rows with a missing value.
To include this rows we have to substitute the missing values of steps. As we see in further analysis that the only values which are missing are regarding the variable steps we only have to think about a strategy for this variable.
The strategy to substitute a missing value for steps is the following:
If one value is missing it is replaced by the average number of the reported steps taken on this day. If all values are missing for one day, we replace the values of that day with 0. The new data is stored in tab3l_clean.
```{r chunk5, echo=T}
tab3l_clean <- tab3l_raw
for (i in seq(1,length(tab3l_clean$steps))){
if(is.na(tab3l_clean$steps[i])){
subset <- tab3l_clean$steps[tab3l_clean$date==tab3l_clean$date[i]]
if(is.nan(mean(subset,na.rm=T))){
tab3l_clean$steps[i]<-0
}else{
tab3l_clean$steps[i]<-mean(subset,na.rm=T)
}
}
}
```
For the "cleaned" data we now check if there are different values for the mean and the median. Afterwards we plot the histogram for this data.
```{r chunk6, echo=T}
total_per_day_clean <- tapply(tab3l_clean$steps, tab3l_clean$date,sum,na.rm=T)
mean_per_days_clean <- mean(total_per_day_clean,na.rm=T)
mean_per_days_clean
median_per_days_clean <- median(total_per_day_clean,na.rm=T)
median_per_days_clean
hist(total_per_day_clean,10, main="Histogram: Total number of steps per day", xlab = "Number of steps")
```
#
We can see that the histogram looks different. Especially we see that there are a quite a lot days now which have very few steps in total. This is probably because we set the value of steps to 0, if all values are missing for one day. Furthermoe the median and the mean values are lower. This can also be explained by the days with total numer of days equal to 0 now.
###Are there differences in activity patterns between weekdays and weekends?
Now we want to see if there are any differences between weekdays and weekends.
Therefore we create a logical vector indicating if we have a weekend-day or not. For reproducibility i use the following expression to get the English names for the weekdays. We can also create a factor for that. Though i don´t really use it.
```{r chunk7, echo=T}
Sys.setlocale("LC_TIME", "C")
days <- weekdays(tab3l$date,abbreviate = F)
days_log <- days == "Saturday" | days == "Sunday"
days_fac <- character(length(days_log))
days_fac[days_log] <- "weekend"
days_fac[!days_log] <- "weekday"
days_fac <- as.factor(days_fac)
summary(days_fac)
```
Now we can run a separate analysis for both categories, we therefore create a big table, in the first rows we have the values (average steps per intervall) for weekend-days and then those of the weekdays. Afterwards we plot them with the lattice-library and in the same way as the other timeseries-plot.
```{r chunk8, echo=T}
average_per_intervall_WE<- tapply(tab3l$steps[days_log], tab3l$interval[days_log],mean,na.rm=T)
average_per_intervall_WD<- tapply(tab3l$steps[!days_log], tab3l$interval[!days_log],mean,na.rm=T)
average_per_intervall_WE <- cbind(steps=average_per_intervall_WE,wday=rep("weekend",length(average_per_intervall_WE)))
average_per_intervall_WD <- cbind(steps=average_per_intervall_WD,wday=rep("weekday",length(average_per_intervall_WD)))
average_both <- rbind(average_per_intervall_WE,average_per_intervall_WD)
average_both_intervall <- data.frame(cbind(average_both,interval=rep(unique(as.character(tab3l$interval)),2)),row.names = 1:length(average_both[,1]))
average_both_intervall$steps <- as.numeric(as.character(average_both_intervall$steps))
average_both_intervall$interval <- as.character(average_both_intervall$interval)
xaxi <- seq(1,length(average_per_intervall_WE[,1]),by= 24)
xyplot(steps ~ rep(1:length(average_per_intervall_WE[,1]),2)| factor(wday),data=average_both_intervall,layout=c(1,2),xlab="time of the day", type="l",scales=list(x=list(labels=average_both_intervall$interval[xaxi],at=xaxi)))
```
#
That was my analysis. I hope you appreciate it. Thanks for rewiewing!