# Difference between K-S manual test and K-S test with R?

On the Internet there is an example of k-s test being applied relative to distribution of number of bird varieties over different five hour periods. The observed distribution was:

a=c(0,1,1,9,4)


The expected distribution (if there is no difference between the five hours) could be:

b=c(3,3,3,3,3)


After I found the two cumulate distributions, I calculated manually, D = 0,4667 (a value that is similar with the internet). But if I try to use R, I find a different value of D:

> a=c(0,1,1,9,4)

> b=c(3,3,3,3,3)

> ks.test(a,b)

Two-sample Kolmogorov-Smirnov test

data: a and b

D = 0.6, p-value = 0.3291

alternative hypothesis: two-sided .......

• What is leading to the difference between my manual calculation and result that R gives?
• @Massimo, please provide link, internet is a large place. Apr 27 '11 at 13:22
• www.sixsigmain.it/ebook/Capu7-15.html Apr 28 '11 at 6:40
• sorry, in italian language Apr 28 '11 at 6:41
• @Massimo, @Henry answer is the correct one. The data in your link comes in tabular format, how many times certain values are observed, ks.test expects simple sample, not the table, hence the difference. Apr 28 '11 at 6:49
• @mpiktas, and then ... how write with R for compare a with b? Apr 28 '11 at 7:47

You are testing a different thing.

While you think c(0,1,1,9,4) means you are looking at 0 values of one, 1 value of two, 1 value of three, 9 values of four, and 4 values of five, R thinks you are looking at one value of 0, two values of 1, one value of 9, and one value of 4.

To get D = 0.4667..., try the rather verbose

ks.test( c(2,3,4,4,4,4,4,4,4,4,4,5,5,5,5),
c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5) )


giving

    Two-sample Kolmogorov-Smirnov test

D = 0.4667, p-value = 0.07626
alternative hypothesis: two-sided

• Wow, good sleuthing - I would have never thought of that. Based on the short description of the data, it seems unlikely that this was the intention (especially for b), but clearly that's what the online calculator is doing. Apr 27 '11 at 14:12
• @Henry +1 for replicating the value of KS test. I would like to add that I suspect that R does not think the way you suggested. c(0,1,1,9,4) is simply the vector with values 0,1,1,9,4. I do not find any suggestion in ks.test manual page to suggest otherwise. Apr 27 '11 at 14:18
• @mpiktas: that is what I meant by "one value of 0, two values of 1, one value of 9, and one value of 4". Apr 27 '11 at 21:12
• @Henry: only now I understand. While I wrote the number of bird, R wanted the distribution of the hours. Apr 28 '11 at 8:48
• To get to the data format used here... rep(1:5, c(0,1,1,9,4)).
– John
May 2 '11 at 14:30

First of all, the Kolmogorov-Smirnoff test is probably incorrect for your situation. I assume you want to test whether the distribution of the number bird sightings is uniform (constant accross time). A chi-square goodness-of-fit test would be the simplest solution for that.

Second, the R-produced value of 0.6 seems correct to me: at $x=2.5$, $F_a(x)=0.6$, while $F_b(x)=0$.