Using R Markdown to Create a Web Report:

This is an assignment on creating a report with R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. The assignment makes use of data from a personal activity monitoring device.This device collects data at 5 minute intervals through out the day.

My Submission:

For this assignment I created this plot with [library(data.table) - library(ggplot2) - library(grid)] a panel plot in a plot of the activity trends to show the distinct trends in week days vs weekends which is blurred in the daily plot.

panel plot in plot

panel plot in plot

To get there we need to resolve these five problems:

With Raw Data(i)What is mean total number of steps taken per day? (ii)What is the average daily activity pattern? (iii) Then Create & Use Tidy Data that is equal to the original dataset but with the missing data filled in. (iv) What is the impact of imputing missing data on the estimates of the total daily number of steps? (v) Are there differences in activity patterns between weekdays and weekends?

View submission on rpubs

Loading and preprocessing the data

  • Please see metadata description, at the end of the Report if you are not familar with this dataset.
library(data.table)
library(ggplot2)

#Show any code that is needed to
#1. Load the data (i.e. `read.csv()`) from the working directory. 
csvfile <- "activity.csv"

if(!file.exists("activity.csv")){
    download.file(
     "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip",
                  destfile="repdataactivity.zip")
    unzip(zipfile="repdataactivity.zip",exdir=getwd()) }

step<-fread("activity.csv",sep=",",header=T)
step$date<-as.Date.character(step$date)

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.
#First Draft:
#1. Make a histogram of the total number of steps taken each day
############################HISTOGRAM_PLOT################################
library(data.table)
library(ggplot2)

totsteps<-step[,sum(steps,na.rm=T),by=date]
names(totsteps)[2]<-"steps"

ggplot(data=totsteps,aes(steps)) + 
    geom_histogram(binwidth=21194/30) + 
    labs(x="Steps per Day", y="Frequency",
         title="Total number of steps taken per day") + 
    theme(plot.title = element_text(hjust = 0.5)) + 
    geom_vline(aes(xintercept=median(totsteps$steps),color="Median"), lwd=1.15) + 
    geom_vline(aes(xintercept=mean(totsteps$steps),color="Mean") , lwd=1.15)

ggsave("plot1.png")
## Saving 7 x 5 in image

Dynamic Report: total number of steps taken per day

meantotsteps <- mean(totsteps$steps)
mediantotsteps <- median(totsteps$steps)

Using the Raw Data, na.rm=TRUE, there were 9354.2295082 mean steps taken each day and I need 10395 median daily steps taken.

What is the average daily activity pattern?

library(data.table)
library(ggplot2)

avgsteps<-step[,mean(steps,na.rm=T),by=interval]
names(avgsteps)[2]<-"steps"
ggplot(data=avgsteps,aes(x=interval,y=steps))+geom_line() +
    labs(x="5 Minute Intervals", y="Average Steps",
         title="Average daily activity pattern") +
    theme(plot.title = element_text(hjust = 0.5)) +     
    scale_x_continuous(breaks=seq(0,2400,200)) +
    geom_vline(aes(xintercept=avgsteps$interval[which.max(avgsteps$steps)])
               , col="red",lwd=1.15 )+
    geom_line(size = 1.25)

ggsave("plot2.png")
## Saving 7 x 5 in image

Dynamic Report: average daily activity pattern

summary(avgsteps)
##     interval          steps        
##  Min.   :   0.0   Min.   :  0.000  
##  1st Qu.: 588.8   1st Qu.:  2.486  
##  Median :1177.5   Median : 34.113  
##  Mean   :1177.5   Mean   : 37.383  
##  3rd Qu.:1766.2   3rd Qu.: 52.835  
##  Max.   :2355.0   Max.   :206.170
maxsteps <- avgsteps[steps==max(avgsteps$steps),]
minsteps <- avgsteps[steps==min(avgsteps$steps),]

Using the Raw Data, na.rm=TRUE, we can see after that there is usually no activity up until the first 400 intervals of 5 minutes, then activity for the next 1600 intervals which peaks (at interval, by steps) (835, 206.1698113) then declines.

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 `NA`s)
totmissing <- is.na(step$steps)
# How many missing
Table1 <-table(totmissing)
#totmissing
#FALSE  TRUE 
#15264  2304 

# 2. 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.

#Input Strategy: 

# (i) Create a copy of the original data table step in variable imputedstep 
inputedstep<-step
#(ii)) Create a logical vector for the locations of the missing values 
missingvec<-!complete.cases(inputedstep)
#(iii) Create a vector of the averages, dim(steps) = 61) 
avgvec<-rep(avgsteps$steps,61)
#(iv) Convert the NA values to 0, in order to be summable.
inputedstep$steps[is.na(inputedstep$steps)]<-0
#(v) Add the product of the two vectors to the steps variable
inputedstep$steps<-inputedstep$steps+avgvec*missingvec

This is a table of missing values = TRUE, if NA in cell: 15264, 2304

Missing values were first replaced with zeros, then filled in with average value for that 5-minute interval, resulting in the mean and median being the same.

  • New Data Set
# 3. Create a new dataset that is equal to the original dataset but
#    with the missing data filled in.
inputedtotsteps<-inputedstep[,sum(steps,na.rm=T),by=date]

names(inputedtotsteps)[2]<-"steps"

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

ggplot(data=inputedtotsteps,aes(steps)) + 
    geom_histogram(binwidth=21194/30) + 
    labs(x="Steps per day", y="Frequency",
         title="Normalised Histogram of Steps per Day") +
    theme(plot.title = element_text(hjust = 0.5)) +
     
    geom_vline(aes(xintercept=median(inputedtotsteps$steps),color="Median"), lwd=2.5) + 
    geom_vline(aes(xintercept=mean(inputedtotsteps$steps),color="Mean"), lwd=1.2)

ggsave("plot3.png")
## Saving 7 x 5 in image

The Mean value is higher after inputing missing data, as the 0 assigned to the missing values were given positive values.

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.

# 2. 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**:
#    ![Sample panel plot](https://github.com/lindangulopez/RepData_PeerAssessment1/raw/master/instructions_fig/sample_panelplot.png) 

step$daytype<-as.factor(weekdays(step$date) %in% c("Saturday","Sunday"))
levels(step$daytype)<-c("Weekday","Weekend")
avgwksteps<-step[ ,mean(steps, na.rm=T),by=list(daytype, interval)]
names(avgwksteps)[3]<-"steps"

# don't do facet_wrap(steps~daytype)!!

ggplot(data=avgwksteps, aes(x=interval,y=steps)) + geom_line() +
    facet_wrap(.~daytype) +
    labs(x="5 Minute Intervals", y="Average Steps",
         title="Differences in activity patterns",
         subtitle= "Weekdays vs Weekends") +
         theme(plot.title = element_text(hjust = 0.5), 
         plot.subtitle = element_text(hjust = 0.5)) +
         scale_x_continuous(breaks=seq(0,2500,500))

ggsave("plot4.png")  
## Saving 7 x 5 in image
## This plot is is different from the example as I used the activity
# monitor data, and the above plot was made with the ggplot2 system.

The resting patterns are the similar, however there is a spike in activity around the weekly mean and/or or weekly median, so it is necessary to treat the weekly and weekday data separately.

Results: Insert Plot

#Set Environment:
library(data.table)
library(ggplot2)
library(grid)

# Get Data & Datsets/
csvfile <- "activity.csv"

if(!file.exists("activity.csv")){
    download.file(
     "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip",
                  destfile="repdataactivity.zip")
    unzip(zipfile="repdataactivity.zip",exdir=getwd()) }

step<-fread("activity.csv",sep=",",header=T)
step$date<-as.Date.character(step$date)
# reset theme
theme_set(theme_bw())

avgsteps<-step[,mean(steps,na.rm=T),by=interval]
names(avgsteps)[2]<-"steps"

step$daytype<-as.factor(weekdays(step$date) %in% c("Saturday","Sunday"))
levels(step$daytype)<-c("Weekday","Weekend")
avgwksteps<-step[ ,mean(steps, na.rm=T),by=list(daytype, interval)]
names(avgwksteps)[3]<-"steps"

# Inset figures:
theme_set(theme_bw() + 
              theme(
                  axis.line = element_line(size=0.25),
                  panel.background = element_rect(fill=NA,size=rel(20)), 
                  panel.grid.minor = element_line(colour = NA), 
                  axis.text = element_text(size=8), 
                  axis.title = element_text(size=12)
                  )
          )

big_plot <- ggplot(data=avgsteps,aes(x=interval,y=steps))+geom_line() +
    labs(x="5 Minute Intervals", y="Average Steps",
         title="Average daily activity pattern") +
    theme(plot.title = element_text(hjust = 0.5)) +     
    scale_x_continuous(breaks=seq(0,2400,200)) +
    geom_vline(aes(xintercept=avgsteps$interval[which.max(avgsteps$steps)])
               , col="red",lwd=1.15 )+
    geom_line(size = 1.25)

big_plot

small_plot <- ggplot(data=avgwksteps, aes(x=interval,y=steps)) + geom_line() +
    facet_wrap(.~daytype) +
    labs(x="5 Minute Intervals", y="Average Steps",
         title="Differences in activity patterns",
         subtitle= "Weekdays vs Weekends") +
         theme(plot.title = element_text(hjust = 0.5), 
         plot.subtitle = element_text(hjust = 0.5)) +
         scale_x_continuous(breaks=seq(0,2500,1000)) +
         theme(axis.title=element_blank()) +
         scale_y_continuous(expand=c(0,0))

small_plot

# Where to put the smaller plot:
library(grid)
vp <- viewport(width = 0.35, height = 0.35, x = 0.75, y = 0.75)
                # width, height, x-position, y-position of the smaller plot

png("C:/Users/angul/OneDrive/R/ReproducibleResearch/figure/inset_plot.png")
print(big_plot)
print(small_plot, vp = vp)
dev.off()
## png 
##   2

This is my main plot: an insert plot of activity pattern, the daytype by weekday vs week end inserted in the daily activity pattern.

Insert Plot

Insert Plot

Readme Metadata:

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

Note that the echo = TRUE was added to the code as a global parameter to allow printing of the R code that generated the plots, if you’d like a report without the R code please edit to echo = FALSE in line 8. For more details on using R Markdown see http://rmarkdown.rstudio.com.

The aim of this assignment is to create and share a plot like this: Sample panel plot

Commits from RStudio to Github:

  • Step 1, Framework Only : 8b3ceb03a3ae75140a84da9a1ab144b23c2b7d9b
  • Step 2, Dynamic Report1 : abc438a667f36f03719a674a245df1eb0cb4b4ac Verified on Github:
  • Step 3, Input Missing Data : b4b367c253bb88258182d4c590f3302beea3b765
  • Step 4, Compared daytype: b00ffd0a7a8706e855f91e7070c3e5cd650ecb90
  • Step 5: Insert Plot: e4ccc0f8eafc5531c941119b482e32e31070e93c
  • Step 6: Edited readme: 2af6fdaefc6c7cfe2270b0c7950ca0038139de5c …etc