Multivariate Analysis - Homework 1 - Hypothesis testing

Patricija Varsek

Source: Data retrived on 6.1.2024, from: https://www.kaggle.com/datasets/uom190346a/sleep-health-and-lifestyle-dataset

Research question: Does average stress level significantly differ between male and female?

library(readxl)
library(psych)
mydata <- read_excel("~/6_IMB/7_Class/7_MultivariateAnalysis/R_HW/Sleep3.xlsx")

head(mydata)
## # A tibble: 6 × 12
##   `Person ID` Gender   Age Occupation           `Quality of Sleep`
##         <dbl> <chr>  <dbl> <chr>                             <dbl>
## 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
## # ℹ 7 more variables: `Physical Activity Level` <dbl>,
## #   StressLevel <dbl>, `BMI Category` <chr>, `Blood Pressure` <chr>,
## #   `Heart Rate` <dbl>, `Daily Steps` <dbl>, `Sleep Disorder` <chr>

Data manipulation:

set.seed(1) #Setting initial point of sampling.
mydata2 <- mydata[sample(nrow(mydata), 150), ] #Random sample of 150 units.
mydata3 <- mydata2[,c(1,2,6,7,11)]
head(mydata2)
## # A tibble: 6 × 12
##   `Person ID` Gender   Age Occupation `Quality of Sleep`
##         <dbl> <chr>  <dbl> <chr>                   <dbl>
## 1         324 Female    53 Engineer                    9
## 2         167 Male      41 Engineer                    8
## 3         129 Male      38 Lawyer                      8
## 4         299 Female    51 Engineer                    9
## 5         270 Female    49 Nurse                       6
## 6         187 Female    43 Teacher                     7
## # ℹ 7 more variables: `Physical Activity Level` <dbl>,
## #   StressLevel <dbl>, `BMI Category` <chr>, `Blood Pressure` <chr>,
## #   `Heart Rate` <dbl>, `Daily Steps` <dbl>, `Sleep Disorder` <chr>
mydata3$GenderN <- factor(mydata3$Gender, 
                          levels = c ("Male", "Female"), 
                          labels = c ("Male", "Female"))
head(mydata3)
## # A tibble: 6 × 6
##   `Person ID` Gender Physical Activity Lev…¹ StressLevel `Daily Steps`
##         <dbl> <chr>                    <dbl>       <dbl>         <dbl>
## 1         324 Female                      30           3          5000
## 2         167 Male                        70           6          6200
## 3         129 Male                        60           5          8000
## 4         299 Female                      30           3          5000
## 5         270 Female                      90           8         10000
## 6         187 Female                      45           4          6000
## # ℹ abbreviated name: ¹​`Physical Activity Level`
## # ℹ 1 more variable: GenderN <fct>

In our data (mydata3) we have 150 different people with different occupations, sleep and daily habits. It includes details on gender,physical activity level, stress level,heart rate and daily steps.

Description:

  • Person ID: an identifier of each individual
  • Gender: The gender of a person (Male, Female)
  • Physical activity level: The number of minutes the person engages in physical activity daily.
  • Stress level: A subjective ranking of the stress level experienced by the person, ranging from 1 - 10
  • Daily steps: The number of the person takes per day
  • GenderN: Gender of a person (Male, Female)
library(psych)
describe(mydata3)
##                         vars   n    mean      sd median trimmed
## Person ID                  1 150  184.54  109.77  178.5  183.89
## Gender*                    2 150    1.49    0.50    1.0    1.49
## Physical Activity Level    3 150   59.94   21.08   60.0   59.92
## StressLevel                4 150    5.33    1.74    5.0    5.29
## Daily Steps                5 150 6926.67 1585.11 7000.0 6833.33
## GenderN*                   6 150    1.51    0.50    2.0    1.51
##                             mad  min   max range  skew kurtosis
## Person ID                148.26    1   373   372  0.05    -1.29
## Gender*                    0.00    1     2     1  0.03    -2.01
## Physical Activity Level   22.24   30    90    60  0.02    -1.32
## StressLevel                1.48    3     8     5  0.17    -1.28
## Daily Steps             1482.60 3700 10000  6300  0.19    -0.63
## GenderN*                   0.00    1     2     1 -0.03    -2.01
##                             se
## Person ID                 8.96
## Gender*                   0.04
## Physical Activity Level   1.72
## StressLevel               0.14
## Daily Steps             129.42
## GenderN*                  0.04

Explanation of (some) parameters:

  • n = 150: There are 150 units in our sample
  • Physical activity - max = 90: The maximum minutes of Physical activity per person is 90 min.
  • StressLevel - mean = 5.33: The stress level of a person is on average 5.33, measured on a scale 1 - 10.
  • Daily steps - median = 7000: 50% of the people do 7000 or less per day and the other 50% do more than 7000 steps per day.

Research question: Does average stress level significantly differ between male and female?

  • unit: one person
  • variable: stress level

Research hypothesis:

  • H0: There is no significant difference in average stress level between male and female.
  • H1: There is a significant difference in average stress level between male and female.

Testing assumptions:

  • The compered variables are numeric (The variable StressLevel, which we are compering between groups is numeric, therefore assumption is not violated. )
  • Normality of the variable in both groups (tested below with Shapiro Wilk’s test)
  • Equality of variances between group (we will use Welch’s correction)

The data is from two independent populations.

describeBy(mydata3$`StressLevel`, mydata3$GenderN)
## 
##  Descriptive statistics by group 
## group: Male
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis
## X1    1 74    6 1.27      6       6 1.48   3   8     5 0.04    -0.71
##      se
## X1 0.15
## ---------------------------------------------------- 
## group: Female
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis
## X1    1 76 4.68 1.89      4     4.5 1.48   3   8     5  0.8    -0.99
##      se
## X1 0.22
#install.packages("psych")
#install.packages("ggplot2")
library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
ggplot(mydata3, aes(x = StressLevel, fill = factor(GenderN))) +
geom_histogram(position = "dodge", bins = 10) +
  scale_fill_manual(values = c("lightblue", "orchid"), 
                  labels = c("Male", "Female")) +
  labs(title = "Distribution of Stress Level by GenderN",
   x = "StressLevel", y = "Frequency", fill = "GenderN") +
  theme_bw()

Normality test with Shapiro Wilks test:

We divide group by gender and apply 2 Shaprio Wilks test. For both tests we set hypothesis:

  • H0: variable (StressLevel) is normally distributed
  • H1: variable (StressLevel) is not normally distributed
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
mydata3 %>%
 group_by(GenderN) %>%
  shapiro_test(StressLevel)
## # A tibble: 2 × 4
##   GenderN variable    statistic             p
##   <fct>   <chr>           <dbl>         <dbl>
## 1 Male    StressLevel     0.905 0.0000378    
## 2 Female  StressLevel     0.767 0.00000000124

Based on our sample data we reject our normality assumption for both groups. The corresponding p-values are less than 0.001. We have non-parametric data, therefore we will perform Wilcoxon Rank Sum Test.

However, for robustness check we will also perform parametric test:

Parametrical test - Welch:

  • H0: There is no significant difference in average StressLevel between male and female.
  • H1: There is a significant difference in average StressLevel between male and female.
t.test(mydata3$StressLevel ~ mydata3$GenderN, 
       paired = FALSE,
       var.equal = FALSE,
       alternative = "two.sided")
## 
##  Welch Two Sample t-test
## 
## data:  mydata3$StressLevel by mydata3$GenderN
## t = 5.0115, df = 131.62, p-value = 1.707e-06
## alternative hypothesis: true difference in means between group Male and group Female is not equal to 0
## 95 percent confidence interval:
##  0.7964157 1.8351633
## sample estimates:
##   mean in group Male mean in group Female 
##             6.000000             4.684211
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::cohens_d(mydata3$StressLevel ~ mydata3$GenderN,
                     pooled_sd = FALSE)
## Cohen's d |       95% CI
## ------------------------
## 0.82      | [0.48, 1.15]
## 
## - Estimated using un-pooled SD.
interpret_cohens_d(0.82, rules = "sawilowsky2009")
## [1] "large"
## (Rules: sawilowsky2009)

Conclusion: Based on our sample data we reject the null hypothesis (p < 0.001) and conclude that female have different stress level than male. The size of the differences is large (d=0.82).

Alternative non-parametric test - Wilcoxon Rank Sum Test:

  • H0: Distribution location of StressLevel is the same for male and female.
  • H1: Distribution location of StressLevel is not the same for male and female.
wilcox.test(mydata3$StressLevel ~ mydata3$GenderN, 
            paired = FALSE, 
            correct = FALSE, 
            exact = FALSE, 
            alternative = "two.sided")
## 
##  Wilcoxon rank sum test
## 
## data:  mydata3$StressLevel by mydata3$GenderN
## W = 4113.5, p-value = 6.858e-07
## alternative hypothesis: true location shift is not equal to 0
library(effectsize)
effectsize(wilcox.test(mydata3$StressLevel ~ mydata3$GenderN, 
                       paried= FALSE, 
                       correct = FALSE, 
                       exact = FALSE, 
                       alternative = "two.sided"))
## r (rank biserial) |       95% CI
## --------------------------------
## 0.46              | [0.31, 0.60]
interpret_rank_biserial(0.46)
## [1] "very large"
## (Rules: funder2019)

Conclusion:

Based on our sample data we reject the null hypothesis (p < 0.001) and conclude that female have different stress level than male. The size of the differences is very large (r=0.46).

```