Rpub Link: Click Here
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.
This assignment will be described in multiple parts. You will need to write a report that answers the questions detailed below. Ultimately, you will need to complete the entire assignment in a single R markdown document that can be processed by knitr and be transformed into an HTML file.
Throughout your report make sure you always include the code that you used to generate the output you present. When writing code chunks in the R markdown document, always use echo = TRUE
so that someone else will be able to read the code. This assignment will be evaluated via peer assessment so it is essential that your peer evaluators be able to review the code for your analysis.
For the plotting aspects of this assignment, feel free to use any plotting system in R (i.e., base, lattice, ggplot2)
Fork/clone the GitHub repository created for this assignment. You will submit this assignment by pushing your completed files into your forked repository on GitHub. The assignment submission will consist of the URL to your GitHub repository and the SHA-1 commit ID for your repository state.
NOTE: The GitHub repository also contains the dataset for the assignment so you do not have to download the data separately.
Show any code that is needed to
Load the data (i.e. read.csv()
)
Process/transform the data (if necessary) into a format suitable for your analysis
# Load all required library
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
library(ggplot2)
library(httpuv)
## Warning: package 'httpuv' was built under R version 3.4.3
# Load file and have a quick look
setwd("C:/Users/TAT/Desktop/Coursera/Lectures/5) Reproducible Research/Assignment 1")
activity <- read.csv("activity.csv", header=TRUE, na.strings = "NA")
# Clean up date class
activity$date <- ymd(activity$date)
# Remove NA
activity1 <- na.omit(activity)
# Make new variables
#activity$monthly <- as.numeric(format(activity$date, "%m"))
#activity$daily <- as.numeric(format(activity$date, "%d"))
# Quick check
summary(activity1)
## steps date interval
## Min. : 0.00 Min. :2012-10-02 Min. : 0.0
## 1st Qu.: 0.00 1st Qu.:2012-10-16 1st Qu.: 588.8
## Median : 0.00 Median :2012-10-29 Median :1177.5
## Mean : 37.38 Mean :2012-10-30 Mean :1177.5
## 3rd Qu.: 12.00 3rd Qu.:2012-11-16 3rd Qu.:1766.2
## Max. :806.00 Max. :2012-11-29 Max. :2355.0
str(activity1)
## 'data.frame': 15264 obs. of 3 variables:
## $ steps : int 0 0 0 0 0 0 0 0 0 0 ...
## $ date : Date, format: "2012-10-02" "2012-10-02" ...
## $ interval: int 0 5 10 15 20 25 30 35 40 45 ...
## - attr(*, "na.action")=Class 'omit' Named int [1:2304] 1 2 3 4 5 6 7 8 9 10 ...
## .. ..- attr(*, "names")= chr [1:2304] "1" "2" "3" "4" ...
head(activity1)
## steps date interval
## 289 0 2012-10-02 0
## 290 0 2012-10-02 5
## 291 0 2012-10-02 10
## 292 0 2012-10-02 15
## 293 0 2012-10-02 20
## 294 0 2012-10-02 25
tail(activity1)
## steps date interval
## 17275 0 2012-11-29 2330
## 17276 0 2012-11-29 2335
## 17277 0 2012-11-29 2340
## 17278 0 2012-11-29 2345
## 17279 0 2012-11-29 2350
## 17280 0 2012-11-29 2355
For this part of the assignment, you can ignore the missing values in the dataset.
# Summarize data for ggplot
activity2 <- summarize(group_by(activity1,date),daily.step=sum(steps))
mean.activity <- as.integer(mean(activity2$daily.step))
median.activity <- as.integer(median(activity2$daily.step))
# Plot histogram
plot.steps.day <- ggplot(activity2, aes(x=daily.step)) +
geom_histogram(binwidth = 1000, aes(y=..count.., fill=..count..)) +
geom_vline(xintercept=mean.activity, colour="red", linetype="dashed", size=1) +
geom_vline(xintercept=median.activity, colour="green" , linetype="dotted", size=1) +
labs(title="Histogram of Number of Steps taken each day", y="Frequency", x="Daily Steps")
plot.steps.day
# Mean total number of steps taken per day
mean.activity
## [1] 10766
# Median total number of steps taken per day
median.activity
## [1] 10765
Mean total number of steps taken per day is 10766.
Median total number of steps taken per day is 10765.
type = "l"
) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)# Prepare data for ggplot
activity3 <- activity1 %>% group_by(interval) %>% summarize(mean.step=mean(steps))
# Plot average number of steps by 5-minute interval
plot.step.interval <- ggplot(activity3, aes(x=interval,y=mean.step)) +
geom_line(color="red") +
labs(title="Average Number of Steps Taken vs 5-min Interval", y="Average Number of Steps", x="5-min Interval Times Series")
plot.step.interval
optimal <- which.max(activity3$mean.step)
optimal.step <- activity3$interval[optimal]
sprintf("Maximum number of steps is coming from %gth 5-min interval", optimal.step)
## [1] "Maximum number of steps is coming from 835th 5-min interval"
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.
NA
s)# Total number of missing values in the dataset
sum(is.na(activity))
## [1] 2304
Ans: Total number of missing values in the dataset is 2304.
impute.activity <- activity
impute.activity$steps[is.na(impute.activity$steps)] <- mean(impute.activity$steps,na.rm=TRUE)
impute.activity$steps <- as.numeric(impute.activity$steps)
impute.activity$interval <- as.numeric(impute.activity$interval)
colSums(is.na(impute.activity))
## steps date interval
## 0 0 0
summary(impute.activity)
## steps date interval
## Min. : 0.00 Min. :2012-10-01 Min. : 0.0
## 1st Qu.: 0.00 1st Qu.:2012-10-16 1st Qu.: 588.8
## Median : 0.00 Median :2012-10-31 Median :1177.5
## Mean : 37.38 Mean :2012-10-31 Mean :1177.5
## 3rd Qu.: 37.38 3rd Qu.:2012-11-15 3rd Qu.:1766.2
## Max. :806.00 Max. :2012-11-30 Max. :2355.0
# Summarize data by date
impute.activity2 <- summarize(group_by(impute.activity,date),daily.step=sum(steps))
mean.impute <- as.integer(mean(impute.activity2$daily.step))
mean.impute
## [1] 10766
median.impute <- as.integer(median(impute.activity2$daily.step))
median.impute
## [1] 10766
# Plot histogram
plot.steps.day <- ggplot(impute.activity2, aes(x=daily.step)) +
geom_histogram(binwidth = 1000, aes(y=..count.., fill=..count..)) +
geom_vline(xintercept=mean.impute, colour="red", linetype="dashed", size=1) +
geom_vline(xintercept=median.impute, colour="green" , linetype="dotted", size=1) +
labs(title="Histogram of Number of Steps taken each day (impute)", y="Frequency", x="Daily Steps")
plot.steps.day
Ans: Mean total number of steps taken per day (after impute) is 10766.
Ans: Median total number of steps taken per day (after impute) is 10766.
Observation: After imputation, the mean and median become the same. Imputing with mean values make the histogram more normally distributed.
For this part the weekdays()
function may be of some help here. Use the dataset with the filled-in missing values for this part.
impute.activity$day <- ifelse(weekdays(impute.activity$date) %in% c("Saturday","Sunday"), "weekday", "weekend")
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:# Preparing data for ggplot
impute.df <- impute.activity %>% group_by(interval,day) %>% summarise(mean.step=mean(steps))
# Plot Average steps across weekday/weekend vs 5-min interval Time Series
plot.weekday.interval <- ggplot(impute.df, aes(x=interval, y=mean.step, color=day)) +
facet_grid(day~.) +
geom_line() +
labs(title="Average Number of Steps Taken vs 5-min Interval on Weekday/Weekend", y="Average Number of Steps", x="5-min Interval Times Series")
plot.weekday.interval