library(readr)
mydata <- read_csv("Sleep_health_and_lifestyle_dataset.csv")
## Rows: 374 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Gender, Occupation, BMI Category, Blood Pressure, Sleep Disorder
## dbl (8): Person ID, Age, Sleep Duration, Quality of Sleep, Physical Activity...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
set.seed(1)
mydata <- mydata[sample(nrow(mydata), 200), ]
#Excluding unimportant variables
mydata<- mydata[, -c(3,4,6,7,9:12)]
#Factoring sleep disorder variable
mydata$`Sleep Disorder` <- factor(mydata$`Sleep Disorder`,
levels = c("None", "Sleep Apnea", "Insomnia"),
labels =c("No", "Yes", "Yes"))
#Factoring gender variable
mydata$Gender <- factor(mydata$Gender,
levels = c("Female", "Male"),
labels =c("Female", "Male"))
#Creating dataset for RQ1
mydataSleep <- mydata[, -c(2,5)]
#Creating dataset for RQ2
mydataSleep2 <- mydata[, -c(3,4)]
head(mydata)
## # A tibble: 6 × 5
## `Person ID` Gender `Sleep Duration` `Stress Level` `Sleep Disorder`
## <dbl> <fct> <dbl> <dbl> <fct>
## 1 324 Female 8.5 3 No
## 2 167 Male 7.3 6 No
## 3 129 Male 7.3 5 No
## 4 299 Female 8.5 3 No
## 5 270 Female 6.1 8 Yes
## 6 187 Female 6.7 4 Yes
The data covers a range of variables related to sleep and health. Unit of observation is one person.
Sample size is 200, there are 5 variables:
Source of data: Kaggle databases - Sleep Health and Lifestyle Dataset
library(psych)
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
summary(mydataSleep[, -c(1)])
## Sleep Duration Stress Level
## Min. :6.000 Min. :3.00
## 1st Qu.:6.400 1st Qu.:4.00
## Median :7.200 Median :5.00
## Mean :7.151 Mean :5.38
## 3rd Qu.:7.800 3rd Qu.:7.00
## Max. :8.500 Max. :8.00
summary(mydataSleep2[, -c(1)])
## Gender Sleep Disorder
## Female: 95 No :121
## Male :105 Yes: 79
People on average sleep 7.151 hours per day. 75% of observed units sleep 7.8 hours or less, the minimum amount of sleep is 6 hours. The average stress level is 5.38 and maximum one 8.
There are 95 females and 105 males, 121 of all people do not have a sleep disorder, while 79 have it.
hist(mydata$`Sleep Duration`,
xlab = "Sleep duration",
ylab = "Frequency",
main = "Histogram of Sleep duration",
col = "mediumblue")
hist(mydata$`Stress Level`,
xlab = "Stress level",
ylab = "Frequency",
main = "Histogram of Stress level",
breaks = seq(from = 2, to = 8, by = 1),
col = "lightgreen")
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
#Checking scatterplot
scatterplot(mydataSleep$`Sleep Duration`, mydataSleep$`Stress Level`,
ylim = c(2, 9),
xlim = c(6,8.5),
col = "blue",
smooth=FALSE)
#Checking normality with Shapiro-Wilk test
shapiro.test(mydataSleep$`Sleep Duration`)
##
## Shapiro-Wilk normality test
##
## data: mydataSleep$`Sleep Duration`
## W = 0.93241, p-value = 5.272e-08
shapiro.test(mydataSleep$`Stress Level`)
##
## Shapiro-Wilk normality test
##
## data: mydataSleep$`Stress Level`
## W = 0.89966, p-value = 2.39e-10
Hypothesis for both variables
H0: Variable is normally distributed.
H1: Variable is not normally distributed.
We reject both H0 at p<0.001. We cannot say variables sleep duration and stress level are normally distributed.
Because variances are not normally distributed, we use Spearman correlation coefficient.
#Checking correlation - Pearson
library(Hmisc)
##
## Attaching package: 'Hmisc'
## The following object is masked from 'package:psych':
##
## describe
## The following objects are masked from 'package:base':
##
## format.pval, units
cor(mydataSleep$`Sleep Duration`, mydataSleep$`Stress Level`,
method= "pearson")
## [1] -0.8034371
cor.test(mydataSleep$`Sleep Duration`, mydataSleep$`Stress Level`,
method = "pearson",
exact = FALSE)
##
## Pearson's product-moment correlation
##
## data: mydataSleep$`Sleep Duration` and mydataSleep$`Stress Level`
## t = -18.988, df = 198, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.8476867 -0.7480849
## sample estimates:
## cor
## -0.8034371
#Checking correlation - Spearman <- CORRECT
cor(mydataSleep$`Sleep Duration`, mydataSleep$`Stress Level`,
method= "spearman")
## [1] -0.7994433
cor.test(mydataSleep$`Sleep Duration`, mydataSleep$`Stress Level`,
method = "spearman",
exact = FALSE)
##
## Spearman's rank correlation rho
##
## data: mydataSleep$`Sleep Duration` and mydataSleep$`Stress Level`
## S = 2399198, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.7994433
Hypothesis:
H0: There is no correlation.
H1: There is a correlation.
We reject H0 at p<0.001. We can conclude that there is a correlation between sleep duration and stress level.
Linear relationship between sleep duration and stress level is negative and strong (Spearman correlation coefficient is -0.799)
results <- chisq.test(mydataSleep2$Gender, mydataSleep2$`Sleep Disorder`,
correct = TRUE) #Correction because of 2x2 table
results
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mydataSleep2$Gender and mydataSleep2$`Sleep Disorder`
## X-squared = 12.031, df = 1, p-value = 0.0005231
Hypothesis:
H0: There is no association between gender and sleep disorder.
H1: There is an association between gender and sleep disorder.
We reject H0 at p<0.001. We can conclude that there is an association between gender and sleep disorder.
#Checking empirical frequencies
addmargins(results$observed)
## mydataSleep2$`Sleep Disorder`
## mydataSleep2$Gender No Yes Sum
## Female 45 50 95
## Male 76 29 105
## Sum 121 79 200
#Checking expected frequencies
round(results$expected, 2)
## mydataSleep2$`Sleep Disorder`
## mydataSleep2$Gender No Yes
## Female 57.48 37.52
## Male 63.52 41.48
#Checking residuals
round(results$residuals, 2)
## mydataSleep2$`Sleep Disorder`
## mydataSleep2$Gender No Yes
## Female -1.65 2.04
## Male 1.57 -1.94
Discrepancy is significant for category Female-Yes at alpha = 5%. The number of females that have sleep disorders is higher than expected.
Category Male-Yes is on the borderline of significance (almost 1.96 -> significance at alpha = 5%, so we could say that there are less than expected men with sleep disorders).
addmargins(round(prop.table(results$observed), 3))
## mydataSleep2$`Sleep Disorder`
## mydataSleep2$Gender No Yes Sum
## Female 0.225 0.250 0.475
## Male 0.380 0.145 0.525
## Sum 0.605 0.395 1.000
Explanation of 0.225 (F-No) -> Out of 200 people, there are 22.5% of them that are female and do not have a sleep disorder.
addmargins(round(prop.table(results$observed, 1), 3), 2)
## mydataSleep2$`Sleep Disorder`
## mydataSleep2$Gender No Yes Sum
## Female 0.474 0.526 1.000
## Male 0.724 0.276 1.000
Explanation of 0.526 (F-Yes) -> Out of all females, 52.6% of them have a sleep disorder.
Explanation of 0.724 (M-No) -> Out of all males, 72.4% of them do not have a sleep disorder.
addmargins(round(prop.table(results$observed, 2), 3), 1)
## mydataSleep2$`Sleep Disorder`
## mydataSleep2$Gender No Yes
## Female 0.372 0.633
## Male 0.628 0.367
## Sum 1.000 1.000
Explanation of 0.633 (F-Yes) Out of all people that have sleep disorder, 63.3% of them are female.
Explanation of 0.367 (M-Yes) Out of all people that have sleep disorder, 36.7% of them are male.
library(effectsize)
##
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
##
## phi
effectsize::cramers_v(mydataSleep2$Gender, mydataSleep2$`Sleep Disorder`)
## Cramer's V (adj.) | 95% CI
## --------------------------------
## 0.25 | [0.12, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.25)
## [1] "medium"
## (Rules: funder2019)
There is a medium effect.
fisher.test(mydataSleep2$Gender, mydataSleep2$`Sleep Disorder`)
##
## Fisher's Exact Test for Count Data
##
## data: mydataSleep2$Gender and mydataSleep2$`Sleep Disorder`
## p-value = 0.0004735
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.1826109 0.6432350
## sample estimates:
## odds ratio
## 0.3453531
Hypothesis:
H0: Odds ratio is equal to 1.
H1: Odds ratio is different than 1.
We reject H0 at p<0.001. We statistically proved that odds ratio is different than 1. Sleep disorder is not equally present in different genders.
Women are more likely to have a sleep disorder than men.