This document is the week 2 assignment of “Reproducible Research”, 1 of 9 courses of Data Science Specialization.

The Data

It is now possible to collect a large amount of data about personal movement using activity monitoring devices such as a Fitbit, Nike Fuelband, or Jawbone Up. These type of devices are part of the “quantified self” movement - a group of enthusiasts who take measurements about themselves regularly to improve their health, to find patterns in their behavior, or because they are tech geeks. But these data remain under-utilized both because the raw data are hard to obtain and there is a lack of statistical methods and software for processing and interpreting the data.

This assignment makes use of data from a personal activity monitoring device. This device collects data at 5 minute intervals through out the day. The data consists of two months of data from an anonymous individual collected during the months of October and November, 2012 and include the number of steps taken in 5 minute intervals each day.

The variables included in this dataset are:

The dataset is stored in a comma-separated-value (CSV) file and there are a total of 17,568 observations in this dataset.

The plotting system used in this assignment is “ggplot”.

library(ggplot2)

Loading and preprocessing the data

  1. Unzip the file and loading of data
unzip(zipfile="activity.zip",exdir=".")

act_data<-read.csv("activity.csv")
  1. Pre processing the data
act_data$date<-as.Date(act_data$date, "%Y-%m-%d")

What is mean total number of steps taken per day?

  1. Calculate the total number of steps taken per day using aggregate function
tot_steps<-aggregate(steps~date, act_data, sum, na.action = na.omit)

head(tot_steps)
##         date steps
## 1 2012-10-02   126
## 2 2012-10-03 11352
## 3 2012-10-04 12116
## 4 2012-10-05 13294
## 5 2012-10-06 15420
## 6 2012-10-07 11015
  1. Make a histogram of the total number of steps taken each day
ggplot(data=tot_steps, aes(steps))+geom_histogram(color="white", fill="black", binwidth = 2000)+ggtitle("Histogram of Total Number of Steps taken each Day")

  1. Calculate and report the mean and median of the total number of steps taken per day
ave<-mean(tot_steps$steps)
med<-median(tot_steps$steps)

ave
## [1] 10766.19
med
## [1] 10765

The averate total number of steps per day is 1.076618910^{4} and the median is 10765

What is the average daily activity pattern?

  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)
int_steps<-aggregate(steps~interval, data= act_data, mean)
g<-ggplot(data=int_steps, aes(interval, steps))+geom_line()+ggtitle("Time Series Plot of 5-minute interval and average number of steps taken")
g

  1. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
max_steps<-int_steps[which.max(int_steps$steps),1]
g+geom_point(data=int_steps[which.max(int_steps$steps),], color="red")+geom_text(data=NULL, y=200, x=max_steps+500, label=paste("Max is", max_steps,"th interval"))

The 835th is the maximum number of steps in 5-minute interval.

Imputing missing values

  1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
m<-complete.cases(act_data)
sum(!m)
## [1] 2304
  1. Devise a strategy for filling in all of the missing values in the dataset.

I used the computed mean for that 5-minute interval to replace the missing value.

  1. Create a new dataset that is equal to the original dataset but with the missing data filled in.
act_data2<-act_data
for(i in 1: length(m)){
        
        if(m[i]){next}
        
        x<-which(int_steps[,1]==act_data2[i,3])
        act_data2[i,1]=int_steps[x,2]
        
}

head(act_data2)
##       steps       date interval
## 1 1.7169811 2012-10-01        0
## 2 0.3396226 2012-10-01        5
## 3 0.1320755 2012-10-01       10
## 4 0.1509434 2012-10-01       15
## 5 0.0754717 2012-10-01       20
## 6 2.0943396 2012-10-01       25
  1. 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.
tot_steps2<-aggregate(steps~date, act_data2, sum, na.action = na.omit)
ave2<-mean(tot_steps2$steps)
med2<-median(tot_steps2$steps)

ggplot(data=tot_steps, aes(steps))+geom_histogram(color="white", fill="black", binwidth = 2000)+ggtitle("Histogram of Total Number of Steps taken each Day with Imputed Missing Value")+geom_vline(data=Null, xintercept =c(ave2, med2), color=c("yellow", "blue"), lwd=c(2,1))+scale_color_manual(name="legend", values = c("Mean"="yellow", "Median"="blue"))

ave2
## [1] 10766.19
med2
## [1] 10766.19

The mean before and after the imputation are just the same since missing values are replace using the mean of the 5 minute interval. However, in terms of median, a slight higher difference is computed after imputing missing value, since it will move because of the positions of the imputed values.

Are there differences in activity patterns between weekdays and weekends?

For this part, the new data set will be use and add additional variables by using weekdays() funtion to determine weekdays and weekends.

  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.
var1<-weekdays(act_data2$date)

for (i in 1: length(var1)){
        
        if(var1[i]=="Saturday"|var1[i]=="Sunday"){
                act_data2$day[i]="Weekend"
        }
        
        else{
                act_data2$day[i]="Weekday"
        }
        
}
act_data2$day<-as.factor(act_data2$day)
levels(act_data2$day)=c("weekend", "weekday")

head(act_data2)
##       steps       date interval     day
## 1 1.7169811 2012-10-01        0 weekend
## 2 0.3396226 2012-10-01        5 weekend
## 3 0.1320755 2012-10-01       10 weekend
## 4 0.1509434 2012-10-01       15 weekend
## 5 0.0754717 2012-10-01       20 weekend
## 6 2.0943396 2012-10-01       25 weekend
tapply(act_data2$steps, act_data2$day, mean)
##  weekend  weekday 
## 35.61058 42.36640
  1. 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).

Since one can use any plotting system, figure below use ggplot.

days_steps<-aggregate(steps~interval+day, data=act_data2, mean)
g<-ggplot(days_steps, aes(interval, steps))
g+geom_line()+facet_grid(day~.)+ggtitle("Time Series Plot of the 5-minute interval and the average number of steps taken")

Figure abaove shows that during 750th to 1000th, weekend produces more steps. However, the general pattern is that there are more steps produces during weekdays which can be examined by comparing the average steps of weekend vs weekdays.

days_steps<-aggregate(steps~interval+day, data=act_data2, mean)
g<-ggplot(days_steps, aes(interval, steps))
g+geom_line(aes(col=day))+ggtitle("Time Series Plot of the 5-minute interval and the average number of steps taken")