## 1.   Gunakan fungsi t.test untuk membandingkan PupalWeight berdasarkan T_treatment
set.seed(123)

data <- data.frame(
  PupalWeight = c(rnorm(20, 50, 5), rnorm(20, 55, 5)),
  T_treatment = rep(c("A", "B"), each = 20)
)

str(data)
## 'data.frame':    40 obs. of  2 variables:
##  $ PupalWeight: num  47.2 48.8 57.8 50.4 50.6 ...
##  $ T_treatment: chr  "A" "A" "A" "A" ...
t_test_result <- t.test(PupalWeight ~ T_treatment, data = data)
t_test_result
## 
##  Welch Two Sample t-test
## 
## data:  PupalWeight by T_treatment
## t = -2.823, df = 37.082, p-value = 0.007607
## alternative hypothesis: true difference in means between group A and group B is not equal to 0
## 95 percent confidence interval:
##  -6.93191 -1.13928
## sample estimates:
## mean in group A mean in group B 
##        50.70812        54.74371
## 2.   Ulangi langkah di atas menggunakan Wilcoxon rank-sum test
wilcox_result <- wilcox.test(PupalWeight ~ T_treatment, data = data)
wilcox_result
## 
##  Wilcoxon rank sum exact test
## 
## data:  PupalWeight by T_treatment
## W = 104, p-value = 0.008712
## alternative hypothesis: true location shift is not equal to 0
## 3. Jalankan kode berikut untuk membuat data
set.seed(123)

base <- rnorm(20, 20, 5)
x <- base + rnorm(20, 0, 0.5)
y <- base + rnorm(20, 1, 0.5)

head(x)
## [1] 16.66371 18.74013 27.28054 19.98810 20.33392 27.73198
head(y)
## [1] 17.85027 19.74515 28.16084 22.43702 22.25042 29.01377
## 4.   Gunakan two-sample t-test untuk membandingkan mean dari x dan y, dengan asumsi varians kedua sampel sama.
t_test_indep <- t.test(x, y, var.equal = TRUE)
t_test_indep
## 
##  Two Sample t-test
## 
## data:  x and y
## t = -0.70564, df = 38, p-value = 0.4847
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -4.174019  2.016277
## sample estimates:
## mean of x mean of y 
##  20.68249  21.76136
## 5.   Ulangi menggunakan paired t-test. Bagaimana perubahan nilai p-value
t_test_paired <- t.test(x, y, paired = TRUE)
t_test_paired
## 
##  Paired t-test
## 
## data:  x and y
## t = -8.0139, df = 19, p-value = 1.633e-07
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  -1.3606441 -0.7970983
## sample estimates:
## mean difference 
##       -1.078871
## 6.   Dari kedua uji tersebut, tentukan uji mana yang paling sesuai dan jelaskan alasannya
t_test_indep$p.value
## [1] 0.4847156
t_test_paired$p.value
## [1] 1.633238e-07
## Kesimpulan
# Data x dan y berasal dari base yang sama → berpasangan
# Uji terbaik: paired t-test
# p-value biasanya lebih kecil → lebih sensitif