A one-sample Kolmogorov-Smirnov test is used to compare a sample with a reference probability. A two-sample Kolmogorov-Smirnov test compares the cumulative distributions of two datasets. In this recipe, we will demonstrate how to perform the Kolmogorov-Smirnov test with R.
Ensure that mtcars
has already been loaded into a DataFrame within an R session. As the ks.test
function is originated from the stats
package, make sure the stats
library is loaded.
Perform the following steps:
- Validate whether the dataset,
x
(generated with thernorm
function), is distributed normally with a one-sample Kolmogorov-Smirnov test:
> x = rnorm(50) > ks.test(x,"pnorm") Output: One-sample Kolmogorov-Smirnov test data: x D = 0.1698, p-value = 0.0994 alternative hypothesis: two-sided
- Next, you can generate uniformly distributed sample data:
> set.seed(3) > x = runif(n=20, min...