Reproducible Research - Week 2

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.

The dataset used in this assignment consists 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:

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 data set is stored in a comma-separated-value (CSV) file and there are a total of 17,568 observations in this dataset.

Assignment

This assignment consists on several questions as follows:

1.Loading and preprocessing the data

Load the data. In the case the data is already available in the working directory, it is loaded directly. On the other hand, it is first downloaded and unzipped.

file_url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
zip_file <- ".\\repdata_data_activity.zip"
if (!file.exists(zip_file)) {
        download.file(file_url, destfile = zip_file, mode = 'wb')
        date_download <- date() 
}
file_name <-".\\activity.csv"
if (!file.exists(file_name)) {
        unzip(zipfile = zip_file, exdir = getwd())
}

activity_data <- read.csv(file_name, header = TRUE)

Process/transform the data (if necessary) into a format suitable for your analysis. Since the date column is a date, it is transform from character type to date type.

activity_data$date <- as.Date(x=activity_data$date, format = "%Y-%m-%d")
original_data <- activity_data

2.What is mean total number of steps taken per day?

For this part of the assignment, you can ignore the missing values in the dataset.

activity_data<- na.omit(activity_data)

Make a histogram of the total number of steps taken each day.

total_steps_per_day <- aggregate(steps ~ date, data = activity_data, FUN = sum)
hist(total_steps_per_day$steps,
     main = "Total number of steps taken each day",
     xlab = "Number of steps per day",
     col = "blue",
     breaks = 30)

Calculate and report the mean and median total number of steps taken per day.

The mean total number of steps taken per day is:

mean_number_steps <- mean(total_steps_per_day$steps)
mean_number_steps
## [1] 10766.19

The median total number of steps taken per day is:

median_number_steps <- median(total_steps_per_day$steps)
median_number_steps
## [1] 10765

3.What is the average daily activity pattern?

Make a time series plot of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)

average_steps_per_interval <- aggregate(steps ~ interval, data = activity_data, FUN = mean)
plot(x = average_steps_per_interval$interval,
     y = average_steps_per_interval$steps,
     type = "l",
     col = "blue",
     xlab = "The 5-minutes interval",
     ylab = "Average number of steps taken across all days",
     main = "The average daily activity pattern")

Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?

five_min_interval <- average_steps_per_interval$interval[which.max(average_steps_per_interval$steps)]

The 5-minute interval rsulted is: 835

4.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.

Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)

na_steps <- sum(is.na(original_data$steps))

The total number of missing values in the dataset is: 2304

The strategy selected for filling in all of the missing values in the dataset is the use of the mean for that 5-minute interval.

average_for_5min_interval <- aggregate(steps ~ interval, data = original_data, FUN = mean, na.rm = TRUE)

Create a new dataset that is equal to the original dataset but with the missing data filled in.

# fillNA_data is the dataset where we fill NA steps
fillNA_data <- original_data

is_na_steps <- is.na(original_data$steps)
aux <- na.omit(subset(average_for_5min_interval, interval == original_data$interval[is_na_steps]))
fillNA_data$steps[is_na_steps] <- aux[, 2]
na_steps_fillNA <- sum(is.na(fillNA_data))

Initially, the NA steps were: 2304 The NA steps after filling them with the mean for that 5-minute interval is: 0

Make a histogram of the total number of steps taken each day.

steps_per_day_noNA <- aggregate(steps ~ date, data = fillNA_data, FUN = sum, na.rm = TRUE)
hist(steps_per_day_noNA$steps,
     main = "Total number of steps taken each day (without missing values)",
     xlab = "Number of steps per day",
     col = "blue",
     breaks = 30)

Calculate and report the mean and median total number of steps taken per day.

The mean total number of steps taken per day is:

mean_steps_per_day <- mean(steps_per_day_noNA$steps)
mean_steps_per_day
## [1] 10766.19

The median total number of steps taken per day:

median_steps_per_day <- median(steps_per_day_noNA$steps)
median_steps_per_day
## [1] 10766.19

Do these values differ from the estimates from the first part of the assignment?

Both mean and median are almost the same values than before filling missing values. There is a slightly increase in the median after filling the NA values, becoming equal to the mean.

What is the impact of imputing missing data on the estimates of the total daily number of steps?

There are mire significant difference in the quartiles, with an increase in the 1st Quartile and an decrease in the 3rd Quartile . Let’s see the summary of both data sets:

The summary of the original data set is:

summary(total_steps_per_day$steps)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##      41    8841   10765   10766   13294   21194

The summary of the data set with missing values filled is:

summary(steps_per_day_noNA$steps)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##      41    9819   10766   10766   12811   21194

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.

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.

weekdays_values = c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
date_type <- ifelse(
                weekdays(fillNA_data$date) %in% weekdays_values,
                'weekday',
                'weekend')
fillNA_data$day <- factor(x = date_type)

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). The plot should look something like the following, which was created using simulated data:

average_steps_per_weekday <- aggregate(steps ~ interval + day, data = fillNA_data, FUN = mean, na.rm = TRUE)

library(ggplot2)
ggplot(average_steps_per_weekday, aes(interval, steps, color = day)) +
       geom_line() +
       facet_grid(day ~ .) +
       xlab('5-minute interval') +
       ylab('Average number of steps') +
       ggtitle('Activity pattern by the week of the day ')