Provisional.Modulation
Provisional.Modulation

Reputation: 724

How to write the Kolmogorov-Smirnov in R

Long story short, I want to manually write the code for the Kolmogorov-Smirnov one-sample statistic instead of using ks.test() in R. From what I understand, the K-S test is basically a ratio between a numerator and a denominator. I am interested in writing out the numerator, and from what I understand it is the maximal absolute difference between a sample of observations and the theoretical assumption. Let's use the below case as an example:

         Data    Expected
1  0.01052632 0.008864266
2  0.02105263 0.010969529
13 0.05263158 0.018282548
20 0.06315789 0.031689751
22 0.09473684 0.046315789
24 0.26315789 0.210526316
26 0.27368421 0.220387812
27 0.29473684 0.236232687
28 0.30526316 0.252520776
3  0.42105263 0.365650970
4  0.42105263 0.372299169
5  0.45263158 0.398781163
6  0.49473684 0.452853186
7  0.50526316 0.460277008
8  0.73684211 0.656842105
9  0.74736842 0.665484765
10 0.75789474 0.691523546
11 0.77894737 0.718005540
12 0.80000000 0.735955679
14 0.84210526 0.791135734
15 0.86315789 0.809972299
16 0.88421053 0.838559557
17 0.89473684 0.857950139
18 0.96842105 0.958337950
19 0.97894737 0.968642659
21 0.97894737 0.979058172
23 0.98947368 0.989473684
25 1.00000000 1.000000000

Here, I want to obtain the maximal absolute difference (Data - Expected).

Anyone have an idea? I can rephrase this question, if necessary. Thanks!

Upvotes: 1

Views: 905

Answers (1)

Provisional.Modulation
Provisional.Modulation

Reputation: 724

I utilized the below function to obtain the answer:

> A <- with(df, max(abs(Data-Expected)))
> A
0.082

Basically, this function calculates the differences between the two columns into a new vector, whose values are transformed into absolute values, and from the absolute values the largest one is obtained.

Credit to Josh O'Brien.

Upvotes: 1

Related Questions