Source: Data retrived on 13.1.2024, from: https://www.kaggle.com/datasets/uom190346a/sleep-health-and-lifestyle-dataset
library(readxl)
mydata <- read_excel("~/6_IMB/7_Class/7_MultivariateAnalysis/R_HW/Sleep3.xlsx")
mydata <- as.data.frame(mydata)
head(mydata)
## Person ID Gender Age Occupation Quality of Sleep
## 1 1 Male 27 Software Engineer 6
## 2 2 Male 28 Doctor 6
## 3 3 Male 28 Doctor 6
## 4 4 Male 28 Sales Representative 4
## 5 5 Male 28 Sales Representative 4
## 6 6 Male 28 Software Engineer 4
## Physical Activity Level StressLevel BMI Category Blood Pressure Heart Rate
## 1 42 6 Overweight 126/83 77
## 2 60 8 Normal 125/80 75
## 3 60 8 Normal 125/80 75
## 4 30 8 Obese 140/90 85
## 5 30 8 Obese 140/90 85
## 6 30 8 Obese 140/90 85
## DailySteps SleepDisorder
## 1 4200 None
## 2 10000 None
## 3 10000 None
## 4 3000 Sleep Apnea
## 5 3000 Sleep Apnea
## 6 3000 Insomnia
Data manipulation
set.seed(1) #Setting initial point of sampling.
mydata2 <- mydata[sample(nrow(mydata), 200), ] #Random sample of 200 units.
mydata3 <- mydata2[,c(1,2,7,11, 12)]
head(mydata3)
## Person ID Gender StressLevel DailySteps SleepDisorder
## 324 324 Female 3 5000 None
## 167 167 Male 6 6200 None
## 129 129 Male 5 8000 None
## 299 299 Female 3 5000 None
## 270 270 Female 8 10000 Sleep Apnea
## 187 187 Female 4 6000 Insomnia
#Changing into factor
mydata3$Gender <- factor(mydata3$Gender,
levels = c ("Male", "Female"),
labels = c ("Male", "Female"))
mydata3$SleepDisorder <- factor(mydata3$SleepDisorder,
levels = c("None", "Sleep Apnea" ,"Insomnia"),
labels = c("None", "Sleep Apnea" ,"Insomnia"))
head(mydata3)
## Person ID Gender StressLevel DailySteps SleepDisorder
## 324 324 Female 3 5000 None
## 167 167 Male 6 6200 None
## 129 129 Male 5 8000 None
## 299 299 Female 3 5000 None
## 270 270 Female 8 10000 Sleep Apnea
## 187 187 Female 4 6000 Insomnia
Explain data
In our sample (n=100) we are observing sleep health and lifestyle of people.
Unit of observation: a person
Descriptive statistics with explanation a few estimates of parameters
summary(mydata3)
## Person ID Gender StressLevel DailySteps SleepDisorder
## Min. : 1.00 Male :105 Min. :3.00 Min. : 3700 None :121
## 1st Qu.: 98.75 Female: 95 1st Qu.:4.00 1st Qu.: 6000 Sleep Apnea: 39
## Median :193.50 Median :5.00 Median : 7000 Insomnia : 40
## Mean :186.16 Mean :5.38 Mean : 6856
## 3rd Qu.:279.25 3rd Qu.:7.00 3rd Qu.: 8000
## Max. :373.00 Max. :8.00 Max. :10000
summary(mydata3[, c(2,5)])
## Gender SleepDisorder
## Male :105 None :121
## Female: 95 Sleep Apnea: 39
## Insomnia : 40
In our sample we have 105 Males and 95 Females. 121 of all people do not have sleep disorder. On the other hand, 79 have it (either Sleep Apnea or Insomnia).
Part1 Insturctions: Using two numerical variables from your dataset, calculate the appropriate correlation coefficient and explain it. Justify your decision. Perform the appropriate statistical test and interpret the result obtained. Answer your first research question clearly
Assumptions:
Normallity:
shapiro.test(mydata3$StressLevel)
##
## Shapiro-Wilk normality test
##
## data: mydata3$StressLevel
## W = 0.89966, p-value = 2.39e-10
We can reject H0 (p<0.001). Variable StressLevel is not normally distributed.
shapiro.test(mydata3$DailySteps)
##
## Shapiro-Wilk normality test
##
## data: mydata3$DailySteps
## W = 0.92504, p-value = 1.386e-08
We can reject H0 (p<0.001). Variable DailySteps is not normally distributed.
Therefore, we will perform non-parametric test (Spearman correlation test).
library(psych)
psych:: describe(mydata3[ , c("StressLevel" ,"DailySteps")])
## vars n mean sd median trimmed mad min max range
## StressLevel 1 200 5.38 1.73 5 5.35 1.48 3 8 5
## DailySteps 2 200 6856.50 1557.66 7000 6767.50 1482.60 3700 10000 6300
## skew kurtosis se
## StressLevel 0.13 -1.30 0.12
## DailySteps 0.26 -0.58 110.14
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
scatterplotMatrix(mydata3[,-c(1,2,4)], smooth = FALSE)
(Already form this Scatterplot matrix, we can see that we don’t have normal distribution.)
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggplot(mydata3, aes(x=StressLevel, y=DailySteps)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "pink")
## `geom_smooth()` using formula = 'y ~ x'
Based on the graph we can confirm assumption that the correlation between Daily Steps and Stress Level is linear.
Because the assumption of normality (checked with Shapiro-Wilks test) is not met, we will perform Spearman test.
cor(mydata3$StressLevel, mydata3$DailySteps,
method = "spearman",
use = "complete.obs")
## [1] 0.1790789
Hypothesis:
H0: There is no linear relationship between StressLevel and DailySteps ρ(StressLevel, DailySteps) = 0
H1: There is linear relationship between StressLevel and DailySteps ρ(StressLevem, DailySteps) =/= 0
cor.test(mydata3$StressLevel, mydata3$DailySteps,
method = "spearman",
exact = FALSE,
use = "compelte.obs")
##
## Spearman's rank correlation rho
##
## data: mydata3$StressLevel and mydata3$DailySteps
## S = 1094534, p-value = 0.01117
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.1790789
Conclusion: Based on sample data we can reject H0 (p = 0.012). We conclude that there is linear relationship between the two variables. The relationship between StressLevel and DailySteps is positive and weak (Spearman coefficient is 0.179).
Part2 Instructions: Using two categorical variables, perform the Pearson Chi2 test. Make sure that the necessary assumptions are met. Write down the null hypothesis and the alternative hypothesis as well as your findings based on the p-value of the test. Also calculate the standardized residuals and all three proportion tables (structures). Interpret standardized residuals and at least one frequency from each of the proportion tables. Calculate the effect size. Answer your second research question clearly.
Assumptions:
All assumptions are met.
Pearson Chi2 test
chi_square <- chisq.test (mydata3$Gender, mydata3$SleepDisorder,
correct = FALSE)
chi_square
##
## Pearson's Chi-squared test
##
## data: mydata3$Gender and mydata3$SleepDisorder
## X-squared = 27.102, df = 2, p-value = 1.303e-06
Conclusion 1: Based on sample data we reject H0 (p < 0.001) and conclude there is a relationship between Gender and SleepDisorder (they are associated).
What is the direction of the relationship?
observed/empirical frequencies
addmargins(chi_square$observed)
## mydata3$SleepDisorder
## mydata3$Gender None Sleep Apnea Insomnia Sum
## Male 76 6 23 105
## Female 45 33 17 95
## Sum 121 39 40 200
theoretical/expected frequencies
addmargins(round(chi_square$expected,2))
## mydata3$SleepDisorder
## mydata3$Gender None Sleep Apnea Insomnia Sum
## Male 63.52 20.48 21 105
## Female 57.48 18.52 19 95
## Sum 121.00 39.00 40 200
standard residuals
round(chi_square$res,2)
## mydata3$SleepDisorder
## mydata3$Gender None Sleep Apnea Insomnia
## Male 1.57 -3.20 0.44
## Female -1.65 3.36 -0.46
There is less people in our sample that have Apnea and are Male than we expected (alpha = 1%). The discrepancy is significant at 1%.
proportional table
addmargins(round(prop.table(chi_square$observed),3))
## mydata3$SleepDisorder
## mydata3$Gender None Sleep Apnea Insomnia Sum
## Male 0.380 0.030 0.115 0.525
## Female 0.225 0.165 0.085 0.475
## Sum 0.605 0.195 0.200 1.000
Comment: Out off 200 people, 11.5% of Male have Insomnia.
conditional proportional table
addmargins(round(prop.table(chi_square$observed, 1), 3), 2)
## mydata3$SleepDisorder
## mydata3$Gender None Sleep Apnea Insomnia Sum
## Male 0.724 0.057 0.219 1.000
## Female 0.474 0.347 0.179 1.000
Comment: Out of all people that are Female, 17.9% of them have Insomnia.
addmargins(round(prop.table(chi_square$observed, 2), 3), 1)
## mydata3$SleepDisorder
## mydata3$Gender None Sleep Apnea Insomnia
## Male 0.628 0.154 0.575
## Female 0.372 0.846 0.425
## Sum 1.000 1.000 1.000
Comment: Out of all people that have no sleep disorder, 62.8% are male.
effect size
library(effectsize)
##
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
##
## phi
effectsize::cramers_v(mydata3$Gender, mydata3$SleepDisorder)
## Cramer's V (adj.) | 95% CI
## --------------------------------
## 0.36 | [0.22, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.36)
## [1] "large"
## (Rules: funder2019)
Because my table is 3x2 I didn’t make odds ratios.
Conclusion 2: There is large effect wheater a person is Male or Female. We can see that there is more Males without sleep disorder than Females.