library(WRS2)
## Warning: 패키지 'WRS2'는 R 버전 4.3.2에서 작성되었습니다
data(diet)
library(dplyr)
## Warning: 패키지 'dplyr'는 R 버전 4.3.2에서 작성되었습니다
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
glimpse(diet)
## Rows: 76
## Columns: 7
## $ gender         <fct> Female, Female, Female, Female, Female, Female, Female,…
## $ age            <int> 22, 46, 55, 33, 50, 50, 37, 28, 28, 45, 60, 48, 41, 37,…
## $ height         <int> 159, 192, 170, 171, 170, 201, 174, 176, 165, 165, 173, …
## $ diet.type      <fct> A, A, A, A, A, A, A, A, A, A, A, A, A, A, B, B, B, B, B…
## $ initial.weight <int> 58, 60, 64, 64, 65, 66, 67, 69, 70, 70, 72, 72, 72, 82,…
## $ final.weight   <dbl> 54.2, 54.0, 63.3, 61.1, 62.2, 64.0, 65.0, 60.5, 68.1, 6…
## $ weight.loss    <dbl> 3.8, 6.0, 0.7, 2.9, 2.8, 2.0, 2.0, 8.5, 1.9, 3.1, 1.5, …
# 과제제출
diet %>% count(diet.type)
##   diet.type  n
## 1         A 24
## 2         B 25
## 3         C 27
gc.out1<-aov(weight.loss~diet.type,data=diet)
summary(gc.out1)
##             Df Sum Sq Mean Sq F value Pr(>F)   
## diet.type    2   60.5  30.264   5.383 0.0066 **
## Residuals   73  410.4   5.622                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 다이어트 종류에 따라 체중감소의 결과가 동일하다는 귀무가설을 기준으로
# P값이 0.0066의 결과치에 따라 0.05이하이므로 대립가설을 채택.
# 따라서 다이어트 종류에 따라서 체중감소는 다르다.



library(multcomp)
## Warning: 패키지 'multcomp'는 R 버전 4.3.2에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: mvtnorm
## Warning: 패키지 'mvtnorm'는 R 버전 4.3.2에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: survival
## 필요한 패키지를 로딩중입니다: TH.data
## Warning: 패키지 'TH.data'는 R 버전 4.3.2에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: MASS
## 
## 다음의 패키지를 부착합니다: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## 다음의 패키지를 부착합니다: 'TH.data'
## The following object is masked from 'package:MASS':
## 
##     geyser
gc.out2<-glht(gc.out1,linfct=mcp(diet.type='Tukey'))
summary(gc.out2)
## 
##   Simultaneous Tests for General Linear Hypotheses
## 
## Multiple Comparisons of Means: Tukey Contrasts
## 
## 
## Fit: aov(formula = weight.loss ~ diet.type, data = diet)
## 
## Linear Hypotheses:
##            Estimate Std. Error t value Pr(>|t|)  
## B - A == 0  -0.0320     0.6776  -0.047   0.9988  
## C - A == 0   1.8481     0.6652   2.778   0.0188 *
## C - B == 0   1.8801     0.6581   2.857   0.0152 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## (Adjusted p values reported -- single-step method)
# 다이어트 종류의 집단간 평균치를 분석함.
# B-A는 P값이 0.05보다 크므로 평균간 차이가 없음
# C-A , C-B는 P값이 0.05보다 작으므로 평균간 차이가 있음.


library(lawstat)
## Warning: 패키지 'lawstat'는 R 버전 4.3.2에서 작성되었습니다
levene.test(diet$weight.loss,diet$diet.type)
## 
##  Modified robust Brown-Forsythe Levene-type test based on the absolute
##  deviations from the median
## 
## data:  diet$weight.loss
## Test Statistic = 0.46291, p-value = 0.6313
# 레벤테스트 결과 P값이 0.63이므로, 등분산성이 있음.

shapiro.test(gc.out1$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  gc.out1$residuals
## W = 0.99175, p-value = 0.9088
# 샤프로테스트 결과 P값이 0.9088이므로, 정규분포임.

kruskal.test(weight.loss~diet.type,data=diet)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  weight.loss by diet.type
## Kruskal-Wallis chi-squared = 9.4159, df = 2, p-value = 0.009023
# 크로칼 테스트(비모수검정) 결과 P값이 0.009이므로, 집단간의 위치모수는는이가 있음


boxplot(weight.loss~diet.type,data=diet)

# 위치모수를 그래프로 확인한 결과 차이 있음.