#title 분포 동일성 검정 [[TableOfContents]] ==== 개인적인 방법 ==== * ks.test --> 같은 분포를 가진 샘플인가? * chisq.test --> 각각의 bin에 차이가 있나? * run.test --> 차이가 정규분포인가? 지속적으로 밑으로 깔려있거나 위에 있으면 이것도 문제 ==== ks.test(콜모고로프-스미르노프 검정) ==== 두 변수의 ecdf의 차이가 가장 큰 값으로 콜모고로프-스미르노프의 검정통계량의 점근분포를 이용하여 검정한다. {{{ x1 <- rnorm(50) x2 <- rnorm(50, -1) compare <- function(x, y) { n <- length(x); m <- length(y) w <- c(x, y) o <- order(w) z <- cumsum(ifelse(o <= n, m, -n)) i <- which.max(abs(z)) w[o[i]] } u <- compare(x1,x2) e.x <- ecdf(x1) e.y <- ecdf(x2) abs(e.x(u) - e.y(u)) ks.test(x1,x2)$statistic plot(e.x, col="Blue", main="ECDF", xlab="Value", ylab="Probability", xlim=range(c(x1,x2))) plot(e.y, add=TRUE, col="Red") lines(c(u,u), c(0,1), col="Gray") lines(c(u,u), c(e.x(u), e.y(u)), lwd=2) text(u*1.04, abs(e.x(u)-e.y(u)) * 1.5, label="D") }}} attachment:분포동일성검정/kstest3.png 또 다른 방법 {{{ x1 <- rnorm(50) x2 <- rnorm(50, -1) library("rgr") gx.ks.test(x1, x2) }}} 결과 {{{ > gx.ks.test(x1, x2) Two-sample Kolmogorov-Smirnov test data: x1 and x2 D = 0.3, p-value = 0.02171 alternative hypothesis: two-sided }}} attachment:분포동일성검정/kstest.png 또 다른 방법 {{{ plot(ecdf(x1), do.points = FALSE, verticals=T, xlim=range(x1, x2)) lines(ecdf(x2), lty=3, do.points = FALSE, verticals=T) ks.test(x1, x2, alternative="two.sided") }}} ==== chisq.test(카이제곱 검정) ==== {{{ chisq.test(x1, p=x2/sum(x2)) }}} 결과 {{{ > chisq.test(x1, p=x2/sum(x2)) Chi-squared test for given probabilities data: x1 X-squared = 169.74, df = 49, p-value = 3.196e-15 Warning message: In chisq.test(x1, p = x2/sum(x2)) : 카이제곱 approximation은 정확하지 않을수도 있습니다 }}} ==== 참고 ==== * http://stackoverflow.com/questions/27233738/finding-location-of-maximum-d-statistic-from-ks-test * https://rpubs.com/mharris/KSplot