Skip to content

Latest commit

 

History

History
778 lines (568 loc) · 27.6 KB

File metadata and controls

778 lines (568 loc) · 27.6 KB
title Reproducible Research - Peer Assessment 1
header-includes \usepackage{caption}
output
html_document
keep_md
true
fig_caption false

Some initial R settings, packages and formatting

#___0.1___Clear workspace
rm(list=ls())

#___0.2___Tables
# \captionsetup[table]{labelformat=empty}

#___0.3___Load packages
# Packages loaded
pck_loaded <- (.packages())

# Packages to load
pck_toload <- c('ggplot2', 'mvtnorm', 'data.table', 'sqldf', 'stargazer', 'xtable')

# Load packages
for(i in 1:length(pck_toload)) {
   if (!pck_toload[i] %in% pck_loaded)
    print(pck_toload[i])
    library(pck_toload[i], character.only = TRUE)
}
remove(i)

#___0.4___ SOme common standards for charts using ggplot2

form_xlab1 <- element_text(face = "italic", color = "blue")
form_title1 <- element_text(face = "bold",lineheight=.8)

Part 1 - Loading and preprocessing the data

1.1 - Load the data

The data is loaded into a data table using read.table and data.table.

The following chunk shows how it's done.

#___1.1___Load the data
setwd("C:/repos_github/coursera/repres/data")
dt_initial <- data.table(read.table("activity.csv", sep = ",", header = TRUE))
head(dt_initial,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

The table above displays the first five observations from the initial data set

1.2 - Process/transform the data into a format suitable for the analysis

Aside from removing missing values, no further processing of the data seems necessary at this point. All observations are of the same format in the different columns, and column names describe the contents well. All further data munging and imputing is done step wise in the assignment.

#___1.2___Process/transform

# Remove all observations with missing data
# The prefix dt indicates that the variable is a datatable
dt_initialNaNo <- dt_initial[complete.cases(dt_initial),]

# Rows with missing data removed
naRemoved <- nrow(dt_initial) - nrow(dt_initialNaNo)

Here's a table showing the first five observations from the initial data set with all missing values removed.

head(dt_initialNaNo,5)
##    steps       date interval
## 1:     0 2012-10-02        0
## 2:     0 2012-10-02        5
## 3:     0 2012-10-02       10
## 4:     0 2012-10-02       15
## 5:     0 2012-10-02       20

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

2.1 - Calculate the total number of steps taken per day

For the total number of steps per day, and the corresponding histogram,it is convenient to transform the data into showing unique dates in the date column.This way, total steps for all time intervals are displayed for each day in the data table dt_dailysteps. There are several ways you can do this.The package data.table has several methods, but mostly I use an sql package for R called sqldf.

#___2.1___Calculate the total number of steps taken per day

# Number of steps per day using data.table
dt_dailystepsDT <- dt_initialNaNo[,.(steps.sum = sum(steps)),by=date]

# Number of steps per day using sqldf
dt_dailysteps <- sqldf("SELECT sum(steps) as stepsum, date 
                          FROM dt_initialNaNo
                          Group by date")
dt_dailysteps <- data.table(dt_dailysteps)

grandtotalsteps <- sum(dt_dailysteps$stepsum)
grandtotalobs <- nrow(dt_initialNaNo)

rows_dt_dailysteps <- nrow(dt_dailysteps)
remove(dt_dailystepsDT)
head(dt_dailysteps, 5)
##    stepsum       date
## 1:     126 2012-10-02
## 2:   11352 2012-10-03
## 3:   12116 2012-10-04
## 4:   13294 2012-10-05
## 5:   15420 2012-10-06

There's a total of 570608 steps recorded accross 15264 observations in the original dataset after accounting for missing values. Ordering the sum of steps for all intervals per day results in a table with 53 records The previous table displays the first 5 rows with the total number of steps per day displayed in the column named stepsum. This table is the basis for the histogram that follows in Figure 1.

2.2 - Make a histogram of the total number of steps taken each day

#___2.2___Make a histogram of number of steps taken each day

h1 <- ggplot(data=dt_dailysteps, aes(dt_dailysteps$stepsum)) 
h1 <- h1 + geom_histogram(colour = "blue", fill = "grey")
h1 <- h1 + theme_classic()
h1 <- h1 + ggtitle("Figure 1 - Histogram of total steps per day") + xlab("steps")
h1 <- h1 + theme(plot.title = form_title1 )
h1 <- h1 + theme(axis.title = form_xlab1)
h1 <- h1 + ylim(0, 14)


#plot(h1)
#setwd("C:/repos_github/coursera/repres")
#ggsave(filename = "Histogram Number of Steps.pdf", plot = h1)
plot(h1)

![plot of chunk Part 2.2 - Figure 1 Histogram](figure/Part 2.2 - Figure 1 Histogram-1.png)

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

#___2.3___Calculate and report the mean and median 

nsteps_NaNo <- sum(dt_dailysteps$steps)
avgsteps_NaNo <- mean(dt_dailysteps$stepsum)
medsteps_NaNo <- median(dt_dailysteps$stepsum)
# h1 <- h1 + geom_vline(xintercept = avgsteps, labels = "mean", show_guide = TRUE, color = "green")
# h1 <- h1 + geom_vline(xintercept = medsteps, labels = "mean", show_guide = TRUE, color = "red")
# h1

#Data table of the results
Method <- c("NA removed", "NA Imputed")
Steptotal <- c(format(round(nsteps_NaNo, 0), nsmall = 0),"")
Averages <- c(format(round(avgsteps_NaNo, 2), nsmall = 2),"")
Medians <-  c(format(round(medsteps_NaNo, 0), nsmall = 0),"")
tb1_methods <- data.table(Method, Steptotal, Averages, Medians)

When accumulating all intervals per day, which is what the assignment asks for, the mean and median number of steps are 10766.19 and 10765, respectively. These findings are displayed in the table below, which will be filled with more findings along the way. So far we've calculated the mean and median after ignoring missing values. Later, we'll calculate the same values using other methods for handling missing values.

tb1 <- data.table(Method, Averages, Medians)
tb1_methods
##        Method Steptotal Averages Medians
## 1: NA removed    570608 10766.19   10765
## 2: NA Imputed

3 - What is the average daily pattern?

3.1 - Make a time series plot of the 5 minute intervals averaged accross all days.

Again, it is convenient to transform the original data. Here, the data is stored as an average for each interval accross all observed days in the data table dt_dailypattern.

#___3.1___Make a time series plot 


# Data transformation
dt_dailypattern <- (sqldf("SELECT interval, avg(steps) as steps 
                            FROM dt_initialNaNo
                            Group by interval"))
dt_dailypattern <- data.table(dt_dailypattern)

# Using the time intervals on a continuos x-axis results in an uneven
# time series due to the jumps between, for example, 955 and 1000.

idx1 <- 1:nrow(dt_dailypattern)
dt_dailypattern <- data.table(dt_dailypattern)
numberofintervals <- nrow(dt_dailypattern)

# The plot, step 1
p1 <- ggplot(dt_dailypattern, aes((interval), steps))
p1 <- p1 + geom_line(colour = "blue") + theme_classic() + 
      ggtitle("Daily pattern by 5 minute interval") + xlab("interval")
p1 <- p1 + theme(plot.title = form_title1 )

# Find interval with max steps
maxsteps = max(dt_dailypattern$steps)

# Look up the corresponding interval for the max number of steps
maxint <- sqldf("SELECT interval, max(steps)
                  FROM dt_dailypattern")

dailymax <- maxint[1,2]

# The plot step 2 - Add a point in the plot indicating the interval 
#                   with max number of steps and plot it.
p1 <- p1 + geom_point(data = subset(dt_dailypattern, interval == maxint$interval[1]),
                      colour = "red")
p1 <- p1 + geom_text(data = subset(dt_dailypattern, interval == maxint$interval[1]),
                      aes(x = interval,y = steps, hjust = -0.01
                          , label = paste("max = ",dailymax, " steps for interval",
                                          maxint[1,1])))
# Plot the plot
p1 <- p1 + theme(plot.title = element_text(lineheight=.8, face="bold"))
p1 <- p1 + ggtitle("Figure 2 - Daily steps for each interval") + xlab("steps")
p1 <- p1 + theme(axis.title = form_xlab1)
p1 <- p1 + ylim(0, 260)
# plot(p1)

The table below shows the results for the data munging in this part of the assignment. Here, we have taken the average for each interval accross all observed days with no missing values. There's a total of 288 intervals. The first and last 5 intervals are shown in the table.

dt_dailypattern
##      interval steps
##   1:        0     1
##   2:        5     0
##   3:       10     0
##   4:       15     0
##   5:       20     0
##  ---               
## 284:     2335     4
## 285:     2340     3
## 286:     2345     0
## 287:     2350     0
## 288:     2355     1

The following figure shows a plot from the previous table.

plot(p1)

![plot of chunk Part 3.1 - Plot Daily Steps](figure/Part 3.1 - Plot Daily Steps-1.png)

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

#___3.2___5 minute interval that contains the maximum number of steps?

# Find interval with max steps
maxsteps = max(dt_dailypattern$steps)

# Look up the corresponding interval for the max number of steps
maxint <- sqldf("SELECT interval, max(steps)
                  FROM dt_dailypattern")

The 5-minute interval which contains the maximum average steps per day, is 835. The average number of steps taken per day during this interval is 206.

4 - Imputing Missing values

4.1 - Calculate and report the total number of missing values

#___4.1___Calculate and report the total number of missing values in the dataset 

# First check for missing values in each column
missingColumnwise <- sapply(dt_initial, function(x) sum(is.na(x)))

# Total number of missing values
missingTotal <- sum(sapply(dt_initial, function(x) sum(is.na(x))))

Checking for missing values in each column using is.na() and sapply reveals that there are no missing observations for date and interval, but 2304 missing observations in the steps column. Of course, the total number of missing values in the dataset is 2304.

4.2 - Devise a strategy for filling in all of the missing values in the dataset

#___4.2___ Devise a strategy for filling in all of the missing values in the dataset

# Make a data table in which missing values will be raplaced.
dt_imputed <- dt_initial

It would be pretty straight forward to replace all missing values with the average or median step for each day. It's a bit more challenging to do the same for each and every interval, so this is what I'm going to do. There are surely many ways to do this, perhaps most efficiently in dplyr. I'm going to brute force the whole process using a nested For Loop. The following code loops through the table dt_imputed. For each missing value, the code loops through the table avgsteps, finds the corresponding step count, and writes it to the table dt_imputed.

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

#___4.3___Create a new dataset that is equal to the original dataset 

# Make a data table that contains daily averages for all intervals
dt_avgsteps <- (sqldf("SELECT interval, avg(steps) as steps 
                        FROM dt_initialNaNo
                        Group by interval"))
dt_avgsteps <- data.table(dt_avgsteps)

# Brute Force For Loop, replacing NAs in dt_imputed with an average step count.
i <- 1
j <- 1
nmiss <- 0
fakesteps <- 0
for(i in 1:length(dt_initial$steps)) {
  #print(dt_avgsteps$steps[i])
  if (is.na(dt_initial$steps[i])) {
        #print(paste(i, "missing", sep = " - "))
        nmiss <- nmiss + 1
        for(j in 1:length(dt_avgsteps$steps)) {
          #print(paste(nmiss,j, "missing", sep = " - "))
          
          if (dt_initial$interval[i] == dt_avgsteps$interval[j]) {
              # print(paste(i,j, "missing", sep = " - "))
              dt_imputed$steps[i] <- dt_avgsteps$steps[j]
              fakesteps <- fakesteps + dt_avgsteps$steps[j]
              break
          }
        }
  }
}
#i
# nmiss
# themissing <- nrow(subset(dx1, is.na(dt_nonmissing$steps)))
# themissing
# fakesteps
# missmix <- data.table(dt_missing, dt_nonmissing$steps)

# Look at the data
# head(dt_imputed)

setwd("C:/repos_github/coursera/repres/data")
write.table(dt_imputed, "imputedData.csv", row.names = FALSE)
#remove(nonmissing)
#nonmissing <- data.table(read.table("imputedData.csv", sep = " ", header = TRUE))

The following table displays the first and last five observations of the imputed data set.

dt_imputed
##        steps       date interval
##     1:     1 2012-10-01        0
##     2:     0 2012-10-01        5
##     3:     0 2012-10-01       10
##     4:     0 2012-10-01       15
##     5:     0 2012-10-01       20
##    ---                          
## 17564:     4 2012-11-30     2335
## 17565:     3 2012-11-30     2340
## 17566:     0 2012-11-30     2345
## 17567:     0 2012-11-30     2350
## 17568:     1 2012-11-30     2355

4.4 - Make a histogram of steps taken each day and find the mean and median.

#___4.4___Make a histogram of the total number of steps taken each day, and report mean and median.


# Order sum of steps per day for all intervals
dt_dailysteps2 <- (sqldf("SELECT sum(steps) as stepsum, date 
                          FROM dt_imputed
                          Group by date"))
dt_dailysteps2 <- data.table(dt_dailysteps2)

# Look at the data
# head(dt_dailysteps)
# head(dt_dailysteps2)

# Number of steps per day, calculations
nstepsNaNo <- sum(dt_dailysteps$steps)
avgstepsNaNo <- mean(dt_dailysteps$steps)
medstepsNaNo <- median(dt_dailysteps$steps)

# Update tb1 where averages and meand 

# Histogram of number of steps per day
h2 <- ggplot(data=dt_dailysteps2, aes(dt_dailysteps2$steps))
h2 <- h2 + geom_histogram(colour = "blue", fill = "grey")
h2 <- h2 + theme_classic()
h2 <- h2 + ggtitle(" Figure 3 - Total number of steps per day") + xlab("steps")
h2 <- h2 + theme(axis.title = form_xlab1)
h2 <- h2 + ylim(0, 14)
#plot(h2)
#setwd("C:/repos_github/coursera/repres")
#ggsave(filename = "Histogram Number of Steps no missing values.pdf", plot = h1)

# Calculate mean and median
nsteps_NaImp <- sum(dt_dailysteps2$stepsum)
avgsteps_NaImp <- mean(dt_dailysteps2$stepsum)
medsteps_NaImp <- median(dt_dailysteps2$stepsum)

#Data table of the results
Method <- c("NA removed", "NA Imputed")
Steptotal <- c(format(round(nstepsNaNo, 0), nsmall = 0),nsteps_NaImp)
Averages <- c(format(round(avgstepsNaNo, 2), nsmall = 2),format(round(avgsteps_NaImp, 2), nsmall = 2))
Medians <-  c(format(round(medstepsNaNo, 0), nsmall = 0),medsteps_NaImp)
tb1_methods <- data.table(Method, Steptotal, Averages, Medians)

impdiff_avg <- format(round(avgsteps_NaImp - avgstepsNaNo, 0), nsmall = 0)
impdiff_med <- format(round(medsteps_NaImp - medstepsNaNo, 0), nsmall = 0)
tb1_methods
##        Method Steptotal Averages Medians
## 1: NA removed    570608 10766.19   10765
## 2: NA Imputed    655736 10749.77   10641

The sum of steps added to the original data after the imputing process is 85128. This appears as the difference between the Steptotal column for NA removed and NA Imputed in the table above. We can also see that the average and median number of steps decreases respectively -16 and -124

Below is a histogram showing the distribution of steps after missing values have been imputed.

plot(h2)

![plot of chunk Part 4.4.1 Histogram Number of Steps Imputed](figure/Part 4.4.1 Histogram Number of Steps Imputed-1.png)

Comparing the histogram in figure 3 with the histogram in figure 1 shows that the imputed data set leads to an increased number of observations at the center of the distribution. Let us take a quick look at why this happens by taking the difference between each observation in the imputed data set and another table where all missing values are replaced by zero.

4.4.2 - Imputing Missing values, a closer look at the implications.

# Make a table of the origninal dataset where all missing values for steps are replaced by 0.
dt_initialNa0 <- dt_initial
dt_initialNa0[is.na(dt_initialNa0)] <- 0
  
dt_diff <- (dt_imputed - dt_initialNa0)
dt_diff$date <- dt_initial$date
dt_diff$interval <- dt_initial$interval
# sum(dt_diff$steps)

dt_diffdays <- data.table(sqldf("SELECT sum(steps) as stepsum, date 
                                  FROM dt_diff
                                  Group by date"))
# sum(dt_diffdays$stepsum)

dt_diffdays <- (sqldf("SELECT sum(steps) as stepsum, date 
                        FROM dt_diff
                        Group by date"))

dt_diffdays <- data.table(dt_diffdays)
# sum(dt_diffdays$stepsum)


# Make datatable of dt_diffdays with all zeros removed
# In other words, alld days with no effects from the imputing process are disregarded.
dt_diffdaysNo0 <- sqldf("SELECT stepsum, date 
                      FROM dt_diffdays
                      Where stepsum <> 0
                      Group by date")
dt_diffdaysNo0 <- data.table(dt_diffdaysNo0)

# UNION (dt_dailysteps + category = nonimputed) and (dt_dailysteps3 + category = imputed)
FactImpNot <- 1:nrow(dt_dailysteps)
FactImp <- 1:nrow(dt_dailysteps2)

FactImp <- rep("NaImputed",length(FactImp))
FactImpNot <- rep("NaIgnored",length(FactImpNot))

NaTreatment <- FactImpNot
dt_dailyStepsCat <- data.table(dt_dailysteps, NaTreatment)

NaTreatment <- FactImp
dt_dailySteps2Cat <- data.table(dt_dailysteps2, NaTreatment)

dt_dailyUnion <- (sqldf("SELECT * from dt_dailyStepsCat 
                            UNION ALL
                              SELECT * from dt_dailySteps2Cat
                            ORDER BY date"))
dt_dailyUnion <- data.table(dt_dailyUnion)

# sum(dt_dailyStepsCat$stepsum)
# sum(dt_dailySteps2Cat$stepsum)

# Histogram of number of steps per day
h3 <- ggplot(data=dt_dailyUnion, aes(dt_dailyUnion$steps, fill = NaTreatment))


h3 <- h3 + geom_histogram(alpha = 0.3, colour = "blue", position="identity")
h3 <- h3 + scale_fill_manual(values = c("black", "grey"))
h3 <- h3 + theme_classic()
h3 <- h3 + ggtitle("Figure 3 - Total number of steps per day") + xlab("steps")
h3 <- h3 + theme(axis.title = form_xlab1)
h3 <- h3 + ylim(0, 14)
h3 <- h3 +  theme(plot.title = element_text(lineheight=.8, face="bold"))
h3 <- h3 + ggtitle(expression(atop(bold("Figure 3 - Total number of steps per day"), atop(italic("Distinguished by method for missing values"), ""))))

#plot(h3)
#setwd("C:/repos_github/coursera/repres")
#ggsave(filename = "Histogram Number of Steps no missing values.pdf", plot = h1)


dt_diffdaysNo0
##    stepsum       date
## 1:   10641 2012-10-01
## 2:   10641 2012-10-08
## 3:   10641 2012-11-01
## 4:   10641 2012-11-04
## 5:   10641 2012-11-09
## 6:   10641 2012-11-10
## 7:   10641 2012-11-14
## 8:   10641 2012-11-30

The previous table shows the sum of the steps that have been added to the data set due to missing values.The fact that the sum of 10641 is equal for all days reveals an interesting detail. It turns out that all dates with missing values, have missing values for all intervals, and that there is no missing interval for all dates contained in the data set. This means that it would not matter much if the imputing process replaced missing values for each and every interval, or for each date only.

It also means that it's not possible to add any particular value to the analysis by replacing missing values in this example. It would have made much more sense if the original dataset was missing observations for some intervals for some days. In this case, it would be possible to add to the value of the analysis, particularly if the same intervals were not missing accross very many days.

This can be further illustrated by combining the two previous histograms in Figure 1 and Figure 2 into a new histogram in Figure 4. Notice that the number of days in the previous table is 8, and that the sum of steps for each day is 10641. The histogram in Figure 3 shows a light grey area that illustrate an increase ofeight counts in the bin where 10641 is located. The only thing you get by adding more days with the average number of steps only, is an increased stepcount around the center of the distribution of steps. The value added to the analysis by including more dates with no initially no observations, I would say is pretty close to zero.

plot(h3)

![plot of chunk Part 4.4.2 Figure 3 - Total number of steps per day](figure/Part 4.4.2 Figure 3 - Total number of steps per day-1.png)

5 - Are there differences in activity patterns between weekdays and weekends?

5.1 - Create a new factor variable in the dataset with two levels

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

# Make variables for factor categories
dayofweek <- 1:nrow(dt_imputed)

# Factor categories
wday <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

# Change regional settings to match the English language.
Sys.setlocale("LC_TIME", "English")
## [1] "English_United States.1252"
# Distribute factor categories
for(i in 1:length(dt_imputed$date)) {
    cday <- weekdays(as.Date(dt_imputed$date[i]))
    if (cday %in% wday) {
        dayofweek[i] <- "weekday"
    }else{
        dayofweek[i] <- "weekend"
    }
}

# Add the factor variables to dt_avgsteps in a new table
dt_imputed2 <- data.table(dt_imputed, dayofweek)

# Take a look at the nwe data
head(dt_imputed2, 5)
##    steps       date interval dayofweek
## 1:     1 2012-10-01        0   weekday
## 2:     0 2012-10-01        5   weekday
## 3:     0 2012-10-01       10   weekday
## 4:     0 2012-10-01       15   weekday
## 5:     0 2012-10-01       20   weekday

The table above shows how the new variable is added to the imputed data set..

5.2 - Make a panel plot

# Reset regional settings
# Sys.setlocale("LC_TIME", "Norwegian")

#_________Calculations on weekdays
# Average steps per interval for each weekday
dt_weekdaypattern <- data.table(sqldf("SELECT interval, avg(steps) as steps, dayofweek 
                            FROM dt_imputed2
                            WHERE dayofweek = 'weekday'
                            Group by interval"))

# Find max interval with corresponding value for weekdays
maxsteps_wday = max(dt_weekdaypattern$steps)

# Look up the corresponding interval for the max number of steps for weekdays
maxint_wday <- sqldf("SELECT interval, max(steps)
        FROM dt_weekdaypattern")

# Make new variables for dt_weekdaypattern in dt_weekdaypattern, indicating interval with max value
remove(intvmax)
stepmax <- 1:nrow(dt_weekdaypattern)
intvmax <- 1:(length(stepmax))
maxintv <- maxint_wday$interval[1]

for(i in 1:nrow(dt_weekdaypattern)) {
    if (dt_weekdaypattern$steps[i] == maxsteps_wday) {
        stepmax[i] <- paste("Max = ", maxsteps_wday, " steps at interval ", maxintv, sep = "")
        intvmax[i] <- maxintv
    }else{
        stepmax[i] <- NA
        intvmax[i] <- NA
    }
}

dt_weekdaypattern <- data.table(dt_weekdaypattern, intvmax, stepmax)


#_________Calculations on weekends
# Average steps per interval for each weekend
dt_weekendpattern <- (sqldf("SELECT interval, avg(steps) as steps, dayofweek 
                              FROM dt_imputed2
                              WHERE dayofweek = 'weekend'
                              Group by interval"))
dt_weekendpattern <- data.table(dt_weekendpattern)

# Find max interval with corresponding value for weekends
maxsteps_wend = max(dt_weekendpattern$steps)

# Look up the corresponding interval for the max number of steps for weekends
maxint_wend <- sqldf("SELECT interval, max(steps)
        FROM dt_weekendpattern")

# Make new variables for dt_weekendpattern in dt_weekendpattern, indicating interval with max value
remove(intvmax)
stepmax <- 1:nrow(dt_weekendpattern)
intvmax <- 1:(length(stepmax))
maxintv <- maxint_wend$interval[1]

for(i in 1:nrow(dt_weekendpattern)) {
    if (dt_weekendpattern$steps[i] == maxsteps_wend) {
        stepmax[i] <- paste("Max = ", maxsteps_wend, " steps at interval ", maxintv, sep = "")
        intvmax[i] <- maxintv
    }else{
        stepmax[i] <- NA
        intvmax[i] <- NA
    }
}

dt_weekendpattern <- data.table(dt_weekendpattern, intvmax, stepmax)

# Union dt_weekdaypattern and dt_weekendpattern into dt_weekpatterns
# dt_weekendpattern <- data.table(dt_weekendpattern, stepmax)

dt_weekpatterns <- (sqldf("SELECT * from dt_weekdaypattern 
                            UNION ALL
                              SELECT * from dt_weekendpattern
                            ORDER BY interval"))
dt_weekpatterns <- data.table(dt_weekpatterns)

# Panel plot for average steps per interval, distiingueshed by weekday or weekend
p3 <- ggplot(dt_weekpatterns, aes(interval, steps)) + geom_line(colour = "blue")
p3 <- p3 + theme_classic()
p3 <- p3 + facet_grid(dayofweek ~ .)

p3 <- p3 + geom_text(aes(x = interval, y = steps, label = stepmax, group=NULL),data=dt_weekpatterns, hjust = -0.2)
p3 <- p3 + geom_point(data=subset(dt_weekpatterns, !is.na(stepmax)), colour = "red")

p3 <- p3 + ggtitle("Figure 4 - Daily pattern by 5 minute interval - Weekdays vs Weekend")

p3 <- p3 + ggtitle(expression(atop(bold("Figure 4 - Daily pattern by 5 minute interval"), atop(italic("Weekdays Vs. Weekends"), ""))))


p3 <- p3 + theme(axis.title = form_xlab1)
p3 <- p3 + theme(plot.title = element_text(lineheight=.8, face="bold"))

p3 <- p3 + ylim(0, 260)
plot(p3)

![plot of chunk Part 5.2 Panel Plot](figure/Part 5.2 Panel Plot-1.png)

Comparing the pattern for average number of steps for each interval for weekdays versus weekends shows that the maximum number of steps is quite a bit lower and appears later in the day on weekends. In other words, you tend to get up later in the weekends and run around less. Sounds sweet. It is also apparent that the tendency to get moving earlier in the day is stronger on weekdays, with a sharp jump in activity around 5 o'clock.

Another interesting thing is that the weekday pattern flattens after the peak at 835, and that the following intervals have a lower step count than compared to the weekends. This becomes more apparent when the two lines are plotted against a common y axis in the figure that follows. Briefly put, the graph will show that, compared to weekends, you get up earlier in the morning and run around a lot more just after getting up. Then, you sit down and more or less stay in the chair before you get up and go to bed. In the weekends, you get up later, move less around just after getting up, but move around a lot more during the day. Which also sounds nice. Have a nice weekend!

5.2.1 Extra - Combine panel plot in a single plot

# Common plot for average steps per interval, distingueshed by day of week 
p4 <- ggplot(dt_weekpatterns, aes(interval, steps, colour = dayofweek )) + geom_line()
p4 <- p4 + theme_classic()
# p4 <- p4 + facet_grid(dayofweek ~ .)

p4 <- p4 + geom_text(aes(x = interval, y = steps, label = stepmax, group=NULL), data=dt_weekpatterns, hjust = -0.2)
p4 <- p4 + geom_point(data=subset(dt_weekpatterns, !is.na(stepmax)), colour = "red")

p4 <- p4 + ggtitle("Daily pattern by 5 minute interval - Weekdays vs Weekend")
p4 <- p4 + theme(axis.title = form_xlab1)
p4 <- p4 + theme(plot.title = element_text(lineheight=.8, face="bold"))
p4 <- p4 + scale_color_manual(values=c("blue", "grey"))

p4 <- p4 + ylim(0, 260)
plot(p4)

![plot of chunk Part 5.2.1 Extra Combine panel plot in a single plot](figure/Part 5.2.1 Extra Combine panel plot in a single plot-1.png)

#___1.0___  Make md file
#setwd("C:/repos_github/coursera/RepData_PeerAssessment1")
#getwd()
#library(knitr)
#knit('PA1_template.Rmd')