library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── 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(rstatix)
##
## 다음의 패키지를 부착합니다: 'rstatix'
##
## The following object is masked from 'package:stats':
##
## filter
data("PlantGrowth")
PlantGrowth <- PlantGrowth %>%
mutate(group = convert_as_factor(group))
ggplot(PlantGrowth, aes(x = group, y = weight)) +
geom_boxplot() +
labs(title = "그룹에 따른 식물 성장", x = "그룹", y = "성장")
shapiro_test(ctrl_data <- filter(PlantGrowth, group == "ctrl")$weight)
## # A tibble: 1 × 3
## variable statistic p.value
## <chr> <dbl> <dbl>
## 1 "ctrl_data <- filter(PlantGrowth, group == \"ctrl\")$weight" 0.957 0.747
shapiro_test(trt1_data <- filter(PlantGrowth, group == "trt1")$weight)
## # A tibble: 1 × 3
## variable statistic p.value
## <chr> <dbl> <dbl>
## 1 "trt1_data <- filter(PlantGrowth, group == \"trt1\")$weight" 0.930 0.452
shapiro_test(trt2_data <- filter(PlantGrowth, group == "trt2")$weight)
## # A tibble: 1 × 3
## variable statistic p.value
## <chr> <dbl> <dbl>
## 1 "trt2_data <- filter(PlantGrowth, group == \"trt2\")$weight" 0.941 0.564
levene_test(weight ~ group, data = PlantGrowth)
## # A tibble: 1 × 4
## df1 df2 statistic p
## <int> <int> <dbl> <dbl>
## 1 2 27 1.12 0.341
t_test_result1 <- t.test(ctrl_data, trt1_data, var.equal = TRUE)
print(t_test_result1)
##
## Two Sample t-test
##
## data: ctrl_data and trt1_data
## t = 1.1913, df = 18, p-value = 0.249
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.2833003 1.0253003
## sample estimates:
## mean of x mean of y
## 5.032 4.661
t_test_result2 <- t.test(ctrl_data, trt2_data, var.equal = TRUE)
print(t_test_result2)
##
## Two Sample t-test
##
## data: ctrl_data and trt2_data
## t = -2.134, df = 18, p-value = 0.04685
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.980338117 -0.007661883
## sample estimates:
## mean of x mean of y
## 5.032 5.526
비교 1의 p value는 0.05보다 크므로 귀무가설을 기각할 수 없다. 즉, 비교 1의 그룹간의 식물 평균차이는 없다. 비교 2의 p value는 0.05보다 작으므로 귀무가설을 기각할 수 있다. 즉, 비교 2의 그룹간의 식물 평균차이는 있다.
?sleep
## httpd 도움말 서버를 시작합니다 ... 완료
data("sleep")
str(sleep)
## 'data.frame': 20 obs. of 3 variables:
## $ extra: num 0.7 -1.6 -0.2 -1.2 -0.1 3.4 3.7 0.8 0 2 ...
## $ group: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ ID : Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
summary(sleep)
## extra group ID
## Min. :-1.600 1:10 1 :2
## 1st Qu.:-0.025 2:10 2 :2
## Median : 0.950 3 :2
## Mean : 1.540 4 :2
## 3rd Qu.: 3.400 5 :2
## Max. : 5.500 6 :2
## (Other):8
mean_change <- sleep %>%
group_by(group) %>%
summarise(mean_extra = mean(extra),
sd_extra = sd(extra),
n = n())
library(ggplot2)
ggplot(sleep, aes(x = as.factor(group), y = extra)) +
geom_boxplot() +
labs(title = "수면약의 효과", x = "그룹", y = "수면시간의 변화")
shapiro_A <- shapiro.test(sleep$extra[sleep$group == 1])
shapiro_B <- shapiro.test(sleep$extra[sleep$group == 2])
shapiro_A
##
## Shapiro-Wilk normality test
##
## data: sleep$extra[sleep$group == 1]
## W = 0.92581, p-value = 0.4079
shapiro_B
##
## Shapiro-Wilk normality test
##
## data: sleep$extra[sleep$group == 2]
## W = 0.9193, p-value = 0.3511
library(car)
## 필요한 패키지를 로딩중입니다: carData
##
## 다음의 패키지를 부착합니다: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
## The following object is masked from 'package:purrr':
##
## some
levene_test <- levene_test(extra ~ group, data = sleep)
levene_test
## # A tibble: 1 × 4
## df1 df2 statistic p
## <int> <int> <dbl> <dbl>
## 1 1 18 0.248 0.624
t_test_results <- t.test(extra ~ group, data = sleep, var.equal = TRUE)
t_test_results
##
## Two Sample t-test
##
## data: extra by group
## t = -1.8608, df = 18, p-value = 0.07919
## alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
## 95 percent confidence interval:
## -3.363874 0.203874
## sample estimates:
## mean in group 1 mean in group 2
## 0.75 2.33
p value가 0.05보다 크므로 귀무가설을 기각할 수 없다. 즉, 두 수면제에 차이는 없다.