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:
Dataset: Activity monitoring data [52K]
Now we will load the data and look it
activity<-read.csv("activity.csv")
str(activity)
## 'data.frame': 17568 obs. of 3 variables:
## $ steps : int NA NA NA NA NA NA NA NA NA NA ...
## $ date : chr "2012-10-01" "2012-10-01" "2012-10-01" "2012-10-01" ...
## $ interval: int 0 5 10 15 20 25 30 35 40 45 ...
summary(activity)
## steps date interval
## Min. : 0.00 Length:17568 Min. : 0.0
## 1st Qu.: 0.00 Class :character 1st Qu.: 588.8
## Median : 0.00 Mode :character Median :1177.5
## Mean : 37.38 Mean :1177.5
## 3rd Qu.: 12.00 3rd Qu.:1766.2
## Max. :806.00 Max. :2355.0
## NA's :2304
First we sum the total steps per day using tapply() function and store the result in tot_steps_pday. Then plot the histogram of steps per day
tot_steps_pday<-tapply(activity$steps,activity$date,sum,na.rm=TRUE)
hist(tot_steps_pday,main = "TOTAL NUMBER OF STEPS TAKEN PER DAY",
xlab="Total steps taken per day",ylab="Frequency",
col="red",breaks = seq(0,25000,2500))
The mean and median of steps per day is:
## [1] "Mean= 9354.22950819672"
## [1] "Median= 10395"
We find mean of total steps per interval across all dates using tapply() function and store the result in avg_steps_pint. Then we make a time series plot of average steps per interval
avg_steps_pint<-tapply(activity$steps,activity$interval,mean,na.rm=TRUE)
avg_steps_pint_table<-cbind(names(avg_steps_pint),avg_steps_pint)
plot(avg_steps_pint_table,main="Average steps taken per Interval across each Date",
xlab="Intervals",ylab="Average Steps Taken per Interval",
col="turquoise",type="l",lwd=2)
The 5 minute interval, on average across all days, which contains the maximum number of steps is
## [1] "835"
Total number of missing values in dataset is
## [1] 2304
The presence of missing days may introduce bias into some calculations or summaries of the data. So to tackle the situation, a strategy has been devised. All the missing values will be replaced by the mean value of steps of the corresponding interval and the new data in new_activity
new_activity<-activity
for(i in 1:nrow(activity)){
if(is.na(new_activity[i,]$steps)){
missing_interval<-new_activity[i,]$interval
new_activity[i,]$steps<-avg_steps_pint[as.character(missing_interval)]
}
}
As new dataset new_activity does not contain any missing values, we again plot the histogram of total steps taken each day
new_tot_steps_pday<-tapply(new_activity$steps,new_activity$date,sum,na.rm=TRUE)
hist(new_tot_steps_pday,main = "TOTAL NUMBER OF STEPS TAKEN PER DAY",
xlab="Total steps taken per day",ylab="Frequency",
col="red",breaks = seq(0,25000,2500))
Mean and Median of total steps taken per day with respect to new_activity is
## [1] "Mean= 10766.1886792453"
## [1] "Median= 10766.1886792453"
So, as the missing values are filled, the mean and median has increased significantly.
We have created new variable in the dataset daytype which identifies whether the specified date falls on Weekday or Weekend using the function weekdays().
library(lubridate)
new_activity$date<-ymd(new_activity$date)
new_activity$day<-weekdays(new_activity$date)
new_activity$daytype<-"Weekday"
for(i in 1:nrow(new_activity)){
if(new_activity[i,]$day=="Saturday"|new_activity[i,]$day=="Sunday"){
new_activity[i,]$daytype<-"Weekend"
}
}
To look at the variations between Weekday and Weekend, we plot a panel plot of average steps taken per interval across all dates.
library(dplyr)
library(lattice)
agg_activity<-aggregate(new_activity$steps~new_activity$interval+new_activity$daytype,new_activity,mean)
names(agg_activity)<-c("interval","daytype","steps")
xyplot(steps~interval|daytype,agg_activity,type="l",layout=c(1,2),
xlab="Interval",ylab="Mean Number of steps")