01Basic

[Fastcampus] RDC 강의 내용 정리 - 이부일 강사님

분산분석(ANOVA : Analysis of Variance)

When?
세 개 이상의 집단 간에 양적 자료에 차이가 있는지를 통계적으로 검정하는 방법

Use library & data

require("readxl")
require("nortest")
require("nparcomp")
require("PMCMR")
require("PMCMRplus")
require("writexl")

houseDF <- readxl::read_excel(path      = "kc_house_data.xlsx",
                              sheet     = 1,
                              col_names = TRUE)
View(houseDF)
str(houseDF)
table(houseDF$condition)
houseDF$condition <- as.factor(houseDF$condition)


1. 질적 자료 1개, 양적 자료 1개

질적 자료는 3개 이상의 유한 집단으로 구성되어 있어야 함.

  • 귀무가설 : condition에 따라 price에 차이가 없다.
  • 대립가설 : condition에 따라 price에 차이가 있다.

1단계 : 정규성 검정

  • 귀무가설 : 정규분포를 따른다.
  • 대립가설 : 정규분포를 따르지 않는다.
by(houseDF$price, houseDF$condition, ad.test)

2단계 : 정규성 가정이 만족이 되었다면

분산분석 : ANOVA
분산분석 결과 <- aov(양적자료 ~ 질적자료, data = dataname)

anova.result <- aov(price ~ condition, data = houseDF)
summary(anova.result)

<결과>
               Df           Sum Sq       Mean Sq F value       Pr(>F)    
condition       1    3851399435634 3851399435634   28.61 0.0000000894 ***
Residuals   21611 2909065362485666  134610400374                         
---
Signif. codes:  0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1

유의확률이 0.000이므로 유의수준 0.05에서 condition에 따라 price에 통계적으로 유의한 차이가 있는 것으로 나타났다.


3단계 : 2단계의 결론이 대립가설이면

다중비교(Multiple Comparison) = 사후분석(Post-Adhoc)
Duncan, Tukey, Scheffee, Bonferroni, Dunnett

TukeyHSD(anova.result)

2단계 : 정규성 가정을 만족하지 않으면

Kruskal - Wallis Test
kruskal.test(양적자료 ~ 질적자료, data = dataname)

> kruskal.test(price ~ condition, data = houseDF)

	Kruskal-Wallis rank sum test

data:  price by condition
Kruskal-Wallis chi-squared = 260.85, df = 4, p-value < 2.2e-16

유의확률이 0.000이므로 유의수준 0.05에서 condition에 따라 price에 통계적으로 유의한 차이가 있는 것으로 나타났다.


3단계 : Kruskal-Wallis Test의 결론이 대립가설이면

다중비교 = 사후분석을 실시함
nparcomp::nparcomp(양적자료 ~ 질적자료, data = dataname)

> PMCMR::posthoc.kruskal.nemenyi.test(price ~ condition, data = houseDF,
+                                     dist="Tukey")

	Pairwise comparisons using Tukey and Kramer (Nemenyi) test
                   with Tukey-Dist approximation for independent samples

data:  price by condition

  1                 2                 3                 4                
2 0.99908           -                 -                 -                
3 0.000025063186814 < 2e-16           -                 -                
4 0.00023           0.000000000000046 0.000000544539610 -                
5 0.000000126256704 < 2e-16           0.000000000002318 0.000000000000046

P value adjustment method: none


Quiz

condition에 따라 price, bedrooms, bathrooms, sqft_living, sqft_lot, sqft_above, sqft_basement, year(2018 - yr_built)에 통계적으로 유의한 차이가 있는지를 검정하시오.

Variable Normaility Method F/Chisqaure pvalue
price No Krukal-Wallis 260.850 0.000
bathrooms Yes ANOVA 100.000 0.012
houseDF$year <- 2018 - houseDF$yr_built
analysis.variable <- c("price", "bedrooms", "bathrooms",
                       "sqft_living", "sqft_lot", "sqft_above",
                       "sqft_basement", "year")
Normality  <- c()
Method     <- c()
FChiSquare <- c()
PValue     <- c()
for(i in analysis.variable){
    normality.result <- by(unlist(houseDF[ , i]), houseDF$condition, ad.test)
    if(normality.result$`1`$p.value < 0.05 |
       normality.result$`2`$p.value < 0.05 |
       normality.result$`3`$p.value < 0.05 |
       normality.result$`4`$p.value < 0.05 |
       normality.result$`5`$p.value < 0.05){
        kruskal.result <- kruskal.test(unlist(houseDF[ , i]) ~ houseDF$condition)
        Normality  <- c(Normality, "No")
        Method     <- c(Method, "kruskal-Wallis")
        FChiSquare <- c(FChiSquare, kruskal.result$statistic)
        PValue     <- c(PValue, kruskal.result$p.value)
    }else{
        aov.result <- aov(unlist(houseDF[ , i]) ~ houseDF$condition)
        aov.result <- summary(aov.result)
        Normality  <- c(Normality, "Yes")
        Method     <- c(Method, "ANOVA")
        FChiSquare <- c(FChiSquare, unlist(aov.result)[7])
        PValue     <- c(PValue, unlist(aov.result)[9])
    }
}

anovaDF <- data.frame(Variables = analysis.variable,
                      Normality,
                      Method,
                      FChiSquare,
                      PValue)
writexl::write_xlsx(anovaDF, path = "anovaResult.xlsx")

+ Recent posts