# 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 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. – Nutle May 5 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" – Sal Mangiafico May 5 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 at 17:14

Edit: This answer heavily revised.

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))
curve(punif(x, min(Values), max(Values)), add=TRUE, col="red") 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))
curve(punif(x, 6, 9), add=TRUE, col="red") • This answer does not respond to the question that was asked. You test against a predetermined Uniform$(0,1)$ distribution whereas the question concerns testing against a distribution whose parameters are determined by the data. – whuber May 5 at 15:42
• @whuber , I revised my answer. Thanks for bringing that up. I was mistaken about how the ks.test function works with the uniform distribution. – Sal Mangiafico May 5 at 16:47
• Thank you. Unfortunately, the application of the KS test is incorrect because it uses a distribution fit from the data rather than a prespecified distribution. The difference isn't huge and can be neglected for medium to large datasets, but it is detectable in smaller ones: your approach tends to produce p-values that are too small. – whuber May 5 at 17:11
• I understand. Thanks. – Sal Mangiafico May 5 at 17:12
• @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 May 6 at 8:30