02Data

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

One Sample t-test

When?
하나의 모집단의 양적 자료의 평균이 기존에 알고 있던 것보다
커졌는지, 작아졌는지, 달라졌는지를 통계적으로 검정하는 방법



1. 일표본 검정

  • 귀무가설 : 성인들의 평균 키는 170cm이다.
  • 대립가설 : 성인들의 평균 키는 170cm보다 크다.

1단계 : 정규성 검정(Normality Test)

  • 귀무가설 : 정규분포를 따른다.
  • 대립가설 : 정규분포를 따르지 않는다.

Shapiro-Wilk test : shapiro.test(data$variable)

height <- c(180, 175, 170, 170, 165, 184, 164, 159, 181, 167, 182, 186)
shapiro.test(height)

<결과>
Shapiro-Wilk normality test
data:  height
W = 0.93844, p-value = 0.4781

유의확률이 0.478이므로 유의수준 0.05에서 height는 정규분포를 따른다고 가정할 수 있다.


2단계 : 일표본 T검정(One sample t-test)

t.test(data$variable, mu = , alternative = )
mu : 귀무가설의 모평균
alternative : 대립가설, "greater", "less", "two.sided"

height.test <- t.test(height, mu = 170, alternative = "greater")
height.test

<결과>
One Sample t-test

data:  height
t = 1.3887, df = 11, p-value = 0.0962
alternative hypothesis: true mean is greater than 170
95 percent confidence interval:
 168.9492      Inf
sample estimates:
mean of x
 173.5833

유의확률이 0.096이므로 유의수준 0.05에서 성인들의 키는 통계적으로 유의하게 커지지 않았다. 성인들의 키는 변화가 없다.

str(height.test)
height.test$statistic    # t
height.test$parameter    # df
height.test$p.value      # p-value
height.test$conf.int     # 95% Confidence Interval, 신뢰구간
height.test$estimate     # 추정치, x bar, 표본의 평균
height.test$null.value   # 귀무가설의 모평균
height.test$alternative  # 대립가설
height.test$method       # One sample t-test
height.test$data.name    # height

2단계 : 윌콕슨의 부호 순위 검정(Wilcoxon's signed rank test)

wilcox.test(data$variable, mu = , alernative = )

wilcox.test(height, mu = 170, alternative = "greater")


Quiz 1.가설검정

귀무가설 : 성인들의 평균 용돈은 200만원이다.
대립가설 : 성인들의 평균 용돈은 200만원보다 작다.
유의수준 : 0.05

money <- c(45, 40, 40, 50, 50, 50, 40, 100, 50)

# 1단계 : 정규성 검정(Normality Test)
options(scipen = 100) # 지수 표현식 사용하지 않음
shapiro.test(money)
# 결론 : 유의확률이 0.000이므로 유의수준 0.05에서
# money는 정규분포를 따르지 않는다. 즉 정규성 가정을 만족하지 않음

# 2단계 : Wilcoxon's signed rank test
wilcox.test(money, mu = 200, alternative = "less")
# 결론 : 유의확률이 0.004이므로 유의수준 0.05에서
# 성인들의 용돈은 200만원보다 작다라는 대립가설을 채택
# 통계적으로 유의하게 성인들의 용돈이 줄어 들었다.


Quiz 2. 가설검정 결과 자동화 코드 작성

bedrooms", "bathrooms", "floors", "waterfront", "view", "condition", "grade" 변수에 대한 가설검정 결과를 엑셀 파일에 저장하시오.
귀무가설은 평균은 5이다로 함.

houseDF <- readxl::read_excel(path = "d:/da/kc_house_data.xlsx",
                              sheet = 1,
                              col_names = TRUE)

analysis.varibles <- c("bedrooms", "bathrooms", "floors", "waterfront", "view", "condition", "grade")


tv        <- c()
pvalue    <- c()
test.type <- c()

for(i in analysis.varibles){
    print(i)
    norm.test <- ad.test(unlist(houseDF[ , i]))
    if(norm.test$p.value > 0.05){
        result.t <- t.test(unlist(houseDF[ , i]), mu = 5, alternative = "two.sided")
        tv       <- c(tv, result.t$statistic)
        pvalue   <- c(pvalue, result.t$p.value)
        test.type <- c(test.type, "ttest")

    }else{
        result.wilcox <- wilcox.test(unlist(houseDF[ , i]), mu = 5, alternative = "two.sided")
        tv            <- c(tv, result.wilcox$statistic)
        pvalue        <- c(pvalue, result.wilcox$p.value)
        test.type <- c(test.type, "wilcox")
    }
}

resultDF <- data.frame(analysis.varibles, tv, pvalue, test.type)
writexl::write_xlsx(resultDF, path = "d:/da/resultDF.xlsx")

+ Recent posts