getwd()
## [1] "C:/data"
setwd('c:/data')
df<-read.delim("Data1.txt")
library(dplyr)
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
glimpse(df)
## Rows: 1,925
## Columns: 26
## $ Q1        <int> 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, …
## $ Q2        <int> 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 2, 2, …
## $ Q3        <int> 2, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 4, 4, 3, 2, 3, …
## $ Q4        <int> 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 2, 4, 4, 4, 2, 2, 4, …
## $ Q5        <int> 4, 4, 2, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 3, 1, 2, …
## $ Q6        <int> 2, 3, 4, 4, 4, 4, 4, 4, 1, 2, 2, 2, 4, 4, 3, 5, 2, 2, 1, 4, …
## $ Q7        <int> 2, 2, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 5, 4, 4, 5, 4, 3, 4, 4, …
## $ Q8        <int> 4, 4, 4, 4, 4, 4, 5, 5, 2, 2, 4, 4, 4, 4, 3, 5, 4, 2, 4, 4, …
## $ Q9        <int> 4, 4, 4, 4, 2, 4, 5, 5, 3, 4, 4, 4, 2, 2, 4, 5, 2, 4, 2, 4, …
## $ Q10       <int> 4, 4, 2, 4, 4, 4, 5, 5, 2, 4, 2, 4, 4, 4, 3, 4, 4, 3, 2, 3, …
## $ Q11       <int> 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 3, 4, 4, 4, 4, 5, 4, 3, 3, …
## $ Q12       <int> 4, 4, 4, 4, 4, 4, 5, 5, 3, 4, 4, 3, 4, 3, 3, 4, 5, 4, 4, 2, …
## $ Q13       <int> 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 4, 2, 4, 4, 4, 5, 4, 4, 4, …
## $ Q14       <int> 4, 4, 4, 4, 4, 4, 5, 5, 5, 4, 4, 4, 3, 4, 5, 4, 5, 4, 4, 4, …
## $ Q15       <int> 4, 4, 3, 4, 4, 4, 4, 2, 3, 4, 4, 3, 1, 4, 4, 4, 5, 4, 4, 4, …
## $ Q16       <int> 4, 4, 4, 4, 4, 4, 5, 2, 4, 4, 4, 4, 4, 4, 5, 4, 5, 4, 4, 4, …
## $ Q17       <int> 4, 3, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 3, 2, 4, 5, 4, 4, 3, 4, …
## $ Q18       <int> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 2, 4, 4, 4, …
## $ Q19       <int> 4, 2, 4, 4, 4, 4, 4, 2, 4, 2, 4, 4, 1, 4, 4, 4, 5, 4, 2, 3, …
## $ Q20       <int> 4, 1, 3, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 2, 4, 5, 5, 4, 2, 4, …
## $ Gender    <int> 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, …
## $ EDU       <int> 1, 1, 2, 1, 2, 1, 1, 1, 4, 3, 2, 1, 1, 3, 3, 2, 1, 1, 1, 4, …
## $ BF        <dbl> 3.4, 4.0, 3.6, 4.2, 4.0, 4.0, 3.6, 3.6, 3.6, 3.2, 4.0, 3.2, …
## $ BM        <dbl> 3.2, 3.4, 3.6, 4.0, 3.6, 4.0, 4.6, 4.6, 2.2, 3.2, 3.2, 3.6, …
## $ Happiness <dbl> 4.0, 4.0, 3.8, 4.0, 4.0, 4.0, 4.8, 4.4, 3.8, 4.0, 4.0, 3.4, …
## $ Peace     <dbl> 4.0, 2.8, 3.8, 4.0, 4.0, 4.0, 3.8, 2.4, 4.0, 3.2, 4.0, 3.9, …
df$EDU<-as.factor(df$EDU)
glimpse(df)
## Rows: 1,925
## Columns: 26
## $ Q1        <int> 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, …
## $ Q2        <int> 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 2, 2, …
## $ Q3        <int> 2, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 4, 4, 3, 2, 3, …
## $ Q4        <int> 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 2, 4, 4, 4, 2, 2, 4, …
## $ Q5        <int> 4, 4, 2, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 3, 1, 2, …
## $ Q6        <int> 2, 3, 4, 4, 4, 4, 4, 4, 1, 2, 2, 2, 4, 4, 3, 5, 2, 2, 1, 4, …
## $ Q7        <int> 2, 2, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 5, 4, 4, 5, 4, 3, 4, 4, …
## $ Q8        <int> 4, 4, 4, 4, 4, 4, 5, 5, 2, 2, 4, 4, 4, 4, 3, 5, 4, 2, 4, 4, …
## $ Q9        <int> 4, 4, 4, 4, 2, 4, 5, 5, 3, 4, 4, 4, 2, 2, 4, 5, 2, 4, 2, 4, …
## $ Q10       <int> 4, 4, 2, 4, 4, 4, 5, 5, 2, 4, 2, 4, 4, 4, 3, 4, 4, 3, 2, 3, …
## $ Q11       <int> 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 3, 4, 4, 4, 4, 5, 4, 3, 3, …
## $ Q12       <int> 4, 4, 4, 4, 4, 4, 5, 5, 3, 4, 4, 3, 4, 3, 3, 4, 5, 4, 4, 2, …
## $ Q13       <int> 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 4, 2, 4, 4, 4, 5, 4, 4, 4, …
## $ Q14       <int> 4, 4, 4, 4, 4, 4, 5, 5, 5, 4, 4, 4, 3, 4, 5, 4, 5, 4, 4, 4, …
## $ Q15       <int> 4, 4, 3, 4, 4, 4, 4, 2, 3, 4, 4, 3, 1, 4, 4, 4, 5, 4, 4, 4, …
## $ Q16       <int> 4, 4, 4, 4, 4, 4, 5, 2, 4, 4, 4, 4, 4, 4, 5, 4, 5, 4, 4, 4, …
## $ Q17       <int> 4, 3, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 3, 2, 4, 5, 4, 4, 3, 4, …
## $ Q18       <int> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 2, 4, 4, 4, …
## $ Q19       <int> 4, 2, 4, 4, 4, 4, 4, 2, 4, 2, 4, 4, 1, 4, 4, 4, 5, 4, 2, 3, …
## $ Q20       <int> 4, 1, 3, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 2, 4, 5, 5, 4, 2, 4, …
## $ Gender    <int> 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, …
## $ EDU       <fct> 1, 1, 2, 1, 2, 1, 1, 1, 4, 3, 2, 1, 1, 3, 3, 2, 1, 1, 1, 4, …
## $ BF        <dbl> 3.4, 4.0, 3.6, 4.2, 4.0, 4.0, 3.6, 3.6, 3.6, 3.2, 4.0, 3.2, …
## $ BM        <dbl> 3.2, 3.4, 3.6, 4.0, 3.6, 4.0, 4.6, 4.6, 2.2, 3.2, 3.2, 3.6, …
## $ Happiness <dbl> 4.0, 4.0, 3.8, 4.0, 4.0, 4.0, 4.8, 4.4, 3.8, 4.0, 4.0, 3.4, …
## $ Peace     <dbl> 4.0, 2.8, 3.8, 4.0, 4.0, 4.0, 3.8, 2.4, 4.0, 3.2, 4.0, 3.9, …
gc.out1<-aov(Happiness~EDU,data=df)
summary(gc.out1)
##               Df Sum Sq Mean Sq F value Pr(>F)  
## EDU            3    4.9  1.6346    2.93 0.0325 *
## Residuals   1921 1071.6  0.5578                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
glimpse(gc.out1)
## List of 13
##  $ coefficients : Named num [1:4] 3.5867 -0.1113 -0.0355 0.0633
##   ..- attr(*, "names")= chr [1:4] "(Intercept)" "EDU2" "EDU3" "EDU4"
##  $ residuals    : Named num [1:1925] 0.413 0.413 0.325 0.413 0.525 ...
##   ..- attr(*, "names")= chr [1:1925] "1" "2" "3" "4" ...
##  $ effects      : Named num [1:1925] -155.627 -1.791 -1.125 0.655 0.531 ...
##   ..- attr(*, "names")= chr [1:1925] "(Intercept)" "EDU2" "EDU3" "EDU4" ...
##  $ rank         : int 4
##  $ fitted.values: Named num [1:1925] 3.59 3.59 3.48 3.59 3.48 ...
##   ..- attr(*, "names")= chr [1:1925] "1" "2" "3" "4" ...
##  $ assign       : int [1:4] 0 1 1 1
##  $ qr           :List of 5
##   ..$ qr   : num [1:1925, 1:4] -43.8748 0.0228 0.0228 0.0228 0.0228 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   .. ..- attr(*, "assign")= int [1:4] 0 1 1 1
##   .. ..- attr(*, "contrasts")=List of 1
##   ..$ qraux: num [1:4] 1.02 1.01 1 1.04
##   ..$ pivot: int [1:4] 1 2 3 4
##   ..$ tol  : num 1e-07
##   ..$ rank : int 4
##   ..- attr(*, "class")= chr "qr"
##  $ df.residual  : int 1921
##  $ contrasts    :List of 1
##   ..$ EDU: chr "contr.treatment"
##  $ xlevels      :List of 1
##   ..$ EDU: chr [1:4] "1" "2" "3" "4"
##  $ call         : language aov(formula = Happiness ~ EDU, data = df)
##  $ terms        :Classes 'terms', 'formula'  language Happiness ~ EDU
##   .. ..- attr(*, "variables")= language list(Happiness, EDU)
##   .. ..- attr(*, "factors")= int [1:2, 1] 0 1
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. ..- attr(*, "term.labels")= chr "EDU"
##   .. ..- attr(*, "order")= int 1
##   .. ..- attr(*, "intercept")= int 1
##   .. ..- attr(*, "response")= int 1
##   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
##   .. ..- attr(*, "predvars")= language list(Happiness, EDU)
##   .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor"
##   .. .. ..- attr(*, "names")= chr [1:2] "Happiness" "EDU"
##  $ model        :'data.frame':   1925 obs. of  2 variables:
##   ..$ Happiness: num [1:1925] 4 4 3.8 4 4 4 4.8 4.4 3.8 4 ...
##   ..$ EDU      : Factor w/ 4 levels "1","2","3","4": 1 1 2 1 2 1 1 1 4 3 ...
##   ..- attr(*, "terms")=Classes 'terms', 'formula'  language Happiness ~ EDU
##   .. .. ..- attr(*, "variables")= language list(Happiness, EDU)
##   .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
##   .. .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..- attr(*, "term.labels")= chr "EDU"
##   .. .. ..- attr(*, "order")= int 1
##   .. .. ..- attr(*, "intercept")= int 1
##   .. .. ..- attr(*, "response")= int 1
##   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
##   .. .. ..- attr(*, "predvars")= language list(Happiness, EDU)
##   .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor"
##   .. .. .. ..- attr(*, "names")= chr [1:2] "Happiness" "EDU"
##  - attr(*, "class")= chr [1:2] "aov" "lm"
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(EDU='Tukey'))
summary(gc.out2)
## 
##   Simultaneous Tests for General Linear Hypotheses
## 
## Multiple Comparisons of Means: Tukey Contrasts
## 
## 
## Fit: aov(formula = Happiness ~ EDU, data = df)
## 
## Linear Hypotheses:
##            Estimate Std. Error t value Pr(>|t|)  
## 2 - 1 == 0 -0.11127    0.05980  -1.861   0.2372  
## 3 - 1 == 0 -0.03552    0.05422  -0.655   0.9105  
## 4 - 1 == 0  0.06330    0.07219   0.877   0.8108  
## 3 - 2 == 0  0.07575    0.04157   1.822   0.2543  
## 4 - 2 == 0  0.17458    0.06324   2.761   0.0287 *
## 4 - 3 == 0  0.09883    0.05799   1.704   0.3122  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## (Adjusted p values reported -- single-step method)
# 둥분산성 검정
#Levene 검정은 표본이 정규성을 만족하든 만족하지 않든 상관없이 사용할 수 있다.

library(lawstat)
## Warning: 패키지 'lawstat'는 R 버전 4.3.2에서 작성되었습니다
levene.test(df$Happiness,df$EDU)
## 
##  Modified robust Brown-Forsythe Levene-type test based on the absolute
##  deviations from the median
## 
## data:  df$Happiness
## Test Statistic = 2.2252, p-value = 0.08335
# 정규성 검정
shapiro.test(gc.out1$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  gc.out1$residuals
## W = 0.97173, p-value < 2.2e-16
# 상자그림
boxplot(Happiness~EDU,data=df)

# kruskal-walls 검정
kruskal.test(Happiness~EDU,data = df)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  Happiness by EDU
## Kruskal-Wallis chi-squared = 13.248, df = 3, p-value = 0.00413
# diet 불러오기

library(dplyr)
library(WRS2)
## Warning: 패키지 'WRS2'는 R 버전 4.3.2에서 작성되었습니다
data(diet)
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 데이터 안에는 76개의 로우데이터와 7개의 컬럼이 존재한다.

diet %>% count(diet.type)
##   diet.type  n
## 1         A 24
## 2         B 25
## 3         C 27
# diet.type의 항목별 개수를 분류한다.

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
# diet.type에 따라서 체중 감소가 된다는 내용의 귀무가설의 Pr(>F)는 0.0066이고 기준값인 0.05 보다 낮으므로, 귀무가설=기각 대립가설=채댁됨
# diet.type과 weight.loss는 관계가 없다는 것이 증명

library(multcomp)
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)
# 결과는 C 집단과 A 집단, C 집단과 B 집단의 차이만 통계적으로 유의한 것이며, 이는 B 집단과 A 집단의 체중감소 차이는 관계가 있다고 보지만 C 집단과 A 집단, C 집단과 B 집단의 차이는 체중 감소의 차이에는 관계가 없다고 판단 

# 등분산성 검정

library(lawstat)
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-value = 0.6313으로 기준인 0.05 보다 높은 수치이며 이는 등분산성을 나타낸다.

# 정규성 검정
shapiro.test(gc.out1$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  gc.out1$residuals
## W = 0.99175, p-value = 0.9088
# p-value = 0.9088으로 기준인 0.05 보다 높은 수치이며 이는 정규분포를 구성한다.

# 상자 그림
boxplot(weight.loss~diet.type,data=diet)

# kruskal-walls 검정
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-value = 0.009023으로 기준인 0.05 보다 낮은 수치이며 이는 귀무가설 = 기각 으로 diet.type에 따른 집단의 weight.loss 분포의 위치모수의 중위수 값이 동일하지 않다는 것을 의미 함.

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