SOAL 1: Uji Normalitas Data Tunggal

# 1. Input Data
penjualan <- c(45, 52, 47, 50, 49, 53, 54, 48, 51, 50, 
               55, 46, 49, 47, 52, 50, 48, 53, 54, 49)
# 2. Visualisasi Data (Histogram dan QQ-Plot)
par(mfrow = c(1, 2))

# Histogram
hist(penjualan, 
     main = "Histogram Penjualan", 
     xlab = "Jumlah Penjualan", 
     col = "lightblue", 
     border = "black", 
     prob = TRUE)
lines(density(penjualan), col = "red", lwd = 2)

# QQ-Plot
qqnorm(penjualan, main = "Normal Q-Q Plot Penjualan")
qqline(penjualan, col = "red", lwd = 2)

par(mfrow = c(1, 1))
# 3. Uji Formal Normalitas
# a. Uji Shapiro-Wilk
shapiro_1 <- shapiro.test(penjualan)
print(shapiro_1)
## 
##  Shapiro-Wilk normality test
## 
## data:  penjualan
## W = 0.96996, p-value = 0.754
# b. Uji Kolmogorov-Smirnov 
ks_1 <- ks.test(scale(penjualan), "pnorm")
## Warning in ks.test.default(scale(penjualan), "pnorm"): ties should not be
## present for the one-sample Kolmogorov-Smirnov test
print(ks_1)
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  scale(penjualan)
## D = 0.11402, p-value = 0.9573
## alternative hypothesis: two-sided

SOAL 2: Uji Normalitas pada Dua Variabel

# 1. Input Data
berat <- c(55, 60, 62, 58, 64, 70, 68, 72, 59, 61,
           63, 65, 66, 67, 71, 69, 60, 58, 62, 64,
           73, 75, 77, 68, 70, 72, 74, 69, 63, 65)
tinggi <- c(160, 162, 164, 158, 166, 172, 170, 175, 161, 163,
            165, 167, 169, 171, 173, 174, 160, 159, 164, 166,
            176, 178, 180, 172, 174, 176, 177, 171, 165, 167)

data_fisik <- list(Berat = berat, Tinggi = tinggi)
print(data_fisik)
## $Berat
##  [1] 55 60 62 58 64 70 68 72 59 61 63 65 66 67 71 69 60 58 62 64 73 75 77 68 70
## [26] 72 74 69 63 65
## 
## $Tinggi
##  [1] 160 162 164 158 166 172 170 175 161 163 165 167 169 171 173 174 160 159 164
## [20] 166 176 178 180 172 174 176 177 171 165 167
# 2. Uji Shapiro-Wilk secara bersamaan dengan lapply()
shapiro_2 <- lapply(data_fisik, FUN = shapiro.test)
print(shapiro_2)
## $Berat
## 
##  Shapiro-Wilk normality test
## 
## data:  X[[i]]
## W = 0.98257, p-value = 0.8889
## 
## 
## $Tinggi
## 
##  Shapiro-Wilk normality test
## 
## data:  X[[i]]
## W = 0.96475, p-value = 0.4071
# 3. QQ-Plot untuk masing-masing variabel
par(mfrow = c(1, 2))

qqnorm(berat, main = "Q-Q Plot Berat Badan")
qqline(berat, col = "skyblue", lwd = 2)

qqnorm(tinggi, main = "Q-Q Plot Tinggi Badan")
qqline(tinggi, col = "pink", lwd = 2)

par(mfrow = c(1, 1))

SOAL 3: Uji Normalitas pada Dataset R (mtcars)

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
data(mtcars)

tabel_hasil <- data.frame(
  Variabel    = c("mpg", "hp", "wt"),
  Statistik_W = c(shapiro.test(mtcars$mpg)$statistic,
                  shapiro.test(mtcars$hp)$statistic,
                  shapiro.test(mtcars$wt)$statistic),
  p_value     = c(shapiro.test(mtcars$mpg)$p.value,
                  shapiro.test(mtcars$hp)$p.value,
                  shapiro.test(mtcars$wt)$p.value)
)

print(tabel_hasil)
##   Variabel Statistik_W    p_value
## 1      mpg   0.9475647 0.12288136
## 2       hp   0.9334193 0.04880824
## 3       wt   0.9432577 0.09265499
# 2. Visualisasi Density Plot dengan Garis Normal Overlay
par(mfrow = c(1, 3))

vars <- c("mpg", "hp", "wt")
titles <- c("Density mpg", "Density hp", "Density wt")

for(i in 1:3) {
  x <- mtcars[[vars[i]]]
  dens <- density(x)
  plot(dens, main = titles[i], xlab = vars[i], col = "blue", lwd = 2,
       ylim = c(0, max(dens$y, dnorm(mean(x), mean(x), sd(x)))))
  curve(dnorm(x, mean = mean(mtcars[[vars[i]]]), sd = sd(mtcars[[vars[i]]])), 
        add = TRUE, col = "red", lty = 2, lwd = 2)
  legend("topright", legend = c("Empirical", "Normal"), col = c("blue", "red"), lty = 1:2, cex = 0.8)
}

par(mfrow = c(1, 1))