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.

Data

The data for this assignment can be downloaded from the course web site:

The variables included in this dataset are:

  • steps: Number of steps taking in a 5-minute interval (missing values are coded as NA)

  • date: The date on which the measurement was taken in YYYY-MM-DD format

  • interval: Identifier for the 5-minute interval in which measurement was taken

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

Loading and preprocessing the data

Unzip the archive to the current working directory and load the data into “activity” variable

setwd("../RepData_peerAssessment1/")
unzip("activity.zip",exdir="./")
activity<-read.csv("activity.csv", header=TRUE, stringsAsFactors = FALSE)
activity$steps<-as.numeric(activity$steps)
activity$date<-as.Date(activity$date)
activity$interval<-as.numeric(activity$interval)

The first few rows of the activity data set are

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?

s<-split(activity, activity$date)
activity_daily<-sapply(s, function(x) sum(x[,"steps"], na.rm=TRUE))
hist(activity_daily, breaks=20, main = "Histogram of the total number of steps taken each day", col="blue3", xlab = "Steps per day", xlim=c(0,25000), ylim=c(0,11))

Average number of steps per day is

mean(activity_daily)
## [1] 9354.23

and the median is

median(activity_daily)
## [1] 10395

What is the average daily activity pattern?

activity_ints <- aggregate(steps ~ interval, activity, FUN=mean)
with(activity_ints, plot(x=interval, y=steps, type="l", main="Average daily activity pattern",xlab="5-minute intervals", ylab="Average number of steps", xaxp = c(0, 2400, 24), col="darkblue"))

Looking for 5-minute interval that contains the maximum number of steps on average across all the days in the dataset:

i<-activity_ints[activity_ints[,"steps"]==max(activity_ints$steps),]
i
##     interval    steps
## 104      835 206.1698

The interval is 835 with the average number of steps 206.2.

Imputing missing values

  1. The total number of missing values in the dataset (i.e. the total number of rows with NAs):
nrow(activity[!complete.cases(activity),])
## [1] 2304
  1. Strategy for filling in missing values: for every row where the number of steps is NA, the assigned value is the mean number of steps for that particular 5-minute interval rounded to the nearest integer.
activity_complete<-activity
for (j in 1:nrow(activity)){
    if (is.na(activity[j,"steps"])){
        activity_complete[j,"steps"]<-round(activity_ints[activity_ints[,"interval"]==activity[j,"interval"], "avg_steps"],0)
    }
}
  1. The new dataset that is equal to the original dataset but with the missing data filled in is stored as “activity_complete”. First rows of the new data set are:
head(activity_complete)
##   steps       date interval
## 1     2 2012-10-01        0
## 2     0 2012-10-01        5
## 3     0 2012-10-01       10
## 4     0 2012-10-01       15
## 5     0 2012-10-01       20
## 6     2 2012-10-01       25
  1. Histogram of the total number of steps taken each day
s2<-split(activity_complete, activity_complete$date)
activity_daily2<-sapply(s2, function(x) sum(x[,"steps"], na.rm=TRUE))
hist(activity_daily2, breaks=20, main = "Histogram of the total number of steps taken each day", col="blue3", xlab = "Steps per day", xlim=c(0,25000), ylim=c(0,20))

New mean and median of the total number of steps taken per day:

mean(activity_daily2)
## [1] 10765.64
median(activity_daily2)
## [1] 10762
  1. Questions. 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?

The new values of mean and median are different from those calculated for the data set with the missing values. After the imputing missing data the estimates of the total daily number of steps (mean and median) become closer to each other and their values are bigger than before the modification.

Are there differences in activity patterns between 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.
for (j in 1:nrow(activity)){
    if (weekdays(activity[j,"date"]) %in% c("Saturday","Sunday")){
        activity_complete[j,"weekday"]<-"weekend"
    } else {
        activity_complete[j,"weekday"]<-"weekday"
    }
}

This is how the data looks like now

head(activity_complete)
##   steps       date interval weekday
## 1     2 2012-10-01        0 weekday
## 2     0 2012-10-01        5 weekday
## 3     0 2012-10-01       10 weekday
## 4     0 2012-10-01       15 weekday
## 5     0 2012-10-01       20 weekday
## 6     2 2012-10-01       25 weekday
  1. Panel plot containing a time series plot of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all weekday days or weekend days (y-axis).
activity_int <- aggregate(steps ~ interval + weekday, data=activity_complete, FUN=mean)
library(lattice)
xyplot(steps~interval|weekday, data = activity_int, layout=c(1,2), type="l", xlab="Interval", ylab="Number of steps")