Github repository for this assignment: https://github.com/grammilo/RepData_PeerAssessment1

Introduction

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.

Analysis

Loading and preprocessing the data

Loading the required dataset and aquick look at the first 6 observations. We find that there are many obseravtions with NA.

library("ggplot2")
library("dplyr")
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
activity<-read.csv("activity.csv")
head(activity)
##   steps       date interval
## 1    NA 2012-10-01        0
## 2    NA 2012-10-01        5
## 3    NA 2012-10-01       10
## 4    NA 2012-10-01       15
## 5    NA 2012-10-01       20
## 6    NA 2012-10-01       25

What is mean total number of steps taken per day?

mean_steps_per_day<-round(sum(activity$steps,na.rm = TRUE)/length(unique(activity$date)),2)

The mean total number of steps taken per day is 9354.23. This is inlcluding days where steps entered is NA.

What is the average daily activity pattern?

activity_wo_NA<-activity[complete.cases(activity),]
activity_by_date<-aggregate(activity_wo_NA$steps,by=list(date=activity_wo_NA$date),FUN=sum)

mean_total_steps<-round(mean(activity_by_date$x),2)
median_total_steps<-round(median(activity_by_date$x),2)

Total number of steps taken each day:

g<-ggplot(data=activity_by_date,aes(x=x))+geom_histogram(fill = "blue")+ggtitle("Total steps taken each day")+theme(plot.title = element_text(hjust=0.5))+ylab("Total steps per day")
g
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

From the plot above , we can say that this person normally takes anywhere around 10000-12000 total steps on a good day. Following are the mean and median total number of steps taken each day:

mean_total_steps
## [1] 10766.19
median_total_steps
## [1] 10765
activity_by_interval<-aggregate(activity_wo_NA$steps,by=list(activity_wo_NA$interval),FUN=mean)
g<-ggplot(data=activity_by_interval,aes(x=Group.1,y=x))+geom_line(colour = "violet")+xlab("Time interval")+ggtitle("Average steps taken over 2 months")+theme(plot.title = element_text(hjust = 0.5))+ylab("Average steps")
g

The time-interval which has the maximum average steps over the duration of 2 months:

activity_by_interval[which.max(activity_by_interval$x),][[1]]
## [1] 835

From the above time-series plot we can say that this particular person starts his daily activities at 500th interval. Then, he is most active around interval 835th, probaby the time when he either goes for a run during the lunch or walks a lot for buying the lunch (assuming he is an employee in some company).

Imputing missing values

missing_vals<-sum(is.na(activity$steps))

The total number of missing values are 2304

Results

Are there differences in activity patterns between weekdays and weekends?

activity_wo_NA$day<-weekdays(as.Date(activity_wo_NA$date))
activity_wo_NA$DOW<-ifelse(activity_wo_NA$day %in% c("Saturday","Sunday"),"weekend","weekday")
comparison_plot_data<-aggregate(activity_wo_NA$steps,by=list(activity_wo_NA$DOW,activity_wo_NA$interval),FUN=mean)
colnames(comparison_plot_data)<-c("DOW","interval","avg_steps")
g<-ggplot(data=comparison_plot_data,aes(x=interval,y=avg_steps,colour = DOW))+geom_line()+facet_grid(DOW~.)
g<-g+ggtitle("Comparison of activities in weekdays and weekend")+theme(plot.title = element_text(hjust=0.5))
g<-g+ylab("Average steps")+xlab("Interval")
g

From the above plot we can say that: this person wakes a bit late during weekends on an average and is little more active on weekends during the late night hours (probably partying). So yes, there is some difference.