6
$\begingroup$

I created a sample with 10000 normally distributed numbers. Subsequently, I used the Kolmogorov-Smirnov test to check if they are indeed normally distributed, and it turned out that they are not. How is this possible?

Below is my code.

data <- rnorm(n=10000, 5, 2)
ks.test(data, "pnorm")

And this is the answer:

Exact one-sample Kolmogorov-Smirnov test

data: data D = 1, p-value < 2.2e-16 alternative hypothesis: two-sided

$\endgroup$
6
  • 2
    $\begingroup$ Hint: What distribution is the rnorm in your code drawing from? What distribution is pnorm defining? $\endgroup$
    – Alex J
    Aug 3 at 6:14
  • 1
    $\begingroup$ If you test at a level of 5%, as you know, 5% of the samples will be rejected under the null. $\endgroup$
    – utobi
    Aug 3 at 6:17
  • $\begingroup$ @AlexJ rnorm generates 1000 normally distributed numbers. Then, I am trying to see if they follow the normal distribution (pnorm). Therefore, the null hypothesis should not be rejected. $\endgroup$ Aug 3 at 6:51
  • $\begingroup$ @utobi , I filled in the answer provided by R. $\endgroup$ Aug 3 at 6:53
  • 7
    $\begingroup$ You should make a habit of using set.seed() to make the random values reproducible. But to the point: If not specified, ks.test will test against a standard normal. So you should use ks.test(data, "pnorm", 5, 2). But please remember that the KS-test assumes the parameters to be known a priori and not estimated from the data. Also pay close attention to what utobi said. Finally, no normality test can confirm that your sample is normally distributed. Non-significance does not mean "normally distributed". $\endgroup$ Aug 3 at 7:04

1 Answer 1

21
$\begingroup$

As highlighted in the comments (Alex J and COOLSerdash), there are two issues here. First, the model used under the KS test is different from the true model that generated the data. The correct way would be either

> set.seed(12)
> set.seed(30823)
> data <- rnorm(n=10000, 5, 2)
> ks.test(data, "pnorm", mean=5, sd=2)

    Asymptotic one-sample Kolmogorov-Smirnov test

data:  data
D = 0.0044899, p-value = 0.9877
alternative hypothesis: two-sided

or

> data1 <- rnorm(n=10000)
> ks.test(data1, "pnorm")

    Asymptotic one-sample Kolmogorov-Smirnov test

data:  data1
D = 0.01079, p-value = 0.1947
alternative hypothesis: two-sided

Second (a minor issue), the test if used at level 0.05 has still (approximately) 5% of a chance to reject the null even if the null is true.

$\endgroup$
1
  • 1
    $\begingroup$ Thank you for your response. The issue has been resolved. $\endgroup$ Aug 3 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.