2
$\begingroup$

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! ;)

$\endgroup$
4
  • 1
    $\begingroup$ 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. $\endgroup$
    – whuber
    May 3 '19 at 14:17
  • $\begingroup$ @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. $\endgroup$
    – runr
    May 5 '19 at 16:36
  • $\begingroup$ 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" $\endgroup$ May 5 '19 at 16:59
  • 1
    $\begingroup$ 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. $\endgroup$
    – whuber
    May 5 '19 at 17:14
4
$\begingroup$

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")

enter image description here

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")

EDCF1

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")

enter image description here

$\endgroup$
6
  • 1
    $\begingroup$ @whuber , I revised my answer. Thanks for bringing that up. I was mistaken about how the ks.test function works with the uniform distribution. $\endgroup$ May 5 '19 at 16:47
  • 1
    $\begingroup$ 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. $\endgroup$
    – whuber
    May 5 '19 at 17:11
  • 1
    $\begingroup$ I understand. Thanks. $\endgroup$ May 5 '19 at 17:12
  • 2
    $\begingroup$ @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. $\endgroup$
    – runr
    May 6 '19 at 8:30
  • 2
    $\begingroup$ @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.$) $\endgroup$
    – whuber
    Jan 14 '20 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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