How to perform one sample Kolmogorov-Smirnov test in R?

R ProgrammingServer Side ProgrammingProgramming

The one sample Kolmogorov-Smirnov test is used to check whether a data set follows certain distribution of not.

For Example, if we have a vector called X and we want to know whether values in X follow normal distribution or not then we can use the command given below −

ks.test(X,"pnorm")

Check out the below Examples to understand how it works.

Example 1

Following snippet creates a sample data frame −

x1<-rnorm(80)
x1

The following dataframe is created

 [1] -0.5930649979 -0.7050971899  1.5036776495  2.0351473077  0.7385834175
 [6] -0.7881248810 -1.4895638061 -1.1605302628  0.9423750498 -0.2009388310
[11] -0.3252013256 -1.4571333512  1.6642185880 -0.6511740441 -0.0076780745
[16] -0.3338502137 -1.0210635809  0.5149113041 -0.6655072430  0.7074352553
[21] -0.8507390736 -1.1406754392 -1.6111072295  1.6772812999 -2.3250173518
[26] -1.4653643311  1.3272904267 -1.8467972822 -0.1593700549 -1.6613273664
[31]  0.2613722540  0.4838800094 -0.5998095101  0.6591245833 -0.5974476415
[36] -0.4589820738  0.2008275688 -1.5262213823 -1.3834131382 -1.4572893292
[41]  0.5105892883 -1.3039612523  1.2814998856  0.9949435321 -0.2800486480
[46] -0.7354803734  1.7473145867 -1.6495713278  0.7750021918 -0.4070139831
[51] -0.9916802542 -1.1538558797 -1.3349762222  0.0002099075  1.0405995439
[56] -0.5756922744  0.8666200332  1.0866830149  0.8970111284 -1.3593719314
[61]  1.1197713017  1.2070772837  1.5081925784 -0.4842669069  0.6687523922
[66]  0.7007919811 -1.1365104248 -0.1864085155  0.2051847966 -1.6965972860
[71]  0.9870287064 -0.1955897595  1.3922165002 -0.1773204636  1.5491799191
[76] -0.7294171557 -0.3850467096 -0.3062196137  0.7902249357  0.5163688587

For the One-sample Kolmogorov-Smirnov test, add the following code to the above snippet −

x1<-rnorm(80)
data: x1
D = 0.12581, p-value = 0.1458
alternative hypothesis: two-sided
ks.test(x1,"pnorm")

Example 2

Following snippet creates a sample data frame −

x2<-rpois(200,2)
x2

The following dataframe is created

 [1] 0 3 2 1 1 1 1 3 1 1 0 2 1 3 1 5 2 1 4 2 3 1 3 1 2 3 3 1 1 3 1 2 2 2 0 2 4
 [38] 2 0 3 2 1 3 3 3 1 3 1 2 3 5 2 3 2 4 3 0 1 2 3 2 2 2 3 1 1 2 9 1 1 2 3 0 4
 [75] 2 3 4 0 4 2 2 3 7 2 4 0 1 1 2 3 3 5 3 2 2 2 3 1 0 2 3 2 1 1 5 0 2 3 2 1 2
[112] 3 0 1 1 2 2 4 2 1 0 5 2 1 0 2 3 4 1 1 2 0 2 4 0 3 3 3 1 0 6 4 4 2 0 1 2 6
[149] 4 2 1 1 3 5 1 2 4 0 1 2 0 1 1 2 4 1 3 0 2 2 2 1 2 1 2 2 0 1 3 2 2 0 1 2 1
[186] 0 0 2 4 1 1 1 4 2 1 2 3 1 1 3

For the One-sample Kolmogorov-Smirnov test, add the following code to the above snippet −

x2<-rpois(200,2)
data: x2
D = 0.72134, p-value < 2.2e-16
alternative hypothesis: two-sided
ks.test(x2,"pnorm")

Warning message

In ks.test(x2, "pnorm") − Ties should not be present for the Kolmogorov-Smirnov test.

Example 3

Following snippet creates a sample data frame −

x3<-sample(1:9,200,replace=TRUE)
x3

The following dataframe is created

 [1] 8 1 9 8 1 2 1 7 6 8 9 1 4 3 1 5 2 8 1 7 2 5 1 2 2 6 9 6 1 8 7 6 9 9 1 7 8
[38] 7 3 3 9 6 9 7 4 3 8 9 3 9 8 8 3 1 4 2 7 3 6 4 9 5 7 5 9 3 2 6 5 1 6 6 8 3
[75] 9 5 9 9 6 2 4 9 7 8 5 7 2 3 9 8 5 8 4 2 9 3 9 7 8 1 1 2 3 2 8 7 5 4 3 3 6
[112] 9 6 8 1 4 2 7 1 5 5 6 4 4 7 3 5 3 4 1 2 7 4 3 5 5 5 7 6 2 6 5 7 3 8 8 3 4
[149] 6 7 4 3 3 8 8 6 7 5 1 6 4 2 4 1 1 9 6 6 9 7 2 5 4 3 8 2 3 7 5 6 3 9 4 3 2
[186] 6 4 2 1 3 1 4 7 3 4 1 5 6 6 2

For the One-sample Kolmogorov-Smirnov test, add the following code to the above snippet −

x3<-sample(1:9,200,replace=TRUE)
D = 0.86725, p-value < 2.2e-16
alternative hypothesis: two-sided
ks.test(x3,"pnorm")

Warning message

In ks.test(x3, "pnorm") − Ties should not be present for the Kolmogorov-Smirnov test

Example 4

Following snippet creates a sample data frame −

x4<-sample(1:100,200,replace=TRUE)
x4

The following dataframe is created

 [1] 36 99 14 23 66 96 8 74 89 88 96 12 8 43 55 94 91 56 6 54 89 74 10 62 60
[26] 79 43 88 95 93 29 43 27 97 22 28 32 98 95 78 74 38 33 22 92 84 32 64 21
37
[51] 70 83 60 50 97 98 84 13 21 66 61 63 25 73 28 60 48 70 87 53 99 37 47 70
58
[76] 6 93 50 18 81 55 47 89 83 52 32 81 45 18 65 97 78 50 74 61 25 85 83 73
26
[101] 20 74 62 42 90 66 21 10 14 54 83 49 93 99 36 86 35 23 33 63 30 85 16 44
76
[126] 71 11 92 94 40 3 9 8 3 58 20 96 34 26 25 82 92 16 76 69 19 79 42 35
86
[151] 92 81 4 67 18 30 92 32 79 86 41 60 35 5 25 83 19 77 91 24 54 74 99 25
71
[176] 26 45 27 15 1 99 96 69 31 52 33 84 62 88 86 19 53 92 44 83 94 12 14 41
33

ks.test(x4,"pnorm")

For the One-sample Kolmogorov-Smirnov test, add the following code to the above snippet −

x4<-sample(1:100,200,replace=TRUE)
data: x4
D = 0.99365, p-value < 2.2e-16
alternative hypothesis: two-sided
ks.test(x4,"pnorm")

Warning message

In ks.test(x4, "pnorm") −

Ties should not be present for the Kolmogorov-Smirnov test.

Example 5

Following snippet creates a sample data frame −

x5<-round(rnorm(200),0)
x5

The following dataframe is created

[1] -3 -1 -1 -1 -1 1 0 0 2 0 1 1 0 -1 -1 -1 0 0 0 1 0 -1 0 0 -1
[26] 0 -2 -1 0 1 1 0 1 -1 2 -2 1 1 0 0 -1 0 0 1 3 1 0 -2 -2 -
2
[51] -1 0 -1 0 0 0 1 0 1 1 -1 1 -1 0 -1 0 1 -1 -2 2 -2 1 0 1
0
[76] 0 0 0 1 -1 -1 0 3 1 0 -2 -2 0 1 0 1 0 0 0 1 0 -1 0 -1 -
1
[101] 1 -1 -1 -1 0 0 -2 2 1 -1 0 -2 -1 0 1 0 0 1 -1 -2 1 0 2 1 -
1
[126] 1 0 0 0 -1 -1 -1 1 -1 0 0 0 1 1 -1 1 0 0 0 1 -1 1 2 1
1
[151] 0 0 0 1 0 0 2 0 0 0 1 -2 0 0 -1 0 1 0 0 0 -3 0 0 1
0
[176] -1 1 -1 1 2 -2 0 0 0 -1 2 -1 -2 0 -1 1 2 2 -1 -1 0 0 -2 1
1

For the One-sample Kolmogorov-Smirnov test, add the following code to the above snippet −

x5<-round(rnorm(200),0)
data: x5
D = 0.2, p-value = 2.251e-07
alternative hypothesis: two-sided
ks.test(x5,"pnorm")

Warning message

In ks.test(x5, "pnorm") − Ties should not be present for the Kolmogorov-Smirnov test

raja
Updated on 05-Nov-2021 10:39:26

Advertisements