# How am I using KS test incorrectly in R?

Here are the codes I input into R:

X = rnorm(10000)
Y = rchisq(10000, 1)

ks.test(X, rchisq(1,1))

Two-sample Kolmogorov-Smirnov test

data:  X and rchisq(1, 1)
D = 0.6369, p-value = 0.812
alternative hypothesis: two-sided


However, if we do ks.test(X, Y), the statistic is highly significant.

I must be using the KS test wrong somehow, but how?

PS I was meant to write pchisq rather than rchisq

When you did ks.test(X,Y) your $Y$ variable consisted of 10000 values.

When you did ks.test(X,rchisq(1,1)) your $Y$ variable consisted of a single value:

$\quad\quad$

Why would you expect those two things to give similar p-values? Their sample sizes are very different, and the corresponding test statistics will be quite different:

I meant to write pchisq in there

While the statistic may be very close, you won't get the same p-values then either!

Consider that the asymptotic critical values for a one sample test will be $Q^{-1}(\alpha)\cdot\sqrt{\frac{1}{n}}$ and those for the two sample test will be $Q^{-1}(\alpha)\cdot\sqrt{\frac{1}{n_1}+\frac{1}{n_2}}$ (where $$Q(x) = 1 - 2\sum_{k=1}^{\infty}(-1)^{k-1}e^{-2k^2x^2}$$ is the K-S asymptotic distribution).

• Oh, that is so bad, I am meant to write pchisq in there! Sep 29, 2014 at 9:32
• See the updates to my answer. Sep 29, 2014 at 9:54
• You may like to update your question to ask the pchisq one as a followup to your present question. Sep 29, 2014 at 10:08