Skip to content

Latest commit

 

History

History
261 lines (194 loc) · 8.42 KB

PA1_template.md

File metadata and controls

261 lines (194 loc) · 8.42 KB
title author output
Reproducible Research: Peer Assessment 1
Antonio Torres
html_document
keep_md
true

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:

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

Show any code that is needed to

  1. Load the data (i.e. read.csv())
unzip("./activity.zip")
data <- read.csv("./activity.csv")
  1. Process/transform the data (if necessary) into a format suitable for your analysis
data$date <- as.Date(data$date, "%Y-%m-%d")
head(data, 5)
##   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

What is mean total number of steps taken per day?

  1. Calculate the total number of steps taken per day
sums_day <- tapply(data$steps, data$date, sum)
sums_day <- cbind.data.frame(unique(data$date), sums_day)
names(sums_day) <- c("date", "sum")
head(sums_day, 5)
##                  date   sum
## 2012-10-01 2012-10-01    NA
## 2012-10-02 2012-10-02   126
## 2012-10-03 2012-10-03 11352
## 2012-10-04 2012-10-04 12116
## 2012-10-05 2012-10-05 13294
  1. If you do not understand the difference between a histogram and a barplot, research the difference between them. Make a histogram of the total number of steps taken each day
library(ggplot2)
g <- ggplot(sums_day, aes(x = sum))
g + geom_histogram(fill = "red", bins = 20) + labs(title = "Daily steps", x = "Steps", y = "Frequency")
## Warning: Removed 8 rows containing non-finite values (stat_bin).

  1. Calculate and report the mean and median of the total number of steps taken per day
mean1 <- mean(sums_day$sum, na.rm = TRUE)
mean1
## [1] 10766.19
median1 <- median(sums_day$sum, na.rm = TRUE)
median1
## [1] 10765

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)
mean_interval <- tapply(data$steps, data$interval, mean, na.rm = TRUE)
mean_interval <- cbind.data.frame(unique(data$interval), mean_interval)
names(mean_interval) <- c("interval", "mean_steps")
g <- ggplot(mean_interval, aes(interval, mean_steps))
g + geom_line() + labs(title = "Average daily steps", x = "Interval", y = "Average Steps")

  1. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
subset(mean_interval, mean_steps == max(mean_interval$mean_steps))$interval
## [1] 835

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)
nrow(subset(data, is.na(steps)))
## [1] 2304
  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.

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

data_na <- data
## Filling using the mean for that interval
data[is.na(data$steps), "steps"] <- mean_interval[mean_interval$interval %in% data[is.na(data$steps), "interval"], "mean_steps"]
head(data, 10)
##        steps       date interval
## 1  1.7169811 2012-10-01        0
## 2  0.3396226 2012-10-01        5
## 3  0.1320755 2012-10-01       10
## 4  0.1509434 2012-10-01       15
## 5  0.0754717 2012-10-01       20
## 6  2.0943396 2012-10-01       25
## 7  0.5283019 2012-10-01       30
## 8  0.8679245 2012-10-01       35
## 9  0.0000000 2012-10-01       40
## 10 1.4716981 2012-10-01       45
  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?
sums_day2 <- tapply(data$steps, data$date, sum)
sums_day2 <- cbind.data.frame(unique(data$date), sums_day2)
names(sums_day2) <- c("date", "sum")
g <- ggplot(sums_day2, aes(x = sum))
g + geom_histogram(fill = "red", bins = 20) + labs(title = "Daily steps", x = "Steps", y = "Frequency")

mean2 <- mean(sums_day2$sum, na.rm = TRUE)
mean2
## [1] 10766.19
median2 <- median(sums_day2$sum, na.rm = TRUE)
median2
## [1] 10766.19
DataSet Mean Steps Median Steps
With NA 1.0766189\times 10^{4} 10765
NA's filled with mean of that interval 1.0766189\times 10^{4} 1.0766189\times 10^{4}

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.
vect <- weekdays(data$date, abbreviate = TRUE) %in% c("sáb", "dom")
divide <- function(x) {
    if (x) {
        "weekend"
    }
    else  {
        "weekday"
    }
}
data$type <- as.factor(sapply(vect, divide))
head(data, 10)
##        steps       date interval    type
## 1  1.7169811 2012-10-01        0 weekday
## 2  0.3396226 2012-10-01        5 weekday
## 3  0.1320755 2012-10-01       10 weekday
## 4  0.1509434 2012-10-01       15 weekday
## 5  0.0754717 2012-10-01       20 weekday
## 6  2.0943396 2012-10-01       25 weekday
## 7  0.5283019 2012-10-01       30 weekday
## 8  0.8679245 2012-10-01       35 weekday
## 9  0.0000000 2012-10-01       40 weekday
## 10 1.4716981 2012-10-01       45 weekday
  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.
mean_interval <- tapply(data$steps, list(as.factor(data$interval), as.factor(data$type)), mean, na.rm = TRUE)
funct <- function(x) {
  interval <- trimws(as.character(x["interval"]))
  type <- as.character(x["type"])
  mean_interval[interval, type]
}
data$avg <- as.numeric(apply(data, 1, funct))
g <- ggplot(data, aes(interval, avg))
g + geom_line() + facet_grid(. ~ type) + labs(title = "Average Steps By Intervals", x = "Interval", y = "Average Steps")