과제

1. 데이터 불러오기 및 확인

library(MASS)
library(ggplot2)
library(dplyr)
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following object is masked from 'package:MASS':
## 
##     select
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data("anorexia")
str(anorexia)
## 'data.frame':    72 obs. of  3 variables:
##  $ Treat : Factor w/ 3 levels "CBT","Cont","FT": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Prewt : num  80.7 89.4 91.8 74 78.1 88.3 87.3 75.1 80.6 78.4 ...
##  $ Postwt: num  80.2 80.1 86.4 86.3 76.1 78.1 75.1 86.7 73.5 84.6 ...
summary(anorexia)
##   Treat        Prewt           Postwt      
##  CBT :29   Min.   :70.00   Min.   : 71.30  
##  Cont:26   1st Qu.:79.60   1st Qu.: 79.33  
##  FT  :17   Median :82.30   Median : 84.05  
##            Mean   :82.41   Mean   : 85.17  
##            3rd Qu.:86.00   3rd Qu.: 91.55  
##            Max.   :94.90   Max.   :103.60

2. 데이터 형태 변형 및 FT 케이스 선택

anorexia_FT <- anorexia %>% filter(Treat == "FT")

summary(anorexia_FT)
##   Treat        Prewt           Postwt      
##  CBT : 0   Min.   :73.40   Min.   : 75.20  
##  Cont: 0   1st Qu.:80.50   1st Qu.: 90.70  
##  FT  :17   Median :83.30   Median : 92.50  
##            Mean   :83.23   Mean   : 90.49  
##            3rd Qu.:86.00   3rd Qu.: 95.20  
##            Max.   :94.20   Max.   :101.60

3. Treatment 전과 후의 weight 변화에 대한 히스토그램을 작성

ggplot(anorexia_FT, aes(x = Prewt)) +
  geom_histogram(fill = "blue") +
  labs(title = "Weight 변화 (전)",
       x = "전 체중") +
  theme_minimal()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(anorexia_FT, aes(x = Postwt)) +
  geom_histogram(fill = "orange") +
  labs(title = "Weight 변화 (후)",
       x = "후 체중") +
  theme_minimal()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

4. 정규성 검정

shapiro_test_before <- shapiro.test(anorexia_FT$Prewt)
shapiro_test_after <- shapiro.test(anorexia_FT$Postwt)

shapiro_test_before
## 
##  Shapiro-Wilk normality test
## 
## data:  anorexia_FT$Prewt
## W = 0.98821, p-value = 0.9972
shapiro_test_after
## 
##  Shapiro-Wilk normality test
## 
## data:  anorexia_FT$Postwt
## W = 0.83928, p-value = 0.007391

5. 통계적 방법 선택 및 설명

shapiro_test_after의 p value는 0.05보다 작으므로 귀무가설을 기각할 수 있다. 즉, 이는 정규성을 만족하지 않고, 두 데이터는 연관되어 있는 대응표뵨이므로 Wilcoxon Signed Rank Test를 사용한다.

6. 가설 설정

귀무가설 : 치료 전과 후의 평균 체중은 차이가 없다. 대립가설 : 치료 전과 후의 평균 체중은 차이가 있다.

7. 평균비교

wilcox_test_results <- wilcox.test(anorexia_FT$Postwt, anorexia_FT$Prewt, paired = TRUE)
print(wilcox_test_results)
## 
##  Wilcoxon signed rank exact test
## 
## data:  anorexia_FT$Postwt and anorexia_FT$Prewt
## V = 142, p-value = 0.0008392
## alternative hypothesis: true location shift is not equal to 0

8. 결론

p value가 0.05보다 현저히 작으므로 귀무가설을 기각할 수 있다. 즉, 치료 전과 후의 평균 체중은 차이가 있다다.