Reproducible Research Peer Graded Assignment 1

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.

Loading and Processing the data

Please set the working directory as it is in your working environment

Below code will read the data from csv file. Later convert into table format in order to use
functions of the dplyr package. See library(dplyr) is already loaded.

library(dplyr)
library(lattice)
setwd("C:/Working Directory/Office/Data Science/Coursera/Reproducible Research/repdata_data_activity")
activity <- read.csv("activity.csv", header = TRUE, sep =",")
activity <- tbl_df(activity)

What is mean total number of steps taken per day?

  • Calculate the total number of steps taken per day
  • Make a histogram of the total number of steps taken each day
  • Calculate and report the mean and median of the total number of steps taken per day
## Total number of steps taken per day ##
activity <- group_by(activity,date)
steps.per.day <- summarise(activity,step1 = sum(steps, na.rm = TRUE))
hist(steps.per.day$step1, col = "blue", xlab = "Number of steps per day", ylab = "Frequency",
     main = "Histogram - Total Number of Steps taken per Day", col.axis ="red")

ungroup(activity)
## Mean and Median of the total number of steps taken per day##
Mean <- mean(steps.per.day$step1)
Median <- median(steps.per.day$step1)
print(paste0("Mean number of steps per day = ", Mean))
## [1] "Mean number of steps per day = 9354.22950819672"
print(paste0("Median number of steps per day = ", Median))
## [1] "Median number of steps per day = 10395"

What is the average daily activity pattern?

  • 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)
  • Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
## Average daily activity pattern ## 
activity <- group_by(activity,interval)
avg.steps.day <-  summarise(activity,avg.steps = mean(steps, na.rm = TRUE))
plot(avg.steps.day$interval,avg.steps.day$avg.steps,type = "l", col = "red", 
     xlab = "5 Minute Interval", ylab = "Avg Steps Taken", main = "Daily Activity Pattern")

max.steps <- which.max(avg.steps.day$avg.steps)
Interval <-  avg.steps.day[max.steps,1]
print(paste0("Interval where the max steps taken = ",Interval))
## [1] "Interval where the max steps taken = 835"
ungroup(activity)  

Imputing missing values

  • Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
  • Strategy for NA - Fill the NAs with the mean number of steps
  • Create a new dataset that is equal to the original dataset but with the missing data filled in.
  • 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. Do these values differ from the estimates from the first part of the assignment? What is the impact of imputing missing data on the estimates of the total daily number of steps?
## Imputing Missing Values ## 
count.NA <- sum(is.na(activity$steps))
print(paste0("Total number of missing values = ",count.NA))
## [1] "Total number of missing values = 2304"
## Replacing the NA values with mean steps of that day  ## 

### Extract rows which have the NA values ###
position.na <- which(is.na(activity$steps))  

### Create a vector of means of the size same as number of NAs ### 
mean_vector <-rep(mean(activity$steps, na.rm = TRUE), times = length(position.na))

###Assign the NA values in steps column with the mean coerced to integer ###
activity$steps[position.na] <-  as.integer(mean_vector)  

## Total number of steps taken per day with the new dataset with no NA values##
activity <- group_by(activity,date)
steps.per.day <- summarise(activity,step1 = sum(steps, na.rm = TRUE))
hist(steps.per.day$step1, col = "blue", xlab = "Number of steps per day", ylab = "Frequency",
     main = "Histogram - Total Number of Steps taken per Day", col.axis ="red")

 ```r
 ungroup(activity)
 ```

** Mean and Median of daily acvity pattern without the NA values **

## Mean and Median of the total number of steps taken per day with the new dataset with no NA values##
Mean_new <- mean(steps.per.day$step1)
Median_new <- median(steps.per.day$step1)
print(paste0("Mean number of steps per day = ", Mean_new))
## [1] "Mean number of steps per day = 10751.737704918"
print(paste0("Median number of steps per day = ", Median_new))
## [1] "Median number of steps per day = 10656"
MeanChange = abs(Mean_new - Mean)
MedianChange = abs(Median_new - Mean)
print (paste0("Change in Mean number of steps per day = ", MeanChange))
## [1] "Change in Mean number of steps per day = 1397.50819672131"
print (paste0("Change in Median number of steps per day = ", MedianChange))
## [1] "Change in Median number of steps per day = 1301.77049180328"
MeanChangePercent = MeanChange/Mean *100
MedianChangePercent = MedianChange/Median *100
print (paste0("% Change in Mean number of steps per day = ", MeanChangePercent))
## [1] "% Change in Mean number of steps per day = 14.9398536298124"
print (paste0("% Change in Median number of steps per day = ", MedianChangePercent))
## [1] "% Change in Median number of steps per day = 12.5230446541922"

Are there differences in activity patterns between weekdays and weekends?

  • 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.
  • 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).
# Differences in activity patterns between weekdays and weekends #
Days =  weekdays(as.Date(activity$date), abbreviate = FALSE)
activity_new <- data.frame(activity$steps, activity$date,activity$interval,Days)
dayType <-  rep("NIL", times = length(activity_new$Days))
cbind(activity_new, dayType)
head(activity_new)
##   activity.steps activity.date activity.interval   Days
## 1             37    2012-10-01                 0 Monday
## 2             37    2012-10-01                 5 Monday
## 3             37    2012-10-01                10 Monday
## 4             37    2012-10-01                15 Monday
## 5             37    2012-10-01                20 Monday
## 6             37    2012-10-01                25 Monday
for (i in 1:length(Days)) 
{ 
  if((Days[i] == "Monday" )|(Days[i] == "Tuesday") |(Days[i]== "Wednesday") 
     |(Days[i]== "Thursday") | (Days[i]=="Friday")) 
    activity_new$dayType[i] <- "Weekday"
  else activity_new$dayType[i] <- "Weekend"
  
}
head(activity_new)
##   activity.steps activity.date activity.interval   Days dayType
## 1             37    2012-10-01                 0 Monday Weekday
## 2             37    2012-10-01                 5 Monday Weekday
## 3             37    2012-10-01                10 Monday Weekday
## 4             37    2012-10-01                15 Monday Weekday
## 5             37    2012-10-01                20 Monday Weekday
## 6             37    2012-10-01                25 Monday Weekday
#Creating a panel plot #
avg_data <- aggregate(activity_new$activity.steps, 
                      by=list(activity_new$dayType, 
                              activity_new$Days, activity_new$activity.interval), mean)
names(avg_data)
## [1] "Group.1" "Group.2" "Group.3" "x"
names(avg_data) <-  c("dayType", "Weekday", "Interval", "Average")

xyplot(Average ~ Interval | dayType, avg_data, type="l", xlab="Interval", ylab="Number of steps", 
       layout=c(1,2))