#Load Packages

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.5
library(dplyr)
## 
## 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(magrittr)

#Load Data

load("brfss2013.Rdata")

#Research question 1: Exploratory data analysis on the variable sleptim1 and addepev2 in terms of the following:

#1.1 What are its statistics using the function summary in R?

#Statistics on variable using the function summary in R

summary(brfss2013$sleptim1)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   0.000   6.000   7.000   7.052   8.000 450.000    7387

#Satistics on variable “addepev2” using the function summary in R

summary(brfss2013$addepev2)
##    Yes     No   NA's 
##  95779 393707   2289

#1.2 Provide statistics using the function summary without NA’s and data with at most 10 hours of sleep.

#Function summary in R without NA’s

withoutNA<-brfss2013%>%
  filter(!is.na(sleptim1))
summary(withoutNA$sleptim1)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   6.000   7.000   7.052   8.000 450.000

#Function summary in R with at most 10 hours of sleep

Atmost10hoursofsleep<-withoutNA%>%
  filter(sleptim1<11)
  summary(Atmost10hoursofsleep$sleptim1)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   6.000   7.000   6.976   8.000  10.000

#Reseach Question 2: What analysis can you share on the Perception of others to the Depressive Disorder of the Respondents with those having less than 6 hours of sleep on average?

y <- withoutNA %>%
  filter(!is.na(sleptim1),!is.na(addepev2),sleptim1<6)%>%
  group_by(addepev2)%>%
  summarise(count=n())
y
## # A tibble: 2 x 2
##   addepev2 count
##   <fct>    <int>
## 1 Yes      17828
## 2 No       34275
ggplot(data=y,aes(x=addepev2,y=count))+geom_bar(stat="identity",color='red',fill='blue')+xlab("Depressive Disorder for people having at most 5 hours of average sleep")+ylab("Number of Respondents")

(17828/(17828+34275))
## [1] 0.3421684

#The above results show that 34.22% are those having Depression Disorder of the Respondents with those having less than 6 hours of sleep on the average.

#Research question 3: What insights can you provide in comparing between having less than 6 hours of sleep and having 6 to 10 hours of sleep that were perceived with depression disorder(addepev2)?

z <- Atmost10hoursofsleep %>%
  filter(!is.na(sleptim1),!is.na(addepev2),sleptim1>5)%>%
  group_by(addepev2)%>%
  summarise(count=n())
z
## # A tibble: 2 x 2
##   addepev2  count
##   <fct>     <int>
## 1 Yes       73771
## 2 No       350259
ggplot(data=z,aes(x=addepev2,y=count))+geom_bar(stat="identity",color='red',fill='blue')+xlab("Depressive Disorder for people having 6 to 10 hours average sleep")+ylab("Number of Respondents ")

73771/(73771+350259)
## [1] 0.1739759

#The above results show that 17.40% are those having 6 to 10 hours of average of sleep have lower depression disorder as perceived by others.