Analyzing Activity Tracker Data

06 Feb 2016

Course Project for Reproducible Research

I’ve just finished the last course in the Data Science Specialization on Coursera. The program is offered by Drs. Jeff Leek, Roger Peng, and Brian Caffo of John Hopkins University. There are 9 courses in all, each with small project at the end. As you’d expect, the projects are targeted toward the topic of the course and not the entire data science workflow. They aren’t comprehensive enough to put on my projects page but I still think they were important enough to share. I think a blog is the right place to share the project.

What follows is my write up for the Reproducible Research course. The goal of this project was to get familiar with using the markdown language and knitr packages in RStudio. The focus was on the tools instead of the analysis so the summaries and charts were closely directed by instructions and was graded by peers according to a defined rubric. The data set was obtained from the instructor provided link and, while I assume it hasn’t been fabricated, I take no responsibility for its validity or accuracy.

Load the data

The first step is to download the data. The data was downloaded as a zip file so must be unzipped before being loaded.

myURL<- ""

Exploratory Analysis

Now that the data has been loaded into R, we can perform the basic exploratory analysis to understand what the data looks like. My preference is to use the str() command. Based on the output we can see that there are 17,568 rows of data and 3 variables. The date variable has 61 levels indicating we have data from 61 different dates. The interval variable is the time of day the measurement was taken.

'data.frame':	17568 obs. of  3 variables:
 $ steps   : int  NA NA NA NA NA NA NA NA NA NA ...
 $ date    : Factor w/ 61 levels "2012-10-01","2012-10-02",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ interval: int  0 5 10 15 20 25 30 35 40 45 ...
[1] 2355

Let’s create a histogram of the number of steps by date.<-aggregate(steps~date,data=Raw.Data,FUN=sum)
     main="Histogram of Total Steps per Day",
     xlab="Total Steps",

plot of chunk ReproResearch1

This shows a clear peak at the 10K - 15K step bin. The data looks normally distributed but we’ll calculate the mean and median steps per day to verify. They mean and median values are pretty close so we can accept our assumption.

[1] 10766.19
[1] 10765

Time Series Analysis

Lets create a time series analysis to understand the activity by time of day. We’ll calculate the average number of steps of at each five minute interval and chart that average.

Avg.Series<- aggregate(steps~interval, data=Raw.Data, FUN=mean)
     main="Average Steps by Time of Day")
axis(side=1, at=seq(0,2400,by=100))
axis(side=2, at=seq(0,225,by=25))

plot of chunk ReproResearch2

There is a clear spike at around 8am but which interval is the actual maximum? The which.max() function can be used in the row index to return the interval to answer that question: 8:35am.

[1] 835

Handling missing values

There are several missing values in the data set. To calculate how many, we can use the complete.cases() function. It returns a logical value- true for complete cases and false for incomplete cases. The logical values can be counted by the sum function.

[1] 2304

Since the data set has missing values, we’ll impute them using the average values by interval calculated in the Step 3 above.


As a check to make sure the data hasn’t been distorted, we’ll regenerate the time series analysis and put it next to the original created in Step 3 above. I’ve used the lattice package to draw the charts and they look identical.

       main=list(label="Comparison of Activty levels",cex=1.25),
       strip=strip.custom(factor.levels=c("NAs Omitted","NAs Imputed")),

plot of chunk ReproResearch3

Comparing Weekday and Weekend Activity

The last step of this analysis is to determine if there is a difference between he activity levels on the weekend. First, we’ll use create a factor to indicate the day as weekday or weekend. The $wday name of POSIXlt is zero indexed so it returns a value 0-6 with Sunday as 0 and Saturday as 6. We’ll use the aggregate function again to calculate the means and then we’ll draw the plots.

New.Data$Weekday<-as.factor(ifelse(New.Data$Weekday<6 & New.Data$Weekday>0,"Weekday","Weekend"))

       main=list(label="Comparison of Average Activity by Weekend and Weekdays",cex=1.25),
       ylab="Steps",xlab="Time of Day", layout=c(1,2),col="black",

plot of chunk unnamed-chunk-7

In comparing the plots, we can see that the general activity patterns fairly different. The weekend activity levels are much higher throughout the day whereas the weekday activity level has a dramatic spike between 8 and 9am. In both charts there is nearly no activity until 5am. These patterns seem reasonable because people are generally sleeping so not moving in the morning and on weekdays may go to the gym in the morning before sitting at their desk for the rest of the day. On weekends, people are off of work so are free to move about all day.

Posted with : R, Data Science Specialization