# Test of uniform distribution using KS-test and chi square in R

I want to test if a given sample $$x$$ of $$n = 500$$ continuous observations is uniformly distributed on a given interval of $$[a,b]$$ ($$a = min(x)$$ and $$b = max(x)$$). Therefore I would like to compare the results of the one-sample KS-test and the Chi-Square test in R given a significance level of 1%. As I am totally new to R, I'm not really sure about the code: So let x be a numeric vector with the given observations.

KS-Test: ks.test(x, "punif") --> question: looks quite wrong to me, how can I specify the significance level?

Chi-Square-Test: chisqr.test(x) --> seems to be even worse... and once again: after I get the code syntax right: how do I integrate the significance level?

Even if I know, this is a real "low-level-question": thanks for any help! ;)

• Your test hypothesis is invalid because it is formulated from data statistics (the max and min). Neither the KS-test nor the chi-squared test (nor any other standard distributional test) is applicable. Indeed, it is certainly the case that this hypothesis is wrong, so you might as well reject it outright without any testing at all: almost surely the underlying distribution's support extends beyond the range of the observed data.
– whuber
May 3 '19 at 14:17
• @whuber isn't this just the case of $H_0: F \sim U(\theta_1, \theta_2)$, where $\theta_i$ are ML estimates (since min/max are essentially that) -- so this corresponds to composite hypothesis on shift/scale distribution, which is shown to have no effect for K-S critical values? Please correct me if I'm wrong, as I'm more curious than just trying to correct your comment.
– runr
May 5 '19 at 16:36
• This question may be a question about R programming and not about statistics. I've answered below, but the heart of the answer may simply be that min(Values), max(Values) needs to be included in the ks.test call when using "punif" May 5 '19 at 16:59
• Because your "hypothesis" is a function of the data, it is not a statement about the underlying distribution, as is required of any valid statistical testing hypothesis. Formally, it's not as you have characterized it, but rather of the type "$H_0:F\sim U(t_1(X),t_2(X))$" where $t_1$ and $t_2$ are functions and $X$ is the data.
– whuber
May 5 '19 at 17:14

One problem in your code may be that punif defaults to a minimum of 0 and a maximum of 1. If these aren't correct for your purposes, they need to be specified

To conduct the hypothesis test, you would compare the reported p value to your selected alpha value (0.01, as stated in the question).

You don't need this line, but it makes the results reproducible for the purpose of this example.

set.seed(sum(utf8ToInt("SalLikesKittens")))


Create some data. Let's say your data range from 5 to 10.

Values = runif(500, min = 5, max = 10)


Plot a histogram of the data.

hist(Values, col="darkgray")


Conduct the KS test. Here the p value (0.615) is greater than your alpha (0.01), so you don't have good evidence to reject the null hypothesis. The null hypothesis is that the data follow a uniform distribution from the minimum value to the maximum value.

ks.test(Values, "punif", min(Values), max(Values))

# One-sample Kolmogorov-Smirnov test
#
# data:  Values
# D = 0.033862, p-value = 0.6151
# alternative hypothesis: two-sided


Here, the red curve is the theoretical cumulative distribution and the black circles are the observed values.

plot(ecdf(Values))


Above we used the min and the max from the observed values. However, you can also specify theoretical min and max values. Here, of course, the data don't fit well a uniform distribution from 6 to 9.

ks.test(Values, "punif", 6, 9)

# One-sample Kolmogorov-Smirnov test
#
# data:  Values
# D = 0.21611, p-value < 2.2e-16
# alternative hypothesis: two-sided

plot(ecdf(Values))

• @whuber Further, by simulations we can easily check, that the difference is also negligible when the sample size is low, say, $n=15$, let alone the case of the OP with $n=500$. See, for example, pastebin.com/331a7Gs7.
• @Nutle Thank you for that. Your results can be analytically justified by considering the sampling distributions of the max and min from the uniform dataset: they tend to be extremely close to the endpoints. Thus, the KS test works well in this case. (It's actually not terribly bad even with $n=5.$)