R Markdown

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.2.0     
## ── 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(lsr)
library(broom)
library(ggplot2)
# iki grup için örnek veri (öğrencilerin stres puanları)
egzersiz_yapan <- c(18, 20, 22, 19, 17, 21, 23, 20)
egzersiz_yapmayan <- c(25, 28, 30, 27, 26, 29, 31, 28)

# veri çerçevesi oluşturalım
stres <- c(egzersiz_yapan, egzersiz_yapmayan)
grup <- factor(c(rep("Yapan", length(egzersiz_yapan)),
                  rep("Yapmayan",
length(egzersiz_yapmayan))))

egzersiz_veri <- data.frame(grup, stres)

egzersiz_veri
##        grup stres
## 1     Yapan    18
## 2     Yapan    20
## 3     Yapan    22
## 4     Yapan    19
## 5     Yapan    17
## 6     Yapan    21
## 7     Yapan    23
## 8     Yapan    20
## 9  Yapmayan    25
## 10 Yapmayan    28
## 11 Yapmayan    30
## 12 Yapmayan    27
## 13 Yapmayan    26
## 14 Yapmayan    29
## 15 Yapmayan    31
## 16 Yapmayan    28

‘factor()’ → kategorik değişken olarak tanımlanır.

sonuc_egzersiz <- wilcox.test(stres ~ grup, data = egzersiz_veri)
## Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
## compute exact p-value with ties
sonuc_egzersiz
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  stres by grup
## W = 0, p-value = 0.0009229
## alternative hypothesis: true location shift is not equal to 0

Testin sonucunda yukarıdaki kod satırından öğrenebileceğiniz gibi aşağıdaki kod yazımı da alternatif bir kullanımdır.

wilcox.test(egzersiz_yapan, egzersiz_yapmayan)
## Warning in wilcox.test.default(egzersiz_yapan, egzersiz_yapmayan): cannot
## compute exact p-value with ties
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  egzersiz_yapan and egzersiz_yapmayan
## W = 0, p-value = 0.0009229
## alternative hypothesis: true location shift is not equal to 0

Egzersiz yapan ve yapmayan öğrencilerin stres düzeyleri arasında anlamlı fark bulunmuştur.

(W = 0, p < .001).

wilcox.test(stres ~ grup, data = egzersiz_veri) %>% 
  tidy()
## Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
## compute exact p-value with ties
## # A tibble: 1 × 4
##   statistic  p.value method                                          alternative
##       <dbl>    <dbl> <chr>                                           <chr>      
## 1         0 0.000923 Wilcoxon rank sum test with continuity correct… two.sided

‘tidy()’ → test çıktısını tablo formatında verir.

ggplot(egzersiz_veri, aes(x = grup, y = stres, fill = grup)) +
  geom_boxplot(alpha = 0.6) +
  geom_jitter(width = 0.1, alpha = 0.6) +
  labs(title = "Egzersız Durumuna Göre Stres", 
       x = "Grup",
       y = "Stres Puanı") +
  theme_minimal()