Preprocessing Data

Memuat Library

# Memuat paket untuk manipulasi data, grafik, uji statistik, dan tabel
# install.packages(c("dplyr", "ggplot2", "car", "knitr"))
library(dplyr)
library(ggplot2)
library(car)
library(knitr)

Import Data

# Membaca data, merapikan nama kolom, dan mengubah faktor ke tipe kategori
data <- read.csv("Sleep_health_and_lifestyle_dataset.csv",
                 check.names = FALSE,
                 stringsAsFactors = FALSE)

names(data) <- make.names(names(data))

data$Sleep.Disorder <- factor(data$Sleep.Disorder,
                              levels = c("None", "Insomnia", "Sleep Apnea"))
data$Gender <- as.factor(data$Gender)

str(data)
## 'data.frame':    374 obs. of  13 variables:
##  $ Person.ID              : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Gender                 : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 2 ...
##  $ 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         : Factor w/ 3 levels "None","Insomnia",..: 1 1 1 3 3 2 2 1 1 1 ...
head(data)
##   Person.ID Gender Age           Occupation Sleep.Duration Quality.of.Sleep
## 1         1   Male  27    Software Engineer            6.1                6
## 2         2   Male  28               Doctor            6.2                6
## 3         3   Male  28               Doctor            6.2                6
## 4         4   Male  28 Sales Representative            5.9                4
## 5         5   Male  28 Sales Representative            5.9                4
## 6         6   Male  28    Software Engineer            5.9                4
##   Physical.Activity.Level Stress.Level 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
##   Daily.Steps Sleep.Disorder
## 1        4200           None
## 2       10000           None
## 3       10000           None
## 4        3000    Sleep Apnea
## 5        3000    Sleep Apnea
## 6        3000       Insomnia

Pemeriksaan Data Kosong dan Duplikat

colSums(is.na(data))   # data kosong
##               Person.ID                  Gender                     Age 
##                       0                       0                       0 
##              Occupation          Sleep.Duration        Quality.of.Sleep 
##                       0                       0                       0 
## Physical.Activity.Level            Stress.Level            BMI.Category 
##                       0                       0                       0 
##          Blood.Pressure              Heart.Rate             Daily.Steps 
##                       0                       0                       0 
##          Sleep.Disorder 
##                       0
sum(duplicated(data))  # data duplikat
## [1] 0

Pemilihan Variabel

# Memilih variabel respons dan dua faktor sesuai rumusan masalah
data_anova <- data %>%
  dplyr::select(Quality.of.Sleep, Sleep.Disorder, Gender)

head(data_anova)
##   Quality.of.Sleep Sleep.Disorder Gender
## 1                6           None   Male
## 2                6           None   Male
## 3                6           None   Male
## 4                4    Sleep Apnea   Male
## 5                4    Sleep Apnea   Male
## 6                4       Insomnia   Male
summary(data_anova)
##  Quality.of.Sleep     Sleep.Disorder    Gender   
##  Min.   :4.000    None       :219    Female:185  
##  1st Qu.:6.000    Insomnia   : 77    Male  :189  
##  Median :7.000    Sleep Apnea: 78                
##  Mean   :7.313                                   
##  3rd Qu.:8.000                                   
##  Max.   :9.000

Eksplorasi Data (EDA)

# Jumlah data pada tiap kombinasi Sleep Disorder dan Gender
table(data_anova$Sleep.Disorder, data_anova$Gender)
##              
##               Female Male
##   None            82  137
##   Insomnia        36   41
##   Sleep Apnea     67   11
# Rata-rata dan simpangan baku kualitas tidur per kelompok
data_anova %>%
  group_by(Sleep.Disorder, Gender) %>%
  summarise(
    n = n(),
    rata_rata = mean(Quality.of.Sleep),
    sd = sd(Quality.of.Sleep),
    .groups = "drop"
  ) %>%
  kable(digits = 2, caption = "Statistik deskriptif per kelompok")
Statistik deskriptif per kelompok
Sleep.Disorder Gender n rata_rata sd
None Female 82 8.28 0.84
None Male 137 7.23 0.83
Insomnia Female 36 6.94 0.63
Insomnia Male 41 6.17 0.77
Sleep Apnea Female 67 7.30 1.61
Sleep Apnea Male 11 6.64 1.80
# Boxplot kualitas tidur per Sleep Disorder, dibedakan menurut Gender
ggplot(data_anova, aes(x = Sleep.Disorder, y = Quality.of.Sleep, fill = Gender)) +
  geom_boxplot(alpha = 0.7) +
  labs(
    title = "Kualitas Tidur Berdasarkan Sleep Disorder dan Gender",
    x = "Sleep Disorder",
    y = "Kualitas Tidur",
    fill = "Gender"
  ) +
  theme_minimal()

# Interaction plot: rata-rata kualitas tidur per kombinasi faktor
ggplot(data_anova, aes(x = Sleep.Disorder, y = Quality.of.Sleep,
                       color = Gender, group = Gender)) +
  stat_summary(fun = mean, geom = "point", size = 3) +
  stat_summary(fun = mean, geom = "line", linewidth = 1) +
  labs(
    title = "Interaction Plot: Sleep Disorder x Gender",
    x = "Sleep Disorder",
    y = "Rata-rata Kualitas Tidur",
    color = "Gender"
  ) +
  theme_minimal()

Model ANOVA Dua Jalur

Ukuran sel tidak seimbang, sehingga digunakan Type II sum of squares.

# Model ANOVA dua jalur dengan interaksi, lalu tabel ANOVA Type II
model <- aov(Quality.of.Sleep ~ Sleep.Disorder * Gender, data = data_anova)
anova_tab <- Anova(model, type = 2)
anova_tab
## Anova Table (Type II tests)
## 
## Response: Quality.of.Sleep
##                       Sum Sq  Df F value    Pr(>F)    
## Sleep.Disorder         93.84   2 43.8984 < 2.2e-16 ***
## Gender                 70.00   1 65.4858 8.618e-15 ***
## Sleep.Disorder:Gender   1.84   2  0.8628    0.4228    
## Residuals             393.34 368                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Uji Asumsi

Normalitas Residual dengan Shapiro-Wilk

# H0: residual berdistribusi normal
res <- residuals(model)
shapiro.test(res)
## 
##  Shapiro-Wilk normality test
## 
## data:  res
## W = 0.94481, p-value = 1.384e-10

Homogenitas Varians

# Uji Levene. H0: varians sama di semua kelompok
leveneTest(Quality.of.Sleep ~ Sleep.Disorder * Gender, data = data_anova)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value    Pr(>F)    
## group   5  17.927 6.475e-16 ***
##       368                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Uji Lanjut (Tukey HSD)

# Perbandingan berpasangan antar kelompok Sleep Disorder
tukey <- TukeyHSD(model, which = "Sleep.Disorder")
tukey
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Quality.of.Sleep ~ Sleep.Disorder * Gender, data = data_anova)
## 
## $Sleep.Disorder
##                            diff        lwr         upr     p adj
## Insomnia-None        -1.0931032 -1.4154371 -0.77076941 0.0000000
## Sleep Apnea-None     -0.4204426 -0.7412440 -0.09964112 0.0062031
## Sleep Apnea-Insomnia  0.6726607  0.2818192  1.06350215 0.0001843
plot(tukey, las = 1)