Performing Kolmogorov-Smirnov tests
We use a one-sample Kolmogorov-Smirnov test to compare a sample with reference probability. A two-sample Kolmogorov-Smirnov test compares the cumulative distributions of two datasets. In this recipe, we will demonstrate how to perform a Kolmogorov-Smirnov test with R.
Getting ready
In this recipe, we will use the ks.test
function from the stat package.
How to do it…
Perform the following steps to conduct a Kolmogorov-Smirnov test:
Validate whether the x dataset (generated with the
rnorm
function) is distributed normally with a one-sample Kolmogorov-Smirnov test:>set.seed(123) > x <-rnorm(50) >ks.test(x,"pnorm") One-sample Kolmogorov-Smirnov test data: x D = 0.073034, p-value = 0.9347 alternative hypothesis: two-sided
Next, we can generate uniformly distributed sample data:
>set.seed(123) > x <- runif(n=20, min=0, max=20) > y <- runif(n=20, min=0, max=20)
We first plot the ECDF of two generated data samples:
>plot(ecdf...