Dataset was used from Kaggle: https://www.kaggle.com/datasets/equilibriumm/sleep-efficiency
Research Question - Does average sleep duration differ between males and females?
mydata <- read.table("/home/mohor/Desktop/MVA/HW1/Sleep_Efficiency.csv", header=TRUE, sep=",", dec=".")
#Chose only selected columns from dataset
mydata2 <- mydata[, c("ID", "Age", "Gender", "Sleep.duration", "Sleep.efficiency", "Deep.sleep.percentage")]
head(mydata2)
## ID Age Gender Sleep.duration Sleep.efficiency Deep.sleep.percentage
## 1 1 65 Female 6.0 0.88 70
## 2 2 69 Male 7.0 0.66 28
## 3 3 40 Female 8.0 0.89 70
## 4 4 40 Female 6.0 0.51 25
## 5 5 57 Male 8.0 0.76 55
## 6 6 36 Female 7.5 0.90 60
Description:
-ID: person ID
-Age: age of the test subject
-Gender: Male/Female -Bedtime: the time the test subject goes to bed
each night -Wake up time: the time the test subject wakes up each
morning -Sleep duration: the total amount of time the test subject slept
(in hours)
-Sleep efficiency: a measure of the proportion of time in bed spent
asleep
-REM sleep percentage: the percentage of total sleep time spent in REM
sleep
-Deep sleep percentage: the percentage of total sleep time spent in deep
sleep
-Light sleep percentage: the percentage of total sleep time spent in
light sleep
-Awakenings: numbers of awakenings
# random sample of 100 units, for the purpose of testing hypothesis and normality
set.seed(2)
mydata2 <- mydata2[sample(nrow(mydata), 100),]
head(mydata2)
## ID Age Gender Sleep.duration Sleep.efficiency Deep.sleep.percentage
## 341 341 53 Male 7 0.85 65
## 198 198 48 Male 6 0.79 60
## 262 262 37 Female 7 0.76 57
## 416 416 49 Male 7 0.67 22
## 392 392 37 Female 5 0.52 20
## 273 273 58 Male 9 0.50 20
#checking if there are any missing values in the dataset
any_na <- any(is.na(mydata2))
print(any_na)
## [1] FALSE
#Changing variable gender to factor variable, so R views it as a categorical variable
mydata2$GenderF <- factor(mydata2$Gender,
levels = c("Male" , "Female"),
labels = c("Male" , "Female"))
summary(mydata2[,c(-1,-3)])
## Age Sleep.duration Sleep.efficiency Deep.sleep.percentage
## Min. : 9.00 Min. : 5.00 Min. :0.5000 Min. :18.00
## 1st Qu.:29.75 1st Qu.: 7.00 1st Qu.:0.6875 1st Qu.:35.00
## Median :41.50 Median : 7.50 Median :0.8200 Median :58.00
## Mean :41.41 Mean : 7.53 Mean :0.7886 Mean :52.16
## 3rd Qu.:51.25 3rd Qu.: 8.00 3rd Qu.:0.9000 3rd Qu.:65.00
## Max. :65.00 Max. :10.00 Max. :0.9600 Max. :72.00
## GenderF
## Male :45
## Female:55
##
##
##
##
We have 45 males and 55 females in sample, 50% of people in sample is older than 41.5 years. In average people in the sample slept 7.53 hours.
library(psych)
describe(mydata2[,c(-1, -3, -7)])
## vars n mean sd median trimmed mad min max
## Age 1 100 41.41 13.49 41.50 41.45 15.57 9.0 65.00
## Sleep.duration 2 100 7.53 0.95 7.50 7.49 0.74 5.0 10.00
## Sleep.efficiency 3 100 0.79 0.14 0.82 0.80 0.13 0.5 0.96
## Deep.sleep.percentage 4 100 52.16 16.26 58.00 53.80 10.38 18.0 72.00
## range skew kurtosis se
## Age 56.00 -0.10 -0.86 1.35
## Sleep.duration 5.00 0.38 0.84 0.09
## Sleep.efficiency 0.46 -0.75 -0.62 0.01
## Deep.sleep.percentage 54.00 -0.92 -0.69 1.63
Explanations of parameters
Age - min: The youngest person in the sample is 9 years old.
Sleep duration - mean: The sleep duration on average is 7.53
hours.
Sleep efficiency - mean: The proportion of time in bed spent asleep on
average is 0.79.
Deep sleep percentage - max: The maximum percentage of total sleep time
spent in deep sleep is 72%.
Does average sleep duration differ between males and females?
Hypothesis
H0: There is no significant difference in average sleep duration between
males and females.
H1: There is a significant difference in average sleep duration between
males and females.
Assumptions
-Variable is numeric(Sleep duration).
-The distribution of the variable is normal in both populations.
-Two independent populations(Males and females are independent).
-Variable has the same variance in both populations.
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggplot(mydata2, aes(x = Sleep.duration)) +
geom_histogram(binwidth = 0.8, colour="gray") +
facet_wrap(~GenderF, ncol = 1) +
ylab("Frequency") +
xlab("Sleep Duration")
Shapiro-Wilk Test for Normality
H0: Variable sleep duration is normally distributed in both groups
H1: Variable sleep duration is not normally distributed in both
groups
library(rstatix)
##
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
##
## filter
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
mydata2 %>%
group_by(GenderF) %>%
shapiro_test(Sleep.duration)
## # A tibble: 2 × 4
## GenderF variable statistic p
## <fct> <chr> <dbl> <dbl>
## 1 Male Sleep.duration 0.878 0.000209
## 2 Female Sleep.duration 0.926 0.00221
We reject H0, and accept H1 for both groups, at p<0.001 for males and p=0.003 for females. Because assumption of normality is violated we need to use WILCOXON RANK SUM TEST.
describeBy(mydata2$Sleep.duration, mydata2$GenderF)
##
## Descriptive statistics by group
## group: Male
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 45 7.54 0.98 7.5 7.47 0.74 5 10 5 0.62 1.18 0.15
## ------------------------------------------------------------
## group: Female
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 55 7.52 0.93 7.5 7.52 0.74 5 10 5 0.14 0.27 0.13
Alternative non-parametrical test (Wilcoxon Rank Sum Test)
H0: Distribution location of sleep duration is the same for males and
females.
H1: Distribution location of sleep duration is not the same for males
and females.
wilcox.test(mydata2$Sleep.duration ~ mydata2$GenderF,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "two.sided")
##
## Wilcoxon rank sum test
##
## data: mydata2$Sleep.duration by mydata2$GenderF
## W = 1235, p-value = 0.9856
## alternative hypothesis: true location shift is not equal to 0
We can not reject the null hypothesis (p=0.99).
library(effectsize)
##
## Attaching package: 'effectsize'
## The following objects are masked from 'package:rstatix':
##
## cohens_d, eta_squared
## The following object is masked from 'package:psych':
##
## phi
effectsize(wilcox.test(mydata2$Sleep.duration ~ mydata2$GenderF,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "two.sided"))
## r (rank biserial) | 95% CI
## ---------------------------------
## -2.02e-03 | [-0.23, 0.22]
interpret_rank_biserial(-0.00202)
## [1] "tiny"
## (Rules: funder2019)
Conclusion
Based on the sample data we can not reject the null hypothesis (p=0.99),
we can not say that the sleep duration differ between males and females.
The effect size is tiny(|r|=0.00202)
As normality assumption is not met, we have to use non-parametric test, however, due to educational reasons we will also show results of parametric test.
H0: There is no significant difference in average sleep duration
between males and females.
H1: There is significant difference in average sleep duration between
males and females.
t.test(mydata2$Sleep.duration ~ mydata2$GenderF,
paired = FALSE,
var.equal = FALSE,
alternative = "two.sided")
##
## Welch Two Sample t-test
##
## data: mydata2$Sleep.duration by mydata2$GenderF
## t = 0.1364, df = 91.838, p-value = 0.8918
## alternative hypothesis: true difference in means between group Male and group Female is not equal to 0
## 95 percent confidence interval:
## -0.3561399 0.4086652
## sample estimates:
## mean in group Male mean in group Female
## 7.544444 7.518182
We can not reject H0(p=0.90).
library(effectsize)
effectsize::cohens_d(mydata2$Sleep.duration ~ mydata2$GenderF,
pooled_sd = FALSE)
## Cohen's d | 95% CI
## -------------------------
## 0.03 | [-0.37, 0.42]
##
## - Estimated using un-pooled SD.
interpret_cohens_d(0.03, rules = "sawilowsky2009")
## [1] "tiny"
## (Rules: sawilowsky2009)
Conclusion
Based on the sample data we can not reject the null hypothesis (p=0.90), we can not say that the average sleep duration differ between males and females. The effect size is tiny(d=0,03).