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 data for this assignment can be downloaded from the course web site.
The analysis steps and answers of individual questions in this assignment are mentioned separately.
data <- read.csv("./activity.csv")
dataclean <- data[-which(is.na(data)),]
For this part of the assignment, you can ignore the missing values in the dataset. * Step 1: Make a histogram of the total number of steps taken each day
totalsteps <- aggregate(steps~date, data=dataclean, sum)
hist(totalsteps$steps, xlab="Total Steps", main="Total Steps taken per day")
mean(totalsteps$steps)
## [1] 10766.19
median(totalsteps$steps)
## [1] 10765
averagestepsperday <- aggregate(steps~interval, data=dataclean, mean)
plot(averagestepsperday$interval, averagestepsperday$steps, type="l")
averagestepsperday$interval[which(averagestepsperday$steps==max(averagestepsperday$steps))]
## [1] 835
Note that there are a number of days/intervals where there are missing values (coded as NA). The presence of missing days may introduce bias into some calculations or summaries of the data. * Step 1: Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
sum(is.na(data[,1]))
## [1] 2304
I plot missing data in order to understand how missing data distribute. I found missing data occur in consequent intervals.
plot(which(is.na(data)), xlab="Interval per day", ylab="Observation Index")
Thus, the strategy I use is following: filling in the missing values with the median value of the total steps / (number of intervals per day) in individual 5-minute interval.
datanew <- data
datanew$steps[which(is.na(data))] <- median(totalsteps$steps)/(max(data$interval)/5)
totalstepsnew <- aggregate(steps~date, data=datanew, sum)
hist(totalstepsnew$steps)
The main difference between the first histgram and the second histgram is the increased frequecny between 5000 and 10000 of total steps. The reason for this difference is that I impute the missing value with the averaged median steps per day; the total steps per day impued equals to median(totalsteps$steps)*5, which equals to 53825.
median(totalsteps$steps)*5
## [1] 53825
Also, the mean and median total number of steps taken per day become values following.
mean(totalstepsnew$steps)
## [1] 10217.5
median(totalstepsnew$steps)
## [1] 10395
For this part the weekdays() function may be of some help here. Use the dataset with the filled-in missing values for this part. * Step 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.: the variable is called averagestepsweekdaysubset.
data$day<-as.factor(ifelse(weekdays(as.Date(data$date))==c("Saturday","Sunday"), "weekend", "weekday"))
averagestepsweekdaysubset <- aggregate(steps~interval+day, data=data, mean)
library(lattice)
xyplot(steps~interval|day, data=averagestepsweekdaysubset, type="l", layout = c(1,2), xlab="Interval", ylab="Averaged numer of steps")