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.
library(ggplot2)
library(scales)
library(Hmisc)
if(!file.exists('activity.csv')){unzip('activity.zip')
}
dataSet <- read.csv('activity.csv')
str(dataSet)
## '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 ...
sbDay <- tapply(dataSet$steps, dataSet$date, sum, na.rm=TRUE)
qplot(sbDay, xlab='Total steps per day', ylab='Frequency using binwith 500', binwidth=500)
2. Calculate and report the mean and median total number of steps taken per day
dailyMean <- mean(sbDay)
dailyMedian <- median(sbDay)
(2.1.) Mean: 9354.2295082 and (2.2.) Median: 10395
avgDailyActiv <- aggregate(x=list(meanSteps=dataSet$steps), by=list(interval=dataSet$interval), FUN=mean, na.rm=TRUE)
ggplot(data=avgDailyActiv, aes(x=interval, y=meanSteps)) + geom_line() + xlab("5-minute interval") + ylab("Average of steps taken")
2. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
mStp <- which.max(avgDailyActiv$meanSteps)
timemStp <- gsub("([0-9]{1,2})([0-9]{2})", "\\1:\\2", avgDailyActiv[mStp,'interval'])
(2.1.) Most Steps : 8:35
nbMissVal <- length(which(is.na(dataSet$steps)))
(1.1.) Number of missing values: 2304
Devise a strategy for filling in all of the missing values in the dataset.
Create a new dataset that is equal to the original dataset but with the missing data filled in.
DSI <- dataSet
DSI$steps <- impute(dataSet$steps, fun=mean)
I <- tapply(DSI$steps, DSI$date, sum)
qplot(I, xlab='Total steps per day (Imputed)', ylab='Frequency using binwith 500', binwidth=500)
… and Calculate and report the mean and median total number of steps taken per day.
dMeanImp <- mean(I)
dMedImp <- median(I)
(4.1.)Imputed Mean: 1.076618910^{4} and (4.2.) Imputed Median > 1.076618910^{4}
DSI$dateType <- ifelse(as.POSIXlt(DSI$date)$wday %in% c(0,6), 'weekend', 'weekday')
AVG <- aggregate(steps ~ interval + dateType, data=DSI, mean)
ggplot(AVG, aes(interval, steps)) + geom_line() + facet_grid(dateType ~ .) + xlab("5-minute interval") + ylab("avarage number of steps")