#STUDY DESCRIPTION AND REVIEW CRITERIA
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:
##REVIEW CRITERIA
Repo
Commit containing full submission
#ANALYSIS - CODES AND PLOTS
Below are all the analyses requested in the assignment
##ENVIRONMENT SETUP
Library calls, dataset pipelines, and all pre-analysis codes
###DECLARING LIBRARIES
library("knitr")
## Warning: package 'knitr' was built under R version 4.2.3
library("xtable")
## Warning: package 'xtable' was built under R version 4.2.3
library("reshape2")
## Warning: package 'reshape2' was built under R version 4.2.3
library("data.table")
## Warning: package 'data.table' was built under R version 4.2.3
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:reshape2':
##
## dcast, melt
library("dplyr")
## Warning: package 'dplyr' was built under R version 4.2.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
##
## between, first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library("lubridate")
## Warning: package 'lubridate' was built under R version 4.2.3
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:data.table':
##
## hour, isoweek, mday, minute, month, quarter, second, wday, week,
## yday, year
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library("ggplot2")
## Warning: package 'ggplot2' was built under R version 4.2.3
library("DT")
## Warning: package 'DT' was built under R version 4.2.3
###FILESET TO DATATABLE PIPELINE Download, unzip, load {#01}
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
download.file(fileUrl, destfile = paste0(getwd(), '/repdata%2Fdata%2Factivity.zip'), method = "curl")
unzip("repdata%2Fdata%2Factivity.zip")
activityDT <- data.table::fread(input = "activity.csv")
##DATA ANALYSIS
StepsPerDay <- activityDT[, c(lapply(.SD, sum, na.rm = FALSE)), .SDcols = c("steps"), by = .(date)]
datatable(StepsPerDay)
ggplot(StepsPerDay, aes(x = steps)) +
geom_histogram(aes(y = after_stat(density)),fill = "#330099", binwidth = 1000) +
geom_density(
lwd=1,
colour = "#CC0066",
fill = "#FF3399",
alpha=0.2
) +
labs(title = "Daily Steps taken", x = "Steps count", y = "Frequency")
## Warning: Removed 8 rows containing non-finite values (`stat_bin()`).
## Warning: Removed 8 rows containing non-finite values (`stat_density()`).
As the plot and the density curve show, the most frequent range of daily steps is between 7000 and 15000, with most observations averaging between 10000 and 11000.
StepsPerDay_2 <- StepsPerDay[, .(MeanSteps = mean(steps, na.rm = TRUE), MedianSteps = median(steps, na.rm = TRUE))]
datatable(StepsPerDay_2)
The calculated mean and median confirm the findings of the histogram + density plot in the previous item.
IntervalDT <- activityDT[, c(lapply(.SD, mean, na.rm = TRUE)), .SDcols = c("steps"), by = .(interval)]
ggplot(IntervalDT, aes(x = interval , y = steps)) +
geom_area(aes(fill=steps),color="#9966FF",alpha=0.4)+
geom_line(color="#330099", size=1,alpha=0.8) +
labs(title = "Average Daily Steps", x = "Interval", y = "Avg. daily steps")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
maxsteps <- IntervalDT[steps == max(steps)]
datatable(maxsteps)
activityDTNew <- data.table::fread(input = "activity.csv")
activityDTNew[is.na(steps), .N ]
## [1] 2304
There are 2304 missing values in the dataset, as shown above
activityDTNew[is.na(steps), "steps"] <- activityDTNew[, c(lapply(.SD, median, na.rm = TRUE)), .SDcols = c("steps")]
The code above replaces all ‘steps’ values equal to NA to the median of all valid ‘steps’ values
data.table::fwrite(x = activityDTNew, file = "activity_noMissingData.csv", quote = FALSE)
Total_Steps_nev <- activityDTNew[, c(lapply(.SD, sum)), .SDcols = c("steps"), by = .(date)]
# mean and median total number of steps taken per day
TSStats <- Total_Steps_nev[, .(MeanSteps = mean(steps), MedianSteps = median(steps))]
datatable(TSStats)
ggplot(Total_Steps_nev, aes(x = steps)) +
geom_histogram(aes(y = after_stat(density)),fill = "#330099", binwidth = 1000) +
geom_density(
lwd=1,
colour = "#CC0066",
fill = "#FF3399",
alpha=0.2
) +
geom_density(
data=StepsPerDay,
aes(x = steps, y = after_stat(density)),
lwd=1,
colour = "#009999",
fill = "#00CCCC",
alpha=0.2
) +
labs(title = "Daily Steps taken", x = "Steps count", y = "Frequency") +
theme(legend.position="bottom") + guides()
## Warning: Removed 8 rows containing non-finite values (`stat_density()`).
The comparison shows a predictable frequency spike of the median value, and a similar decrease in null values; the mean, median and frequency density changed little, as can be seen both in the plot above and the summary below:
| Type of Estimate | Mean_Steps | Median_Steps |
|---|---|---|
| DT with NA (TEAL LINE) | 10765 | 10765 |
| Filling NA w/ Median (PINK LINE) | 9354 | 10395 |
Sys.setlocale("LC_TIME", "C")
## [1] "C"
activityDT_WD <- data.table::fread(input = "activity.csv")
activityDT_WD[, date := as.POSIXct(date, format = "%Y-%m-%d")]
activityDT_WD[, `DOW`:= weekdays(x = date)]
activityDT_WD[grepl(pattern = "Monday|Tuesday|Wednesday|Thursday|Friday", x = `DOW`), "weekday_type"] <- "Weekday"
activityDT_WD[grepl(pattern = "Saturday|Sunday", x = `DOW`), "weekday_type"] <- "Weekend"
activityDT_WD[, `weekday_type` := as.factor(`weekday_type`)]
datatable(activityDT_WD)
## Warning in instance$preRenderHook(instance): It seems your data is too big for
## client-side DataTables. You may consider server-side processing:
## https://rstudio.github.io/DT/server.html
Tidying data up
activityDT_WD[is.na(steps), "steps"] <- activityDT_WD[, c(lapply(.SD, median, na.rm = TRUE)), .SDcols = c("steps")]
IntervalDT_WD <- activityDT_WD[, c(lapply(.SD, mean, na.rm = TRUE)), .SDcols = c("steps"), by = .(interval, `weekday_type`)]
datatable(IntervalDT_WD)
ggplot(IntervalDT_WD , aes(x = interval , y = steps, color=`weekday_type`)) +
geom_line(size=1) +
labs(title = "Avg. Daily Steps by Weektype", x = "Interval", y = "No. of Steps") +
facet_wrap(~`weekday_type` , ncol = 1, nrow=2)