1. Persiapan Data

1.1 Load Library

# ===============================
# LIBRARY
# ===============================
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(MVN)
## Registered S3 method overwritten by 'lme4':
##   method           from
##   na.action.merMod car
library(biotools)
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## ---
## biotools version 4.3
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## The following object is masked from 'package:purrr':
## 
##     some
library(heplots)
## Loading required package: broom
## Warning in rgl.init(initValue, onlyNULL): RGL: unable to open X11 display
## Warning: 'rgl.init' failed, will use the null device.
## See '?rgl.useNULL' for ways to avoid this warning.
## 
## Attaching package: 'heplots'
## 
## The following object is masked from 'package:biotools':
## 
##     boxM
library(emmeans)
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'

1.2 Load Dataset

# ===============================
# DATA UNDERSTANDING
# ===============================
data <- read.csv("Sleep_health_and_lifestyle_dataset.csv")

str(data)
## 'data.frame':    374 obs. of  13 variables:
##  $ Person.ID              : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Gender                 : chr  "Male" "Male" "Male" "Male" ...
##  $ Age                    : int  27 28 28 28 28 28 29 29 29 29 ...
##  $ Occupation             : chr  "Software Engineer" "Doctor" "Doctor" "Sales Representative" ...
##  $ Sleep.Duration         : num  6.1 6.2 6.2 5.9 5.9 5.9 6.3 7.8 7.8 7.8 ...
##  $ Quality.of.Sleep       : int  6 6 6 4 4 4 6 7 7 7 ...
##  $ Physical.Activity.Level: int  42 60 60 30 30 30 40 75 75 75 ...
##  $ Stress.Level           : int  6 8 8 8 8 8 7 6 6 6 ...
##  $ BMI.Category           : chr  "Overweight" "Normal" "Normal" "Obese" ...
##  $ Blood.Pressure         : chr  "126/83" "125/80" "125/80" "140/90" ...
##  $ Heart.Rate             : int  77 75 75 85 85 85 82 70 70 70 ...
##  $ Daily.Steps            : int  4200 10000 10000 3000 3000 3000 3500 8000 8000 8000 ...
##  $ Sleep.Disorder         : chr  "None" "None" "None" "Sleep Apnea" ...
summary(data)
##    Person.ID         Gender               Age         Occupation       
##  Min.   :  1.00   Length:374         Min.   :27.00   Length:374        
##  1st Qu.: 94.25   Class :character   1st Qu.:35.25   Class :character  
##  Median :187.50   Mode  :character   Median :43.00   Mode  :character  
##  Mean   :187.50                      Mean   :42.18                     
##  3rd Qu.:280.75                      3rd Qu.:50.00                     
##  Max.   :374.00                      Max.   :59.00                     
##  Sleep.Duration  Quality.of.Sleep Physical.Activity.Level  Stress.Level  
##  Min.   :5.800   Min.   :4.000    Min.   :30.00           Min.   :3.000  
##  1st Qu.:6.400   1st Qu.:6.000    1st Qu.:45.00           1st Qu.:4.000  
##  Median :7.200   Median :7.000    Median :60.00           Median :5.000  
##  Mean   :7.132   Mean   :7.313    Mean   :59.17           Mean   :5.385  
##  3rd Qu.:7.800   3rd Qu.:8.000    3rd Qu.:75.00           3rd Qu.:7.000  
##  Max.   :8.500   Max.   :9.000    Max.   :90.00           Max.   :8.000  
##  BMI.Category       Blood.Pressure       Heart.Rate     Daily.Steps   
##  Length:374         Length:374         Min.   :65.00   Min.   : 3000  
##  Class :character   Class :character   1st Qu.:68.00   1st Qu.: 5600  
##  Mode  :character   Mode  :character   Median :70.00   Median : 7000  
##                                        Mean   :70.17   Mean   : 6817  
##                                        3rd Qu.:72.00   3rd Qu.: 8000  
##                                        Max.   :86.00   Max.   :10000  
##  Sleep.Disorder    
##  Length:374        
##  Class :character  
##  Mode  :character  
##                    
##                    
## 
sum(is.na(data))
## [1] 0

Dataset yang digunakan adalah Sleep Health and Lifestyle Dataset yang berisi 374 data observasi dengan 13 variabel. Variabel dalam dataset terdiri dari: a) Variabel numerik (Age, Sleep Duration, Stress Level, dll) b) Variabel kategorik (Gender, BMI Category, Sleep Disorder) Data ini digunakan untuk menganalisis faktor yang mempengaruhi kualitas tidur.

1.3 Preprocessing

# ===============================
# PREPROCESSING
# ===============================

# hapus ID
data <- data %>% dplyr::select(-`Person.ID`)

# ubah ke factor
data$Gender <- as.factor(data$Gender)
data$`BMI.Category` <- as.factor(data$`BMI.Category`)
data$Occupation <- as.factor(data$Occupation)
data$`Sleep.Disorder` <- as.factor(data$`Sleep.Disorder`)

# ubah variabel numerik
data$Quality.of.Sleep <- as.numeric(data$Quality.of.Sleep)
data$Stress.Level <- as.numeric(data$Stress.Level)
data$Sleep.Duration <- as.numeric(data$Sleep.Duration)

2. Definsi Variabel & Statistik Deskriptif

2.1 Variabel dependen (Y)

Y <- data %>% 
  dplyr::select(`Quality.of.Sleep`, `Stress.Level`, `Sleep.Duration`)

2.2 Variabel dependen (X)

X <- data %>% 
  dplyr::select(-`Quality.of.Sleep`, -`Stress.Level`, -`Sleep.Duration`)

2.3 Statistik, Summary Variabel

summary(data)
##     Gender         Age             Occupation Sleep.Duration  Quality.of.Sleep
##  Female:185   Min.   :27.00   Nurse     :73   Min.   :5.800   Min.   :4.000   
##  Male  :189   1st Qu.:35.25   Doctor    :71   1st Qu.:6.400   1st Qu.:6.000   
##               Median :43.00   Engineer  :63   Median :7.200   Median :7.000   
##               Mean   :42.18   Lawyer    :47   Mean   :7.132   Mean   :7.313   
##               3rd Qu.:50.00   Teacher   :40   3rd Qu.:7.800   3rd Qu.:8.000   
##               Max.   :59.00   Accountant:37   Max.   :8.500   Max.   :9.000   
##                               (Other)   :43                                   
##  Physical.Activity.Level  Stress.Level          BMI.Category Blood.Pressure    
##  Min.   :30.00           Min.   :3.000   Normal       :195   Length:374        
##  1st Qu.:45.00           1st Qu.:4.000   Normal Weight: 21   Class :character  
##  Median :60.00           Median :5.000   Obese        : 10   Mode  :character  
##  Mean   :59.17           Mean   :5.385   Overweight   :148                     
##  3rd Qu.:75.00           3rd Qu.:7.000                                         
##  Max.   :90.00           Max.   :8.000                                         
##                                                                                
##    Heart.Rate     Daily.Steps        Sleep.Disorder
##  Min.   :65.00   Min.   : 3000   Insomnia   : 77   
##  1st Qu.:68.00   1st Qu.: 5600   None       :219   
##  Median :70.00   Median : 7000   Sleep Apnea: 78   
##  Mean   :70.17   Mean   : 6817                     
##  3rd Qu.:72.00   3rd Qu.: 8000                     
##  Max.   :86.00   Max.   :10000                     
## 
summary(Y)
##  Quality.of.Sleep  Stress.Level   Sleep.Duration 
##  Min.   :4.000    Min.   :3.000   Min.   :5.800  
##  1st Qu.:6.000    1st Qu.:4.000   1st Qu.:6.400  
##  Median :7.000    Median :5.000   Median :7.200  
##  Mean   :7.313    Mean   :5.385   Mean   :7.132  
##  3rd Qu.:8.000    3rd Qu.:7.000   3rd Qu.:7.800  
##  Max.   :9.000    Max.   :8.000   Max.   :8.500
apply(Y, 2, sd)
## Quality.of.Sleep     Stress.Level   Sleep.Duration 
##        1.1969559        1.7745264        0.7956567
apply(Y, 2, mean)
## Quality.of.Sleep     Stress.Level   Sleep.Duration 
##         7.312834         5.385027         7.132086
apply(Y, 2, min)
## Quality.of.Sleep     Stress.Level   Sleep.Duration 
##              4.0              3.0              5.8
apply(Y, 2, max)
## Quality.of.Sleep     Stress.Level   Sleep.Duration 
##              9.0              8.0              8.5

3. Visualisasi Data

3.1 Histogram

par(mfrow=c(1,3))

hist(data$Quality.of.Sleep,
     main="Quality of Sleep",
     col="skyblue")

hist(data$Stress.Level,
     main="Stress Level",
     col="maroon")

hist(data$Sleep.Duration,
     main="Sleep Duration")

3.2 Boxplot

# boxplot
boxplot(Y,
        main="Distribusi Variabel Dependen",
        col=c("skyblue","maroon","pink"))

3.3 Heatmap

# korelasi(HEATMAP)
cor_matrix <- cor(Y)
cor_melted <- as.data.frame(as.table(cor_matrix))
ggplot(data = cor_melted, aes(x=Var1, y=Var2, fill=Freq)) + 
  geom_tile(color = "white") +
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
                       midpoint = 0, limit = c(-1,1), space = "Lab", 
                       name="Pearson\nCorrelation") +
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, size = 10, hjust = 1),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) +
  coord_fixed() +
  geom_text(aes(Var1, Var2, label = round(Freq, 2)), color = "black", size = 4) +
  labs(title = "Heatmap Korelasi Variabel Dependen")

4. Peng-Ujian

4.1 Uji Outlier

# OUTLIER (MAHALANOBIS)
mahal <- mahalanobis(Y, colMeans(Y), cov(Y))
cutoff <- qchisq(0.975, df = 3)
outlier <- mahal > cutoff

which(outlier)
##  [1]   4   5   6  17  19  31  32  81  82 104 106 148
sum(outlier)
## [1] 12

4.2 Uji Normalitas

hasil_normalitas <- MVN::mvn(Y)
hasil_normalitas
## $multivariate_normality
##            Test Statistic p.value     Method          MVN
## 1 Henze-Zirkler    21.194  <0.001 asymptotic ✗ Not normal
## 
## $univariate_normality
##               Test         Variable Statistic p.value    Normality
## 1 Anderson-Darling Quality.of.Sleep    15.554  <0.001 ✗ Not normal
## 2 Anderson-Darling     Stress.Level    12.641  <0.001 ✗ Not normal
## 3 Anderson-Darling   Sleep.Duration     7.304  <0.001 ✗ Not normal
## 
## $descriptives
##           Variable   n  Mean Std.Dev Median Min Max 25th 75th   Skew Kurtosis
## 1 Quality.of.Sleep 374 7.313   1.197    7.0 4.0 9.0  6.0  8.0 -0.207    2.246
## 2     Stress.Level 374 5.385   1.775    5.0 3.0 8.0  4.0  7.0  0.154    1.674
## 3   Sleep.Duration 374 7.132   0.796    7.2 5.8 8.5  6.4  7.8  0.037    1.715
## 
## $data
##     Quality.of.Sleep Stress.Level Sleep.Duration
## 1                  6            6            6.1
## 2                  6            8            6.2
## 3                  6            8            6.2
## 4                  4            8            5.9
## 5                  4            8            5.9
## 6                  4            8            5.9
## 7                  6            7            6.3
## 8                  7            6            7.8
## 9                  7            6            7.8
## 10                 7            6            7.8
## 11                 6            8            6.1
## 12                 7            6            7.8
## 13                 6            8            6.1
## 14                 6            8            6.0
## 15                 6            8            6.0
## 16                 6            8            6.0
## 17                 5            7            6.5
## 18                 6            8            6.0
## 19                 5            7            6.5
## 20                 7            6            7.6
## 21                 7            6            7.7
## 22                 7            6            7.7
## 23                 7            6            7.7
## 24                 7            6            7.7
## 25                 7            6            7.8
## 26                 7            6            7.9
## 27                 7            6            7.8
## 28                 7            6            7.9
## 29                 7            6            7.9
## 30                 7            6            7.9
## 31                 5            7            6.4
## 32                 5            7            6.4
## 33                 8            4            7.9
## 34                 6            8            6.1
## 35                 7            6            7.7
## 36                 6            8            6.1
## 37                 6            8            6.1
## 38                 7            6            7.6
## 39                 7            6            7.6
## 40                 7            6            7.6
## 41                 7            6            7.7
## 42                 7            6            7.7
## 43                 7            6            7.7
## 44                 7            6            7.8
## 45                 7            6            7.7
## 46                 7            6            7.8
## 47                 7            6            7.7
## 48                 7            6            7.8
## 49                 7            6            7.7
## 50                 7            6            7.7
## 51                 8            3            7.5
## 52                 8            3            7.5
## 53                 6            8            6.0
## 54                 7            6            7.6
## 55                 6            8            6.0
## 56                 6            8            6.0
## 57                 7            6            7.7
## 58                 6            8            6.0
## 59                 6            8            6.0
## 60                 7            6            7.7
## 61                 6            8            6.0
## 62                 6            8            6.0
## 63                 6            8            6.2
## 64                 6            8            6.2
## 65                 6            8            6.2
## 66                 6            8            6.2
## 67                 8            6            7.2
## 68                 6            8            6.0
## 69                 6            6            6.2
## 70                 6            6            6.2
## 71                 6            8            6.1
## 72                 6            8            6.1
## 73                 6            8            6.1
## 74                 6            8            6.1
## 75                 6            8            6.0
## 76                 6            8            6.0
## 77                 6            8            6.0
## 78                 6            8            6.0
## 79                 6            8            6.0
## 80                 6            8            6.0
## 81                 4            8            5.8
## 82                 4            8            5.8
## 83                 7            5            6.7
## 84                 7            5            6.7
## 85                 8            5            7.5
## 86                 8            4            7.2
## 87                 8            4            7.2
## 88                 8            4            7.2
## 89                 8            4            7.3
## 90                 8            4            7.3
## 91                 8            4            7.3
## 92                 8            4            7.3
## 93                 8            5            7.5
## 94                 7            5            7.4
## 95                 8            4            7.2
## 96                 8            4            7.1
## 97                 8            4            7.2
## 98                 8            4            7.1
## 99                 8            4            7.1
## 100                8            4            7.1
## 101                8            4            7.2
## 102                8            4            7.2
## 103                8            4            7.2
## 104                5            7            6.6
## 105                8            4            7.2
## 106                5            7            6.6
## 107                6            6            6.1
## 108                8            4            7.8
## 109                8            4            7.8
## 110                8            5            7.4
## 111                8            4            7.2
## 112                8            5            7.4
## 113                8            4            7.2
## 114                8            5            7.4
## 115                8            4            7.2
## 116                8            4            7.2
## 117                8            4            7.2
## 118                8            4            7.2
## 119                8            4            7.2
## 120                8            4            7.2
## 121                8            4            7.2
## 122                8            4            7.2
## 123                8            4            7.2
## 124                8            4            7.2
## 125                8            4            7.2
## 126                8            4            7.5
## 127                8            5            7.3
## 128                8            4            7.1
## 129                8            5            7.3
## 130                8            5            7.3
## 131                8            4            7.1
## 132                8            5            7.3
## 133                8            5            7.3
## 134                8            4            7.1
## 135                8            5            7.3
## 136                8            5            7.3
## 137                8            4            7.1
## 138                8            5            7.1
## 139                8            4            7.1
## 140                8            5            7.1
## 141                8            4            7.1
## 142                8            5            7.1
## 143                8            4            7.1
## 144                8            4            7.1
## 145                8            5            7.1
## 146                7            5            7.4
## 147                8            5            7.2
## 148                5            7            6.5
## 149                7            6            6.9
## 150                9            3            8.0
## 151                9            3            8.0
## 152                8            5            7.2
## 153                8            5            7.2
## 154                8            5            7.2
## 155                8            5            7.2
## 156                8            5            7.2
## 157                8            5            7.2
## 158                8            5            7.2
## 159                8            5            7.2
## 160                8            5            7.2
## 161                8            5            7.2
## 162                8            6            7.2
## 163                8            6            7.2
## 164                8            5            7.9
## 165                8            5            7.9
## 166                8            5            7.6
## 167                8            6            7.3
## 168                7            6            7.1
## 169                7            6            7.1
## 170                8            5            7.7
## 171                8            5            7.7
## 172                8            5            7.7
## 173                8            5            7.7
## 174                8            5            7.7
## 175                8            5            7.6
## 176                8            5            7.6
## 177                8            5            7.6
## 178                6            7            6.5
## 179                8            5            7.8
## 180                8            5            7.8
## 181                8            5            7.8
## 182                8            5            7.8
## 183                8            5            7.8
## 184                8            5            7.8
## 185                6            7            6.8
## 186                6            7            6.8
## 187                7            4            6.7
## 188                6            7            6.3
## 189                7            4            6.7
## 190                6            7            6.5
## 191                7            4            6.7
## 192                6            7            6.4
## 193                6            7            6.5
## 194                6            7            6.5
## 195                6            7            6.5
## 196                6            7            6.5
## 197                6            7            6.5
## 198                6            7            6.5
## 199                6            7            6.5
## 200                6            7            6.5
## 201                6            7            6.5
## 202                8            5            7.8
## 203                8            5            7.8
## 204                6            7            6.9
## 205                8            4            7.6
## 206                8            5            7.7
## 207                8            5            7.7
## 208                8            5            7.7
## 209                8            5            7.7
## 210                8            5            7.8
## 211                8            5            7.7
## 212                8            5            7.8
## 213                8            5            7.8
## 214                8            5            7.8
## 215                8            5            7.8
## 216                8            5            7.8
## 217                8            5            7.8
## 218                8            5            7.8
## 219                8            5            7.8
## 220                6            7            6.5
## 221                7            4            6.6
## 222                6            7            6.4
## 223                6            7            6.3
## 224                6            7            6.4
## 225                7            4            6.6
## 226                6            7            6.3
## 227                7            4            6.6
## 228                6            7            6.3
## 229                7            4            6.6
## 230                6            7            6.3
## 231                7            4            6.6
## 232                6            7            6.3
## 233                7            4            6.6
## 234                6            7            6.3
## 235                7            4            6.6
## 236                6            7            6.3
## 237                6            7            6.4
## 238                7            4            6.5
## 239                6            7            6.3
## 240                6            7            6.4
## 241                7            4            6.5
## 242                6            7            6.3
## 243                6            7            6.4
## 244                7            4            6.5
## 245                6            7            6.3
## 246                7            4            6.5
## 247                6            7            6.3
## 248                7            7            6.8
## 249                6            7            6.4
## 250                6            7            6.5
## 251                7            6            6.8
## 252                7            6            6.8
## 253                7            4            6.5
## 254                7            4            6.5
## 255                7            4            6.5
## 256                7            4            6.5
## 257                7            4            6.6
## 258                7            4            6.6
## 259                7            4            6.6
## 260                7            4            6.6
## 261                7            4            6.6
## 262                7            4            6.6
## 263                7            4            6.6
## 264                7            5            6.9
## 265                7            5            7.3
## 266                6            8            5.9
## 267                7            5            7.3
## 268                6            8            6.2
## 269                6            8            6.0
## 270                6            8            6.1
## 271                6            8            6.1
## 272                6            8            6.1
## 273                6            8            6.1
## 274                6            8            6.2
## 275                6            8            6.2
## 276                6            8            6.2
## 277                9            3            8.1
## 278                9            3            8.1
## 279                6            8            6.1
## 280                9            3            8.3
## 281                6            8            6.0
## 282                6            8            6.1
## 283                6            8            6.0
## 284                6            8            6.0
## 285                6            8            6.0
## 286                6            8            6.0
## 287                6            8            6.0
## 288                6            8            6.0
## 289                6            8            6.0
## 290                6            8            6.1
## 291                6            8            6.0
## 292                6            8            6.1
## 293                6            8            6.1
## 294                6            8            6.0
## 295                6            8            6.1
## 296                6            8            6.0
## 297                6            8            6.1
## 298                6            8            6.1
## 299                9            3            8.5
## 300                9            3            8.5
## 301                9            3            8.5
## 302                9            3            8.5
## 303                7            6            7.1
## 304                6            8            6.0
## 305                6            8            6.1
## 306                6            8            6.1
## 307                7            7            6.5
## 308                7            7            6.5
## 309                7            7            6.6
## 310                7            7            6.6
## 311                7            7            6.6
## 312                7            7            6.6
## 313                9            3            8.4
## 314                9            3            8.4
## 315                9            3            8.4
## 316                9            3            8.3
## 317                9            3            8.5
## 318                9            3            8.5
## 319                9            3            8.4
## 320                9            3            8.4
## 321                9            3            8.5
## 322                9            3            8.4
## 323                9            3            8.4
## 324                9            3            8.5
## 325                9            3            8.3
## 326                9            3            8.5
## 327                9            3            8.3
## 328                9            3            8.5
## 329                9            3            8.3
## 330                9            3            8.5
## 331                9            3            8.5
## 332                9            3            8.4
## 333                9            3            8.4
## 334                9            3            8.4
## 335                9            3            8.4
## 336                9            3            8.4
## 337                9            3            8.4
## 338                9            3            8.4
## 339                9            3            8.5
## 340                9            4            8.1
## 341                9            4            8.1
## 342                9            3            8.2
## 343                9            3            8.2
## 344                9            3            8.1
## 345                9            3            8.2
## 346                9            3            8.2
## 347                9            3            8.2
## 348                9            3            8.2
## 349                9            3            8.2
## 350                9            3            8.1
## 351                9            3            8.1
## 352                9            3            8.1
## 353                9            3            8.0
## 354                9            3            8.0
## 355                9            3            8.0
## 356                9            3            8.0
## 357                9            3            8.0
## 358                9            3            8.0
## 359                9            3            8.0
## 360                9            3            8.1
## 361                9            3            8.2
## 362                9            3            8.2
## 363                9            3            8.2
## 364                9            3            8.2
## 365                9            3            8.0
## 366                9            3            8.0
## 367                9            3            8.1
## 368                9            3            8.0
## 369                9            3            8.1
## 370                9            3            8.1
## 371                9            3            8.0
## 372                9            3            8.1
## 373                9            3            8.1
## 374                9            3            8.1
## 
## $subset
## NULL
## 
## $outlierMethod
## [1] "none"
## 
## attr(,"class")
## [1] "mvn"

4.3 Uji Dependensi

# UJI DEPENDENSI (BARTLETT - FIXED)
library(psych)
## 
## Attaching package: 'psych'
## The following object is masked from 'package:car':
## 
##     logit
## The following object is masked from 'package:MVN':
## 
##     mardia
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
cortest.bartlett(cor(Y), n = nrow(data))
## $chisq
## [1] 1176.769
## 
## $p.value
## [1] 8.045963e-255
## 
## $df
## [1] 3

4.4 Uji Homogenitas

# ===============================
# UJI HOMOGENITAS (BOX'S M)
# ===============================
boxM(Y, data$`Sleep.Disorder`)
## 
##  Box's M-test for Homogeneity of Covariance Matrices 
## 
## data:  Y by data$Sleep.Disorder 
## Chi-Sq (approx.) = 242.4278, df = 12, p-value = < 2.2e-16
boxM(Y, data$`BMI.Category`)
## 
##  Box's M-test for Homogeneity of Covariance Matrices 
## 
## data:  Y by data$BMI.Category 
## Chi-Sq (approx.) = 296.0651, df = 18, p-value = < 2.2e-16

3. Analisis Manova

# TWO-WAY MANOVA
manova_model <- manova(
  cbind(Quality.of.Sleep, Stress.Level, Sleep.Duration) ~ 
    Sleep.Disorder * BMI.Category,
  data = data
)

# Pillai (robust)
summary(manova_model, test="Pillai")
##                              Df  Pillai approx F num Df den Df    Pr(>F)    
## Sleep.Disorder                2 0.34856  25.4685      6    724 < 2.2e-16 ***
## BMI.Category                  3 0.19698   8.5032      9   1089 2.278e-12 ***
## Sleep.Disorder:BMI.Category   5 0.20907   5.4384     15   1089 1.006e-10 ***
## Residuals                   363                                             
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4. Analisis Anova (Univariat)

# ===============================
# ANOVA (UNIVARIAT)
# ===============================
summary.aov(manova_model)
##  Response Quality.of.Sleep :
##                              Df Sum Sq Mean Sq F value    Pr(>F)    
## Sleep.Disorder                2  69.21  34.607 29.6904 1.142e-12 ***
## BMI.Category                  3  11.00   3.666  3.1455   0.02522 *  
## Sleep.Disorder:BMI.Category   5  31.07   6.214  5.3308 9.643e-05 ***
## Residuals                   363 423.12   1.166                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response Stress.Level :
##                              Df  Sum Sq Mean Sq F value   Pr(>F)   
## Sleep.Disorder                2   40.38 20.1877  6.5540 0.001599 **
## BMI.Category                  3    1.31  0.4382  0.1423 0.934579   
## Sleep.Disorder:BMI.Category   5   14.76  2.9513  0.9581 0.443442   
## Residuals                   363 1118.11  3.0802                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response Sleep.Duration :
##                              Df  Sum Sq Mean Sq F value    Pr(>F)    
## Sleep.Disorder                2  34.662 17.3308 33.3827 4.915e-14 ***
## BMI.Category                  3   7.955  2.6518  5.1079  0.001792 ** 
## Sleep.Disorder:BMI.Category   5   5.065  1.0131  1.9514  0.085220 .  
## Residuals                   363 188.453  0.5192                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

5. UJI Asumsi Ancova

# ===============================
# contoh uji interaksi (Gender dengan covariate)
uji_homogen <- lm(Quality.of.Sleep ~ Gender*Age, data=data)
anova(uji_homogen)
uji_homogen2 <- lm(Stress.Level ~ Gender*Age, data=data)
anova(uji_homogen2)
uji_homogen3 <- lm(Sleep.Duration ~ Gender*Age, data=data)
anova(uji_homogen3)

6. Analisis Mancova

# ===============================
model_mancova <- manova(
  cbind(Quality.of.Sleep, Stress.Level, Sleep.Duration) ~
    Gender + BMI.Category + Sleep.Disorder +
    Age + Physical.Activity.Level + Heart.Rate + Daily.Steps,
  data = data
)

summary(model_mancova, test = "Wilks")
##                          Df   Wilks approx F num Df den Df    Pr(>F)    
## Gender                    1 0.44887   147.75      3 361.00 < 2.2e-16 ***
## BMI.Category              3 0.32261    57.78      9 878.73 < 2.2e-16 ***
## Sleep.Disorder            2 0.56363    39.95      6 722.00 < 2.2e-16 ***
## Age                       1 0.24323   374.39      3 361.00 < 2.2e-16 ***
## Physical.Activity.Level   1 0.83885    23.12      3 361.00 1.039e-13 ***
## Heart.Rate                1 0.34619   227.26      3 361.00 < 2.2e-16 ***
## Daily.Steps               1 0.63118    70.32      3 361.00 < 2.2e-16 ***
## Residuals               363                                             
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

7. Analisis Ancova

7.1 Quality of Sleep

# ===============================
ancova1 <- aov(Quality.of.Sleep ~ Gender + BMI.Category + Sleep.Disorder +
                 Age + Physical.Activity.Level + Heart.Rate + Daily.Steps, data=data)

summary(ancova1)
##                          Df Sum Sq Mean Sq F value   Pr(>F)    
## Gender                    1  45.37   45.37  235.45  < 2e-16 ***
## BMI.Category              3 106.40   35.47  184.07  < 2e-16 ***
## Sleep.Disorder            2   8.94    4.47   23.19 3.33e-10 ***
## Age                       1 196.28  196.28 1018.68  < 2e-16 ***
## Physical.Activity.Level   1   9.23    9.23   47.90 2.04e-11 ***
## Heart.Rate                1  80.61   80.61  418.34  < 2e-16 ***
## Daily.Steps               1  17.63   17.63   91.50  < 2e-16 ***
## Residuals               363  69.94    0.19                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

7.2 Stress Level

ancova2 <- aov(Stress.Level ~ Gender + BMI.Category + Sleep.Disorder +
                 Age + Physical.Activity.Level + Heart.Rate + Daily.Steps, data=data)

summary(ancova2)
##                          Df Sum Sq Mean Sq F value   Pr(>F)    
## Gender                    1  184.2   184.2  368.99  < 2e-16 ***
## BMI.Category              3  130.0    43.3   86.83  < 2e-16 ***
## Sleep.Disorder            2   27.8    13.9   27.84 5.63e-12 ***
## Age                       1  215.2   215.2  431.16  < 2e-16 ***
## Physical.Activity.Level   1    5.2     5.2   10.35  0.00141 ** 
## Heart.Rate                1  332.9   332.9  666.75  < 2e-16 ***
## Daily.Steps               1   98.0    98.0  196.36  < 2e-16 ***
## Residuals               363  181.2     0.5                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

7.3 Sleep Duration

ancova3 <- aov(Sleep.Duration ~ Gender + BMI.Category + Sleep.Disorder +
                 Age + Physical.Activity.Level + Heart.Rate + Daily.Steps, data=data)

summary(ancova3)
##                          Df Sum Sq Mean Sq F value   Pr(>F)    
## Gender                    1   3.49    3.49   22.72 2.71e-06 ***
## BMI.Category              3  47.82   15.94  103.77  < 2e-16 ***
## Sleep.Disorder            2   4.22    2.11   13.75 1.75e-06 ***
## Age                       1  78.35   78.35  510.11  < 2e-16 ***
## Physical.Activity.Level   1   4.61    4.61   30.03 7.98e-08 ***
## Heart.Rate                1  23.29   23.29  151.63  < 2e-16 ***
## Daily.Steps               1  18.59   18.59  121.06  < 2e-16 ***
## Residuals               363  55.76    0.15                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

8. Uji Lanjut (POST-HOC)

TukeyHSD(ancova1, "BMI.Category")
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: Age
## Warning in replications(paste("~", xx), data = mf): non-factors ignored:
## Physical.Activity.Level
## Warning in replications(paste("~", xx), data = mf): non-factors ignored:
## Heart.Rate
## Warning in replications(paste("~", xx), data = mf): non-factors ignored:
## Daily.Steps
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Quality.of.Sleep ~ Gender + BMI.Category + Sleep.Disorder + Age + Physical.Activity.Level + Heart.Rate + Daily.Steps, data = data)
## 
## $BMI.Category
##                                 diff        lwr        upr     p adj
## Normal Weight-Normal     -0.46874303 -0.7289413 -0.2085448 0.0000276
## Obese-Normal             -1.10256828 -1.4699048 -0.7352318 0.0000000
## Overweight-Normal        -1.03318256 -1.1566929 -0.9096722 0.0000000
## Obese-Normal Weight      -0.63382525 -1.0691122 -0.1985383 0.0011386
## Overweight-Normal Weight -0.56443954 -0.8286240 -0.3002551 0.0000004
## Overweight-Obese          0.06938572 -0.3007851  0.4395565 0.9626665
TukeyHSD(ancova1, "Sleep.Disorder")
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: Age
## Warning in replications(paste("~", xx), data = mf): non-factors ignored:
## Physical.Activity.Level
## Warning in replications(paste("~", xx), data = mf): non-factors ignored:
## Heart.Rate
## Warning in replications(paste("~", xx), data = mf): non-factors ignored:
## Daily.Steps
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Quality.of.Sleep ~ Gender + BMI.Category + Sleep.Disorder + Age + Physical.Activity.Level + Heart.Rate + Daily.Steps, data = data)
## 
## $Sleep.Disorder
##                             diff        lwr       upr     p adj
## None-Insomnia         0.28972133  0.1528572 0.4265854 0.0000029
## Sleep Apnea-Insomnia  0.27221667  0.1062640 0.4381694 0.0003933
## Sleep Apnea-None     -0.01750466 -0.1537181 0.1187088 0.9508399

8. Visualisasi

8.1 Boxplot Quality of Sleep berdasarkan BMI Category

boxplot(Quality.of.Sleep ~ BMI.Category, data=data,
        col="skyblue",
        main="Quality of Sleep berdasarkan BMI Category")

8.2 Scatterplot Hubungan Age dengan Quality of Sleep

plot(data$Age, data$Quality.of.Sleep,
     main="Hubungan Age dan Quality of Sleep")