T-test dapat digunakan untuk uji rata-rata dua variabel. T-test dapat digunakan jika data memenuhi asumsi-asumsi uji parametrik. Uji normalitas dapat dilihat di sini.
Data yang digunakan adalah data dari R (R data sets),
"CO2"
.
data("CO2")
head(CO2)
Terdapat 3 jenis t-test, One-sample t-test, Two-sample t-test, dan Paired t-test.
Berdasarkan data yang digunakan, jenis t-test yang tepat digunakan adalah Two-sample t-test. Tapi kita bisa juga mencoba uji Paired t-test dengan data yang sama, untuk membandingkan hasilnya dengan Two-sample t-test.
# Two-sample t-test
two <- t.test(uptake ~ Treatment, data = CO2)
two
##
## Welch Two Sample t-test
##
## data: uptake by Treatment
## t = 3.0485, df = 80.945, p-value = 0.003107
## alternative hypothesis: true difference in means between group nonchilled and group chilled is not equal to 0
## 95 percent confidence interval:
## 2.382366 11.336682
## sample estimates:
## mean in group nonchilled mean in group chilled
## 30.64286 23.78333
# atau
install.packages("rstatix")
library("rstatix")
stat.two <- CO2 %>%
t_test(uptake ~ Treatment) %>%
add_significance()
stat.two
## # A tibble: 1 × 9
## .y. group1 group2 n1 n2 statistic df p p.signif
## <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <chr>
## 1 uptake nonchilled chilled 42 42 3.05 80.9 0.00311 **
# Paired t-test
pair <- t.test(uptake ~ Treatment, data = CO2, paired = TRUE)
pair
##
## Paired t-test
##
## data: uptake by Treatment
## t = 7.939, df = 41, p-value = 8.051e-10
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 5.114589 8.604458
## sample estimates:
## mean difference
## 6.859524
# atau
stat.pair <- CO2 %>%
t_test(uptake ~ Treatment, paired = TRUE) %>%
add_significance()
stat.pair
## # A tibble: 1 × 9
## .y. group1 group2 n1 n2 statistic df p p.signif
## <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <chr>
## 1 uptake nonchilled chilled 42 42 7.94 41 8.05e-10 ****
Selain itu, kita bisa melakukan uji statistik deskriptif sederhana untuk mencari nilai rata-rata (mean) dan standar deviasi (sd). Bisa juga dilakukan uji statistik deskriptif secara lebih detail.
# Sederhana
aggregate(uptake ~ Treatment,
data = CO2,
function(x) round(c(mean = mean(x), sd = sd(x)), 2)
)
## Treatment uptake.mean uptake.sd
## 1 nonchilled 30.64 9.70
## 2 chilled 23.78 10.88
# Detail
CO2 %>%
group_by(Treatment) %>%
get_summary_stats(uptake, type = "common")
## # A tibble: 2 × 11
## Treatment variable n min max median iqr mean sd se ci
## <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 nonchilled uptake 42 10.6 45.5 31.3 12.2 30.6 9.70 1.50 3.02
## 2 chilled uptake 42 7.7 42.4 19.7 20.4 23.8 10.9 1.68 3.39
Jika data tidak memenuhi asumsi-asumsi uji parametrik, maka dapat menggunakan uji non-parametrik.
Uji non-parametrik yang dapat digunakan sebagai alternatif adalah uji Mann–Whitney U (Two-sample t-test) dan uji Wilcoxon Signed Rank (Paired t-test).
# Uji Mann–Whitney U
stat.mann <- CO2 %>%
wilcox_test(uptake ~ Treatment, paired = FALSE) %>%
add_significance()
stat.mann
## # A tibble: 1 × 8
## .y. group1 group2 n1 n2 statistic p p.signif
## <chr> <chr> <chr> <int> <int> <dbl> <dbl> <chr>
## 1 uptake nonchilled chilled 42 42 1188. 0.00636 **
# Uji Wilcoxon Signed Rank
stat.wil <- CO2 %>%
wilcox_test(uptake ~ Treatment, paired = TRUE) %>%
add_significance()
stat.wil
## # A tibble: 1 × 8
## .y. group1 group2 n1 n2 statistic p p.signif
## <chr> <chr> <chr> <int> <int> <dbl> <dbl> <chr>
## 1 uptake nonchilled chilled 42 42 861 0.0000000251 ****
## R version 4.3.2 (2023-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19045)
##
## Matrix products: default
##
##
## locale:
## [1] LC_COLLATE=English_United States.utf8
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## time zone: Asia/Jakarta
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] rstatix_0.7.2
##
## loaded via a namespace (and not attached):
## [1] vctrs_0.6.5 cli_3.6.2 knitr_1.45 rlang_1.1.3
## [5] xfun_0.42 purrr_1.0.2 car_3.1-2 generics_0.1.3
## [9] jsonlite_1.8.8 glue_1.7.0 backports_1.4.1 htmltools_0.5.7
## [13] sass_0.4.8 fansi_1.0.6 rmarkdown_2.25 abind_1.4-5
## [17] carData_3.0-5 evaluate_0.23 jquerylib_0.1.4 tibble_3.2.1
## [21] fastmap_1.1.1 yaml_2.3.8 lifecycle_1.0.4 compiler_4.3.2
## [25] dplyr_1.1.4 pkgconfig_2.0.3 tidyr_1.3.1 rstudioapi_0.15.0
## [29] digest_0.6.34 R6_2.5.1 tidyselect_1.2.0 utf8_1.2.4
## [33] pillar_1.9.0 magrittr_2.0.3 bslib_0.6.1 withr_3.0.0
## [37] tools_4.3.2 broom_1.0.5 cachem_1.0.8