title: Kolmogorov-Smirnov test: exact p-values for a two-sample test applied to a discrete variable when ties exist
body: <p>I got two samples from different sites. The parameter I am interested in is discrete (frequencies). I did simulations for both sites, so I know the probabilities of a random distribution for each site. Because of my simulations I know that the deviation of my parameter from its mean is not normally distributed so I went for a parametric test. I checked with one-sample Kolmogorov-Smirnov if the samples might derive from these random distributions (example data, not real):</p>
  
  <pre><code>sample1 &lt;- rep(1:5, c(25, 12, 12, 0, 1))
  rand.prob1 &lt;- c(.51, .28, .111, .08, 0.019)
  StepProb1 &lt;- stepfun(0:4, c(0, cumsum(rand.prob1)), right = T)
  dgof::ks.test(sample1, StepProb1)
  
  sample2 &lt;- rep(1:5, c(19, 13, 10, 5, 3))
  rand.prob2 &lt;- c(.61, .18, .14, .05, 0.02)
  StepProb2 &lt;- stepfun(0:4, c(0, cumsum(rand.prob2)), right = T)
  dgof::ks.test(sample2, StepProb2)
  </code></pre>
  
  <p>In a next step I want to check if the samples of both sites might derive from the same distribution. Both implemetations of the KS-test (packages <code>stats</code> and <code>dgof</code>) issue a warning because my samples have ties:</p>
  
  <pre><code>stats::ks.test(sample1, sample2)
  dgof::ks.test(sample1, sample2)
  </code></pre>
  
  <p>If I understand <a href="http://ideas.repec.org/p/cir/cirwor/2001s-56.html">Dufour and Farhat</a> (2001) correctly, there is a way to calculate exact p-values through tie-breaking via Monte Carlo simulations. And if I understand the package description of the dgof package correctly, its implementation of Monte Carlo simulations only works for the one-sample test.</p>
  
  <p>So my question: Does anybody know how to calculate exact p-values in R for a two-sample Kolmogorov-Smirnov test applied to a discrete variable when ties exist?</p>
  
  <p>Or alternatively (though not specifically related to R): If nobody knows how to do this with a tolerable workload, I would go for the uncorrected p-values and as a consequence discuss results with care. But with p-values below 0.0001. I'm actually not overly concerned about it. But what do I know... Do you think this is right or am I making a grave mistake in this case?</p>
  
  <p>Thanks in advance, I already appreciate that you read until here.</p>