[R] kolmogorov-Smirnov critical values

Petr Savicky savicky at cs.cas.cz
Wed May 16 21:22:27 CEST 2012


On Wed, May 16, 2012 at 06:52:48AM -0700, aramos wrote:
> Hi!
> 
> Any one knows how to obtain critical values for the k-s statistic, using R?

Hi.

I do not know, whether there is a function for this. However, the following
randomized approach allows to extract a table of statistic/p.value pairs
from ks.test() for fixed sample sizes.

  n1 <- 30
  n2 <- 50
  d <- 10000
  res <- matrix(nrow=d, ncol=2)
  for (i in seq.int(length=d)) {
      x1 <- runif(n1) + runif(1)
      x2 <- runif(n2) + runif(1)
      out <- ks.test(x1, x2)
      res[i, 1] <- out$statistic
      res[i, 2] <- out$p.value
  }
  tab <- unique(res[order(res[, 1]), ])
  colnames(tab) <- c("statistic", "p.val")

If you are mainly interested in the range of the p-values for relatively
close distributions, then replace

      x1 <- runif(n1) + runif(1)
      x2 <- runif(n2) + runif(1)

by

      x1 <- runif(n1)
      x2 <- runif(n2)

Part of the obtained table is

         statistic        p.val
  
  [39,] 0.30000000 5.642910e-02
  [40,] 0.30666667 4.815638e-02
  [41,] 0.31333333 4.091424e-02
  [42,] 0.32000000 3.466530e-02
  [43,] 0.32666667 2.925540e-02
  [44,] 0.33333333 2.458672e-02
  [45,] 0.34000000 2.060188e-02
  [46,] 0.34666667 1.719140e-02
  [47,] 0.35333333 1.428992e-02
  [48,] 0.36000000 1.183727e-02
  [49,] 0.36666667 9.767969e-03

Hope this helps.

Petr Savicky.



More information about the R-help mailing list