Project 1 | Reproducible Research

This is a r markdown file for the project 1 in resproducible research course. Make sure you use the html version to see plots along with codes

First of all we’ll get the required libraries

library(readr)
## Warning: package 'readr' was built under R version 4.1.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.1.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3

Download and Read the Datafile

destfile <- "D:/Learn/eLearning Database/R for Data Science/Reproducible Research/Week I/repdata_data_activity.zip"
        
        download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip", destfile = destfile )
        
        unzip("repdata_data_activity.zip")
        
        #Read Data
        
                activity <- read_csv("activity.csv")
## Rows: 17568 Columns: 3
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## dbl  (2): steps, interval
## date (1): date
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
        # Data Cleaning
              
                activity <- as_tibble(activity) 

Now we’ll solve all the questions one-by-one

Question A: What is the mean total number of steps taken per day ?

1. Calculate Total Number of Steps Taken Per Day

        spd <- activity %>% group_by(date) %>% summarise(steps = sum(steps))
        spd
## # A tibble: 61 x 2
##    date       steps
##    <date>     <dbl>
##  1 2012-10-01    NA
##  2 2012-10-02   126
##  3 2012-10-03 11352
##  4 2012-10-04 12116
##  5 2012-10-05 13294
##  6 2012-10-06 15420
##  7 2012-10-07 11015
##  8 2012-10-08    NA
##  9 2012-10-09 12811
## 10 2012-10-10  9900
## # ... with 51 more rows

2. Make a histogram of the total number of steps taken each day

        ggplot(spd, aes(steps)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 8 rows containing non-finite values (stat_bin).

3. Calculate and report the mean and median of the total number of steps taken per day

        spd %>% select(steps) %>% drop_na() %>% summarise(mean = mean(steps), median=median(steps))
## # A tibble: 1 x 2
##     mean median
##    <dbl>  <dbl>
## 1 10766.  10765

Question B: What is the average daily activity pattern?

1. Make a time series plot (i.e.type = “l”) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)

                activity %>% drop_na() %>%
                        group_by(interval) %>%
                        summarise(steps = mean(steps)) %>%
                        ggplot(aes(x=interval, y=steps)) +
                        geom_line()

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

                activity %>% drop_na() %>%
                        group_by(interval) %>%
                        summarise(steps = mean(steps)) %>% arrange(desc(steps))
## # A tibble: 288 x 2
##    interval steps
##       <dbl> <dbl>
##  1      835  206.
##  2      840  196.
##  3      850  183.
##  4      845  180.
##  5      830  177.
##  6      820  171.
##  7      855  167.
##  8      815  158.
##  9      825  155.
## 10      900  143.
## # ... with 278 more rows

Question C: Imputing Missing Values

1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)

                activity %>% select(steps) %>%  count(is.na(.))
## # A tibble: 2 x 2
##   `is.na(.)`[,"steps"]     n
##   <lgl>                <int>
## 1 FALSE                15264
## 2 TRUE                  2304

2. Devise a strategy for filling in all of the missing values in the dataset.The strategy does not need to be sophisticated. For example,you could use the mean/median for that day, or the mean for that 5-minute interval, etc.

                refill_for_na <- mean(activity$steps, na.rm = T)

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

                new_data <- activity
                
                for (i in 1:length(new_data$steps)) {
                        if (is.na(new_data$steps[i]))
                                new_data$steps[i] <- refill_for_na
                        
                }
                
                new_data
## # A tibble: 17,568 x 3
##    steps date       interval
##    <dbl> <date>        <dbl>
##  1  37.4 2012-10-01        0
##  2  37.4 2012-10-01        5
##  3  37.4 2012-10-01       10
##  4  37.4 2012-10-01       15
##  5  37.4 2012-10-01       20
##  6  37.4 2012-10-01       25
##  7  37.4 2012-10-01       30
##  8  37.4 2012-10-01       35
##  9  37.4 2012-10-01       40
## 10  37.4 2012-10-01       45
## # ... with 17,558 more rows

4. Make a histogram of the total number of steps taken each day and Calculate and report the mean and median total number of steps taken per day. Do these values differ from the estimates from the first part of the assignment? What is the impact of imputing missing data on the estimates of the total daily number of steps?

Histogram | Total Steps Each Day

                new_data %>% group_by(date) %>% summarise(steps = sum(steps)) %>%
                        ggplot(aes(steps)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Mean and Median

                new_data %>% group_by(date) %>% summarise(steps = sum(steps)) %>%
                        summarise(mean= mean(steps), median = median(steps))
## # A tibble: 1 x 2
##     mean median
##    <dbl>  <dbl>
## 1 10766. 10766.

Difference from earlier data

Answer
both mean and median is 10766 in new data in earlier data set mean was 10766 and median was 10765 so the impact here is that in new data set after after replacing NAs with mean has pushed median in new dataset to go more towards mean.

Qustion D: Are there differences in activity patterns between weekdays and weekends?

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.

                new_data$day <- ifelse(
                        weekdays(new_data$date) %in% c("Saturday", "Sunday"),
                        "weekday",
                        "weekend"
                )

2. Make a panel plot containing a time series plot (i.e.type = “l”) of the 5-minute interval (x-axis) and the average number of steps taken,averaged across all weekday days or weekend days (y-axis). See the README file in the GitHub repository to see an example of what this plot should look like using simulated data.

                new_data %>% group_by(interval,day) %>% summarise(steps=mean(steps)) %>%
                        ggplot(aes(x=interval, y=steps, color=day)) + geom_line()
## `summarise()` has grouped output by 'interval'. You can override using the
## `.groups` argument.

Thank You! | The End