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 project 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 project can be downloaded from the course web site:
The variables included in this dataset are:
library(data.table)
library(ggplot2)
library(scales)
library(lubridate)
Downloading and unzipping data to obtain the csv file.
url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
download.file(url, destfile = "Factivity.zip")
unzip("Factivity.zip")
Reading csv into a data.table.
activity_data <-fread("activity.csv")
activity_data$date <- as.Date(activity_data$date)
total_steps <- aggregate(steps ~ date, data = activity_data, sum)
plot1 <- ggplot(total_steps, aes(x=steps)) +
geom_histogram(fill = "deepskyblue4", bins = 11) +
labs(title = "Daily Steps", x = "Step Count", y = "Frequency")
print(plot1)
mean(total_steps$steps, na.rm = T)
## [1] 10766.19
median(total_steps$steps, na.rm = T)
## [1] 10765
Calculating the average steps taken for each 5-minute interval.
interval_steps <- aggregate(steps ~ interval, data = activity_data, mean)
Converting ‘interval’ column data to a vailid date-time format.
interval_steps$time <- as.character(interval_steps$interval)
for (i in 1:2){
interval_steps$time[i] <- as.character(paste0("0",interval_steps$time[i]))
}
for (i in 1:12){
interval_steps$time[i] <- as.character(paste0("00",interval_steps$time[i]))
}
for (i in 13:120){
interval_steps$time[i] <- as.character(paste0("0",interval_steps$time[i]))
}
interval_steps$time <- as.POSIXct(interval_steps$time, format = "%H%M")
Now that we have the valid date-time format we can have a cleaner looking time-series plot, for the average 24-hour period.
plot2 <- ggplot(interval_steps, aes(x = time, y = steps)) +
geom_line(col = "deepskyblue4") +
labs(title = "Time Series Plot of Average Steps Taken", x = "Time of Day", y = "Steps") +
scale_x_datetime(labels = date_format("%H:%M", tz = "MST"), date_breaks = "4 hours")
print(plot2)
interval_steps[which.max(interval_steps$steps),1:2]
## interval steps
## 104 835 206.1698
The total amount of NA’s and the percentage of missing step data.
nas <- is.na(activity_data$steps)
sum(nas)
## [1] 2304
mean(nas)
## [1] 0.1311475
Function to impute the mean for a 5-minute interval into the appropriate mssing data (NA’s).
replaceNas= function(steps, interval) {
replace = NA
if (!is.na(steps)) {
replace = steps }
else {
replace = interval_steps[interval_steps$interval == interval, "steps"]}
return(replace) }
Apply ‘replaceNas’ function.
filled_activity_data = activity_data
filled_activity_data$steps = mapply(replaceNas, filled_activity_data$steps, filled_activity_data$interval)
total_steps_filled <- aggregate(steps ~ date, data = filled_activity_data, sum)
plot3 <- ggplot(total_steps_filled, aes(x = steps)) +
geom_histogram(fill = "deepskyblue4", bins = 11) +
labs(title = "Daily Steps with replaces NA's", x = "Step Count", y = "Frequency")
print(plot3)
Creating two new factor variables showing the day of the week, and if its a weekday of weekend.
filled_activity_data$day <- weekdays(filled_activity_data$date)
weekday <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
weekDayOp <- function(dayofweek) {
fill = ""
if (dayofweek %in% weekday) {
fill = "Weekday" }
else {
fill = "Weekend" }
return(fill) }
filled_activity_data$weekday <- mapply(weekDayOp,filled_activity_data$day)
Calculating the average number of steps per 5-minute interval for weekdays and weekends.
filled_totals_day <- aggregate(steps ~ interval + weekday, data = filled_activity_data, mean)
Coverting ‘interval’ column data to a vailid date-time format.
filled_totals_day$time <- as.character(filled_totals_day$interval)
for (i in 1:2){
filled_totals_day$time[i] <- as.character(paste0("0",filled_totals_day$time[i]))
}
for (i in 1:12){
filled_totals_day$time[i] <- as.character(paste0("00",filled_totals_day$time[i]))
}
for (i in 13:120){
filled_totals_day$time[i] <- as.character(paste0("0",filled_totals_day$time[i]))
}
for (i in 289:290){
filled_totals_day$time[i] <- as.character(paste0("0",filled_totals_day$time[i]))
}
for (i in 289:300){
filled_totals_day$time[i] <- as.character(paste0("0",filled_totals_day$time[i]))
}
for (i in 301:408){
filled_totals_day$time[i] <- as.character(paste0("0",filled_totals_day$time[i]))
}
filled_totals_day$time <- as.POSIXct(filled_totals_day$time, format = "%H%M")
Now that we have the valid date time format we can have a cleaner looking Time-Seriesplot, for comparison of the average 24-hour period on weekdays and weekends.
plot4 <- ggplot(filled_totals_day, aes(time, steps, col = factor(weekday))) +
facet_grid(.~factor(weekday)) +
geom_line(show.legend = F) +
labs(x = "Time of Day") +
labs(y = "Steps") +
labs(title = "Time Series Plot Comparison of Steps") +
scale_x_datetime(labels = date_format("%H:%M", tz = "MST"), date_breaks = "6 hours")
print(plot4)