Reproducible Research: Peer Assessment 1

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.

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

Dataset: Activity monitoring data [52K]

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 required libraries

These two packages are for creating new column(s) and plotting, respectively.

library(dplyr)
library(ggplot2)

And this is the colour code for histogram filling and line plotting. You could change it to any colour you'd like.

colorcode <- "#36B8B8"
filename <- "../../inst/extdata/activity.csv"

Loading and preprocessing the data

Give the csv file a name, download the zip file, unzip it, read the csv file in, and get basic summary info.

fileurl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"  
download.file(fileurl, destfile = 'data_activity.zip')  
unzip('data_activity.zip', filename)  
data <- read.csv(filename)
str(data)
summary(data)

What is mean total number of steps taken per day?

  1. calculate the total number of steps taken per day

Use aggregate to sum up the steps according to dates.

numStepsDaily <- aggregate(steps ~ date, data = data, FUN = sum, na.rm = FALSE)
head(numStepsDaily)
  1. make a histogram ofthe total number of steps taken each day

Cast the format of dates by using as.Date. binwidth is arbitrarily chosen, so you could change it for your own aesthetic view.

data$date <- as.Date(data$date)

ggplot(numStepsDaily, aes(x = steps)) + 
  geom_histogram(fill = colorcode, binwidth = 1000) + 
  labs(title = 'Histogram of the total number of steps',
    x = "Number of steps per day", y = "Number of times in a day")
  1. calculate and report the mean and median of the total number of steps per day
meanNumStepsDaily <- mean(numStepsDaily$steps)
meanNumStepsDaily
medianNumStepsDaily <- median(numStepsDaily$steps)
medianNumStepsDaily

What is the average daily activity pattern?

  1. 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)
avgDailyActivity <- aggregate(steps~interval,data = data, FUN = mean, na.rm = TRUE)
head(avgDailyActivity)
ggplot(avgDailyActivity, aes(x = interval, y = steps)) +
  geom_line(colour = colorcode) + 
  labs(title = "Average daily activity pattern", 
       x = "Interval", y = "Steps")
  1. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
maxNumOfSteps <- avgDailyActivity[which.max(avgDailyActivity$steps),]
maxNumOfSteps['interval']

Imputing missing values

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.

  1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
imputeMissing <- sum(is.na(data$steps))
imputeMissing
  1. Devise a strategy for filling in all of the missing values in the dataset. The strategy does not need to be sophisticated. For example, you could use the mean/median for that day, or the mean for that 5-minute interval, etc.
dataFilled <- data
meanSteps <- mean(data$steps, na.rm=TRUE)
dataFilled$steps[is.na(dataFilled$steps)] <- meanSteps
  1. Create a new dataset that is equal to the original dataset but with the missing data filled in.
str(dataFilled)
  1. 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?
numStepsDailyFilled <- aggregate(steps ~ date, 
                                 data = dataFilled, 
                                 FUN = sum, 
                                 na.rm = FALSE)

ggplot(numStepsDailyFilled, aes(x = steps)) + 
  geom_histogram(fill=colorcode, binwidth = 1000) + 
  labs(title = "Histogram of the total number of steps", 
       x = "Number of steps per day", y = "Number of times in a day")

Now that the NAs are removed, we would assume some changes in mean and median:

meanNumStepsDailyFilled <- mean(numStepsDailyFilled$steps)
meanNumStepsDailyFilled
medianNumStepsDailyFilled <- median(numStepsDailyFilled$steps)
medianNumStepsDailyFilled

But this is not enough for us to see the changes. How about a quick, easy summary?

summary(numStepsDaily)
summary(numStepsDailyFilled)

It is clear to see that the steps quantiles have changed much!

Are there differences in activity patterns between weekdays and weekends?

For this part the weekdays() function may be of some help here. Use the dataset with the filled-in missing values for this part.

  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 dataset with the filled-in missing values is called dataFilled. We assign the dataset to a new one called dataNew, and use mutate in dplyr to create a new column called dayType.

dataNew <- dataFilled
dataNew <- dataNew %>% mutate(dayType = ifelse(weekdays(dataNew$date) == "Saturday" | weekdays(dataNew$date) == "Sunday", "weekend", "weekday"))
head(dataNew)
  1. 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). See the README file in the GitHub repository to see an example of what this plot should look like using simulated data.
numStepsDailyNew <- aggregate(steps ~ interval, 
                              data = dataNew, 
                              FUN = mean, 
                              na.rm = TRUE)
ggplot(dataNew, aes(x = interval, y = steps, color = dayType)) + 
  geom_line() + 
  labs(title = "Average daily steps", 
       x = "Interval", 
       y = "Total number of steps") + 
  facet_wrap(~dayType, ncol = 1, nrow = 2)


AlfonsoRReyes/RepDataPeerAssessment1 documentation built on May 5, 2019, 4:53 a.m.