Dataset was used from Kaggle: https://www.kaggle.com/datasets/equilibriumm/sleep-efficiency

Research Question
RQ1: Is there correlation between alcohol consumption and sleep efficiency?
RQ2: Is there an association between smoking status and gender?

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.efficiency", "Sleep.duration", "Alcohol.consumption", "Smoking.status")]
head(mydata2)
##   ID Age Gender Sleep.efficiency Sleep.duration Alcohol.consumption
## 1  1  65 Female             0.88            6.0                   0
## 2  2  69   Male             0.66            7.0                   3
## 3  3  40 Female             0.89            8.0                   0
## 4  4  40 Female             0.51            6.0                   5
## 5  5  57   Male             0.76            8.0                   3
## 6  6  36 Female             0.90            7.5                   0
##   Smoking.status
## 1            Yes
## 2            Yes
## 3             No
## 4            Yes
## 5             No
## 6             No

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
-Caffeine consumption: the amount of caffeine consumed in the 24 hours prior to bedtime (in mg)
-Alcohol consumption: the amount of alcohol consumed in the 24 hours prior to bedtime (in oz)
-Smoking status: whether or not the test subject smokes
-Exercise frequency: the number of times the test subject exercises each week

# random sample of 200 units, for the purpose of testing hypothesis and normality
set.seed(1)
mydata2 <- mydata2[sample(nrow(mydata), 200),]
head(mydata2)
##      ID Age Gender Sleep.efficiency Sleep.duration Alcohol.consumption
## 324 324  41   Male             0.93            7.5                   0
## 167 167  40 Female             0.85            7.0                   0
## 129 129  33 Female             0.73            9.0                   1
## 418 418  44   Male             0.66            7.0                   3
## 299 299  35 Female             0.75            8.5                   1
## 270 270  27   Male             0.94            7.0                   0
##     Smoking.status
## 324             No
## 167             No
## 129             No
## 418            Yes
## 299            Yes
## 270             No
#checking if there are any missing values in the dataset  
any_na <- any(is.na(mydata2)) 

if (any_na) {
  na_values <- which(is.na(mydata2), arr.ind = TRUE)
  print(na_values)
}
##     row col
## 130  55   6
## 193  60   6
## 141  79   6
## 149  86   6
## 441  90   6
# Removing rows with NA values
mydata2 <- na.omit(mydata2)
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"))

mydata2$SmokingF <- factor(mydata2$Smoking.status,
                           levels = c("Yes", "No"),
                           labels = c("Yes", "No"))

#summary(mydata2[,c(-1,-3, -7)])
head(mydata2)
##      ID Age Gender Sleep.efficiency Sleep.duration Alcohol.consumption
## 324 324  41   Male             0.93            7.5                   0
## 167 167  40 Female             0.85            7.0                   0
## 129 129  33 Female             0.73            9.0                   1
## 418 418  44   Male             0.66            7.0                   3
## 299 299  35 Female             0.75            8.5                   1
## 270 270  27   Male             0.94            7.0                   0
##     Smoking.status GenderF SmokingF
## 324             No    Male       No
## 167             No  Female       No
## 129             No  Female       No
## 418            Yes    Male      Yes
## 299            Yes  Female      Yes
## 270             No    Male       No
summary(mydata2[,c(-1,-3, -7)])
##       Age        Sleep.efficiency Sleep.duration   Alcohol.consumption
##  Min.   : 9.00   Min.   :0.5000   Min.   : 5.000   Min.   :0.000      
##  1st Qu.:29.00   1st Qu.:0.6700   1st Qu.: 7.000   1st Qu.:0.000      
##  Median :38.00   Median :0.8100   Median : 7.500   Median :0.000      
##  Mean   :39.33   Mean   :0.7818   Mean   : 7.423   Mean   :1.179      
##  3rd Qu.:50.00   3rd Qu.:0.9000   3rd Qu.: 8.000   3rd Qu.:2.000      
##  Max.   :67.00   Max.   :0.9800   Max.   :10.000   Max.   :5.000      
##    GenderF   SmokingF 
##  Male  :99   Yes: 69  
##  Female:96   No :126  
##                       
##                       
##                       
## 

Explanations of parameters
The youngest person in the sample is 9 years old and the oldest is 69 years old.
Sleep efficiency is on average 78%.
50% of people in the sample sleep more than 7.5 hours per night.
Maximum alcohol consumption before sleep in liters is 5 liters.
We have 99 males and 96 females in the sample.

Correlation analysis

RQ1: Is there correlation between alcohol consumption and sleep efficiency?

Assumptions
- both variables are numerical
- linear relationship between the variables
- errors are normally distributed(Variable is normally distributed –> errors are normaly distributed, and vice verso)

#check linearity
library(car)
## Loading required package: carData
scatterplotMatrix(mydata2[ , c(-1, -2, -3, -5, -7, -8, -9)], smooth=FALSE)

#Checking scatter plot
scatterplot(mydata2$Alcohol.consumption, mydata2$Sleep.efficiency,
            ylab="Sleep Efficiency",
            xlab ="Alcohol Consumption",
            col = "blue",
            smooth=FALSE,
            boxplot=FALSE)

From the graph above we can observe linear relationship between sleep efficiency and alcohol consumption.

shapiro.test(mydata2$Alcohol.consumption)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata2$Alcohol.consumption
## W = 0.74666, p-value < 2.2e-16
shapiro.test(mydata2$Sleep.efficiency)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata2$Sleep.efficiency
## W = 0.91812, p-value = 5.975e-09

H0: The variable is normally distributed.
H1: The variable is not normally distributed.

For both sleep efficiency and alcohol consumption we can reject the null hypothesis and conclude that neither of the variables is normally distributed at statistical significance (p<0.001).

I also checked the distribution graphically in the scatter plot and neither sleep efficiency nur alcohol consumption are normally distributed. Because the distributions of variables are not normal, we will use spearman correlation.

cor(mydata2$Sleep.efficiency, mydata2$Alcohol.consumption,
    method = "spearman",
    use = "complete.obs")
## [1] -0.424002

Given that we have quite large sample, n=195, we would probably also be able to do pearson, because of CLT(Central limit theorem).

cor(mydata2$Sleep.efficiency, mydata2$Alcohol.consumption,
    method = "pearson",
    use = "complete.obs")
## [1] -0.4177708

As we see results for pearson and spearman are very similar. Person has more statistical power but due to robustness of results I will stick with spearman.

We can see that there is a semi-strong negative linear relationship between sleep efficiency and alcohol consumption.

cor.test(mydata2$Sleep.efficiency, mydata2$Alcohol.consumption,
    method = "spearman",
    use = "complete.obs")
## Warning in cor.test.default(mydata2$Sleep.efficiency,
## mydata2$Alcohol.consumption, : Cannot compute exact p-value with ties
## 
##  Spearman's rank correlation rho
## 
## data:  mydata2$Sleep.efficiency and mydata2$Alcohol.consumption
## S = 1759753, p-value = 6.546e-10
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##       rho 
## -0.424002

With this test, our hypotheses are as follows:

H0: Rho of sleep efficiency and alcohol consumption is equal to 0.
H0: Rho of sleep efficiency and alcohol consumption is not equal to 0.

Conclusion
We can reject the null hypothesis and conclude that Rho of sleep efficiency and alcohol consumption differs from 0 at statistical significance; there is semi-strong negative(-0.424) correlation between sleep efficiency and alcohol consumption(p<0.001).

Association between 2 categorical variables

RQ2: Is there an association between smoking status and gender?

Assumptions:
- independence of observation
- expected frequencies need to be more than 5
(I have 2x2 matrix so can’t apply rule 20% of values can be between 1-5)

head(mydata2[colnames(mydata2) %in% c("GenderF", "SmokingF")])
##     GenderF SmokingF
## 324    Male       No
## 167  Female       No
## 129  Female       No
## 418    Male      Yes
## 299  Female      Yes
## 270    Male       No

Descriptions
GenderF: Male/Female
SmokingF: Smoking Yes/No

results <- chisq.test(mydata2$SmokingF, mydata2$GenderF,
                      correct = TRUE) # we have 2x2 so we have to use Yates correction
results
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  mydata2$SmokingF and mydata2$GenderF
## X-squared = 13.953, df = 1, p-value = 0.0001875

HO: There is no association between gender and smoking(2 categorical variables).
H1: There is association between gender and smoking(2 categorical variables).

Based on the sample data we can reject H0 (p<0.001). We can claim that there is association between gender and smoking status. Testing statistics only shows whether there is association or not, that’s why we continue with observing standard residuals: they show the direction of relationship.

#Checking empirical frequencies
addmargins(results$observed)
##                 mydata2$GenderF
## mydata2$SmokingF Male Female Sum
##              Yes   48     21  69
##              No    51     75 126
##              Sum   99     96 195
#Checking expected frequencies
round(results$expected, 2)
##                 mydata2$GenderF
## mydata2$SmokingF  Male Female
##              Yes 35.03  33.97
##              No  63.97  62.03

Expected frequencies tell us the number of units from our sample we would expect to see in each of the possible combination of categories.

#Checking residuals
round(results$residuals, 2)
##                 mydata2$GenderF
## mydata2$SmokingF  Male Female
##              Yes  2.19  -2.23
##              No  -1.62   1.65

2.19 There is more than expected number of people in category male and Smoking (α=0.05).
-2.23 There is less then expected people in category female and smoking(α=0.05).
(-1.62, 1.65) < 1.95 We can not say there is significant difference between genders in category not smoking.

addmargins(round(prop.table(results$observed), 4))
##                 mydata2$GenderF
## mydata2$SmokingF   Male Female    Sum
##              Yes 0.2462 0.1077 0.3539
##              No  0.2615 0.3846 0.6461
##              Sum 0.5077 0.4923 1.0000

Out of 195(all) people in the sample there is 24,6% of males who smoke.

addmargins(round(prop.table(results$observed,1), 3), 2)
##                 mydata2$GenderF
## mydata2$SmokingF  Male Female   Sum
##              Yes 0.696  0.304 1.000
##              No  0.405  0.595 1.000

Out of all people in the sample that are not smoking, 59,6% of them is females.

addmargins(round(prop.table(results$observed, 2), 3), 1)
##                 mydata2$GenderF
## mydata2$SmokingF  Male Female
##              Yes 0.485  0.219
##              No  0.515  0.781
##              Sum 1.000  1.000

Out of all males in the sample 51,5% of them are not smoking.

library(effectsize)
effectsize::cramers_v(mydata2$GenderF, mydata2$SmokingF)
## Cramer's V (adj.) |       95% CI
## --------------------------------
## 0.27              | [0.14, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].

We can use both effect sizes.
Cramer’s V statistic can be used always and odds ratio only in the cases when we have 2x2 matrix.

interpret_cramers_v(0.27)
## [1] "medium"
## (Rules: funder2019)
oddsratio(mydata2$GenderF, mydata2$SmokingF)
## Odds ratio |       95% CI
## -------------------------
## 3.36       | [1.80, 6.27]
interpret_oddsratio(3.36)
## [1] "small"
## (Rules: chen2010)

Conclusion:
We conclude that there is medium effect(r=0.27) on smoking whether a person is male or female. Males are more likely to smoke.
OR: The odds male is smoking are 3.36 times the female smokes.