## [1] "C:/data"
데이터의 분포를 박스 플롯으로 시각화하고 주요 기술통계를 본다.
# boxplot
boxplot(price ~ color,
data = diamonds,
main = "Boxplot of price for each color",
xlab = "Factor Levels : color",
ylab = "price")## $D
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 357 911 1838 3170 4214 18693
##
## $E
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 326 882 1739 3077 4003 18731
##
## $F
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 342 982 2344 3725 4868 18791
##
## $G
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 354 931 2242 3999 6048 18818
##
## $H
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 337 984 3460 4487 5980 18803
##
## $I
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 334 1120 3730 5092 7202 18823
##
## $J
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 335 1860 4234 5324 7695 18710
잔차의 정규성은 표본수가 30 이상이므로 확인하지 않는다.
등분산성을 가설검정할 때 요인의 수준이 2 개인 경우에는 두 모집단의 분산의 차이 검정을 F-분포를 사용하여 수행할 수 있다. 이 경우에는 var.test() 함수를 이용하여 가설검정을 수행하였다. 그러나 요인의 수준이 3 개 이상인 경우에는 bartlet.test()을 사용한다.
# Bartlett test to test the null hypothesis of equal group variances: 등분산 검정
bartlett.test( price ~ color, data = diamonds )##
## Bartlett test of homogeneity of variances
##
## data: price by color
## Bartlett's K-squared = 1402.4, df = 6, p-value < 2.2e-16
p-값<0.05이므로 등분산 가정도 기각했다. 따라서 이 경우에는 Welch’s 일원 배치 분산 분석 (Welch’s One way ANOVA)을 시행한다.
다이아몬드 컬러별 가격 수준에서 하나 이상의 차이가 있는지 확인하기 위해 Welch’s 일원 배치 분산 분석 (Welch’s One way ANOVA)과 Brown-Forsythe’s 일원 배치 분산 분석 (Brown-Forsythe’s One way ANOVA) 를 수행한다.
# Welch's 일원 배치 분산 분석 (Welch's One way ANOVA)
result <- oneway.test( price ~ color, data = diamonds, var.equal=FALSE )
result##
## One-way analysis of means (not assuming equal variances)
##
## data: price and color
## F = 280.55, num df = 6, denom df = 18316, p-value < 2.2e-16
# Brown-Forsythe's 일원 배치 분산 분석 (Brown-Forsythe's One way ANOVA)
# install.packages("onewaytests")
library("onewaytests")
diamonds$color <-as.factor(diamonds$color)
bf.test(price ~ color, data = diamonds)##
## Brown-Forsythe Test (alpha = 0.05)
## -------------------------------------------------------------
## data : price and color
##
## statistic : 275.2607
## num df : 6
## denom df : 34219.87
## p.value : 0
##
## Result : Difference is statistically significant.
## -------------------------------------------------------------
분석 결과 최소한 하나 이상의 컬러간 가격 차이가 있다고 보인다. (p<0.05이므로)
Welch’s 일원 배치 분산 분석 (Welch’s One way ANOVA), Brown-Forsythe’s 일원 배치 분산 분석 (Brown-Forsythe’s One way ANOVA) 역시 분석을 통해 평균이 같다는 귀무가설이 기각되면, 어느 수준 간의 차이에 의해 귀무가설이 기각되었는지 살펴보아야 한다.
하지만 등분산 가정 위배되는 경우 사후 검정 방법은 Tamhane’s T2, Games-Howell, Dunnett’s T3 정도이다.
# install.packages("PMCMRplus")
library("PMCMRplus")
#1) Tamhane's T2
tamhaneT2Test(price ~ color, data = diamonds)##
## Pairwise comparisons using Tamhane's T2-test for unequal variances
## data: price by color
## D E F G H I
## E 0.82 - - - - -
## F < 2e-16 < 2e-16 - - - -
## G < 2e-16 < 2e-16 9.6e-06 - - -
## H < 2e-16 < 2e-16 < 2e-16 9.3e-15 - -
## I < 2e-16 < 2e-16 < 2e-16 < 2e-16 4.5e-13 -
## J < 2e-16 < 2e-16 < 2e-16 < 2e-16 < 2e-16 0.45
##
## P value adjustment method: T2 (Sidak)
## alternative hypothesis: two.sided
##
## Pairwise comparisons using Games-Howell test
## data: price by color
## D E F G H I
## E 0.58 - - - - -
## F 1.7e-08 2.2e-08 - - - -
## G 2.5e-08 < 2e-16 9.4e-06 - - -
## H 1.2e-08 1.9e-08 3.0e-08 3.4e-08 - -
## I < 2e-16 2.0e-11 < 2e-16 < 2e-16 < 2e-16 -
## J 3.6e-08 1.3e-08 3.0e-08 2.8e-08 4.0e-08 0.30
##
## P value adjustment method: none
## alternative hypothesis: two.sided
##
## Pairwise comparisons using Dunnett's T3 test for multiple comparisons
## with unequal variances
## data: price by color
## D E F G H I
## E 0.82 - - - - -
## F < 2e-16 < 2e-16 - - - -
## G < 2e-16 < 2e-16 9.6e-06 - - -
## H < 2e-16 < 2e-16 < 2e-16 9.8e-15 - -
## I < 2e-16 < 2e-16 < 2e-16 < 2e-16 4.5e-13 -
## J < 2e-16 < 2e-16 < 2e-16 < 2e-16 < 2e-16 0.45
##
## P value adjustment method: single-step
## alternative hypothesis: two.sided
## D E F G H I J
## 3169.954 3076.752 3724.886 3999.136 4486.669 5091.875 5323.818
3가지 사후 검정 모두 다음과 같은 결과를 제시하고 있다.
\[J=I>H>G>F>D=E\]