Data downloaded from: https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip 2023-Sep-05

Description of data from: https://github.com/ZachArnt99/RepData_PeerAssessment1_Zach-Arnt/blob/master/README.md 2023-SEP-05

Description of data: 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 dataset is stored in a comma-separated-value (CSV) file and there are a total of 17,568 observations in this dataset.

Analysis

#Load analysis package(s)

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

#Read in the data

activity <- read.csv("activity.csv")

#Add columns for use in analysis and plot histogram

activity <- mutate(activity, "intervalID" = 1:17568,
                   "day" = ceiling(intervalID*5/(60*24)),
                   "intervalNUMBER" = rep(1:288,61))
head(activity)
##   steps       date interval intervalID day intervalNUMBER
## 1    NA 2012-10-01        0          1   1              1
## 2    NA 2012-10-01        5          2   1              2
## 3    NA 2012-10-01       10          3   1              3
## 4    NA 2012-10-01       15          4   1              4
## 5    NA 2012-10-01       20          5   1              5
## 6    NA 2012-10-01       25          6   1              6
stepsperday <- aggregate(activity$steps, by = list(day = activity$day),
                         FUN = sum, simplify=TRUE, na.rm=TRUE)
head(stepsperday)
##   day     x
## 1   1     0
## 2   2   126
## 3   3 11352
## 4   4 12116
## 5   5 13294
## 6   6 15420
hist(stepsperday$x, breaks=20, main = "Histogram of all 61 Days",
     xlab = "Steps per Day", ylab = "Number of Days")
rug(stepsperday$x)

#Average and median steps per day, ignoring missing values:

mean(stepsperday$x)
## [1] 9354.23
median(stepsperday$x)
## [1] 10395

#Store results in a data.frame for later analysis

results <- data.frame("NA Handling" = "ignore", "Mean" = mean(stepsperday$x), 
                      "Median" = median(stepsperday$x))

#Plot the average number of steps taken during each interval over 61 days.

stepsperinterval <- aggregate(activity[, "steps"],
                              by = list(interval = activity$intervalNUMBER),
                              FUN = mean, simplify=TRUE, na.rm=TRUE)

plot(stepsperinterval$interval,stepsperinterval$x,type="l",
     main="Average Steps Taken During Each Interval\nAveraged Over 61 Days",
     xlab= "Interval Number",ylab= "Avg. Steps Taken")

#On average, the interval number with the most steps over the 61-day trail is

which(stepsperinterval$x == max(stepsperinterval$x))
## [1] 104

#The total number of rows with NA is

nrow(activity) - nrow(na.omit(activity))
## [1] 2304

#The rows with NAs are:

NArows <- which(is.na(activity$steps))

#These correspond to intervalNUMBERs

NAintervals <- activity$intervalNUMBER[NArows]

#Method 1: Impute a complete dataset by replacing NAs with averages.

activity1 <- activity
means1 <- stepsperinterval$x
activity1$steps[NArows] <- means1[NAintervals]

#Histogram of steps per day in imputed dataset using Method 1 (mean)

imputed1 <- aggregate(activity1[, "steps"],
                      by = list(day = activity1$day),
                      FUN = sum)
hist(imputed1$x, breaks=20, main = "Imputed Histogram of all 61 Days\nMethod 1 (Mean)",
     xlab = "Steps per Day", ylab = "Number of Days")
rug(imputed1$x)

#Average and median steps per day after imputing missing values with Method 1 (mean):

mean(imputed1$x)
## [1] 10766.19
median(imputed1$x)
## [1] 10766.19

#Update results data.frame for later analysis

results[2,1] <- "impute MEAN"
results[2,2] <- mean(imputed1$x)
results[2,3] <- median(imputed1$x)

#Method 2: Impute a complete dataset by replacing NAs with medians.

activity2 <- activity
stepsperinterval2 <- aggregate(activity2[, "steps"],
                               by = list(interval = activity2$intervalNUMBER),
                               FUN = median, simplify=TRUE, na.rm=TRUE)
medians <- stepsperinterval2$x
activity2$steps[NArows] <- medians[NAintervals]

#Histogram of steps per day in imputed dataset using Method 2 (median)

imputed2 <- aggregate(activity2[, "steps"],
                      by = list(day = activity2$day),
                      FUN = sum)

hist(imputed2$x, breaks=20, main = "Imputed Histogram of all 61 Days\nMethod 2 (Median)",
     xlab = "Steps per Day", ylab = "Number of Days")
rug(imputed2$x)

#Average and median steps per day after imputing missing values with Method 2 (median):

mean(imputed2$x)
## [1] 9503.869
median(imputed2$x)
## [1] 10395

#Update results data.frame and compare results

results[3,1] <- "impute MEDIAN"
results[3,2] <- mean(imputed2$x)
results[3,3] <- median(imputed2$x)

#The table of results shows how estimates of mean and median steps per day change with the method of dealing with NA values. Both of the means go up. However, when imputing with the median, four of the new values are greater than 10,395 and four are less, so the median does not change.

results
##     NA.Handling      Mean   Median
## 1        ignore  9354.230 10395.00
## 2   impute MEAN 10766.189 10766.19
## 3 impute MEDIAN  9503.869 10395.00

#Factorize imputed data set by dayTYPE to compare weekdays and weekends

dayvect <- weekdays(as.Date(activity2$date),abbreviate=TRUE)
wkends <- c("Sat", "Sun")
dayTYPE <- factor((dayvect %in% wkends), 
                  levels=c(TRUE,FALSE), labels=c("weekend", "weekday")) 

activity2 <- mutate(activity2,dayTYPE=dayTYPE)
head(activity2)
##   steps       date interval intervalID day intervalNUMBER dayTYPE
## 1     0 2012-10-01        0          1   1              1 weekday
## 2     0 2012-10-01        5          2   1              2 weekday
## 3     0 2012-10-01       10          3   1              3 weekday
## 4     0 2012-10-01       15          4   1              4 weekday
## 5     0 2012-10-01       20          5   1              5 weekday
## 6     0 2012-10-01       25          6   1              6 weekday
stepsperinterval3 <- split(activity2,f=dayTYPE)

weekdays <- aggregate(stepsperinterval3$weekday[, "steps"],
                      by = list(interval = stepsperinterval3$weekday$intervalNUMBER),
                      FUN = mean)
weekends <- aggregate(stepsperinterval3$weekend[, "steps"],
                      by = list(interval = stepsperinterval3$weekend$intervalNUMBER),
                      FUN = mean)

par(mfrow=c(1,2),mar=c(4,4,5,1))
plot(weekdays$interval,weekdays$x,type="l",
     main="Average Steps Taken on\nWeekdays During Each Interval\nImputed with Median and\nAveraged Over 61 Days",
     xlab= "Interval Number",ylab= "Avg. Steps Taken")
plot(weekends$interval,weekends$x,type="l",
     main="Average Steps Taken on\nWeekends During Each Interval\nImputed with Median and\nAveraged Over 61 Days",
     xlab= "Interval Number",ylab= "Avg. Steps Taken")