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:


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


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?
  • 2
    $\begingroup$ @Massimo, please provide link, internet is a large place. $\endgroup$
    – mpiktas
    Apr 27 '11 at 13:22
  • $\begingroup$ www.sixsigmain.it/ebook/Capu7-15.html $\endgroup$
    – Massimo
    Apr 28 '11 at 6:40
  • $\begingroup$ sorry, in italian language $\endgroup$
    – Massimo
    Apr 28 '11 at 6:41
  • $\begingroup$ @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. $\endgroup$
    – mpiktas
    Apr 28 '11 at 6:49
  • $\begingroup$ @mpiktas, and then ... how write with R for compare a with b? $\endgroup$
    – Massimo
    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) ) 


    Two-sample Kolmogorov-Smirnov test 

D = 0.4667, p-value = 0.07626 
alternative hypothesis: two-sided  
  • 1
    $\begingroup$ 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. $\endgroup$
    – Aniko
    Apr 27 '11 at 14:12
  • $\begingroup$ @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. $\endgroup$
    – mpiktas
    Apr 27 '11 at 14:18
  • $\begingroup$ @mpiktas: that is what I meant by "one value of 0, two values of 1, one value of 9, and one value of 4". $\endgroup$
    – Henry
    Apr 27 '11 at 21:12
  • $\begingroup$ @Henry: only now I understand. While I wrote the number of bird, R wanted the distribution of the hours. $\endgroup$
    – Massimo
    Apr 28 '11 at 8:48
  • 2
    $\begingroup$ To get to the data format used here... rep(1:5, c(0,1,1,9,4)). $\endgroup$
    – 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$.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.