GaryBAYLOR / R-code

A collection of algorithms written by myself for solving statistical problems
0 stars 0 forks source link

Wilcoxon Signed-Rank test Normal approximation #14

Open GaryBAYLOR opened 9 years ago

GaryBAYLOR commented 9 years ago

We can use Wilcoxon signed rank test if the paired - t test can't be used in testing paired samples. When the number of pair N is large, we can use normal approximation. The following R simulation shows that When N >= 15, the approximation is pretty good.

rankdist <- function(n = 3, k = 1e3) {
    x <- 1:n
    res <- numeric(k)
    for (i in 1:k) {
        sign <- sample(c(1, -1), size = n, replace = TRUE)
        y <- x * sign
        res[i] <- sum(y)
    }

        t <- max(density(res)$y)
        hist(res, freq = FALSE, ylim = c(0, t * 1.1), main = "Barplot of the Signed-Rank Statistic", xlab = "signed-rank")
        mu <- 0
        sd <- sqrt(n * (n + 1) * (2 * n + 1) / 6)
        xseq <- seq(-120, 120, length = 1000)
        yseq <- dnorm(xseq, mu, sd)
        lines(xseq, yseq, type = "l", lwd = 2, col = "red")
}

image N = 5 for the top graph, N = 10 for the second, N = 15 for the bottom graph.