과제 1

식물의 성장 데이터를 포함하고 있는 R 내장 데이터 PlantGrowth를 불러옵니다.

  • 비교 1: 그룹 ctrl과 trt1 간의 식물 성장률 (weight)의 평균차이가 존재하는지를 유의수준 5%로 검정하시오.
  • 비교 2: 그룹 ctrl과 trt2 간의 식물 성장률 (weight)의 평균차이가 존재하는지를 유의수준 5%로 검정하시오

1. 데이터 불러오기, 처리리

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))

2. 가설 설정

  • 귀무가설 : 두 그룹의 평균차이는 없다
  • 대립가설 : 두 그룹의 평균 차이가 있다.

3. 시각화

ggplot(PlantGrowth, aes(x = group, y = weight)) +
  geom_boxplot() +
  labs(title = "그룹에 따른 식물 성장", x = "그룹", y = "성장")

4. 정규성 검정

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

5. 등분산 검정

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

6. 평균 비교

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

7. 결론

비교 1의 p value는 0.05보다 크므로 귀무가설을 기각할 수 없다. 즉, 비교 1의 그룹간의 식물 평균차이는 없다. 비교 2의 p value는 0.05보다 작으므로 귀무가설을 기각할 수 있다. 즉, 비교 2의 그룹간의 식물 평균차이는 있다.

과제 2

수면제 A와 B의 효과를 유의수준 5%로 검정하시오

  • 총10명의 시험자를 대상으로 두 개의 수면제를 비교하는 실험을 진행하였다.
  • 시험 시작전 평균 수면시간을 측정하고 수면제 투약후 수면시간 측정하였다
  • 수면시간의 변화 = 수면제 투약 후 수면시간 - 투약전 평균 수면시간

1. 데이터 특성 알아보기

?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

2. 수면 시간의 변화

mean_change <- sleep %>%
  group_by(group) %>%
  summarise(mean_extra = mean(extra),
            sd_extra = sd(extra),
            n = n())  

3. 가설 설정

  • 귀무가설 : 수면제 A와 B의 효과는 같다.
  • 대립가설 : 수면제 A와 B의 효과는 다르다.

4. 시각화

library(ggplot2)

ggplot(sleep, aes(x = as.factor(group), y = extra)) +
  geom_boxplot() +
  labs(title = "수면약의 효과", x = "그룹", y = "수면시간의 변화")

5. 정규성 검사

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

6. 등분산 검정

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

7. 비교

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

8. 결론

p value가 0.05보다 크므로 귀무가설을 기각할 수 없다. 즉, 두 수면제에 차이는 없다.